【数据结构】关于停车场的管理

前端之家收集整理的这篇文章主要介绍了【数据结构】关于停车场的管理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. #include<string.h>
  5. #include<windows.h>
  6. #define SIZE 100
  7. #define PRICE 10
  8. typedef struct
  9. {
  10. char ID;
  11. clock_t start;
  12. }Nodestack;
  13. typedef struct
  14. {
  15. Nodestack* top;
  16. Nodestack* base;
  17. int size;
  18. }SQStack;
  19. typedef struct node
  20. {
  21. char data;
  22. struct node *next;
  23. }node;
  24. typedef struct
  25. {
  26. node *front;
  27. node *rear;
  28. }Linkqueue;
  29. /////////////////////////////////
  30. void Initstack(SQStack &l)
  31. {
  32. l.base=(Nodestack *)malloc(sizeof(Nodestack)*SIZE);
  33. l.top=l.base;
  34. l.size=SIZE;
  35. }
  36. void push(SQStack &l,Nodestack p)
  37. {
  38. *(l.top)=p;
  39. l.top++;
  40. }
  41. void popstack(SQStack &l,Nodestack &a)
  42. {
  43. a=*(--l.top);
  44. }
  45.  
  46. char gettop(SQStack &l)
  47. {
  48. if(l.top!=l.base)
  49. {
  50. Nodestack p;
  51. p=*(l.top-1);
  52. return p.ID;
  53. }
  54. return 0;
  55. }
  56. int stackempty(SQStack &l)
  57. {//空返回1
  58. if(l.top==l.base) return 1;
  59. else return 0;
  60. }
  61. void traverstack(SQStack l)
  62. {
  63. long temp_1;
  64. char temp_2;
  65. int i=0;
  66. Nodestack temp_3;
  67. if(stackempty(l)==1) printf("栈中无元素\n");
  68. else
  69. {
  70. for(i=0;l.top-i!=l.base;i++)//非空时执行
  71. {
  72. temp_3=*(l.top-i-1);
  73. temp_1=temp_3.start;
  74. temp_2=temp_3.ID;
  75. printf("%c %ld\n",temp_2,temp_1);
  76. }
  77. }
  78. }
  79. int lenghtstack(SQStack l)
  80. {
  81. int i=0;
  82. while(l.top-i!=l.base)
  83. {
  84. i++;
  85. }
  86. return i;
  87. }
  88. /////////////////////////////////////
  89. void Initqueue(Linkqueue &l)
  90. //初始化队列
  91. {
  92. l.front=l.rear=(node *)malloc(sizeof(node));
  93. if(!l.front) exit(0);
  94. l.front->next=NULL;
  95. }
  96. int queueempty(Linkqueue &l)
  97. //判断队列是否为空,空返回1,非空返回0
  98. {
  99. if(l.front==l.rear) return 1;
  100. else return 0;
  101. }
  102. int lenghtqueue(Linkqueue &l)
  103. //返回队列的长度
  104. {
  105. int i=0;
  106. node *p;
  107. p=l.front->next;
  108. while(p!=NULL)
  109. {
  110. i++;
  111. p=p->next;
  112. }
  113.  
  114. return i;
  115. }
  116. void enqueue(Linkqueue &l,char e)
  117. //对尾入栈
  118. {
  119. node *p;
  120. p=(node*)malloc(sizeof(node));
  121. p->data=e;
  122. l.rear->next=p;
  123. p->next=NULL;
  124. l.rear=p;
  125. }
  126.  
  127. char dequeue(Linkqueue &l)
  128. //对头出栈
  129. {
  130. node *p;
  131. if(l.front==l.rear) return 0;
  132. char q;
  133. p=l.front->next;
  134. q=(*p).data;
  135. l.front->next=p->next;
  136. if(p==l.rear) l.front=l.rear;
  137. free(p);
  138. return q;
  139. }
  140. void traversequeue(Linkqueue &l)
  141. //遍历队列
  142. {
  143. node *p=l.front->next;
  144. if(queueempty(l)==1) printf("队列里没有元素");
  145. while(p!=NULL&&queueempty(l)==0)
  146. {
  147. printf("%c",(*p).data);
  148. p=p->next;
  149. }
  150. }
  151. /////////////////////////////////////////////////////////////////////////////
  152. void function1(SQStack &l,Linkqueue &s,int n)
  153. {
  154. char temp;
  155. static int max=0;
  156. clock_t start;
  157. Nodestack nodes;
  158. //node nodeq;
  159. printf("请输入需要停车的车牌号:\t");
  160. scanf("%c",&temp);
  161. getchar();
  162. max=lenghtstack(l);
  163. if(max<n)
  164. {
  165. start=clock();
  166. nodes.ID=temp;
  167. nodes.start=start;
  168. push(l,nodes);
  169. printf("在停车场的第%d个位置,进入时间为:%ld",max+1,start);
  170. }
  171. else
  172. {
  173. start=clock();
  174. enqueue(s,temp);
  175. printf("在便道的第%d个位置,lenghtqueue(s),start);
  176. }
  177. Sleep(2000);
  178. }
  179. ////////////////////////////////////////////////////////////////////////////
  180. int function2(SQStack &l,SQStack &m)
  181. {
  182. char car_id,temp_1;
  183. double staytime;
  184. Nodestack temp_2,del;
  185. clock_t start,finish;
  186. printf("请输入离开车辆的车牌号:");
  187. scanf("%c",&car_id);
  188. getchar();
  189. temp_1=gettop(l);
  190. while(temp_1!=car_id)
  191. {
  192. if(stackempty(l)==1)
  193. {
  194. printf("不存在车牌号为%c的车辆",car_id);
  195. return 0;
  196. }
  197. else
  198. {
  199. popstack(l,temp_2);
  200. push(m,temp_2);
  201. temp_1=gettop(l);
  202. }//else
  203. }//while
  204. if(temp_1==car_id)
  205. {
  206. popstack(l,del);
  207. while(stackempty(m)==0)
  208. {
  209. popstack(m,temp_2);
  210. push(l,temp_2);
  211. }
  212. if(queueempty(s)==0)
  213. {
  214. temp_1=dequeue(s);
  215. start=clock();
  216. temp_2.ID=temp_1;
  217. temp_2.start=start;
  218. push(l,temp_2);
  219. }
  220. finish=clock();
  221. staytime=(double)(finish-del.start)/CLOCKS_PER_SEC;
  222. printf("车牌号:%c\t停留时长:%f sec\t应付款:¥%f\t",del.ID,staytime,staytime*PRICE );
  223. }
  224. Sleep(2000);
  225. return 0;
  226. }
  227. ////////////////////////////////////////////////
  228. void function3(SQStack &l,Linkqueue &s)
  229. {
  230. printf("便道上的车辆:\n");
  231. traversequeue(s);
  232. printf("\n");
  233. printf("停车道上的车辆:\n");
  234. traverstack(l);
  235. printf("\n");
  236. Sleep(2000);
  237. }
  238. ////////////////////////////////////
  239. void meun()
  240. {
  241. system("cls");
  242. printf("\t\t停车场管理目录\n");
  243. printf("\tA: 停车\n");
  244. printf("\tD: 离开\n");
  245. printf("\tC: 查看车场情况\n");
  246. printf("\tE: 退出系统\n");
  247. }
  248. ///////////////////////////////////////////////////////////////////////////
  249. void main()
  250. {
  251. SQStack stack1,stack2;
  252. Linkqueue queue;
  253. int n;//车库的容量
  254. char choice;
  255. Initstack(stack1);
  256. Initstack(stack2);
  257. Initqueue(queue);
  258.  
  259. printf("请输入停车场的容量:\t");
  260. scanf("%d",&n);
  261. getchar();
  262. meun();
  263. scanf("%c",&choice);
  264. getchar();
  265. while(choice!='E')
  266. {
  267. switch(choice)
  268. {
  269. case 'A':function1(stack1,queue,n); break;
  270. case 'D':function2(stack1,stack2); break;
  271. case 'C':function3(stack1,queue);break;
  272. default:;
  273. }
  274. if(choice=='E')
  275. printf("欢迎下次使用!\n");
  276. meun();
  277. scanf("%c",&choice);
  278. getchar();
  279. }
  280. }
弊端是输入的车牌号只是一位字符,可以做适当的优化

猜你在找的数据结构相关文章