WHCSRL 技术网

(c)(数据结构)栈与队列的应用--停车场管理

题目描述

 

 

 

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define TRUE 1
  5. #define FALSE 0
  6. #define N 2
  7. typedef struct
  8. {
  9. char num[20];//车牌照号码
  10. int arriveh,arrivem,arrives;//到达的时分秒
  11. int leaveh,leavem,leaves;//离开的时分秒
  12. }CAR;
  13. typedef struct
  14. {
  15. CAR car[N];
  16. int top;
  17. }PARKING_STACK;
  18. typedef struct
  19. {
  20. CAR car[N];
  21. int front;
  22. int rear;
  23. }SIDEWALK_QUEUE;
  24. void InitStack(PARKING_STACK *stack)
  25. {
  26. stack->top=-1;
  27. }
  28. void InitQueue(SIDEWALK_QUEUE *queue)
  29. {
  30. queue->front=queue->rear=0;
  31. }
  32. int PushStack(PARKING_STACK *stack,CAR car)
  33. {
  34. if(stack->top==N)
  35. return FALSE;
  36. stack->car[++stack->top]=car;
  37. return TRUE;
  38. }
  39. CAR PopStack(PARKING_STACK *stack)
  40. {
  41. if(stack->top==-1)
  42. {
  43. printf("栈空 ");
  44. return;
  45. }
  46. return stack->car[stack->top--];
  47. }
  48. int EnterQueue(SIDEWALK_QUEUE *queue,CAR car)
  49. {
  50. if((queue->rear+1)%%%%N==queue->front)
  51. return FALSE;
  52. queue->car[queue->rear]=car;
  53. queue->rear=(queue->rear+1)%%%%N;
  54. return TRUE;
  55. }
  56. CAR DeleteQueue(SIDEWALK_QUEUE *queue)
  57. {
  58. CAR car;
  59. if(queue->front==queue->rear)
  60. {
  61. printf("队列空 ");
  62. return;
  63. }
  64. car=queue->car[queue->front];
  65. queue->front=(queue->front+1)%%%%N;
  66. return car;
  67. }
  68. void PrintCar(PARKING_STACK *stack,SIDEWALK_QUEUE *queue)
  69. {
  70. int i;
  71. printf("PARKING: ");
  72. for(i=0;i<=stack->top;i++)
  73. {
  74. printf("%%%%d %%%%s ",i+1,stack->car[i].num);
  75. }
  76. printf("SIDEWALK: ");
  77. for(i=queue->front;i!=queue->rear;i=(i+1)%%%%N)
  78. {
  79. printf("%%%%d %%%%s ",i+1,queue->car[i].num);
  80. }
  81. }
  82. void ExitParking(PARKING_STACK *stack,PARKING_STACK *side_stack,CAR car)
  83. {
  84. CAR leaveCar;
  85. int minute;
  86. for(;strcmp(stack->car[stack->top].num,car.num)!=0;)
  87. {
  88. PushStack(side_stack,PopStack(stack));
  89. }
  90. leaveCar=PopStack(stack);
  91. minute=(car.leaveh*60+car.leavem)-(leaveCar.arriveh*60+leaveCar.arrivem);
  92. printf("%%%%s已离开,共停留%%%%d分钟 ",car.num,minute);
  93. for(;side_stack->top!=-1;)
  94. {
  95. PushStack(stack,PopStack(side_stack));
  96. }
  97. }
  98. int main()
  99. {
  100. int ch1,ch2;
  101. PARKING_STACK stack,side_stack;
  102. SIDEWALK_QUEUE queue;
  103. InitStack(&stack);
  104. InitStack(&side_stack);
  105. InitQueue(&queue);
  106. while(1)
  107. {
  108. CAR car;
  109. printf("请输入选项: ");
  110. printf("1:查看停车场和便道状态 ");
  111. printf("2:管理汽车 ");
  112. printf("3:exit ");printf("top:%%%%d ",stack.top);
  113. scanf("%%%%d",&ch1);
  114. switch(ch1)
  115. {
  116. case 1:
  117. PrintCar(&stack,&queue);
  118. break;
  119. case 2:
  120. printf("请输入到达还是离去(1或2),车牌号,以及对应时间(时 分 秒) ");
  121. scanf("%%%%d",&ch2);
  122. if(ch2==1)//到达
  123. {
  124. scanf("%%%%s %%%%d %%%%d %%%%d",car.num,&car.arriveh,&car.arrivem,&car.arrives);
  125. if(stack.top==N-1)//入队列
  126. {
  127. if(EnterQueue(&queue,car)==TRUE)
  128. printf("%%%%s成功入队! ",car.num);
  129. else
  130. printf("%%%%s未能入队! ",car.num);
  131. }
  132. else//能入停车场
  133. {
  134. if(PushStack(&stack,car)==TRUE)
  135. printf("%%%%s进入停车场! ",car.num);
  136. else
  137. printf("队满,%%%%s未能进停车场! ",car.num);
  138. }
  139. }
  140. if(ch2==2)
  141. {
  142. int flag=0;
  143. int i;
  144. scanf("%%%%s %%%%d %%%%d %%%%d",car.num,&car.leaveh,&car.leavem,&car.leaves);
  145. for(i=0;i<=stack.top;i++)
  146. {
  147. if(strcmp(car.num,stack.car[i].num)==0)
  148. {
  149. flag=1;
  150. break;
  151. }
  152. }
  153. if(!flag)
  154. {
  155. printf("查无此车! ");
  156. break;
  157. }
  158. ExitParking(&stack,&side_stack,car);
  159. if(queue.front!=queue.rear)
  160. {
  161. CAR car=DeleteQueue(&queue);
  162. PushStack(&stack,car);
  163. printf("%%%%s已进入停车场! ",car.num);
  164. }
  165. }
  166. break;
  167. case 3:
  168. exit(0);
  169. default:
  170. printf("input again! ");
  171. }
  172. }
  173. return 0;
  174. }

推荐阅读