服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C语言实现停车场项目

C语言实现停车场项目

2022-10-20 13:34淹不死的狐狸 C/C++

这篇文章主要为大家详细介绍了C语言实现停车场项目,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

停车场项目需求

问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车要先退出,待它走后在依次进入。汽车离开时按停放时间收费。

基本功能要求:

(1)建立三个数据结构分别是:停放栈、让路栈、等候队列。
(2)输入数据模拟管理过程,数据(入或出,车号)

功能描述:进车登记、出车登记、按车牌号查询停车车辆信息、查询出入车记录、查询场内车辆信息、查询等候车辆信息、退出系统。

(1)linux系统编写(链表、栈、队列);
(2)进车登记:登记车牌号以及入场时间;
(3)出车登记:计算出停车时间,记录车辆车牌;
(4)按车牌号查询车辆信息:停车时间,是否来过停车场,是否还在停车场
(5)查询出入记录:所有车辆,包括已经离开的
(6)查询场内车辆信息:列出所有场内车辆信息
(7)查询等候车辆信息:显示等候车辆数量以及所有车牌号
(8)退出系统。

分析,底层需要写以下内容:

两个栈,停放栈(车牌号,出场时间,入场时间)[初始化,判断是否为空,出栈,入栈,查栈] 让路栈(车牌号)[初始化,出栈,入栈,判断是否为空]
一个队列,[初始化,出队列,入队列](车牌号)
一个链表,(车牌号,出入厂状态,入场时间,出场时间)[初始化,入链表,查找数据,遍历打印]

Windows下代码

工程分为三个文件,分别是main.c fun.c pack.h

main.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
#include<stdio.h>
#include"park.h"
#include<stdlib.h>
#include<time.h>
#include<string.h>
/*
main函数
功能:显示界面,判断用户要使用哪个功能。
入参:无
返回值:无
*/
void main() 
{
    int ret=0;
    char number[10];
    park_init();
    //所有数据结构初始化
    //存放用户输入的字符
    while(1) 
    {
        /*显示界面*/
        printf("##################          停车场  v1.0           ##################\n");
        printf("#                                                                   #\n");
        printf("#####################################################################\n");
        printf("#                                                                   #\n");
        printf("#           1-----------------进车登记-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           2-----------------出车登记-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           3-----------------车辆查询-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           4-----------------出入记录-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           5-----------------场内车辆-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           6-----------------等候车辆-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           7-----------------退出系统-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#####################################################################\n");
        printf("#                                                  201808           #\n");
        printf("#####################################################################\n");
        gets( number );
        switch ( *number ) //选择需要什么功能 
        {
            case '1': //进车登记 
            {
                system("cls");
                ret=car_in();
                if(ret==FAILURE) 
                {
                    printf("输入错误!");
                }
                printf("入车成功!\n");
                getchar();
                system("cls");
            }
            break;
            case '2': //出车登记 
            {
                system("cls");
                ret=car_out();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                printf("出车成功!\n");
                getchar();
                system("cls");
            }
            break;
            case '3': //查找车辆 
            {
                system("cls");
                ret=find_car();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                //printf("-------------------------------------------------------------------------------------------------------------");
                getchar();
                system("cls");
            }
            break;
            case '4': //出入记录 
            {
                system("cls");
                ret=record_all();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                getchar();
                system("cls");
            }
            break;
            case '5': //场内车辆 
            {
                system("cls");
                ret=car_park();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                getchar();
                system("cls");
            }
            break;
            case '6'
            {
                system("cls");
                //等候车辆
                ret=Car_wait();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                getchar();
                system("cls");
            }
            break;
            case '7':
                        printf("欢迎下次使用\n");
            break;
            default:
                            system("cls");
            printf( "操作错误,此项不存在!\n" );
            getchar();
            system("cls");
            break;
        }
        if ( strcmp( number, "7" ) == 0 )
                        break;
    }
}

park.h

?
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
#ifndef _PARK_H
#define _PAEK_H
#include <time.h>
#include <stdio.h>
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10002
#define FALSE 10003
#define SIZE 6   //车场大小
/*汽车信息,节点结构体
*/
struct car_info 
{
    char name[10];
    //  车牌
    time_t time1;
    //  入场时间
    time_t time2;
    //  出场时间
}
;
typedef struct car_info CAR_I;
/*停放,让路顺序栈结构体
需要的参数为 :车牌号,出场时间,入场时间
配套的子函数 :初始化,判断是否为空,出栈,入栈,查栈*/
struct sequencestack 
{
    int top;
    CAR_I *date;
}
;
typedef struct sequencestack ST;
/*队列节点结构体
需要的参数为 :车牌号,下一个节点地址
*/
struct car_wait 
{
    char name[10];
    struct car_wait *next;
}
;
typedef struct car_wait CAR_W;
/*等候链式队列结构
需要的参数为 :头指针,尾指针
配套的子函数 :初始化,出队列,入队列
*/
struct linkqueue 
{
    CAR_W *front;
    CAR_W *rear;
}
;
typedef struct linkqueue LQ;
/*存放所有信息的链表
需要的参数为:车牌号,出入厂状态,入场时间,出场时间,下一个节点的地址
需要的函数为:初始化,入链表,查找数据,遍历打印
*/
struct all_info 
{
    char name[10];
    char sit[10];
    time_t time1;
    //  入场时间
    time_t time2;
    //  出场时间
    struct all_info *next;
}
;
typedef struct all_info A_INFO;
int car_in();
int car_out();
int find_car();
int record_all();
int car_park();
int Car_wait();
int STinit(ST **q);
int STempty(ST *q);
int STinsert(ST *q, char na[] ,time_t time);
int STout(ST *q,char *a, time_t *time);
int LQinit(LQ **q);
int LQinsert(LQ *q, char na[]);
int LQout(LQ *q, char *a);
int LLinit(A_INFO **q);
int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB);
int LLfind(A_INFO *q, char na[]);
int LLget(A_INFO *q, int n, A_INFO **k);
int LLtra(A_INFO *q);
int parkadd(CAR_I car[], char na[], time_t time);
int parkdel(CAR_I car[] ,char na[],time_t time);
int print(CAR_I car[]);
int park_init();
struct tm * local_time(time_t *tmpcal_ptr);
int exchange(A_INFO *q, char na[], char s[]);
#endif

fun.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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#include<stdio.h>
#include"park.h"
#include<stdlib.h> 
#include<time.h>
#include<string.h>
int num = 0;
//场内车辆数
ST *stack_park;
//停放栈
ST *stack_exchange;
//交换栈
LQ *queue_wait;
//等候队列
A_INFO  *all_car;
//存放所有信息的链表
/*
顺序栈初始化:
    参数:结构体指针
*/
int STinit(ST **q) 
{
    if(q == NULL) 
    {
        return FAILURE;
    }
    *q = (ST *)malloc(sizeof(ST)*1);
    //为结构体指针分配空间
    (*q)->top = -1;
    (*q)->date = (CAR_I *)malloc(sizeof(CAR_I)*SIZE);
    //为内容指针分配空间
    return SUCCESS;
}
/*
顺序栈判断是否为空
    参数:结构体指针
*/
int STempty(ST *q) 
{
    if(q == NULL) 
    {
        return FAILURE;
    }
    return(q->top == -1)? TRUE:FALSE;
    //空返回true   不空返回false
}
/*
顺序栈入栈
    参数:结构体指针,车牌,入场时间
*/
int STinsert(ST *q, char na[] ,time_t time
{
    if(q == NULL || q->top >= SIZE-1) 
    {
        return FAILURE;
    }
    strcpy( q->date[q->top+1].name, na );
    q->date[q->top+1].time1 = time;
    q->top++;
    return SUCCESS;
}
/*
结构体出栈
    参数:结构体指针,保存车牌的形参,保存入场时间的结构体指针
*/
int STout(ST *q,char *a, time_t *time
{
    if(q == NULL) 
    {
        return FAILURE;
        //错误返回failure
    }
    if(q->top == -1) 
    {
        return FALSE;
        //空返回false
    }
    strcpy(a, q->date[q->top].name);
    //a被修改为出场的车牌
    *time = q->date[q->top].time1;
    //time被修改为入场时间
    q->top--;
    return SUCCESS;
}
/*
链式队列初始化
    参数:队列的结构体指针
*/
int LQinit(LQ **q) 
{
    CAR_W (*p);
    (*q) = (LQ *)malloc(sizeof(LQ));
    if( (*q) == NULL ) 
    {
        return FAILURE;
    }
    p = (CAR_W *)malloc(sizeof(CAR_W));
    if(p == NULL) 
    {
        return FAILURE;
    }
    p->next = NULL;
    (*q)->front = (*q)->rear =p;
    return SUCCESS;
}
/*
链式队列入队
    参数:队列的结构体指针,车牌
*/
int LQinsert(LQ *q, char na[]) 
{
    CAR_W *p;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = (CAR_W *)malloc(sizeof(CAR_W));
    if (NULL == p) 
    {
        return FAILURE;
    }
    strcpy(p->name, na);
    p->next = NULL;
    q->rear->next = p;
    q->rear = p;
    return SUCCESS;
}
/*
链式队列出队
    参数:队列结构体指针
*/
int LQout(LQ *q, char *a) 
{
    //  char na[10];
    CAR_W *p = q->front->next;
    if (NULL == q ) 
    {
        return FAILURE;
    }
    if(q->rear == q->front) 
    {
        return FALSE;
    }
    strcpy(a, p->name);
    q->front->next = p->next;
    if (NULL == p->next) 
    {
        q->rear = q->front;
        //用参数a保存出去的车牌
    }
    free(p);
    return SUCCESS;
}
/*
链表初始化
    参数:头指针
*/
int LLinit(A_INFO **q) 
{
    (*q) = (A_INFO *)malloc(sizeof(A_INFO));
    if( (*q) == NULL) 
    {
        return FAILURE;
    }
    (*q)->next = NULL;
    return SUCCESS;
}
/*
   链表插入(头插)
        参数:头指针,插入的位置(1), 车牌, 状态, 入场时间, 出场时间
*/
int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB) 
{
    A_INFO *l;
    A_INFO *p = q;
    int k = 1;
    if (NULL == q) 
    {
        return FAILURE;
    }
    while(k < n && p != NULL) 
    {
        p=p->next;
        k++;
    }
    if(k > n || p == NULL) 
    {
        return FAILURE;
    }
    l = (A_INFO *)malloc(sizeof(A_INFO)*1);
    if (NULL == l) 
    {
        return FAILURE;
    }
    strcpy(l->name, na);
    strcpy(l->sit ,s);
    l->time1 = timeA;
    l->time2 = timeB;
    l->next = p->next;
    p->next = l;
    return SUCCESS;
}
/*
链表定位
    参数: 头指针,车牌
*/
int LLfind(A_INFO *q, char na[]) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            return len;
            //找到返回位置
        }
        p = p->next;
        len++;
    }
    return FALSE;
    //未找到返回false
}
/*
链表查询
        参数:头指针,位置
*/
int LLget(A_INFO *q, int n, A_INFO **k) 
{
    int i;
    A_INFO *p = q;
    if (NULL == q) 
    {
        return FAILURE;
    }
    for (i = 0; i < n && p != NULL ; i++) 
    {
        p = p->next;
    }
    if(!p) 
    {
        return FAILURE;
    }
    (*k) = p;
    //用k指针保存找到的结构体地址
    return SUCCESS;
}
/*
链表遍历
    参数:头指针
*/
int LLtra(A_INFO *q) 
{
    double cost;
    struct tm *time1;
    struct tm *time2;
    time_t timenow;
    A_INFO *p;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q;
    while (p->next) 
    {
        p = p->next;
        ////////////////////记得改这里
        time1 = local_time(&p->time1);
        if(time1 != NULL) 
        {
            printf("车牌:%s   状态:%s   入场时间:%d:%d:%d",p->name,p->sit, time1->tm_hour, time1->tm_min, time1->tm_sec);
            time2 = local_time(&p->time2);
            if(time2 != NULL) 
            {
                cost = difftime(p->time2,p->time1);
                printf("  出场时间:%d:%d:%d", time2->tm_hour, time2->tm_min, time2->tm_sec);
                printf("  停车时间:%f秒\n",cost);
                printf("-------------------------------------------------------------------------------------------------------------\n");
            } else
            {
                time(&timenow);
                cost = difftime(timenow,p->time1);
                printf("  停车时间为:%f秒\n" ,cost);
                printf("-------------------------------------------------------------------------------------------------------------\n");
                //return 0;
            }
        } else
        {
            printf("车牌:%s  排队中\n",p->name);
            printf("-------------------------------------------------------------------------------------------------------------\n");
            //  return 0;
        }
 
    }
    return SUCCESS;
}
/*
结构体数组增加
    参数:结构体数组, 车牌,入场时间(下标使用全局变量-车的数量)
*/
int parkadd(CAR_I car[], char na[], time_t time
{
    if(num>=6) 
    {
        return FAILURE;
    }
    strcpy(car[num].name, na);
    car[num].time1 = time;
    num++;
    return SUCCESS;
}
/*
结构体数组减少
    参数:同上(时间为出厂)
*/
int parkdel(CAR_I car[] ,char na[],time_t time
{
    int i;
    if(num == 0) 
    {
        return FAILURE;
    }
    for (i = 0; i < num; i++) 
    {
        if(strcmp(car[i].name, na)==0) 
        {
            for (; i<num; i++) 
            {
                car[i] = car[i+1];
            }
            break;
        }
    }
    num--;
    return SUCCESS;
}
/*
打印所有
    参数:结构体数组
*/
int print(CAR_I car[]) 
{
    int i;
    for (i = 0; i<num; i++) 
    {
        printf("场内车辆信息");
    }
    return SUCCESS;
}
/*
修改链表状态函数
    参数 :头指针,所需修改的车牌,修改为的状态
*/
int exchange(A_INFO *q, char na[], char s[]) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            strcpy( p->sit, s ) ;
            return SUCCESS;
        }
        p = p->next;
        len++;
    }
    return FALSE;
}
/*
修改链表time2函数
    参数 :头指针,所需修改的车牌,修改为的time
*/
int exchange1(A_INFO *q, char na[], time_t tmpcal_ptr) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            p->time2 = tmpcal_ptr;
            return SUCCESS;
        }
        p = p->next;
        len++;
    }
    return FALSE;
}
/*
修改链表time1函数
    参数 :头指针,所需修改的车牌,修改为的time
*/
int exchange2(A_INFO *q, char na[], time_t tmpcal_ptr) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            p->time1 = tmpcal_ptr;
            return SUCCESS;
        }
        p = p->next;
        len++;
    }
    return FALSE;
}
/*////////////////////////////////////
进车登记函数
功能:车辆进入登记,修改各存放结构体中的信息。
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int car_in() 
{
    int i;
    char a[12];
    //记录车牌号
    int ret;
    time_t tmpcal_ptr;
    time_t tmpcal_ptr1;
    time_t tmpcal_ptr2;
    //为了给出车时间赋空值
    printf("请输入车牌号:\n");
    gets(a);
    time(&tmpcal_ptr);
    ret=STinsert(stack_park, a ,tmpcal_ptr);
    if(ret==FAILURE) 
    {
        printf("停车场已满,排队中\n");
        ret=LQinsert(queue_wait, a);
        LLinsert(all_car, 1, a, "排队中",tmpcal_ptr1, tmpcal_ptr2);
        // exchange(all_car, a, "排队中");
        if(FAILURE==ret) 
        {
            return FAILURE;
        }
    } else
    {
        LLinsert(all_car, 1, a, "在场中",tmpcal_ptr, tmpcal_ptr2);
    }
    for (i=0; i<stack_park->top+1; i++) 
    {
        printf("车牌 :%s\n",stack_park->date[i].name);
    }
    return SUCCESS;
}
/*////////////////////////////////////
出车登记函数
功能:车辆出场登记,修改各存放结构体中的信息。
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int car_out() 
{
    char a[12];
    char b[12];
    char c[12];
    int ret;
    time_t tmpcal_ptr3;
    //出场时间
    time_t tmpcal_ptr4;
    //
    printf("\n请输入出车车牌号:\n\n");
    gets(a);
    time(&tmpcal_ptr3);
    //  ret =STout(stack_park, b, &tmpcal_ptr4 );
    //  while(strcmp(b,a)!=0)
    while((ret = STout(stack_park, b, &tmpcal_ptr4 ))!=FALSE) 
    {
        if(strcmp(b,a)==0) 
        {
            break;
        }
        STinsert(stack_exchange, b, tmpcal_ptr4);
        //  ret =STout(stack_park, b, &tmpcal_ptr4 );
    }
    if(ret == FALSE) 
    {
        printf("未找到该车!\n\n");
        while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE) 
        {
            STinsert(stack_park, b,tmpcal_ptr4 );
        }
        return FAILURE;
    } else
    {
        ret = exchange(all_car , b ,"出场了");
        ret = exchange1(all_car ,b ,tmpcal_ptr3);
        while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE) 
        {
            STinsert(stack_park, b,tmpcal_ptr4 );
        }
        if((ret = LQout(queue_wait, c))!=FALSE) 
        {
            time(&tmpcal_ptr4);
            STinsert(stack_park, c,tmpcal_ptr4);
            ret = exchange(all_car , c ,"在场中");
            ret = exchange2(all_car ,c ,tmpcal_ptr3);
        }
    }
    return SUCCESS;
}
/*////////////////////////////////////
寻找车辆函数
功能:在存放信息的链表中寻找车辆。
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int find_car() 
{
    int len;
    char a[12];
    double cost;
    struct tm *time1;
    struct tm *time2;
    time_t timenow;
    A_INFO *k;
    int ret;
    printf("请输入你要查询的车牌:\n");
    gets(a);
    len = LLfind(all_car, a);
    if(len == FALSE) 
    {
        printf("未来过\n\n");
        return FAILURE;
    }
    ret = LLget(all_car, len, &k);
    time1 = local_time(&k->time1);
    if(time1 != NULL)
    {
    printf("车牌:%s\n状态:%s\n入场时间:%d:%d:%d\n",k->name,k->sit, time1->tm_hour, time1->tm_min, time1->tm_sec);
    time2 = local_time(&k->time2);
    if(time2 != NULL) 
    {
        cost = difftime(k->time2,k->time1);
        printf("出场时间:%d:%d:%d\n", time2->tm_hour, time2->tm_min, time2->tm_sec);
        printf("停车时间:%f\n秒",cost);
    } else
    {
        time(&timenow);
        cost = difftime(timenow,k->time1);
        printf("出场时间:未出场\n停车时间:%f秒\n",cost);
        return 0;
    }
    }
    else
        printf("等候中");
    return SUCCESS;
}////////////////////////
查看出入登记函数
功能:遍历打印存放所有信息的链表
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int record_all() 
{
    LLtra(all_car);
    return SUCCESS;
}
/*////////////////////////////////////
查看停车场内车辆信息函数
功能:遍历打印存放停车场内信息的数组
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int car_park() 
{
    int i;
    double cost;
    struct tm *time1;
    time_t timenow;
    for (i=0; i<stack_park->top+1; i++) 
    {
        time1 = local_time(&stack_park->date[i].time1);
        printf("车牌:%s     入场时间:%d:%d:%d",stack_park->date[i].name, time1->tm_hour, time1->tm_min, time1->tm_sec);
        time(&timenow);
        cost = difftime(timenow,stack_park->date[i].time1);
        printf("  未出场,停车时间为:%f秒\n\n",cost);
        printf("-------------------------------------------------------------------------------------------------------------\n");
    }
    return SUCCESS;
}
/*////////////////////////////////////
查看等候队列内车辆信息函数
功能:遍历打印等候队列中车辆
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int Car_wait() 
{
    CAR_W *p;
    p = queue_wait->front;
    while(p->next) 
    {
        p=p->next;
        printf("车牌:%s\n\n",p->name);
    }
    return SUCCESS;
}
/*////////////////////////////////////
初始化函数
功能:初始化
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int park_init() 
{
    STinit(&stack_park);
    STinit(&stack_exchange);
    LQinit(&queue_wait);
    LLinit(&all_car);
    return SUCCESS;
}
/*////////////////////////////////////
时间函数
功能:转换成当地时间
入参:time_t *tmpcal_ptr(等待转换的时间)
返回值:time_local(当地时间)
////////////////////////////////////*/
struct tm * local_time(time_t *tmpcal_ptr) 
{
    struct tm *time_local = NULL;
    time_local = localtime(tmpcal_ptr);
    //转换成当地时间
    return time_local;
}

现象:

C语言实现停车场项目

C语言实现停车场项目

linux下代码

分为三个文件

main_u.c fun_u.c park_u.h

main_u.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
#include<stdio.h>
#include"park_u.h"
#include<stdlib.h>
#include<time.h>
#include<string.h>
/*
main函数
功能:显示界面,判断用户要使用哪个功能。
入参:无
返回值:无
*/
void main() 
{
    int ret=0;
    char number[10];
    park_init();
    //所有数据结构初始化
    //存放用户输入的字符
    while(1) 
    {
        /*显示界面*/
        printf("##################          停车场  v1.0           ##################\n");
        printf("#                                                                   #\n");
        printf("#####################################################################\n");
        printf("#                                                                   #\n");
        printf("#           1-----------------进车登记-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           2-----------------出车登记-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           3-----------------车辆查询-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           4-----------------出入记录-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           5-----------------场内车辆-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           6-----------------等候车辆-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           7-----------------退出系统-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#####################################################################\n");
        printf("#                                                  201808           #\n");
        printf("#####################################################################\n");
        gets( number );
        switch ( *number ) //选择需要什么功能 
        {
            case '1': //进车登记 
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                ret=car_in();
                if(ret==FAILURE) 
                {
                    printf("输入错误!");
                }
                printf("入车成功!\n");
                getchar();
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
            break;
            case '2': //出车登记 
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                ret=car_out();
                if(ret!=FAILURE) 
                {
                    printf("出车成功!\n");  
                }
 
                getchar();
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
            break;
            case '3': //查找车辆 
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                ret=find_car();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                //printf("-------------------------------------------------------------------------------------------------------------");
                getchar();
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
            break;
            case '4': //出入记录 
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                ret=record_all();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                getchar();
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
            break;
            case '5': //场内车辆 
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                ret=car_park();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                getchar();
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
            break;
            case '6'
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                //等候车辆
                ret=Car_wait();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                getchar();
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
            break;
            case '7':
                        printf("欢迎下次使用\n");
            break;
            default:
                            printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            printf( "操作错误,此项不存在!\n" );
            getchar();
            printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            break;
        }
        if ( strcmp( number, "7" ) == 0 )
                        break;
    }
}

fun_u.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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#include<stdio.h>
#include"park_u.h"
#include<stdlib.h> 
#include<time.h>
#include<string.h>
int num = 0;
//场内车辆数
ST *stack_park;
//停放栈
ST *stack_exchange;
//交换栈
LQ *queue_wait;
//等候队列
A_INFO  *all_car;
//存放所有信息的链表
/*
顺序栈初始化:
    参数:结构体指针
*/
int STinit(ST **q) 
{
    if(q == NULL) 
    {
        return FAILURE;
    }
    *q = (ST *)malloc(sizeof(ST)*1);
    //为结构体指针分配空间
    (*q)->top = -1;
    (*q)->date = (CAR_I *)malloc(sizeof(CAR_I)*SIZE);
    //为内容指针分配空间
    return SUCCESS;
}
/*
顺序栈判断是否为空
    参数:结构体指针
*/
int STempty(ST *q) 
{
    if(q == NULL) 
    {
        return FAILURE;
    }
    return(q->top == -1)? TRUE:FALSE;
    //空返回true   不空返回false
}
/*
顺序栈入栈
    参数:结构体指针,车牌,入场时间
*/
int STinsert(ST *q, char na[] ,time_t time
{
    if(q == NULL || q->top >= SIZE-1) 
    {
        return FAILURE;
    }
    strcpy( q->date[q->top+1].name, na );
    q->date[q->top+1].time1 = time;
    q->top++;
    return SUCCESS;
}
/*
结构体出栈
    参数:结构体指针,保存车牌的形参,保存入场时间的结构体指针
*/
int STout(ST *q,char *a, time_t *time
{
    if(q == NULL) 
    {
        return FAILURE;
        //错误返回failure
    }
    if(q->top == -1) 
    {
        return FALSE;
        //空返回false
    }
    strcpy(a, q->date[q->top].name);
    //a被修改为出场的车牌
    *time = q->date[q->top].time1;
    //time被修改为入场时间
    q->top--;
    return SUCCESS;
}
/*
链式队列初始化
    参数:队列的结构体指针
*/
int LQinit(LQ **q) 
{
    CAR_W (*p);
    (*q) = (LQ *)malloc(sizeof(LQ));
    if( (*q) == NULL ) 
    {
        return FAILURE;
    }
    p = (CAR_W *)malloc(sizeof(CAR_W));
    if(p == NULL) 
    {
        return FAILURE;
    }
    p->next = NULL;
    (*q)->front = (*q)->rear =p;
    return SUCCESS;
}
/*
链式队列入队
    参数:队列的结构体指针,车牌
*/
int LQinsert(LQ *q, char na[]) 
{
    CAR_W *p;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = (CAR_W *)malloc(sizeof(CAR_W));
    if (NULL == p) 
    {
        return FAILURE;
    }
    strcpy(p->name, na);
    p->next = NULL;
    q->rear->next = p;
    q->rear = p;
    return SUCCESS;
}
/*
链式队列出队
    参数:队列结构体指针
*/
int LQout(LQ *q, char *a) 
{
    //  char na[10];
    CAR_W *p = q->front->next;
    if (NULL == q ) 
    {
        return FAILURE;
    }
    if(q->rear == q->front) 
    {
        return FALSE;
    }
    strcpy(a, p->name);
    q->front->next = p->next;
    if (NULL == p->next) 
    {
        q->rear = q->front;
        //用参数a保存出去的车牌
    }
    free(p);
    return SUCCESS;
}
/*
链表初始化
    参数:头指针
*/
int LLinit(A_INFO **q) 
{
    (*q) = (A_INFO *)malloc(sizeof(A_INFO));
    if( (*q) == NULL) 
    {
        return FAILURE;
    }
    (*q)->next = NULL;
    return SUCCESS;
}
/*
   链表插入(头插)
        参数:头指针,插入的位置(1), 车牌, 状态, 入场时间, 出场时间
*/
int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB) 
{
    A_INFO *l;
    A_INFO *p = q;
    int k = 1;
    if (NULL == q) 
    {
        return FAILURE;
    }
    while(k < n && p != NULL) 
    {
        p=p->next;
        k++;
    }
    if(k > n || p == NULL) 
    {
        return FAILURE;
    }
    l = (A_INFO *)malloc(sizeof(A_INFO)*1);
    if (NULL == l) 
    {
        return FAILURE;
    }
    strcpy(l->name, na);
    strcpy(l->sit ,s);
    l->time1 = timeA;
    l->time2 = timeB;
    l->next = p->next;
    p->next = l;
    return SUCCESS;
}
/*
链表定位
    参数: 头指针,车牌
*/
int LLfind(A_INFO *q, char na[]) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            return len;
            //找到返回位置
        }
        p = p->next;
        len++;
    }
    return FALSE;
    //未找到返回false
}
/*
链表查询
        参数:头指针,位置
*/
int LLget(A_INFO *q, int n, A_INFO **k) 
{
    int i;
    A_INFO *p = q;
    if (NULL == q) 
    {
        return FAILURE;
    }
    for (i = 0; i < n && p != NULL ; i++) 
    {
        p = p->next;
    }
    if(!p) 
    {
        return FAILURE;
    }
    (*k) = p;
    //用k指针保存找到的结构体地址
    return SUCCESS;
}
/*
链表遍历
    参数:头指针
*/
int LLtra(A_INFO *q) 
{
    double cost;
    struct tm *time1;
    struct tm *time2;
    time_t timenow=0;
    A_INFO *p;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q;
    while (p->next) 
    {
        p = p->next;
        time1 = local_time(&p->time1);
        if(time1 != NULL) 
        {
            printf("车牌:%s   状态:%s   入场时间:%d:%d:%d",p->name,p->sit, time1->tm_hour, time1->tm_min, time1->tm_sec);
            time2 = local_time(&p->time2);
            if(!(time2->tm_hour==8&&time2->tm_min==0&&time2->tm_sec==0)) 
            {
                cost = difftime(p->time2,p->time1);
                printf("  出场时间:%d:%d:%d", time2->tm_hour, time2->tm_min, time2->tm_sec);
                printf("  停车时间:%f秒\n",cost);
                printf("-------------------------------------------------------------------------------------------------------------\n");
            } else
            {
                time(&timenow);
                cost = difftime(timenow,p->time1);
                printf("  停车时间为:%f秒\n" ,cost);
                printf("-------------------------------------------------------------------------------------------------------------\n");
                //return 0;
            }
        } else
        {
            printf("车牌:%s  排队中\n",p->name);
            printf("-------------------------------------------------------------------------------------------------------------\n");
            //  return 0;
        }
 
    }
    return SUCCESS;
}
/*
结构体数组增加
    参数:结构体数组, 车牌,入场时间(下标使用全局变量-车的数量)
*/
int parkadd(CAR_I car[], char na[], time_t time
{
    if(num>=6) 
    {
        return FAILURE;
    }
    strcpy(car[num].name, na);
    car[num].time1 = time;
    num++;
    return SUCCESS;
}
/*
结构体数组减少
    参数:同上(时间为出厂)
*/
int parkdel(CAR_I car[] ,char na[],time_t time
{
    int i;
    if(num == 0) 
    {
        return FAILURE;
    }
    for (i = 0; i < num; i++) 
    {
        if(strcmp(car[i].name, na)==0) 
        {
            for (; i<num; i++) 
            {
                car[i] = car[i+1];
            }
            break;
        }
    }
    num--;
    return SUCCESS;
}
/*
打印所有
    参数:结构体数组
*/
int print(CAR_I car[]) 
{
    int i;
    for (i = 0; i<num; i++) 
    {
        printf("场内车辆信息");
    }
    return SUCCESS;
}
/*
修改链表状态函数
    参数 :头指针,所需修改的车牌,修改为的状态
*/
int exchange(A_INFO *q, char na[], char s[]) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            strcpy( p->sit, s ) ;
            return SUCCESS;
        }
        p = p->next;
        len++;
    }
    return FALSE;
}
/*
修改链表time2函数
    参数 :头指针,所需修改的车牌,修改为的time
*/
int exchange1(A_INFO *q, char na[], time_t tmpcal_ptr) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            p->time2 = tmpcal_ptr;
            return SUCCESS;
        }
        p = p->next;
        len++;
    }
    return FALSE;
}
/*
修改链表time1函数
    参数 :头指针,所需修改的车牌,修改为的time
*/
int exchange2(A_INFO *q, char na[], time_t tmpcal_ptr) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            p->time1 = tmpcal_ptr;
            return SUCCESS;
        }
        p = p->next;
        len++;
    }
    return FALSE;
}
/*////////////////////////////////////
进车登记函数
功能:车辆进入登记,修改各存放结构体中的信息。
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int car_in() 
{
    int i;
    char a[12];
    //记录车牌号
    int ret;
    time_t tmpcal_ptr;
    time_t tmpcal_ptr1;
    time_t tmpcal_ptr2;
    //为了给出车时间赋空值
    printf("请输入车牌号:\n");
    gets(a);
    time(&tmpcal_ptr);
    ret=STinsert(stack_park, a ,tmpcal_ptr);
    if(ret==FAILURE) 
    {
        printf("停车场已满,排队中\n");
        ret=LQinsert(queue_wait, a);
        LLinsert(all_car, 1, a, "排队中",tmpcal_ptr1, tmpcal_ptr2);
        // exchange(all_car, a, "排队中");
        if(FAILURE==ret) 
        {
            return FAILURE;
        }
    } else
    {
        LLinsert(all_car, 1, a, "在场中",tmpcal_ptr, tmpcal_ptr2);
    }
    for (i=0; i<stack_park->top+1; i++) 
    {
        printf("车牌 :%s\n",stack_park->date[i].name);
    }
    return SUCCESS;
}
/*////////////////////////////////////
出车登记函数
功能:车辆出场登记,修改各存放结构体中的信息。
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int car_out() 
{
    char a[12];
    char b[12];
    char c[12];
    int ret;
    time_t tmpcal_ptr3;
    //出场时间
    time_t tmpcal_ptr4;
    //
    printf("\n请输入出车车牌号:\n\n");
    gets(a);
    time(&tmpcal_ptr3);
    //  ret =STout(stack_park, b, &tmpcal_ptr4 );
    //  while(strcmp(b,a)!=0)
    while((ret = STout(stack_park, b, &tmpcal_ptr4 ))!=FALSE) 
    {
        if(strcmp(b,a)==0) 
        {
            break;
        }
        STinsert(stack_exchange, b, tmpcal_ptr4);
        //  ret =STout(stack_park, b, &tmpcal_ptr4 );
    }
    if(ret == FALSE) 
    {
        printf("未找到该车!\n\n");
        while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE) 
        {
            STinsert(stack_park, b,tmpcal_ptr4 );
        }
        return FAILURE;
    } else
    {
        ret = exchange(all_car , b ,"出场了");
        ret = exchange1(all_car ,b ,tmpcal_ptr3);
        while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE) 
        {
            STinsert(stack_park, b,tmpcal_ptr4 );
        }
        if((ret = LQout(queue_wait, c))!=FALSE) 
        {
            time(&tmpcal_ptr4);
            STinsert(stack_park, c,tmpcal_ptr4);
            ret = exchange(all_car , c ,"在场中");
            ret = exchange2(all_car ,c ,tmpcal_ptr3);
        }
    }
    return SUCCESS;
}
/*////////////////////////////////////
寻找车辆函数
功能:在存放信息的链表中寻找车辆。
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int find_car() 
{
    int len;
    char a[12];
    double cost;
    struct tm *time1;
    struct tm *time2;
    time_t timenow;
    A_INFO *k;
    int ret;
    printf("请输入你要查询的车牌:\n");
    gets(a);
    len = LLfind(all_car, a);
    if(len == FALSE) 
    {
        printf("未来过\n\n");
        return FAILURE;
    }
    ret = LLget(all_car, len, &k);
    time1 = local_time(&k->time1);
    if(time1 != NULL)
    {
    printf("车牌:%s\n状态:%s\n入场时间:%d:%d:%d\n",k->name,k->sit, time1->tm_hour, time1->tm_min, time1->tm_sec);
    time2 = local_time(&k->time2);
    if(time2 != NULL) 
    {
        cost = difftime(k->time2,k->time1);
        printf("出场时间:%d:%d:%d\n", time2->tm_hour, time2->tm_min, time2->tm_sec);
        printf("停车时间:%f\n秒",cost);
    } else
    {
        time(&timenow);
        cost = difftime(timenow,k->time1);
        printf("出场时间:未出场\n停车时间:%f秒\n",cost);
        return 0;
    }
    }
    else
        printf("等候中");
    return SUCCESS;
}
 
/*////////////////////////////////////
查看出入登记函数
功能:遍历打印存放所有信息的链表
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int record_all() 
{
    LLtra(all_car);
    return SUCCESS;
}
/*////////////////////////////////////
查看停车场内车辆信息函数
功能:遍历打印存放停车场内信息的数组
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int car_park() 
{
    int i;
    double cost;
    struct tm *time1;
    time_t timenow;
    for (i=0; i<stack_park->top+1; i++) 
    {
        time1 = local_time(&stack_park->date[i].time1);
        printf("车牌:%s     入场时间:%d:%d:%d",stack_park->date[i].name, time1->tm_hour, time1->tm_min, time1->tm_sec);
        time(&timenow);
        cost = difftime(timenow,stack_park->date[i].time1);
        printf("  未出场,停车时间为:%f秒\n\n",cost);
        printf("-------------------------------------------------------------------------------------------------------------\n");
    }
    return SUCCESS;
}
/*////////////////////////////////////
查看等候队列内车辆信息函数
功能:遍历打印等候队列中车辆
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int Car_wait() 
{
    CAR_W *p;
    p = queue_wait->front;
    while(p->next) 
    {
        p=p->next;
        printf("车牌:%s\n\n",p->name);
    }
    return SUCCESS;
}
/*////////////////////////////////////
初始化函数
功能:初始化
入参:无
返回值:SUCCESS/FAILURE
////////////////////////////////////*/
int park_init() 
{
    STinit(&stack_park);
    STinit(&stack_exchange);
    LQinit(&queue_wait);
    LLinit(&all_car);
    return SUCCESS;
}
/*////////////////////////////////////
时间函数
功能:转换成当地时间
入参:time_t *tmpcal_ptr(等待转换的时间)
返回值:time_local(当地时间)
////////////////////////////////////*/
struct tm * local_time(time_t *tmpcal_ptr) 
{
    struct tm *time_local = NULL;
    time_local = localtime(tmpcal_ptr);
    //转换成当地时间
    return time_local;
}

park_u.h

?
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
#ifndef _PARK_H
#define _PAEK_H
#include <time.h>
#include <stdio.h>
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10002
#define FALSE 10003
#define SIZE 6   //车场大小
/*汽车信息,节点结构体
*/
struct car_info 
{
    char name[10];
    //  车牌
    time_t time1;
    //  入场时间
    time_t time2;
    //  出场时间
}
;
typedef struct car_info CAR_I;
/*停放,让路顺序栈结构体
需要的参数为 :车牌号,出场时间,入场时间
配套的子函数 :初始化,判断是否为空,出栈,入栈,查栈*/
struct sequencestack 
{
    int top;
    CAR_I *date;
}
;
typedef struct sequencestack ST;
/*队列节点结构体
需要的参数为 :车牌号,下一个节点地址
*/
struct car_wait 
{
    char name[10];
    struct car_wait *next;
}
;
typedef struct car_wait CAR_W;
/*等候链式队列结构
需要的参数为 :头指针,尾指针
配套的子函数 :初始化,出队列,入队列
*/
struct linkqueue 
{
    CAR_W *front;
    CAR_W *rear;
}
;
typedef struct linkqueue LQ;
/*存放所有信息的链表
需要的参数为:车牌号,出入厂状态,入场时间,出场时间,下一个节点的地址
需要的函数为:初始化,入链表,查找数据,遍历打印
*/
struct all_info 
{
    char name[10];
    char sit[10];
    time_t time1;
    //  入场时间
    time_t time2;
    //  出场时间
    struct all_info *next;
}
;
typedef struct all_info A_INFO;
int car_in();
int car_out();
int find_car();
int record_all();
int car_park();
int Car_wait();
int STinit(ST **q);
int STempty(ST *q);
int STinsert(ST *q, char na[] ,time_t time);
int STout(ST *q,char *a, time_t *time);
int LQinit(LQ **q);
int LQinsert(LQ *q, char na[]);
int LQout(LQ *q, char *a);
int LLinit(A_INFO **q);
int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB);
int LLfind(A_INFO *q, char na[]);
int LLget(A_INFO *q, int n, A_INFO **k);
int LLtra(A_INFO *q);
int parkadd(CAR_I car[], char na[], time_t time);
int parkdel(CAR_I car[] ,char na[],time_t time);
int print(CAR_I car[]);
int park_init();
struct tm * local_time(time_t *tmpcal_ptr);
int exchange(A_INFO *q, char na[], char s[]);
#endif

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

原文链接:https://blog.csdn.net/weixin_42828324/article/details/81671568

延伸 · 阅读

精彩推荐
  • C/C++关于C语言qsort函数详解

    关于C语言qsort函数详解

    这篇文章主要介绍了关于C语言qsort函数详解的相关资料,需要的朋友可以参考下面文章内容...

    芒果再努力4752021-12-29
  • C/C++C语言实现扫雷游戏简易版

    C语言实现扫雷游戏简易版

    这篇文章主要为大家详细介绍了C语言实现扫雷游戏简易版,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    看满山暴雨打落花一定很10002021-10-08
  • C/C++VS2019+Opencv4.0+Win10配置详解

    VS2019+Opencv4.0+Win10配置详解

    这篇文章主要介绍了VS2019+Opencv4.0+Win10配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随...

    kiripeng5792021-08-31
  • C/C++基于C++ bitset常用函数及运算符(详解)

    基于C++ bitset常用函数及运算符(详解)

    下面小编就为大家带来一篇基于C++ bitset常用函数及运算符(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    胡小兔10172021-06-09
  • C/C++使用c语言输出杨辉三角形的简单方法

    使用c语言输出杨辉三角形的简单方法

    这篇文章主要给大家介绍了关于如何使用c语言输出杨辉三角形的简单方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习...

    li13764175396682021-10-26
  • C/C++老生常谈C++中实参形参的传递问题

    老生常谈C++中实参形参的传递问题

    下面小编就为大家带来一篇老生常谈C++中实参形参的传递问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C++教程网6762021-05-04
  • C/C++C++内存池两种方案解析

    C++内存池两种方案解析

    这篇文章主要详情介绍了C++内存池两种方案做对比,对此感兴趣的小伙伴一起来看看吧...

    Hickey Zhang5582021-12-24
  • C/C++C指针原理教程之垃圾回收-内存泄露

    C指针原理教程之垃圾回收-内存泄露

    C语言没有运行时库,无法自动压缩使用中的内存,缩小堆栈所需内存空间。若只申请内存,没有释放,势必造成系统内存不断减少、丢失。长时间的运行,...

    myhaspl11182021-07-21