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

来源:互联网 发布:linux编程获取cpu温度 编辑:程序博客网 时间:2024/05/18 02:23
/*
Copyright (c)2016,烟台大学计算机与控制工程学院
All rights reserved.
文件名称:第7周项目项目6 - 停车场模拟.cpp
作 者:程德泉
完成日期:2016年10月13日
版 本 号:v1.0

问题描述:设停车场是一个可停放n辆汽车的狭长死胡同,南边封口,汽车只能从北边进出(这样的停车场世间少有)。
汽车在停车场内按车辆到达时间的先后顺序,最先到达的第一辆车停放在车场的最南端,依次向北排开。若车场内已停满n辆汽车,则后来的汽车只能在门外的候车场上等候,一旦有车开走,
则排在候车场上的第一辆车即可开入。当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路(假定停车场内设有供车辆进出的便道,所有的司机也必须在车内随时待命),
待该辆车开出外,其他车辆再按原次序进入车场。每辆停放在车场的车在它离开停车场时,要按停留的时间长短交纳费用。
试为停车场编制按上述要求进行管理的模拟程序。
输入描述:按屏幕提示输入。
程序输出:各操作后的输出。

*/

代码:

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


学习心得:

停车场模拟可以看成一个简单的栈或队列结构,可以先进先出,也可以设成摩天轮停车场的模型,顺时针逆时针都可以转。

0 0
原创粉丝点击