第七周 项目六——停车场

来源:互联网 发布:unity3d xlua编程 编辑:程序博客网 时间:2024/05/16 10:58

问题及代码:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*                
  2. Copyright (c)2016,烟台大学计算机与控制工程学院                
  3. All rights reserved.                
  4. 文件名称:排队停车.cpp                
  5. 作    者:   陈朋          
  6. 完成日期:2016年10月13日                
  7. 版 本 号:v1.0                   
  8. 问题描述:           
  9. 输入描述:数字               
  10. 程序输出:若干。             
  11. */       
  12. #include <stdio.h>  
  13. #include <malloc.h>  
  14. #define N 10                    /*停车场内最多的停车数*/  
  15. #define M 10                    /*候车场内最多的停车数*/  
  16. #define Price 2             /*每单位时间停车费用*/  
  17. typedef struct  
  18. {  
  19.     int CarNo[N];           /*车牌号*/  
  20.     int CarTime[N];         /*进场时间*/  
  21.     int top;                /*栈指针*/  
  22. } SqStack;                  /*定义顺序栈类型,用于描述停车场*/  
  23.   
  24. typedef struct  
  25. {  
  26.     int CarNo[M];           /*车牌号*/  
  27.     int front,rear;         /*队首和队尾指针*/  
  28. } SqQueue;                  /*定义循环队类型,用于描述候车场*/  
  29.   
  30. /*以下为顺序栈的基本运算算法*/  
  31. void InitStack(SqStack *&s)  
  32. {  
  33.     s=(SqStack *)malloc(sizeof(SqStack));  
  34.     s->top=-1;  
  35. }  
  36. int StackEmpty(SqStack *s)  
  37. {  
  38.     return(s->top==-1);  
  39. }  
  40. int StackFull(SqStack *s)  
  41. {  
  42.     return(s->top==N-1);  
  43. }  
  44. int Push(SqStack *&s,int e1,int e2)  
  45. {  
  46.     if (s->top==N-1)  
  47.         return 0;  
  48.     s->top++;  
  49.     s->CarNo[s->top]=e1;  
  50.     s->CarTime[s->top]=e2;  
  51.     return 1;  
  52. }  
  53. int Pop(SqStack *&s,int &e1,int &e2)  
  54. {  
  55.     if (s->top==-1)  
  56.         return 0;  
  57.     e1=s->CarNo[s->top];  
  58.     e2=s->CarTime[s->top];  
  59.     s->top--;  
  60.     return 1;  
  61. }  
  62. void DispStack(SqStack *s)  
  63. {  
  64.     int i;  
  65.     for (i=s->top; i>=0; i--)  
  66.         printf("%d ",s->CarNo[i]);  
  67.     printf("\n");  
  68. }  
  69.   
  70. /*以下为循环队列的基本运算算法*/  
  71. void InitQueue(SqQueue *&q)  
  72. {  
  73.     q=(SqQueue *)malloc (sizeof(SqQueue));  
  74.     q->front=q->rear=0;  
  75. }  
  76. int QueueEmpty(SqQueue *q)  
  77. {  
  78.     return(q->front==q->rear);  
  79. }  
  80. int QueueFull(SqQueue *q)       /*判断队满*/  
  81. {  
  82.     return ((q->rear+1)%M==q->front);  
  83. }  
  84. int enQueue(SqQueue *&q,int e)      /*进队*/  
  85. {  
  86.     if ((q->rear+1)%M==q->front)    /*队满*/  
  87.         return 0;  
  88.     q->rear=(q->rear+1)%M;  
  89.     q->CarNo[q->rear]=e;  
  90.     return 1;  
  91. }  
  92. int deQueue(SqQueue *&q,int &e)     /*出队*/  
  93. {  
  94.     if (q->front==q->rear)          /*队空的情况*/  
  95.         return 0;  
  96.     q->front=(q->front+1)%M;  
  97.     e=q->CarNo[q->front];  
  98.     return 1;  
  99. }  
  100. void DispQueue(SqQueue *q)      /*输出队中元素*/  
  101. {  
  102.     int i;  
  103.     i=(q->front+1)%M;  
  104.     printf("%d ",q->CarNo[i]);  
  105.     while ((q->rear-i+M)%M>0)  
  106.     {  
  107.         i=(i+1)%M;  
  108.         printf("%d ",q->CarNo[i]);  
  109.     }  
  110.     printf("\n");  
  111. }  
  112.   
  113. //main函数用于模拟停车场的工作  
  114. int main()  
  115. {  
  116.     int comm;  
  117.     int no,e1,time,e2;  
  118.     int i,j,t;  
  119.     SqStack *St,*St1;  //St是停车场,St1是在有车离开时,记录为该车移开位置的车辆  
  120.     SqQueue *Qu;   //Qu是候车场  
  121.     InitStack(St);  
  122.     InitStack(St1);  
  123.     InitQueue(Qu);  
  124.     do  
  125.     {  
  126.         printf("输入指令(1:到达 2:离开 3:显示停车场 4:显示候车场 0:退出):");  
  127.         scanf("%d",&comm);  
  128.         switch(comm)  
  129.         {  
  130.         case 1:     /*汽车到达*/  
  131.             printf("输入车号和时间(设车号和时间均为整数): ");  
  132.             scanf("%d%d",&no,&time);  
  133.             if (!StackFull(St))         /*停车场不满*/  
  134.             {  
  135.                 Push(St,no,time);  
  136.                 printf("  >>停车场位置:%d\n",St->top+1);  
  137.             }  
  138.             else                        /*停车场满*/  
  139.             {  
  140.                 if (!QueueFull(Qu))     /*候车场不满*/  
  141.                 {  
  142.                     enQueue(Qu,no);  
  143.                     printf("  >>候车场位置:%d\n",Qu->rear);  
  144.                 }  
  145.                 else  
  146.                     printf("  >>候车场已满,不能停车\n");  
  147.             }  
  148.             break;  
  149.         case 2:     /*汽车离开*/  
  150.             printf("输入车号和时间(设车号和时间均为整数): ");  
  151.             scanf("%d%d",&no,&time);  
  152.             for (i=0; i<=St->top && St->CarNo[i]!=no; i++);  //在栈中找  
  153.             if (i>St->top)  
  154.                 printf("  >>未找到该编号的汽车\n");  
  155.             else  
  156.             {  
  157.                 t = St->top - i;  //需要出栈的车辆数目  
  158.                 for (j=0; j<t; j++)  //for (j=i; j<=St->top; j++)1楼评论讲的原错误写法  
  159.                 {  
  160.                     Pop(St,e1,e2);  
  161.                     Push(St1,e1,e2);        /*倒车到临时栈St1中*/  
  162.                 }  
  163.                 Pop(St,e1,e2);              /*该汽车离开*/  
  164.                 printf("  >>%d汽车停车费用:%d\n",no,(time-e2)*Price);  
  165.                 while (!StackEmpty(St1))    /*将临时栈St1重新回到St中*/  
  166.                 {  
  167.                     Pop(St1,e1,e2);  
  168.                     Push(St,e1,e2);  
  169.                 }  
  170.                 if (!QueueEmpty(Qu))        /*队不空时,将队头进栈St*/  
  171.                 {  
  172.                     deQueue(Qu,e1);  
  173.                     Push(St,e1,time);       /*以当前时间开始计费*/  
  174.                 }  
  175.             }  
  176.             break;  
  177.         case 3:     /*显示停车场情况*/  
  178.             if (!StackEmpty(St))  
  179.             {  
  180.                 printf("  >>停车场中的车辆:"); /*输出停车场中的车辆*/  
  181.                 DispStack(St);  
  182.             }  
  183.             else  
  184.                 printf("  >>停车场中无车辆\n");  
  185.             break;  
  186.         case 4:     /*显示候车场情况*/  
  187.             if (!QueueEmpty(Qu))  
  188.             {  
  189.                 printf("  >>候车场中的车辆:"); /*输出候车场中的车辆*/  
  190.                 DispQueue(Qu);  
  191.             }  
  192.             else  
  193.                 printf("  >>候车场中无车辆\n");  
  194.             break;  
  195.         case 0:     /*结束*/  
  196.             if (!StackEmpty(St))  
  197.             {  
  198.                 printf("  >>停车场中的车辆:"); /*输出停车场中的车辆*/  
  199.                 DispStack(St);  
  200.             }  
  201.             if (!QueueEmpty(Qu))  
  202.             {  
  203.                 printf("  >>候车场中的车辆:"); /*输出候车场中的车辆*/  
  204.                 DispQueue(Qu);  
  205.             }  
  206.             break;  
  207.         default:    /*其他情况*/  
  208.             printf("  >>输入的命令错误\n");  
  209.             break;  
  210.         }  
  211.     }  
  212.     while(comm!=0);  
  213.     return 0;  
  214. }  
  215. 运行结果:

0 0