第七周--项目6停车场模拟

  1. *Copyright(c)2016,烟台大学计算机与控制工程学院  
  2.  *All right reserved.  
  3.  *文件名称:停车场模拟.cpp  
  4.  *作者:陈晓琳  
  5.  *完成日期;2015年10月14日  
  6.  *版本号;v1.0  
  7.  *  
  8.  *问题描述:   
  9.    设停车场是一个可停放n辆汽车的狭长死胡同,南边封口,汽车只能从北边进出(这样的停车场世间少有)。  
  10. 汽车在停车场内按车辆到达时间的先后顺序,最先到达的第一辆车停放在车场的最南端,  
  11. 依次向北排开。若车场内已停满n辆汽车,则后来的汽车只能在门外的候车场上等候,一旦有车开走,  
  12. 则排在候车场上的第一辆车即可开入。当停车场内某辆车要离开时,  
  13. 在它之后进入的车辆必须先退出车场为它让路(假定停车场内设有供车辆进出的便道,所有的司机也必须在车内随时待命),  
  14. 待该辆车开出外,其他车辆再按原次序进入车场。每辆停放在车场的车在它离开停车场时,要按停留的时间长短交纳费用。  
  15. 试为停车场编制按上述要求进行管理的模拟程序。   
  16.   
  17.   这里写图片描述  
  18.   
  19.   
  20. 提示:   
  21.    以栈模拟停车场,以队列模拟车场外的候车场,有车离开时,供车辆进出的便道也应该用栈表示。  
  22. 按照从键盘读入的输入数据序列进行模拟管理。  
  23. 汽车到达和离开时,每一组输入数据包括汽车牌照号码(设为整数)以及到达或离去的时刻(为简单化,也设为整数,如1,代表停车场开始营业的第1小时)。  
  24. 对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出汽车在停车场内或修车场上的停车位置;  
  25. 若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在候车场上停留的时间不收费)。  
  26. 栈以顺序结构实现,队列以链表结构实现。  
  27.   
  28.   
  29.  *输入描述:根据要求输入数字  
  30.  *程序输出:根据输入的数字判断停车场与候车场的情况  
  31. */  
  32. #include <stdio.h>  
  33. #include <malloc.h>  
  34. #define N 3                 /*停车场内最多的停车数*/  
  35. #define M 4                 /*候车场内最多的停车数*/  
  36. #define Price 2             /*每单位时间停车费用*/  
  37. typedef struct  
  38. {  
  39.     int CarNo[N];           /*车牌号*/  
  40.     int CarTime[N];         /*进场时间*/  
  41.     int top;                /*栈指针*/  
  42. } SqStack;                  /*定义顺序栈类型,用于描述停车场*/  
  43.   
  44. typedef struct  
  45. {  
  46.     int CarNo[M];           /*车牌号*/  
  47.     int front,rear;         /*队首和队尾指针*/  
  48. } SqQueue;                  /*定义循环队类型,用于描述候车场*/  
  49.   
  50. /*以下为顺序栈的基本运算算法*/  
  51. void InitStack(SqStack *&s)  
  52. {  
  53.     s=(SqStack *)malloc(sizeof(SqStack));  
  54.     s->top=-1;  
  55. }  
  56. int StackEmpty(SqStack *s)  
  57. {  
  58.     return(s->top==-1);  
  59. }  
  60. int StackFull(SqStack *s)  
  61. {  
  62.     return(s->top==N-1);  
  63. }  
  64. int Push(SqStack *&s,int e1,int e2)  
  65. {  
  66.     if (s->top==N-1)  
  67.         return 0;  
  68.     s->top++;  
  69.     s->CarNo[s->top]=e1;  
  70.     s->CarTime[s->top]=e2;  
  71.     return 1;  
  72. }  
  73. int Pop(SqStack *&s,int &e1,int &e2)  
  74. {  
  75.     if (s->top==-1)  
  76.         return 0;  
  77.     e1=s->CarNo[s->top];  
  78.     e2=s->CarTime[s->top];  
  79.     s->top–;  
  80.     return 1;  
  81. }  
  82. void DispStack(SqStack *s)  
  83. {  
  84.     int i;  
  85.     for (i=s->top; i>=0; i–)  
  86.         printf(“%d “,s->CarNo[i]);  
  87.     printf(“\n”);  
  88. }  
  89.   
  90. /*以下为循环队列的基本运算算法*/  
  91. void InitQueue(SqQueue *&q)  
  92. {  
  93.     q=(SqQueue *)malloc (sizeof(SqQueue));  
  94.     q->front=q->rear=0;  
  95. }  
  96. int QueueEmpty(SqQueue *q)  
  97. {  
  98.     return(q->front==q->rear);  
  99. }  
  100. int QueueFull(SqQueue *q)       /*判断队满*/  
  101. {  
  102.     return ((q->rear+1)%M==q->front);  
  103. }  
  104. int enQueue(SqQueue *&q,int e)      /*进队*/  
  105. {  
  106.     if ((q->rear+1)%M==q->front)    /*队满*/  
  107.         return 0;  
  108.     q->rear=(q->rear+1)%M;  
  109.     q->CarNo[q->rear]=e;  
  110.     return 1;  
  111. }  
  112. int deQueue(SqQueue *&q,int &e)     /*出队*/  
  113. {  
  114.     if (q->front==q->rear)          /*队空的情况*/  
  115.         return 0;  
  116.     q->front=(q->front+1)%M;  
  117.     e=q->CarNo[q->front];  
  118.     return 1;  
  119. }  
  120. void DispQueue(SqQueue *q)      /*输出队中元素*/  
  121. {  
  122.     int i;  
  123.     i=(q->front+1)%M;  
  124.     printf(“%d “,q->CarNo[i]);  
  125.     while ((q->rear-i+M)%M>0)  
  126.     {  
  127.         i=(i+1)%M;  
  128.         printf(“%d “,q->CarNo[i]);  
  129.     }  
  130.     printf(“\n”);  
  131. }  
  132.   
  133. //main函数用于模拟停车场的工作  
  134. int main()  
  135. {  
  136.     int comm;  
  137.     int no,e1,time,e2;  
  138.     int i,j;  
  139.     SqStack *St,*St1;  //St是停车场,St1是在有车离开时,记录为该车移开位置的车辆  
  140.     SqQueue *Qu;   //Qu是候车场  
  141.     InitStack(St);  
  142.     InitStack(St1);  
  143.     InitQueue(Qu);  
  144.     do  
  145.     {  
  146.         printf(“输入指令(1:到达 2:离开 3:显示停车场 4:显示候车场 0:退出):”);  
  147.         scanf(“%d”,&comm);  
  148.         switch(comm)  
  149.         {  
  150.         case 1:     /*汽车到达*/  
  151.             printf(“输入车号和时间(设车号和时间均为整数): “);  
  152.             scanf(“%d%d”,&no,&time);  
  153.             if (!StackFull(St))         /*停车场不满*/  
  154.             {  
  155.                 Push(St,no,time);  
  156.                 printf(”  >>停车场位置:%d\n”,St->top+1);  
  157.             }  
  158.             else                        /*停车场满*/  
  159.             {  
  160.                 if (!QueueFull(Qu))     /*候车场不满*/  
  161.                 {  
  162.                     enQueue(Qu,no);  
  163.                     printf(”  >>候车场位置:%d\n”,Qu->rear);  
  164.                 }  
  165.                 else  
  166.                     printf(”  >>候车场已满,不能停车\n”);  
  167.             }  
  168.             break;  
  169.         case 2:     /*汽车离开*/  
  170.             printf(“输入车号和时间(设车号和时间均为整数): “);  
  171.             scanf(“%d%d”,&no,&time);  
  172.             for (i=0; i<=St->top && St->CarNo[i]!=no; i++);  //在栈中找  
  173.             if (i>St->top)  
  174.                 printf(”  >>未找到该编号的汽车\n”);  
  175.             else  
  176.             {  
  177.                 for (j=i; j<=St->top; j++)  
  178.                 {  
  179.                     Pop(St,e1,e2);  
  180.                     Push(St1,e1,e2);        /*倒车到临时栈St1中*/  
  181.                 }  
  182.                 Pop(St,e1,e2);              /*该汽车离开*/  
  183.                 printf(”  >>%d汽车停车费用:%d\n”,no,(time-e2)*Price);  
  184.                 while (!StackEmpty(St1))    /*将临时栈St1重新回到St中*/  
  185.                 {  
  186.                     Pop(St1,e1,e2);  
  187.                     Push(St,e1,e2);  
  188.                 }  
  189.                 if (!QueueEmpty(Qu))        /*队不空时,将队头进栈St*/  
  190.                 {  
  191.                     deQueue(Qu,e1);  
  192.                     Push(St,e1,time);       /*以当前时间开始计费*/  
  193.                 }  
  194.             }  
  195.             break;  
  196.         case 3:     /*显示停车场情况*/  
  197.             if (!StackEmpty(St))  
  198.             {  
  199.                 printf(”  >>停车场中的车辆:”); /*输出停车场中的车辆*/  
  200.                 DispStack(St);  
  201.             }  
  202.             else  
  203.                 printf(”  >>停车场中无车辆\n”);  
  204.             break;  
  205.         case 4:     /*显示候车场情况*/  
  206.             if (!QueueEmpty(Qu))  
  207.             {  
  208.                 printf(”  >>候车场中的车辆:”); /*输出候车场中的车辆*/  
  209.                 DispQueue(Qu);  
  210.             }  
  211.             else  
  212.                 printf(”  >>候车场中无车辆\n”);  
  213.             break;  
  214.         case 0:     /*结束*/  
  215.             if (!StackEmpty(St))  
  216.             {  
  217.                 printf(”  >>停车场中的车辆:”); /*输出停车场中的车辆*/  
  218.                 DispStack(St);  
  219.             }  
  220.             if (!QueueEmpty(Qu))  
  221.             {  
  222.                 printf(”  >>候车场中的车辆:”); /*输出候车场中的车辆*/  
  223.                 DispQueue(Qu);  
  224.             }  
  225.             break;  
  226.         default:    /*其他情况*/  
  227.             printf(”  >>输入的命令错误\n”);  
  228.             break;  
  229.         }  
  230.     }  
  231.     while(comm!=0);  
  232.     return 0;  
  233. }  

运行结果:

《第七周--项目6停车场模拟》

知识点总结:

先来候车的汽车先去停车,所以这就运用了队列先进先出的特点。无论是候车室还是停车场,栈满与队满同样输出不能再进入车辆,车辆的停车费则只是根据时间的差距来计算。

学习心得:

学习要越挫越勇

    原文作者:停车场模拟问题
    原文地址: https://blog.csdn.net/cc1562cxl/article/details/52813595
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞