C语言实现简单停车场管理系统

时间:2022-12-05 19:08:05

本文实例为大家分享了C语言停车场管理系统的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/***************************************************************************
项目要求
停车场管理
问题描述:停车场是一个能放n辆车的狭长通道,
只有一个大门,汽车按到达的先后次序停放。若
车场满了,车要停在门外的便道上等候,一旦有
车走,则便道上第一辆车进入。当停车场中的车
离开时,由于通道窄,在它后面的车要先退出,
待它走后再依次进入。汽车离开时按停放时间收费。
基本功能要求:
(1) 建立三个数据结构分别是:停放栈、让路
栈、等候队列。
(2) 输入数据模拟管理过程,数据(入或出,车号)。
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define D (24*60*60)
#define H (60*60)
#define M (60)
#define OK 1
#define ERROR 0
#define MAX_STACK_SIZE 10 /* 栈向量大小 */
typedef int StackData;
typedef int QueueData;
typedef int ElemType;
typedef struct Node
{
 int No;  /* 车号 */
 int Timeinit; /* 进入停车场的时间*/
}Node;
typedef struct QueueNode /* 队列结点*/
{
 struct Node data;
 struct QueueNode* next;
} QueueNode;
typedef struct LinkQueue /* 链式队列结构体 */
{
 struct QueueNode *rear, *front;
} LinkQueue;
 
 
typedef struct SqStackNode /* 链式栈结构体 */
{
 int top;
 int bottom;
 struct Node stack_array[MAX_STACK_SIZE+1] ;
}SqStackNode ;
 
//***************************************************************
SqStackNode* InitStack()    /* 初始化栈*/
{
 SqStackNode *S=(SqStackNode *)malloc(sizeof(SqStackNode));
 S->bottom=S->top=0;
 return (S);
}
int FullStack(SqStackNode *S)   /* 满栈 */
{
 return S->top==MAX_STACK_SIZE;
}
int pushStack(SqStackNode *S,Node data) /* 入栈 */
{
 if(FullStack(S))
 {
 return ERROR;  /* 栈满,返回错误标志 */
 }
 S->top++ ;  
 (S->stack_array[S->top]).No=data.No ;
 (S->stack_array[S->top]).Timeinit=data.Timeinit;
 return OK;   /* 压栈成功 */
}
int popStack(SqStackNode *S,Node *data)  /*弹出栈顶元素*/
{
 if(S->top==0)
 {
 return ERROR;  /* 栈空,返回错误标志 */
 }
 (*data).No=(S->stack_array[S->top]).No;
 (*data).Timeinit=(S->stack_array[S->top]).Timeinit;
 S->top--;
 return OK;
}
int FinfStack(SqStackNode *S,Node data) /* 搜索栈内元素data*/
{
 int i;
 if(S->top==0)
 {
 return ERROR;  /* 栈空,返回错误标志 */
 }
 
 for(i=1;i<=S->top;i++)
 {
 if(S->stack_array[i].No == data.No)
 {
  return OK;
 }
 }
 return ERROR;
}
 
 
 
//****************************************************
LinkQueue* InitQueue (void)  /* 初始化队列 */
{
 LinkQueue *Q=( LinkQueue * ) malloc( sizeof ( LinkQueue ) );
 Q->rear=Q->front=NULL;
 return Q;
}
 int QueueEmpty ( LinkQueue *Q ) /* 空队列*/
 {
 return Q->front == NULL;
}
 
int GetFrontQueue ( LinkQueue *Q, Node *data ) /* 取队首 */
{
 if ( QueueEmpty (Q) ) return 0;
 (*data).No = (Q->front->data).Timeinit; return 1;
}
int EnQueue ( LinkQueue **Q, Node data) /* 入队*/
{
 QueueNode *p = ( QueueNode * ) malloc( sizeof ( QueueNode ) );
 (p->data).No = data.No;
 (p->data).Timeinit = data.Timeinit;
 p->next = NULL;
 if ( (*Q)->front == NULL )
 {
 (*Q)->front = (*Q)->rear = p;
 }
 else
 {
 
 (*Q)->rear = (*Q)->rear->next = p;
 }
 return 1;
}
int DeQueue ( LinkQueue **Q, Node *data) /* 出对*/
{
 if ( QueueEmpty (*Q) )
 {
 return 0;
 }
 QueueNode *p = (*Q)->front;
 (*data).No = p->data.No;  
 (*data).Timeinit = p->data.Timeinit;
 (*Q)->front = (*Q)->front->next;
 if ((*Q)->front == NULL) (*Q)->rear = NULL;
 free (p);
 return 1;
}
/*********************************************************/
int now_time(void) /* 获取当日时间,单位秒*/
{
 time_t t1;
 time(&t1);
 int time=t1%D;
 return time;
}
 
Parking(LinkQueue **Q,SqStackNode *S) /* 停车*/
{
 int i,time_now;
 Node data;
 printf("Input the Car No:\n");
 scanf(" %d",&data.No);
 
 for(i=1;i<=S->top;i++)
 {
 
 if(S->stack_array[i].No == data.No)/* 车号已存在*/
 {
 printf("The Car is existed\n");
 return ;
 }
 }
 
 EnQueue(Q,data);/* 进去等待队列*/
 while(!QueueEmpty(*Q))
 {
 if(FullStack(S)) /* 停放栈满*/
 {
 printf("Please Wait...\n");
  break;
 }
 else /* 停放栈未满 */
 {
 DeQueue(Q,&data);/* 等待队列车出对 */
 data.Timeinit=now_time();/* 记录当前时间*/
 pushStack(S,data);/* 进入停放栈*/
 printf("Park Success\n");
 }
 }
 return ;
}
leaving(SqStackNode *S,SqStackNode *B,LinkQueue **Q)/* 离开*/
{
 if(S->bottom == S->top)/* 停放栈空*/
 {
 printf("Parking is Empty:\n");
 }
 else
 {
 Node data;
 int i,h,m,s;
 float charge;
 int time_now,parking_time;
 printf("Leaving No:\n");
 scanf(" %d",&i);
 data.No=i;
 if(!FinfStack(S,data))/* 停放栈内无此车*/
 {
 printf("Do not find the car\n");
 return ;
 }
 else/* 停放栈内有此车*/
 {
 while(S->stack_array[S->top].No != i)/* 此车后的车依次出栈入让路栈*/
 {
 popStack(S,&data);
 pushStack(B,data);
 }
 popStack(S,&data);/* 此车出停放栈*/
 time_now=now_time();
 parking_time=time_now-data.Timeinit;/* 计算停车时间*/
 
 h = parking_time/H;
 parking_time = parking_time%H;
 m = parking_time/M;
 s = parking_time%M;
 charge = 6*h+0.1*(m+1);/* 计算停车收费*/
 printf("The leaving car:%d Parking time:%d:%d:%d Charge($6/h):$%g\n",data.No,h,m,s,charge);
 
 while(B->bottom != B->top)/* 让路栈内的车依次出栈入停放栈*/
 {
 popStack(B,&data);
 pushStack(S,data);
 }
 while(!FullStack(S)&&(!QueueEmpty(*Q)))/* 停放栈未满且等待队列未空*/
 {
 DeQueue(Q,&data); /* 等待队列车出队*/
 data.Timeinit=now_time();
 pushStack(S,data);/* 出队的车入停放栈*/
 }
 }
 
 }
}
situation(SqStackNode *S,LinkQueue **Q)/* 查看停车场当前情况*/
{
 Node data;
 int i;
 int time_now,parking_time;
 int h,m,s;
 struct QueueNode *p;
 int wait_count=0;
 p=(*Q)->front;
 if(p == NULL)/* 等待队列空*/
 {
 printf("Waiting car :0\n");
 }
 else/* 等待队列未空*/
 {
 do
 {
  wait_count++;
 p=p->next;
 }while(p!=NULL);/* 计算等待队列内车数*/
 printf("Waiting car :%d\n",wait_count);
 }
 
 printf("Car No: ");
 for(i=1;i<=S->top;i++)
 {
 printf("%-10d",S->stack_array[i].No);
 
 if(S->stack_array[i].No == data.No)
 {
  return OK;
 }
 }
 printf("\nPark time:");
 for(i=1;i<=S->top;i++)
 {
 time_now = now_time();
 parking_time = time_now - S->stack_array[i].Timeinit;/* 计算截止当前停车时间*/
 h = parking_time/H;
 parking_time = parking_time%H;
 m = parking_time/M;
 s = parking_time%M;
 printf("%02d:%02d:%02d ",h,m,s);
 }
 printf("\n");
 
}
 
int main()
{
 int i;
 Node data;
 SqStackNode *park;/* 停放栈*/
 SqStackNode *back;/* 让路栈*/
 LinkQueue *wait; /* 等待队列*/
 park=InitStack();
 back=InitStack();
 wait=InitQueue();
 while(1)
 {
 system("clear\n");
 printf("----------Welcome to our Car Parking----------\n");
 printf("  1.Parking \n");
 printf("  2.leaving \n");
 printf("  3.situation \n");
 printf("  4.exit \n");
 scanf(" %d",&i);
 switch(i)
 {
 case 1:/* 停车*/
 {
 system("clear\n");
 Parking(&wait,park);
 setbuf(stdin,NULL);
 getchar();
 break;
 }
 case 2:/* 离开 */
 {
 leaving(park,back,&wait);
 setbuf(stdin,NULL);
 getchar();
 break;
 }
 case 3:/* 查看停车情况*/
 {
 
 system("clear\n");
 situation(park,&wait);
 setbuf(stdin,NULL);
 getchar();
 break;
 }
 case 4:/* 退出*/
 {
 return 0;
 }
 default:
 {
 break;
 }
 }
 }
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/ccj2020/article/details/7771332