C语言小功能实现--停车场功能

问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门

外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车

要先退出,待它走后在依次进入。

  4 *   文件名称:park.c
  5 *   创 建 者:Y
  6 *   创建日期: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 1
 16 #define FALSE 0
 17 #define MAX 2
 18 #define Status int
 19 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 struct
 45 {
 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 struct
 58 {
 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 }

还有很多需要优化改进的地方,但基本功能都有。

    原文作者:停车场模拟问题
    原文地址: https://blog.csdn.net/ArrowYL/article/details/79426942
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞