C语言小功能实现--停车场功能
问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门
外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车
要先退出,待它走后在依次进入。
4 * 文件名称:park.c5 * 创 建 者:Y6 * 创建日期:2018年02月22日7 * 描 述:8 *9 *****************************************************************/10 11 #include <string.h>12 #include <stdio.h>13 #include <time.h>14 #include <stdlib.h>15 #define TRUE 116 #define FALSE 017 #define MAX 218 #define Status int19 typedef struct //定义车辆数据类型 20 {21 int year,month,day,hour,min,sec;22 int n;23 }Car;24 25 typedef struct //停车栈 顺序结构26 {27 Car car[MAX];28 int top1;29 }park;30 31 typedef struct //让路栈顺序结构32 {33 Car car[MAX];34 int top2;35 }giveway;36 37 typedef struct LeaveQueue //存放已经离开的车队列,链表结构38 {39 Car car;40 struct LeaveQueue *next;41 }LeaveQueue,*LQueue;42 43 44 typedef struct45 {46 LQueue front;47 LQueue rear;48 }lqueue;49 50 51 typedef struct waitqueue //等待队列,链表结构52 {53 int data;54 struct waitqueue *next;55 }WaitQueue,*WQueue;56 57 typedef struct58 {59 WQueue front;60 WQueue rear;61 }wqueue;62 63 Status ParkInit(park *p) //新建停车栈64 {65 p->top1=-1;66 return TRUE;67 }68 69 Status GivewayInit(giveway *g) //新建让路栈70 {71 g->top2=-1;72 return TRUE;73 }74 75 Status WaitQueueInit(wqueue *q) //新建等候队列76 {77 78 q->front=q->rear=(WQueue)malloc(sizeof(WaitQueue));79 if(!q->front)80 {81 printf("Memory allocation failed ! press any key to exit...");82 getchar();83 exit(0);84 }85 q->front->next=NULL;86 return TRUE;87 }88 89 Status LeaveQueueInit(lqueue *l) //新建离开车辆的队列90 {91 92 l->front=l->rear=(LQueue)malloc(sizeof(LeaveQueue));93 if(!l->front)94 {95 printf("Memory allocation failed ! press any key to exit...");96 getchar();97 exit(0);98 }99 l->front->next=NULL;
100 return TRUE;
101 }
102
103
104
105 ParkEmpty(park p) //判断停车栈是否为空
106 {if (-1 == p.top1)
107 {
108 return TRUE;
109 }
110 else
111 {
112 return FALSE;
113 }
114
115 }
116
117
118 Parkfull(park p) //判断停车栈是否满
119 {
120 if(p.top1 == MAX-1)
121 {
122 printf("停车场满了!\n");
123 return TRUE;
124 }
125 else
126 return FALSE;
127 }
128
129 QueueEmpty(wqueue q) //判断等候队列是否为空
130 {
131 if(q.rear==q.front)
132 {
133 return TRUE;
134 }
135 else
136 return FALSE;
137 }
138
139
140
141 int Copy(Car *p,struct tm *it) //时间转化,将tm时间格式的时间转化为现实时间
142 {
143 p->year = it->tm_year+1900;
144 p->month = it->tm_mon+1;
145 p->day = it->tm_mday;
146 p->hour = it->tm_hour;
147 p->min = it->tm_min;
148 p->sec = it->tm_sec;
149 }
150
151
152 int LTime(Car *p1,Car *p2) //计算停留时间,用离开时的时间减去停车时间
153 {
154 p2->year = p2->year-p1->year;
155 p2->month = p2->month-p1->month;
156 p2->day = p2->day-p1->day;
157 p2->hour = p2->hour-p1->hour;
158 p2->min = p2->min-p1->min;
159 p2->sec = p2->sec-p1->sec;
160 }
161
162
163 PushPark(park *p,int n) //车辆停车,入停车栈
164 {
165 Car e;
166 time_t t;
167 struct tm *it;
168 time(&t);
169 it = localtime(&t);
170 Copy(&e,it);
171 e.n=n;
172 p->top1++;
173 p->car[p->top1]=e;
174 printf("车牌号为= %d 停车成功\n",e.n);
175 printf("进入时间= %d %d.%d %d:%d:%d\n ",e.year,e.month,e.day,e.hour,e.min,e.sec);
176 return TRUE;
177 }
178
179
180 PopPark(park *p,giveway *g,wqueue *q,int n,lqueue *l) //车辆离开出栈
181 {
182 int a = p->top1;
183 int c = 0;
184 time_t t;
185 struct tm *t1;
186 time (&t);
187 t1 = localtime(&t);
188 Car e,f;
189 e.n=n;
190 while(p->car[p->top1].n!=n) //后面的车先压入让路栈
191 {
192 f=p->car[p->top1];
193 p->top1--;
194 g->top2++;
195 g->car[g->top2]=f;
196 }
197 Copy(&e,t1);
198 LTime(&(p->car[p->top1]),&e);
199 EnLeaveQueue(l,e); //将离开的车信息插入离开的车链表中
200 p->top1--;
201 printf("车牌号为 %d 的车已经离开,停留时间:%d天%d时%d分%d秒\n",n,e.day,e.hour,e.min,e.sec);
202 while(g->top2 >= 0) //让路栈的车出栈,一次压入停车栈
203 {
204 f=g->car[g->top2];
205 g->top2--;
206 p->top1++;
207 p->car[p->top1]=f;
208 }
209 p->top1 = a-1;
210 if(QueueEmpty(*q)== 0) //等候队列出一辆车,压入停车栈
211 {
212 WQueue l=(WQueue)malloc(sizeof(WaitQueue));
213 l=q->front->next;
214 printf("一辆车来自等候队列:");
215 PushPark(p,l->data);
216 q->front->next=l->next;
217 free(l);
218 }
219
220
221 }
222
223 EnLeaveQueue(lqueue *q,Car e) //离开的车队列 插入新节点
224 {
225 LQueue node=(LQueue)malloc(sizeof(LeaveQueue));
226 if(!node)
227 {
228 return FALSE;
229 }
232 q->rear->next = node;
233 q->rear = node;
234 return TRUE;
235 }
236
237 EnWaitQueue(wqueue *q,int n) //等候队列插入新节点
238 {
239 WQueue node=(WQueue)malloc(sizeof(WaitQueue));
240 if(!node)
241 {
242 return FALSE;
243 }
244 node->data = n;
245 node->next = NULL;
246 q->rear->next = node;
247 q->rear = node;
248 printf("这辆车(牌号为 %d )在等候队列等待",n);
249 return TRUE;
250 }
251
252 int SearchCar(wqueue *q,park *p,lqueue *l,int n) //输入车牌号查找车辆信息
253 {
254 WQueue e = q->front->next;
255 LQueue f = l->front->next;
256 int a = p->top1;
257 while(p->top1>=0) //先在停车场内寻找
258 {
259 if(p->car[p->top1].n == n)
260 {
262 return TRUE;
263 }
264 p->top1--;
267 while(e) //再在等候队列里寻找
268 {
269 if(e->data == n)
270 {
271 printf("这辆车在等待队列中\n");
272 return FALSE;
273 }
274 e=e->next;
275 }
276 while(f) //然后再已经离开的车里寻找
277 {
278 if(f->car.n == n)
279 {
280 printf("这辆车已经离开,停留时间为%d天%d时%d分%d秒\n",f->car.day,f->car.hour,f->car.min,f->car.sec);
281 return FALSE;
282 }
283 f=f->next;
284 }
285 printf("无记录\n");
286 }
287
288
289 ParkInfo(park *p) //停车场内车辆信息
290 {
291 int a = p->top1;
292 int i=0;
293 while(p->top1>=0)
294 {
295 i++;
297 p->top1--;
298 }
299 p->top1=a;
300 printf("总共%d辆车在停车场中,还有%d个停车空位\n",i,MAX-i);
301 }
302
303 WaitQueueInfo(wqueue q) //等候队列内车辆信息
304 {
305 WQueue s;
306 int i=0;
307 s=q.front->next;
308 while(s)
309 {
310 i++;
311 printf("第%d辆在等候队列等待,车牌号为%d\n",i,s->data);
312 s=s->next;
313 }
314 printf("总共%d辆车在等待队列\n",i);
315 }
316
317 LeaveInfo(lqueue l) //离开队列内车辆信息
318 {
319 LQueue k;
320 int i=0;
321 k=l.front->next;
322 while(k)
323 {
324 i++;
325 printf("第%d辆离开的车牌号为%d,停留时间为%d天%d时%d分%d秒\n",i,k->car.n,k->car.day,k->car.hour,k->car.min,k->car.sec);;
326 k=k->next;
327 }
328 printf("总共%d辆车已经离开\n\n",i);
329 }
330
331 char *mainmenu[]= //打印主菜单
332 {
333 "*****************************************************\n",
334 "*********1.进车登记 2.出车登记*************\n",
335 "*********3.查询车辆信息 4.查询出入记录*********\n",
336 "*********5.查询场内车辆信息 6.查询等候车辆信息*****\n",
337 "*********7.退出**************************************\n",
338 "*****************************************************\n"};
339
340 char menu(char *str[],int len) //打印菜单与选择函数
341 {
342 int i;
343 char sel;
344 for(i=0;i<len;i++)
345 {
346 printf("%s",str[i]);
347 }
348 printf("请输入你的选择:\n");
349 scanf(" %c",&sel); //空格防止读缓冲区
350 getchar();
351 return sel;
352 }
353
354 int main(int argc,char *argv[])
355 {
356 park p;
357 lqueue l;
358 wqueue q;
359 giveway g;
360 int n; //定义数据结构并判断是否成功
361 char sel;
362 if(ParkInit(&p) != TRUE || GivewayInit(&g) != TRUE|| WaitQueueInit(&q) != TRUE ||LeaveQueueInit(&l)!=TRUE)
363 {
364 printf("Init failure\n");
365 getchar();
366 exit(1);
367 }
368 else
369 printf("Init success\n");
370 while(1)
371 {
372 sel=menu(mainmenu,6);
373 switch(sel)
374 {
375 case '1': { //来车登记
376 printf("请输入车牌号:\n");
377 scanf("%d", &n);
378 if(Parkfull(p))
379 EnWaitQueue(&q,n);
380 else
381 PushPark(&p,n);
382 }break;
383 case '2': { //出车登记
384 printf("请输入车牌号:\n");
385 scanf("%d",&n);
386 if(1 == SearchCar(&q,&p,&l,n))
387 PopPark(&p,&g,&q,n,&l);
388 else
389 printf("停车场里找不到你的车");
390 }break;
391 case '3': printf("请输入你的车牌号:\n"); //输入车牌号查找车辆信息
392 scanf("%d", &n);
393 SearchCar(&q,&p,&l,n);break;
394 case '4': printf("在停车场里:\n"); //显示所有车记录
395 ParkInfo(&p);
396 printf("\n");
397 printf("在等待队列里:\n");
398 WaitQueueInfo(q);
399 printf("\n");
400 printf("在已经离开的车里:\n");
401 LeaveInfo(l);break;
402 printf("\n");
403 case '5': ParkInfo(&p);break; //显示停车场内信息
404 case '6': WaitQueueInfo(q);break; //显示等候队列内信息
405 case '7': printf("Welcome next time!Press any key to quit...\n");getchar();exit(0);
406 default:break;
407 }
408 }
409 return 0;
410
411 }
还有很多需要优化改进的地方,但基本功能都有。
C语言小功能实现--停车场功能相关推荐
- 小鹏VPA停车场记忆泊车功能试驾体验和解读
OTA,已经成为汽车智能化的标志性技术之一. 一辆智能汽车,通过不断的OTA,能够持续给用户带来价值和惊喜:而一辆无法OTA的汽车,将会打上"传统汽车"的标签. 汽车的使用场景和功 ...
- 日历小程序C语言,小程序日历功能实现
前言:之前想过写一下小程序的日历功能练练手,只是确实有些懒没怎么动手,最近项目有这方面的需求就花了小半天的时间实现了一下,总的来说并不复杂. 先看下接入项目前的日历样式: 屏幕快照 2019-03-2 ...
- atitit 音频 项目 系列功能表 音乐 v3 t67.docx Atitit 音频 项目 系列功能表 1.音频 音乐 语言领域的功能表 听歌识曲功能 酷我功能。 铃声 功能。。 音频切割(按
atitit 音频 项目 系列功能表 音乐 v3 t67.docx Atitit 音频 项目 系列功能表 音频 音乐 语言领域的功能表 听歌识曲功能 酷我功能. 铃声 功能.. 音频切割(按照副歌部分 ...
- atitit 音频 项目 系列功能表 音乐 v3 t67.docx Atitit 音频 项目 系列功能表 音频 音乐 语言领域的功能表 听歌识曲功能 酷我功能。 铃声 功能。。 音频切割(按照副歌部
atitit 音频 项目 系列功能表 音乐 v3 t67.docx Atitit 音频 项目 系列功能表 音频 音乐 语言领域的功能表 听歌识曲功能 酷我功能. 铃声 功能.. 音频切割(按照副歌部分 ...
- 微信小程序原生开发功能合集一:微信小程序开发介绍
一.专栏介绍 本专栏主要内容为微信小程序常用功能开发过程的介绍说明,包括开发微信小程序常用组件的封装.常用功能的开发等,提供源代码.开发过程讲解视频.完整的课程等. 组件封装: 下拉选择组件. ...
- php小程序session取不到,微信小程序实现Session功能及无法获取session问题的解决方法...
因为小程序原生不支持Cookie,因此也不支持Session. 网上找到的的一些方法有缺陷,而且很多累赘,估计没有实际测试过,在此直接给出实测可用的代码. 大概思路就是借助小程序本地储存+网络请求的h ...
- 小鹏自动泊车功能体验
小鹏自动泊车功能体验 作为一个做自动泊车的软件开发人员,体验了解竞品功能是很有必要的.所以,昨天(5.11)预约了公司的小鹏P7体验了一下APA功能.由于公司的产品暂时未接入USS(超声波雷达),所以 ...
- C语言 -- string.h中函数功能详解与手动实现 - 02(常用函数memcpy、memmove、strcpy、strdup、strcat、strtok...)
内容预览 3.5.搬迁类型 --- 函数功能详细说明 :将内存空间中内容移动.复制到另一内存空间 3.6.搬迁类型 --- 函数功能测试与手动实现 3.6.1.memcpy.memccpy 3.6.2 ...
- 黯然微信小程序杂记(三):微信小程序实现倒计时功能 附讲解教学 附源码
黯然微信小程序杂记(三):微信小程序实现倒计时功能 附超详细注释 附源码 一.功能描述 二.界面展示 三.test.wxml代码 四.test.js代码(注释很详细 很易懂) CSDN私信我,有关微信 ...
最新文章
- 刺客信条奥德赛无法加载库_点评刺客信条起源、奥德赛、英灵殿,哪个最好玩?...
- News Break!沈向洋投资并出任美国版“今日头条”董事长
- 【学习笔记】34、函数是Python的头等对象
- git bash 操作文件及文件夹命令
- 【NLP】全方位解读 | Facebook的搜索是怎么做的?
- 解决客户端从服务器请求数据乱码问题
- 云从科技宣布B+轮融资,多家国有基金入股,累计获资35亿元
- Python默认参数的坑
- Thinkphp仿众图网图片素材下载站源码/自适应手机端资源下载站源码
- 22021成都市高考成绩查询,四川大学本科教务系统登录
- maxon电机加速度上不去的原因及解决
- 电脑自带的Windows照片查看器不见了,可以试试这种方法。
- 自定义Openstack图标
- 【TLD】改进后的TLD视频目标跟踪方法的MATLAB仿真
- Linux的numactl
- html怎么打五角星符号怎么打出来,电脑小技巧 五角星符号在哪里打出来 - 驱动管家...
- 前人铺路系列组件hevue-img-preview 2.5更新,增加键盘控制,节流,图片加载等待等,来看看有没有你可以借鉴的思路或功能
- Flutter 解决软键盘弹出背景图片变形
- php 微信 语音,PHP语言微信开发:微信录音临时转永久存储
- http://blog.sina.com.cn/s/blog_5da93c8f0102w86x.html