找回密码
 立即注册
查看: 804|回复: 11

求助,一个简单的数组先进先出C程序!!!

[复制链接]
  • TA的每日心情
    慵懒
    6 天前
  • 签到天数: 2 天

    [LV.1]初来乍到

    18

    主题

    41

    回帖

    426

    积分

    中级会员

    积分
    426
    发表于 2023-2-9 20:28:16 | 显示全部楼层 |阅读模式
    求助,一个简单的数组先进先出C程序!!!
    回复 送花

    使用道具 举报

    该用户从未签到

    547

    主题

    9127

    回帖

    1万

    积分

    管理员

    积分
    14043
    发表于 2023-2-9 20:42:45 | 显示全部楼层

    求助,一个简单的数组先进先出C程序!!!
    ===我不知道梁工的串口程序有没有这段程序,发个链接你看下,
    ===不行请梁工再亲自写段程序给您
    1.png
    【新提醒】求STC8H/STC8G系列 4个串口同时通信的演示程序,Modbus 演示程序 - 串行口,DMA支持的4组串口 国芯论坛-STC全球32位8051爱好者互助交流社区 - STC全球32位8051爱好者互助交流社区 (stcaimcu.com)
    2.png
    不知用到先进先出没有
  • TA的每日心情
    奋斗
    12 小时前
  • 签到天数: 139 天

    [LV.7]常住居民III

    126

    主题

    179

    回帖

    1524

    积分

    金牌会员

    积分
    1524
    发表于 2023-2-10 00:12:30 | 显示全部楼层

  • TA的每日心情
    开心
    17 小时前
  • 签到天数: 148 天

    [LV.7]常住居民III

    9

    主题

    833

    回帖

    3207

    积分

    论坛元老

    积分
    3207
    发表于 2023-2-10 08:25:56 | 显示全部楼层

    1. #include <stdio.h>
    2. # define SIZE 100
    3. void enqueue();
    4. void dequeue();
    5. void show();
    6. int inp_arr[SIZE];
    7. int Rear = - 1;
    8. int Front = - 1;
    9. main()
    10. {
    11.     int ch;
    12.     while (1)
    13.     {
    14.         printf("1.Enqueue Operation\n");
    15.         printf("2.Dequeue Operation\n");
    16.         printf("3.Display the Queue\n");
    17.         printf("4.Exit\n");
    18.         printf("Enter your choice of operations : ");
    19.         scanf("%d", &ch);
    20.         switch (ch)
    21.         {
    22.             case 1:
    23.             enqueue();
    24.             break;
    25.             case 2:
    26.             dequeue();
    27.             break;
    28.             case 3:
    29.             show();
    30.             break;
    31.             case 4:
    32.             exit(0);
    33.             default:
    34.             printf("Incorrect choice \n");
    35.         }
    36.     }
    37. }
    38. void enqueue()
    39. {
    40.     int insert_item;
    41.     if (Rear == SIZE - 1)
    42.        printf("Overflow \n");
    43.     else
    44.     {
    45.         if (Front == - 1)
    46.       
    47.         Front = 0;
    48.         printf("Element to be inserted in the Queue\n : ");
    49.         scanf("%d", &insert_item);
    50.         Rear = Rear + 1;
    51.         inp_arr[Rear] = insert_item;
    52.     }
    53. }
    54. void dequeue()
    55. {
    56.     if (Front == - 1 || Front > Rear)
    57.     {
    58.         printf("Underflow \n");
    59.         return ;
    60.     }
    61.     else
    62.     {
    63.         printf("Element deleted from the Queue: %d\n", inp_arr[Front]);
    64.         Front = Front + 1;
    65.     }
    66. }
    67. void show()
    68. {
    69.    
    70.     if (Front == - 1)
    71.         printf("Empty Queue \n");
    72.     else
    73.     {
    74.         printf("Queue: \n");
    75.         for (int i = Front; i <= Rear; i++)
    76.             printf("%d ", inp_arr[i]);
    77.         printf("\n");
    78.     }
    79. }
    复制代码


    输出。
    1. 1.Enqueue Operation
    2. 2.Dequeue Operation
    3. 3.Display the Queue
    4. 4.Exit
    5. Enter your choice of operations : 1
    6. Element to be inserted in the Queue: 10
    7. 1.Enqueue Operation
    8. 2.Dequeue Operation
    9. 3.Display the Queue
    10. 4.Exit
    11. Enter your choice of operations : 1
    12. Element to be inserted in the Queue: 20
    13. 1.Enqueue Operation
    14. 2.Dequeue Operation
    15. 3.Display the Queue
    16. 4.Exit
    17. Enter your choice of operations : 3
    18. Queue:
    19. 10 20
    20. 1.Enqueue Operation
    21. 2.Dequeue Operation
    22. 3.Display the Queue
    23. 4.Exit
    24. Enter your choice of operations : 2
    25. Element deleted from the Queue: 10
    26. 1.Enqueue Operation
    27. 2.Dequeue Operation
    28. 3.Display the Queue
    29. 4.Exit
    30. Enter your choice of operations: 3
    31. Queue:
    32. 20
    复制代码

    点评

    打错个字,是芯片的空间不够,完整的代码超出太多,只能用简单的程序,先谢谢了  发表于 2023-2-12 14:14
    芯片的空间足够,完整的代码超出太多,只能用简单的程序,先谢谢了  发表于 2023-2-12 14:13

    该用户从未签到

    45

    主题

    2854

    回帖

    6416

    积分

    超级版主

    积分
    6416
    发表于 2023-2-10 11:25:13 | 显示全部楼层
    两个变量做指示,write用于写(入),read用于读(出)。read追着write跑即可,相等则无新数据。

    点评

    谢谢  发表于 2023-2-12 14:12
    回复 支持 1 反对 0 送花

    使用道具 举报

  • TA的每日心情
    开心
    2024-1-4 08:56
  • 签到天数: 14 天

    [LV.3]偶尔看看II

    16

    主题

    711

    回帖

    2510

    积分

    超级版主

    积分
    2510
    发表于 2023-2-10 17:16:31 | 显示全部楼层


    STC-ISP下载软件里面的串口范例程序中
    缓冲区就是最简单的先进先出的应用范例(FIFO队列环)
    1.png
  • TA的每日心情
    慵懒
    6 天前
  • 签到天数: 2 天

    [LV.1]初来乍到

    18

    主题

    41

    回帖

    426

    积分

    中级会员

    积分
    426
     楼主| 发表于 2023-2-12 14:20:06 | 显示全部楼层
    zhp 发表于 2023-2-10 17:16
    STC-ISP下载软件里面的串口范例程序中
    缓冲区就是最简单的先进先出的应用范例(FIFO队列环)

    谢谢提示

    该用户从未签到

    547

    主题

    9127

    回帖

    1万

    积分

    管理员

    积分
    14043
    发表于 2023-2-12 15:08:38 | 显示全部楼层
    本以为是最简单的演示程序,结果还有最简单的:最简单的先进先出的应用范例(FIFO队列环)
  • TA的每日心情
    慵懒
    6 天前
  • 签到天数: 2 天

    [LV.1]初来乍到

    18

    主题

    41

    回帖

    426

    积分

    中级会员

    积分
    426
     楼主| 发表于 2023-2-12 23:27:07 | 显示全部楼层
    神农鼎 发表于 2023-2-12 15:08
    本以为是最简单的演示程序,结果还有最简单的:最简单的先进先出的应用范例(FIFO队列环) ...

    经过测试,软件例程里串口那个确实是最简单的,几行代码就完成了需要的先进先出程序。
    移植到我的工程里,运行了一下,输入端的几路脉冲信号都能按时间顺序放进数组,然后通过串口发送到上位机。
    感谢官方支持,感谢热心网友支持!!!
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

    GMT+8, 2024-4-20 17:56 , Processed in 0.080388 second(s), 68 queries .

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

    快速回复 返回顶部 返回列表