学习日志4
姓名:朱启香 日期:2018.9.13
今日学习任务:编写关于停车场的代码 完成编译
今日任务完成情况:完成编译,代码数量500+
1、 车辆出栈
2、 出去的进来
3、 等候队列进来
今日开发中出现的问题汇总:编写程序容易出现小错误,敲打代码速度较慢
今日未解决问题:车辆出车问题未解决
自我评价:虽然能顺利完成编译 但是有时出现的错误自己找不出来,在同学的帮助下共同完成,也学到了很多新知识。
main.c
1 #include "park.h"
2 #include <stdio.h>
3
4 int main()
5 {
6 int choice;
7 stack park_stack,leave_stack;
8 queue wait_queue;
9
10 welcome();
11
12 Init(&park_stack,&leave_stack,&wait_queue);
13
14 while(1)
15 {
16 menu();
17 scanf("%d", &choice);
18
19 switch(choice)
20 {
21 case 1:
22 EnterPark(&park_stack,&wait_queue);
23 break;
24 case 2:
25 LeavePark(&park_stack,&leave_stack,&wait_queue);
26 break;
27 case 3:
28 ShowParkInfo(&park_stack);
29 break;
30 case 4:
31 ShowWaitInfo(&wait_queue);
32 break;
33 case 5:
34 bye();
35 break;
36 default:
37 printf("输入有误\n");
38 break;
39 }
40 }
41 return 0;
park.h
1 #ifndef PARK_H
2 #define PARK_H
3
4 #include <time.h>
5 #include <string.h>
6
7 #define MAXSIZE 5
8 #define SUCCESS 1000
9#define FAILURE 1001
10 #define FULL 1002
11
12 struct carinfo
13{
14 char number[10]; //车牌号
15 time_t park_time; //进场时间
16 struct carinfo *next; //指向下一辆车
17};
18 typedef struct carinfo car;
19
20 struct stackinfo
21 {
22 car data[MAXSIZE]; //结构他数组
23 int top; //栈顶指针
24 };
25 typedef struct stackinfo stack;
26
27 struct queueinfo
28 {
29 car *front; //队头指针
30 car *rear; //队尾指针
31};
32 typedef struct queueinfo queue;
33
34 void welcome();
35 void menu();
36 void bye();
37 void Init(stack *s1,stack *s2,queue *q);
38 int InitQueue(queue *q);
39 int InitStack(stack *s);
40 void EnterPark(stack *s,queue *q);
41 int push(stack *s,char *id);
42 int EnterQueue(queue *q,char *id);
43 void ShowParkInfo(stack *s);
44 void ShowWaitInfo(queue *q);
45 int EmptyQueue(queue *q);
46 void LeavePark(stack *s1,stack *s2,queue *q);
47 car pop(stack *s);
48 int EmptyStack(stack *s);
49 char *DelQueue(queue *q);
50
51 #endif
queue.c
1 #include "park.h"
2 #include <stdlib.h>
3 #include <string.h>
4
5 /*初始化队列*/
6 int InitQueue(queue *q)
7 {
8 if(NULL == q)
9 {
10 return FAILURE;
11 }
12 car *p = (car *)malloc(sizeof(car));
13 if(NULL == p)
14 {
15 return FAILURE;
16 }
17 p->next =NULL;
18 q->front = q->rear = p;
19 return SUCCESS;
20 }
21
22 int EnterQueue(queue *q,char *id)
23 {
24 if(NULL == q || NULL == id)
25 {
26 return FAILURE;
27 }
28 car *p = (car *)malloc(sizeof(car));
29 if(NULL == p)
30 {
31 return FAILURE;
32 }
33
34 strcpy(p->number,id);
35 p->next = NULL;
36
37 q->rear->next = p;
38 q->rear = p;
39
40 return SUCCESS;
41 }
42
43 /*判断队列是否为空*/
44 int EmptyQueue(queue *q)
45 {
46 if(NULL == q)
47 {
48 return FAILURE;
49 }
50
51 return (q->front == q->rear) ? SUCCESS : FAILURE;
52 }
53
54 /*出队操作,返回车牌号*/
55 char *DelQueue(queue *q)
56 {
57 if(NULL == q)
58 {
59 return NULL;
60 }
61 char *id =(char *)malloc(sizeof(char) * 10);
62 car *c = q->front->next;
63
64 if(c != NULL)
65 {
66 strcpy(id,c->number); //保存车牌号
67 q->front->next = c->next;
68 free(c);
69 }
70 if(c == q->rear) //如果队列只剩一辆车
71 {
72 q->rear = q->front;
73 }
74 return id;
75 }
park.c
1 #include "park.h"
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5
6 void welcome()
7 {
8 system("clear");
9
10 printf("\n\n\n\n\n\n");
11 printf("\t\t\t***********************\n");
12 printf("\t\t\t********WELCOME********\n");
13 printf("\t\t\t***********************\n");
14
15 sleep(2);
16 }
17
18 void menu()
19 {
20 system("clear");
21
22 printf("\n\n\n");
23 printf("\t\t1.停车\n");
24 printf("\t\t2.出车\n");
25 printf("\t\t3.查看场内车辆信息\n");
26 printf("\t\t4.查看等候车辆信息\n");
27 printf("\t\t5.退出系统\n");
28
29 printf("请输入你的选择:");
30 }
31
32 void bye()
33 {
34 system("clear");
35
36 printf("\n\n\n\n\n\t\t\tbyebye...\n");
37 sleep(1);
38 system("clear");
39 exit(1);
40 }
41
42 void Init(stack *s1, stack *s2, queue *q)
43 {
44 int ret;
45
46 ret=InitStack(s1);
47 if (FAILURE==ret)
48 {
49 printf("Init Stack Failure!\n");
50 }
51
52 ret=InitStack(s2);
53 if (FAILURE==ret)
54 {
55 printf("Init Stack Failure!\n");
56 }
57
58 ret=InitQueue(q);
59 if (FAILURE==ret)
60 {
61 printf("Init Queue Failure!\n");
62 }
63 }
64
65 void EnterPark(stack *s,queue *q)
66 {
67 char id[10]={0};
68 if(NULL==s || NULL==q)
69 {
70 return;
71 }
72
73 printf("请输入车牌号: ");
74 scanf("%s",id);
75
76 int ret=push(s,id);
77 if(ret==FAILURE)
78 {
79 printf("进场失败!\n");
80 }
81 else if(ret==FULL)
82 {
83
84 printf("停车场已满,进入等待队列\n");
85 sleep(1);
86 EnterQueue(q, id);
87 }
88 else
89 {
90 printf("停车成功\n");
91 sleep(1);
92 }
93 }
94
95 void ShowParkInfo(stack *s)
96 {
97 int i;
98 if(NULL==s)
99 {
100 return;
101 }
102
103 for(i=0;i<=s->top; i++)
104 {
105 printf("车牌号: %s\n",s->data[i].number);
106 printf("停车时长: %d\n",(int)(time(NULL)-s->data[i].park_time));
107 printf("****\n");
108 }
109 printf("Press Enter Continue...\n");
110 getchar();
111 getchar();
112}
113
114
115 void ShowWaitInfo(queue *q)
116 {
117 if(NULL==q)
118 {
119 return;
120 }
121
122 if(EmptyQueue(q)==SUCCESS)
123 {
124 printf("等候队列没有车辆\n");
125 sleep(1);
126 }
127 else
128 {
129 car *p=q->front->next;
130
131 while(p)
132 {
133 printf("车牌号: %s\n",p->number);
134 p=p->next;
135 }
136
137 printf("Press Enter Continue...\n");
138 getchar();
139 getchar();
140 }
141
142 }
143
144 void LeavePark(stack *s1,stack *s2,queue *q)
145 {
146 char id[10]={0};
147 int i;
148
149 if(NULL==s1|| NULL==s2||NULL==q)
150 {
151 return;
152 }
153 printf("请输入车牌号: ");
154 scanf("%s", id);
155
156 int length=s1->top;
157 for(i=0;i<=length;i++)
158 {
159 if(!strcmp(id,s1->data[s1->top].number))//匹配到车辆
160 {
161 pop(s1); //匹配到的车辆出栈
162 while (EmptyStack(s2) !=SUCCESS)
163 {
164 car c=pop(s2);//从让路栈出来
165 push(s1,c.number);//进入停车栈
166 }
167
168 if(EmptyQueue(q)!=SUCCESS)
169 {
170 char *id=DelQueue(q);
171 push(s1,id);
172 free(id);
173 }
174 break;
175 }
176 else
177 {
park.h
1 #ifndef PARK_H
2 #define PARK_H
3
4 #include <time.h>
5 #include <string.h>
6
7 #define MAXSIZE 5
8 #define SUCCESS 1000
9 #define FAILURE 1001
10 #define FULL 1002
11
12 struct carinfo
13{
14 char number[10];
15 time_t park_time;
16 struct carinfo *next;
17 };
18 typedef struct carinfo car;
19
20 struct stackinfo
21 {
22 car data[MAXSIZE];
23 int top;
24 };
25 typedef struct stackinfo stack;
26
27 struct queueinfo
28 {
29 car *front;
30 car *rear;
31 };
32 typedef struct queueinfo queue;
33
34 void welcome();
35 void menu();
36 void bye();
37 void Init(stack *s1, stack *s2, queue *q);
38 int InitStack(stack *s);
39 int InitQueue(queue *q);
40 int push(stack *s,char *id);
41 int EnterQueue(queue *q,char *id);
42 void EnterPark(stack *s,queue *q);
43 void ShowParkInfo(stack *s);
44 int EmptyQueue(queue *q);
45 void ShowWaitInfo(queue *q);
46 void LeavePark(stack *s1,stack *s2,queue *q);
47 car pop(stack *s);
48 int EmptyStack(stack *s);
49 char *DelQueue(queue *q);
50 #endif
stack.c
1 #include "park.h"
2 #include <string.h>
3
4 /*栈的初始化*/
5 int InitStack(stack *s)
6 {
7 if(NULL == s)
8 {
9 return FAILURE;
10 }
11 s->top = -1;
12
13 return SUCCESS;
14 }
15
16 /*进栈的操作*/
17 int push(stack *s,char *id)
18 {
19 if(NULL == s)
20 {
21 return FAILURE;
22 }
23
24 if(s->top == MAXSIZE - 1)
25 {
26 return FULL;
27 }
28 strcpy(s->data[s->top + 1].number,id); //拷贝车牌号
29 s->data[s->top + 1].park_time = time(NULL); //记录进场时间
30
31 s->top++;
32 return SUCCESS;
33 }
34
35 /*出栈操作*/
36 car pop(stack *s)
37 {
38 car c;
39 if(NULL == s)
40 {
41 return c;
42 }
43 if(s->top == -1)
44 {
45 return c;
46 }
47
48
49 strcpy(c.number, s->data[s->top].number);
50 c.park_time = s->data[s->top].park_time;
51
52 s->top--;
53 return c;
54 }
55
56 int EmptyStack(stack *s)
57 {
58 if(NULL == s)
59 {
60 return FAILURE;
61 }
62 return (s->top == -1) ? SUCCESS : FAILURE;
63 }