项目介绍

设停车场是一个可停放n辆汽车的狭长死胡同,南边封口,汽车只能从北边进出(这样的停车场世间少有)。汽车在停车场内按车辆到达时间的先后顺序,最先到达的第一辆车停放在车场的最南端,依次向北排开。若车场内已停满n辆汽车,则后来的汽车只能在门外的候车场上等候,一旦有车开走,则排在候车场上的第一辆车即可开入。当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路(假定停车场内设有供车辆进出的便道,所有的司机也必须在车内随时待命),待该辆车开出大门外,其他车辆再按原次序进入车场。每辆停放在车场的车在它离开停车场时,要按停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模拟程序。

image-20221107175154319

准备知识

获取时间

time函数

time_t now;

time(&now);// 等同于now = time(NULL)

printf(“now time is %d\n”, now);

返回值 返回1970-1-1, 00:00:00以来经过的秒数。(失败:-1)
原型 time_t time(time_t *calptr)
头文件 <time.h>

举例:

1
2
3
4
5
6
7
8
9
#include<stdio.h>
#include<time.h>
int main()
{
time_t now;
time(&now);// 等同于now = time(NULL)
printf("now time is %d\n", now);
return 0;
}

结果如下

image-20221109095159376

时间格式化

使用localtime函数,将时间数值变换成本地时间,考虑到本地时区和夏令时标志。

原型 struct tm *localtime(const time_t * calptr);
返回值 struct tm *结构体 (失败返回NULL)
头文件 <time.h>

struct tm *结构体 ,原型如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct tm {
int tm_sec; /* 秒 – 取值区间为[0,59] */

int tm_min; /* 分 - 取值区间为[0,59] */

int tm_hour; /* 时 - 取值区间为[0,23] */

int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */

int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */

int tm_year; /* 年份,其值等于实际年份减去1900 */

int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一 */

int tm_yday; /* 从每年1月1日开始的天数– 取值区间[0,365],其中0代表1月1日 */

int tm_isdst; /* 夏令时标识符,夏令时tm_isdst为正;不实行夏令时tm_isdst为0 */

};

//此结构体空间由内核自动分配, 而且不要去释放它.
//失败: NULL

举例

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
#include<time.h>
int main()
{
time_t now;
struct tm *tm_now ;
time(&now) ;
tm_now = localtime(&now) ;
printf("现在的时间: %d-%d-%d %d:%d:%d\n",
tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec) ;

return 0;
}

结果如下:

image-20221109100018423

源程序

程序如下:

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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#include <time.h>


#define MAX_STOP 5
#define MAX_PAVE 4
#define MAX_History 20


//定义车的结构体,包含了进入,离开停车场的时间属性
typedef struct{
int TimeIn;
int TimeOut;

long int leave_time;

char ct[50];
char Lincense[10];
}Car;

//停车
typedef struct{
Car Stop[MAX_STOP];
int top;
}Stopping;


typedef struct{
Car Stop[MAX_History];
int top;

}History;


//等待队列的创建
typedef struct{
Car Pave[MAX_PAVE];
int count;
int front, rear;
}Pavement;

typedef struct{
Car Let[MAX_STOP];
int top;
}Buffer;

typedef struct{
Car Wait[MAX_PAVE];
int count;
int front, rear;
}Waiting;

Stopping s;
Pavement p;
Buffer b;
Car c;
Waiting w;
History h;
char C[10];

void Car_Come();
void Car_Leave();
void Stop_To_Pave();
void Stop_To_Buff();
void Leave_Pavement();
void DisPlay();
void DisPlayPave();
void Welcome();
void SmallWelcome();
void Car_Leave_menu();
void Search();
void lucky_draw(int a); /*进入抽奖环节 */
int draw_result();
int mod_price();
void history(); //停车历史记录

History* history_push(Stopping s,time_t t1) ;


float Price = 6;
/*停车场默认是6元/分钟,程序相减单位为60,以秒计费,所以需要改动 */



void Car_Come(){
printf("请输入即将停车的车牌号:");
scanf("%s", &C);
int i = s.top;
while(i != -1){
if(0 == strcmp(s.Stop[i].Lincense, C)){
printf("输入有误,此汽车已存在!\n");
return;
}
i--;
}
int k = MAX_PAVE;
while(k != 0){
if(0 == strcmp(p.Pave[k].Lincense, C)){
printf("输入有误,此汽车已存在!\n");
return;
}
k--;
}
if (s.top >= MAX_STOP - 1){
Stop_To_Pave();
}
else{
time_t t1;
long int t = time(&t1);
char* t2;
t2 = ctime(&t1);
s.Stop[++s.top].TimeIn = t;

strcpy(s.Stop[s.top].ct, t2);
strcpy(s.Stop[s.top].Lincense, C);
printf("牌照为%s的汽车停入停车位的%d车位,当前时间:%s\n", C, s.top+1, t2);
}
}


void Search(){
printf("请输入要搜索的车牌号:\n");
scanf("%s", &C);
int i, j, k, flag = 0;
time_t t1;
long int t = time(&t1);
if(s.top >= 0){
for(i = s.top; i >= 0; i--){
if(0 == strcmp(s.Stop[i].Lincense, C)){
printf("此汽车在停车场内,信息如下:\n");
printf("\t车牌号\t\t停车位序\t当前所需支付金额\t进入时间\t\n");
printf("\t%s\t第%d个\t\t%0.f元\t\t\t%s", s.Stop[i].Lincense, i+1, (Price/60) * (t - s.Stop[i].TimeIn), s.Stop[i].ct);
flag = 1;
break;
}
}
}
if(flag == 0 && p.count > 0){
i = p.front, k = 1, j = p.rear;
while(i != j ){
if(0 == strcmp(p.Pave[i].Lincense, C)){
printf("此汽车在停便道上\n");
printf("\t车牌号\t\t停车位序\n");
printf("\t%s\t第%d个",p.Pave[i].Lincense, k);
flag = 2;
break;
}
i++;
k++;
}
}
if(0 == flag)
printf("停车场内外不存在该汽车信息!\n");

}


void Car_Leave(){
printf("请输入即将离开的车牌号:");
scanf("%s", &C);
int i, j, flag = 1, flag2 = 1;
if(s.top >= 0){
for(i = s.top; i >=0; i-- ){
flag = flag * strcmp(s.Stop[i].Lincense, C);

}
}

if(0 == flag){
Stop_To_Buff();
}

if(flag !=0 /*&& flag2 != 0*/)
printf("停车场内没有该汽车的信息!\n");
}


void Leave_Pavement(){
int i, j, flag = 0;
printf("请输入即将离开的车牌号:");
scanf("%s", &C);
if(p.count <= 0){
printf("便道上不存在汽车!\n");
return;
}
while(p.count > 0){
i = p.front;
if(0 == strcmp(p.Pave[i].Lincense, C)){
break;
}
printf("牌照为%s的汽车暂时从便道进入临时便道\n", p.Pave[p.front].Lincense);
strcpy(w.Wait[w.rear].Lincense, p.Pave[p.front].Lincense);
p.front = (p.front + 1) % MAX_PAVE;
w.rear = (w.rear + 1) % MAX_PAVE;
w.count++;
p.count--;
}
printf("\n牌照为%s的汽车从便道上开走,不收取任何费用!\n\n", p.Pave[i].Lincense);
p.front = (p.front + 1) % MAX_PAVE;
p.count--;
while(p.count > 0){
printf("牌照为%s的汽车暂时从便道进入临时便道\n", p.Pave[p.front].Lincense);
strcpy(w.Wait[w.rear].Lincense, p.Pave[p.front].Lincense);
p.front = (p.front + 1) % MAX_PAVE;
w.rear = (w.rear + 1) % MAX_PAVE;
w.count++;
p.count--;
}
while(w.count > 0){
printf("\n牌照为%s的汽车返回便道\n",w.Wait[w.front].Lincense);
strcpy(p.Pave[p.rear].Lincense, w.Wait[w.front].Lincense);
w.front = (w.front + 1) % MAX_PAVE;
p.rear = (p.rear + 1) % MAX_PAVE;
w.count--;
p.count++;
}
}



//离开计费
void Stop_To_Buff(){
while (s.top >= 0){
if(0 == strcmp(s.Stop[s.top].Lincense, C)){
break;
}
strcpy(b.Let[b.top++].Lincense, s.Stop[s.top].Lincense);
printf("牌照为%s的汽车暂时退出停车场\n", s.Stop[s.top--].Lincense);
}

printf("牌照为%s的汽车从停车场开走\n", s.Stop[s.top].Lincense);
time_t t1;
long int t = time(&t1);
s.Stop[s.top].TimeOut = t;
char* t2;
t2 = ctime(&t1);


printf("离开时间%s\n需付费%.0f元\n", t2,(Price/60) * (t - s.Stop[s.top].TimeIn));
history_push(s,t1) ; //加入进历史记录

s.top--;
while(b.top > 0){
strcpy(s.Stop[++s.top].Lincense, b.Let[--b.top].Lincense);
printf("牌照为%s的汽车停回停车位%d车位\n", b.Let[b.top].Lincense, s.top+1);
}
while(s.top < MAX_STOP-1){
if(0 == p.count)
break;
else{
strcpy(s.Stop[++s.top].Lincense, p.Pave[p.front].Lincense);
printf("牌照为%s的汽车从便道中进入停车位的%d车位\n", p.Pave[p.front].Lincense, s.top+1);
time_t t1;
long int t = time(&t1);
char* t2;
s.Stop[s.top].TimeIn = t;
p.front = (p.front + 1) % MAX_PAVE;
p.count--;
}
}
}
//离开的车辆压入栈,作为历史记录
History* history_push(Stopping s,time_t t1)
{


h.Stop[++h.top].TimeIn = s.Stop[s.top].TimeIn;
h.Stop[h.top].leave_time = time(&t1);

strcpy(h.Stop[h.top].ct, s.Stop[s.top].ct);

strcpy(h.Stop[h.top].Lincense, s.Stop[s.top].Lincense);




}
void history()
{

int i = h.top;
if(-1 == i)
printf("暂时没有停车记录哦!\n");

printf("\t车牌号\t\t停放时间\t已支付金额\t停放位序\n");
while(i != -1){
printf("\t%s\t\t%d分%d秒\t\t%.0f元\t\t\t第%d个\n", h.Stop[i].Lincense,
(h.Stop[i].leave_time-h.Stop[i].TimeIn)/60,(h.Stop[i].leave_time-h.Stop[i].TimeIn) % 60, (Price/60) * (h.Stop[i].leave_time - h.Stop[i].TimeIn), i+1);
i--;
}
}


void Stop_To_Pave(){
if(p.count > 0 && (p.front == (p.rear + 1) % MAX_PAVE))
printf("便道已满,请下次再来!\n");
else{
strcpy(p.Pave[p.rear].Lincense, C);
p.rear = (p.rear + 1) % MAX_PAVE;
p.count++;
printf("牌照为%s的汽车停入便道上\n", C);
}
}


void DisPlay(){
int i = s.top;
if(-1 == i)
printf("停车场目前为空\n");
time_t t1;
long int t = time(&t1);
printf("\t车牌号\t\t停放时间\t当前所需支付金额\t停放位序\n");
while(i != -1){
printf("\t%s\t\t%d分%d秒\t\t%.0f元\t\t\t第%d个\n", s.Stop[i].Lincense,
(t - s.Stop[i].TimeIn)/60,(t - s.Stop[i].TimeIn) % 60, (Price/60) * (t - s.Stop[i].TimeIn), i+1);
i--;
}
}


void DisPlayPave(){
int i = p.front;
int k = 1;
if(0 == p.count)
printf("便道目前为空\n");
printf("\t车牌号\t\t停放位序\n");
while(i != p.rear && k <= p.count){
printf("\t%s\t第%d个\n", p.Pave[i].Lincense, k++);
i = (i + 1) % MAX_PAVE;
}
}


void Car_Leave_menu(){
while(1){
system("cls");
SmallWelcome();
int i, cho;
scanf("%d", &i);
if(1 == i) Car_Leave();
if(2 == i) Leave_Pavement();
if(3 == i) draw_result();
if(4 == i) return;
printf("\n返回请输入0\n");
top:
scanf("%d", &cho);
if(0 == cho){
continue;
}
else{
printf("您的输入有误,请重新输入\n");
goto top;
}
}
}

void SmallWelcome(){
printf ("\t*******************目前停车场状况***********************\n");
printf ("\t停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d/%d辆车\n",
MAX_STOP, s.top+1, (p.rear + MAX_PAVE - p.front) % MAX_PAVE, MAX_PAVE-1);
printf ("\t********************************************************\n");
printf ("\t---------Welcome to Ep's Car Parking next time----------\n");
printf ("\t* *\n");
printf ("\t* 1.从停车场内驶出汽车 *\n");
printf ("\t* 2.从便道上驶出汽车 *\n");
printf ("\t* 3.进入抽奖 *\n");
printf ("\t* 4.退出子管理系统 *\n");
printf ("\t*请注意:从停车场内驶离的汽车按照%.0f元/分钟计费 *\n",Price);
printf ("\t*望周知:从便道上驶离的汽车不收取费用 *\n");
printf ("\t* *\n");
printf ("\t*------------------------------------------------------*\n");
printf ("\t--------Press key(1/2/3) to continue the program--------\n");
}
void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void lucky_draw(int a) /*进入抽奖环节 抽奖函数*/
{

int e;
if( a!=1 )
{
printf("你选择了不抽奖");
return;
}
else
srand((unsigned)time(NULL));//利用时间作为种子
e=100+rand()%1000;//随机抽奖
printf("中奖号码为666或888,你的号码为%d\n",e);
{
if(a==666||a==888)
{
printf("恭喜你获得了免费停车一年特权");
return;
}
else
{
printf("很遗憾您没有中奖");
return;
}

}
}
int draw_result() //抽奖结果

{
int select;
printf("请确认是否进行抽奖,如果抽奖请按1,如果不请随意\n");
scanf("%d",&select);
lucky_draw(select);

return 0;
}

int mod_price(){ //修改停车价格

printf("修改停车场计费(默认6元一分钟):\n");
printf("在此输入:");
int temp ;
scanf("%d",&temp);
Price = temp;
return Price;

}

void Welcome(){
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ("\t\t\t%s", asctime(timeinfo) );
HideCursor();
printf ("\t*******************目前停车场状况***********************\n");
printf ("\t停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d/%d辆车\n",
MAX_STOP, s.top+1, (p.rear + MAX_PAVE - p.front) % MAX_PAVE, MAX_PAVE-1);
printf ("\t********************************************************\n");
printf ("\t--------------Welcome to Ep's Car Parking---------------\n");
printf ("\t* *\n");
printf ("\t* 1.停车场停车信息显示 *\n");
printf ("\t* 2.便道上停车信息显示 *\n");
printf ("\t* 3.汽车到达停车场操作 *\n");
printf ("\t* 4.汽车离去停车场操作 *\n");
printf ("\t* 5.查找汽车功能 *\n");
printf ("\t* 6.停车历史记录 *\n");
printf ("\t* 7.系统设置 *\n");
printf ("\t* 8.退出管理系统 *\n");
printf ("\t* 收费标准:本停车场按照%.0f元/分钟计费,望周知 *\n",Price);
printf ("\t* *\n");
printf ("\t*------------------------------------------------------*\n");
printf ("\t---------Press key(1/2/3/4/5/6/7) to run program----------\n\n\n");
printf("请输入:");


}

int main(){
s.top = -1;
h.top = -1;
b.top = 0;
p.rear = 0;
p.count = 0;
p.front = 0;
w.count = 0;
w.front = 0;
w.rear = 0;

while(1){
system("color 0B");
system("cls");
Welcome();
int i, cho;
scanf("%d", &i);
if(1 == i) DisPlay();
if(2 == i) DisPlayPave();
if(3 == i) Car_Come();
if(4 == i) Car_Leave_menu();
if(5 == i) Search();
if(6 == i) history();
if(7 == i) mod_price();


if(8 == i) {
printf("\n欢迎您再次使用本系统呦 ε=ε=ε=(~ ̄▽ ̄)~\n\n");
break;
}


printf("\n返回请输入0\n");
begin:
scanf("%d", &cho);
if(0 == cho){
continue;
}
else{
printf("您的输入有误,请重新输入\n");
goto begin;
}
}
return 0;
}

程序截屏如下:

image-20221109100736119