第七周 项目6

来源:互联网 发布:互联网大数据论文题目 编辑:程序博客网 时间:2024/06/05 23:24

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

提示:以栈模拟停车场,以队列模拟车场外的候车场,有车离开时,供车辆进出的便道也应该用栈表示。按照从键盘读入的输入数据序列进行模拟管理。汽车到达和离开时,每一组输入数据包括汽车牌照号码(设为整数)以及到达或离去的时刻(为简单化,也设为整数,如1,代表停车场开始营业的第1小时)。对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出汽车在停车场内或修车场上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在候车场上停留的时间不收费)。栈以顺序结构实现,队列以链表结构实现。


[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #define N 10                    /*停车场内最多的停车数*/  
  4. #define M 10                    /*候车场内最多的停车数*/  
  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,t;  
  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.                 t = St->top - i;  //需要出栈的车辆数目  
  147.                 for (j=0; j<t; j++)  //for (j=i; j<=St->top; j++)1楼评论讲的原错误写法  
  148.                 {  
  149.                     Pop(St,e1,e2);  
  150.                     Push(St1,e1,e2);        /*倒车到临时栈St1中*/  
  151.                 }  
  152.                 Pop(St,e1,e2);              /*该汽车离开*/  
  153.                 printf("  >>%d汽车停车费用:%d\n",no,(time-e2)*Price);  
  154.                 while (!StackEmpty(St1))    /*将临时栈St1重新回到St中*/  
  155.                 {  
  156.                     Pop(St1,e1,e2);  
  157.                     Push(St,e1,e2);  
  158.                 }  
  159.                 if (!QueueEmpty(Qu))        /*队不空时,将队头进栈St*/  
  160.                 {  
  161.                     deQueue(Qu,e1);  
  162.                     Push(St,e1,time);       /*以当前时间开始计费*/  
  163.                 }  
  164.             }  
  165.             break;  
  166.         case 3:     /*显示停车场情况*/  
  167.             if (!StackEmpty(St))  
  168.             {  
  169.                 printf("  >>停车场中的车辆:"); /*输出停车场中的车辆*/  
  170.                 DispStack(St);  
  171.             }  
  172.             else  
  173.                 printf("  >>停车场中无车辆\n");  
  174.             break;  
  175.         case 4:     /*显示候车场情况*/  
  176.             if (!QueueEmpty(Qu))  
  177.             {  
  178.                 printf("  >>候车场中的车辆:"); /*输出候车场中的车辆*/  
  179.                 DispQueue(Qu);  
  180.             }  
  181.             else  
  182.                 printf("  >>候车场中无车辆\n");  
  183.             break;  
  184.         case 0:     /*结束*/  
  185.             if (!StackEmpty(St))  
  186.             {  
  187.                 printf("  >>停车场中的车辆:"); /*输出停车场中的车辆*/  
  188.                 DispStack(St);  
  189.             }  
  190.             if (!QueueEmpty(Qu))  
  191.             {  
  192.                 printf("  >>候车场中的车辆:"); /*输出候车场中的车辆*/  
  193.                 DispQueue(Qu);  
  194.             }  
  195.             break;  
  196.         default:    /*其他情况*/  
  197.             printf("  >>输入的命令错误\n");  
  198.             break;  
  199.         }  
  200.     }  
  201.     while(comm!=0);  
  202.     return 0;  
  203. }  
原创粉丝点击