鱼C论坛

 找回密码
 立即注册
查看: 285|回复: 2

求助

[复制链接]
发表于 2023-11-2 11:41:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.超市内商品的信息包括商品编号和生产日期。用于商品展示的货架,是一个半封闭的货架,客人拿取商品,工作人员上架新的商品,都只能固定在开放的一端进行。当工作人员上架新商品时,需要整理展示架上的商品,使生产日期较早的商品,放在靠近固定开放的一端。请编程模拟对商品展示架中已有商品按生产日期进行整理的过程。

    2.“小猫钓鱼”的游戏规则是:将一副扑克牌平均分成两份,每人拿一份。玩家甲先拿出手中的第一张扑克牌放在桌上,然后玩家乙也拿出手中的第一张扑克牌,并放在玩家甲刚打出的扑克牌的上面,就像这样两个玩家交替出牌。出牌时,如果某人打出的牌与桌上某张牌的牌面相同,即可将两张相同的牌及其中间所夹的牌全部取走,并依次放到自己手中牌的末尾。当任意一个人手中的牌全部出完时,游戏结束,对手获胜。

   要求编写程序来模拟这场游戏,并判断出谁最后获胜,获胜的同时打印出获胜者手中的牌以及桌上可能剩余的牌。

   为了简化实现,先做这样一个约定,玩家甲和乙手中牌的牌面值只有1-9。

   测试样例(测试样例均采用输入前规定当前发牌数量的方式)

序号

输入

输出

1

请输入发牌数:6

玩家甲:2 4 1 2 5 6

玩家乙:3 1 3 5 6 4

游戏结束:玩家乙赢

玩家乙手中牌为:6 5 2 3 4 1

桌面上还有牌为:3 4 5 6 2 1

2

请输入发牌数:8

玩家甲:5 4 1 3 7 9 6 2

玩家乙:6 2 4 8 5 7 1 3

游戏结束:玩家乙赢

玩家乙手中牌为:4  1  2  4  5  7  8  3  6  5

桌面上还有牌为:9  7  6  1  2  3

3

请输入发牌数:9

玩家甲:3 4 5 6 2 1 8 7 9

玩家乙:6 5 2 3 4 1 9 7 8

游戏结束:玩家甲赢

玩家乙手中牌为:2  1  6  3  4  2  9  8  9  5  6  3  7  5

桌面上还有牌为:1  4  8  7
以下是我写的代码,有许多问题,可以帮我看看吗。
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define maxqsize 100
  4. #define stack_init_size 100
  5. #define stackincrement 10
  6. #define OK 1
  7. #define ERROR 0
  8. typedef int status;
  9. typedef struct            //声明结构体Production用于表示生产日期
  10. {   int year;
  11.     int month;
  12.     int day;
  13. }production;
  14. typedef struct
  15. {    int  number;                  //商品编号
  16.          production date;           //商品生产日期
  17. }selemtype;
  18. typedef struct{
  19.         selemtype *base;//栈底指针
  20.         selemtype *top;//栈顶指针
  21.         int date[9];
  22.         int stacksize;
  23. }sqstack;
  24. typedef struct{  //队列的结构体
  25.         selemtype *base;
  26.         int front;
  27.         int rear;
  28.         int date[100];
  29. }sqqueue;
  30. void initqueue(sqqueue &q)  //队列初始化
  31. {
  32.         q.base=(selemtype*)malloc(maxqsize*sizeof(selemtype));
  33.         q.rear=q.front=0;
  34. }
  35. status enqueue(sqqueue &q,selemtype e)//入队
  36. {
  37.                 q.base[q.rear]=e;
  38.                 q.rear=(q.rear+1)%maxqsize;
  39.                 return OK;
  40. }
  41. status dequeue(sqqueue &q,selemtype &e)//出队
  42. {
  43.                 e=q.base[q.front];
  44.                 q.front=(q.front+1)%maxqsize;
  45.                 return OK;
  46. }
  47. void printq(sqqueue q)
  48. {
  49.         int r=q.front;
  50.                 while(r!=q.rear)
  51.                 {printf("%d ",q.base[r]);
  52.                  r++;}
  53.         printf("\n");
  54. }
  55. void initstack(sqstack &s)
  56. {
  57.         s.base=(selemtype*)malloc(stack_init_size*sizeof(selemtype));
  58.         s.top=s.base;
  59.         s.stacksize=stack_init_size;
  60. }
  61. void push(sqstack &s,selemtype e)//入栈
  62. {
  63.         if(s.top-s.base>=s.stacksize){
  64.                 s.base=(selemtype*)realloc(s.base,(s.stacksize+stack_init_size)*sizeof(selemtype));
  65.                 if(!s.base)  
  66.                        printf("error");
  67.                            s.top=s.base+s.stacksize;
  68.                            s.stacksize+=stack_init_size;
  69.         }
  70.         *s.top=e;
  71.         s.top++;
  72. }
  73. status pop(sqstack &s,selemtype &k)//出栈
  74. {
  75.         int i=0;
  76.         if(s.base==s.top){
  77.                 push(s,k);
  78.         }
  79.         s.top--;
  80.         k=*s.top;
  81.         return OK;
  82. }
  83. status gettop(sqstack s,selemtype &k)//取栈顶元素
  84. {
  85.         if(s.top!=s.base){
  86.       k=*(s.top-1);
  87.           return OK;
  88.         }
  89.         return ERROR;
  90. }
  91. void print(sqstack &s)//输出栈
  92. {
  93.         selemtype *e=s.base;
  94.         int i=0;
  95.         while(e<s.top)
  96.         {
  97.                 printf("商品编号为%d,生产日期为%d %d %d\n",e->number,e->date.year,e->date.month,e->date.day);
  98.                 i++;
  99.                 e++;
  100.         }
  101. }
  102. status judge(sqstack &s,selemtype e)//栈顶元素大于输入返回1
  103. {
  104.         selemtype k;
  105.         gettop(s,k);
  106.         if(k.date.year>e.date.year){
  107.                 return OK;
  108.         }
  109.         else if(k.date.year==e.date.year){
  110.                 if(k.date.month<e.date.month){
  111.                         return OK;}
  112.                 else if(k.date.month==e.date.month){
  113.                         if(k.date.day>e.date.day){
  114.                                 return OK;
  115.                         }
  116.                         else if(k.date.day==e.date.day){
  117.                                 return OK;
  118.                         }
  119.                 }
  120.                 }
  121.         else {
  122.                 return ERROR;
  123.         }
  124. }
  125. void Lineup()
  126. {
  127.         int i,j=1,t;
  128.         sqstack s,temp;
  129.         initstack(s);
  130.         initstack(temp);
  131.         while(j!=4)
  132.         {
  133.                 printf("1.入库  2.出库  3.查看货物   4.退出\n");
  134.                 scanf("%d",&j);
  135.                 switch(j){
  136.                         case 1:{
  137.                                         selemtype e,k;
  138.                                         printf("请输入商品编号及生产日期:\n");
  139.                                    scanf("%d %d %d %d",&e.number,&e.date.year,&e.date.month,&e.date.day);
  140.                                         if(s.top==s.base)
  141.                                         {
  142.                                                 push(s,e);
  143.                                         }
  144.                                         else{
  145.                                                         t=judge(s,e);
  146.                                                         while(t==0){
  147.                                                                   pop(s,k);
  148.                                                                   push(temp,k);
  149.                                                                   t=judge(s,e);
  150.                                                         }
  151.                                                         push(s,e);
  152.                                                         while(temp.base!=temp.top)
  153.                                                         {
  154.                                                                 pop(temp,k);
  155.                                                                 push(s,k);
  156.                                                         }
  157.                                                 }
  158.                                                 break;}
  159.                         case 2: {
  160.                                          selemtype w;
  161.                                      pop(s,w);
  162.                                          printf("该商品编号为%d,生产日期为%d %d %d\n",w.number,w.date.year,w.date.month,w.date.day);
  163.                                          break;}
  164.                     case 3:{
  165.                                         print(s);
  166.                                         break;}
  167.                 }
  168.                 }
  169. }

  170. void game()
  171. {
  172.         int n,i,b[9],t,x,j;
  173.         sqqueue q1,q2;
  174.         sqstack s2;
  175.         selemtype e,k,f;
  176.         for(i=0;i<9;i++)
  177.         {
  178.                 b[i]=0;
  179.         }
  180.         initqueue(q1);
  181.         initqueue(q2);
  182.         initstack(s2);
  183.         printf("请输入发牌数:(1-9)");
  184.         scanf("%d",&n);
  185.         printf("玩家甲的牌为:");
  186.         for(i=0;i<n;i++){
  187.                 scanf("%d",&e);
  188.                 enqueue(q1,e);
  189.         }
  190.         printq(q1);
  191.         printf("玩家乙的牌为:");
  192.         for(j=0;j<n;j++){
  193.                 scanf("%d",&e);
  194.                 enqueue(q2,e);
  195.         }
  196.         printq(q2);
  197.         int a=1;
  198.         printf("%d",q1.date[q1.front]);
  199.         while(q1.front!=q1.rear&&q2.front!=q2.rear)
  200.         {
  201.                 if(a==1){
  202.                         dequeue(q1,e);
  203.                         t=q1.front;
  204.                         a=0;
  205.                 }
  206.                 else
  207.                 {
  208.                         dequeue(q2,e);
  209.                         t=q2.front;
  210.                         a=1;
  211.                 }
  212.                         if(b[t]==1){
  213.                                 if(a==0){
  214.                                         enqueue(q1,e);
  215.                                     while(t!=x){
  216.                                         pop(s2,k);
  217.                                         x=(int)s2.top-1;
  218.                                         b[x]=0;
  219.                                         enqueue(q1,k);
  220.                                     }
  221.                                 }
  222.                                 else
  223.                                         enqueue(q2,e);
  224.                                     while(t!=x){
  225.                                         pop(s2,k);
  226.                                         x=(int)s2.top-1;
  227.                                         b[x]=0;
  228.                                         enqueue(q2,k);
  229.                                     }
  230.                         }
  231.                         else
  232.                                 b[t]=0;
  233.         }
  234.         if(q1.front==q1.rear){
  235.                 printf("游戏结束:玩家乙获胜!\n");
  236.                 printf("玩家乙手中牌为:");
  237.                  printq(q2);
  238.         }
  239.         else
  240.                 printf("游戏结束:玩家甲获胜!\n");
  241.                 printf("玩家甲手中牌为:");
  242.                  printq(q2);
  243.         printf("桌面上剩的牌为:");
  244.         print(s2);
  245. }
  246. int main()
  247. {
  248.         Lineup();
  249.         game();
  250.     return 0;
  251. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-2 11:41:44 | 显示全部楼层
问题1:代码中存在一些错误和问题,需要进行修正和改进。

1. 在函数 printq(sqqueue q) 中,打印队列元素时,应该使用 q.base[r].number 而不是 q.base[r] 。

2. 在函数 print(sqstack &s) 中,打印栈元素时,应该使用 e->date.year 、 e->date.month 和 e->date.day 而不是 e->date.year 、 e->date.month 和 e->date.day 。

3. 在函数 judge(sqstack &s,selemtype e) 中,应该将 selemtype k 的声明改为 selemtype k = {}; ,否则会导致未初始化的变量被使用。

4. 在函数 judge(sqstack &s,selemtype e) 中,判断条件中的 k.date.day == e.date.day 应该改为 k.date.day > e.date.day ,因为题目要求将生产日期较早的商品放在靠近固定开放一端。

5. 在函数 Lineup() 中,第一个 while 循环的条件应该是 j != 4 而不是 j != 0 ,因为题目要求输入4时退出。

6. 在函数 Lineup() 中,第二个 while 循环中的 scanf("%d %d %d %d", &e.number, &e.date.year, &e.date.month, &e.date.day); 应该改为 scanf("%d %d %d %d", &e.number, &e.date.year, &e.date.month, &e.date.day); ,因为需要输入4个整数。

7. 在函数 Lineup() 中,第二个 while 循环中的 push(s, k); 应该改为 push(s, k); ,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-2 11:41:50 | 显示全部楼层
你的输入太长了系统无法处理

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-29 01:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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