-
Notifications
You must be signed in to change notification settings - Fork 0
/
停车栈.cpp
259 lines (238 loc) · 6.44 KB
/
停车栈.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define MaxCarparkCapacity 2
#define MaxCarWaitParkCapacity 2
typedef struct
{
time_t enCarpark ;
int carNumber ;
}Car;
typedef struct
{
Car data[MaxCarparkCapacity];
int top ;
}Carpark;
typedef struct
{
Car data[MaxCarWaitParkCapacity];
int rear ;
int front ;
}CarWaitPark;
void mainMenu(int *choice);//显示停车场主菜单
bool in_waitingP(CarWaitPark *carWaitPark,Car car,Carpark *carpark) ;//进入候车场,并调整两个停车场的车辆
bool out_waitingP(CarWaitPark *carWaitPark,Car *car) ;//出去候车场
Car * initCar(void);//初始化车
Carpark *initCarpark(void);//初始化停车场
CarWaitPark * initCarWaitPark(void);//初始化候车场
void inputCarNumber(Car *car) ; //输入汽车编号
bool carWaitParkEmpty(CarWaitPark *carWaitPark);//候车场空
bool carparkEmpty(Carpark *carpark) ;//停车场空
double parkingFee(Car car); //计费
void displayCarpark(Carpark carpark,int *choice);//显示停车场的车辆,并选择出去的车编号
bool popCarpark(Carpark *carpark, Car *car);//出车(出栈)
void adjustCarForOut(Carpark *carpark ,int choice,CarWaitPark *carWaitPark);//出停车场,并调整两个停车场的车辆
bool pushCarpark(Carpark *carpark,Car car);//进车(进栈)
int main()
{
Carpark *carpark = initCarpark();
CarWaitPark *carWaitPark = initCarWaitPark();
int choice;
while (true)
{
mainMenu(&choice);
if (choice == 1)
{
Car *car = initCar() ;
if ((carWaitPark->rear + 1)%MaxCarWaitParkCapacity == carWaitPark->front)
{
printf("候车场已满!\n");
}
else
{
inputCarNumber(car);
in_waitingP(carWaitPark, *car,carpark);
printf("汽车%d已进入候车场\n",car->carNumber);
}
}
else
{
displayCarpark(*carpark, &choice);
if (choice != -1)
{
adjustCarForOut(carpark, choice,carWaitPark);
}
}
}
return 0;
}
void mainMenu(int *choice)
{
reInput:
printf("-----------------菜单----------------\n");
printf("请选择:\n");
printf("1.进入候车场\n");
printf("2.缴费\n");
printf("-------------------------------------\n");
scanf("%d",choice);
if (*choice>=3 || *choice<=0)
{
printf("输入不合法,重新输入\n");
goto reInput ;
}
}
bool in_waitingP(CarWaitPark *carWaitPark,Car car,Carpark *carpark)
{
if ((carWaitPark->rear + 1)%MaxCarWaitParkCapacity == carWaitPark->front) {
return false ;
}else{
carWaitPark->rear = (carWaitPark->rear + 1)%MaxCarWaitParkCapacity ;
carWaitPark->data[carWaitPark->rear] = car ;
}
Car tempCar ;
while (carpark->top < MaxCarparkCapacity-1 && !carWaitParkEmpty(carWaitPark)) {
out_waitingP(carWaitPark, &tempCar);
time(&tempCar.enCarpark);
pushCarpark(carpark, tempCar);
}
return true ;
}
bool out_waitingP(CarWaitPark *carWaitPark,Car *car)
{
if (carWaitParkEmpty(carWaitPark))
{
return false ;
}
else
{
carWaitPark->front = (carWaitPark->front + 1)%MaxCarWaitParkCapacity ;
*car = carWaitPark->data[carWaitPark->front] ;
return true;
}
}
Car * initCar(void)
{
Car *car = (Car *)malloc(sizeof(Car));
car->carNumber = 0;
time(&car->enCarpark);
return car;
}
Carpark *initCarpark(void)
{
Carpark *carpark = (Carpark *)malloc(sizeof(Carpark));
carpark->top = -1;
return carpark ;
}
CarWaitPark * initCarWaitPark(void)
{
CarWaitPark *carWaitPark = (CarWaitPark *)malloc(sizeof(CarWaitPark));
carWaitPark->front = 0;
carWaitPark->rear = 0;
return carWaitPark ;
}
void inputCarNumber(Car *car)
{
printf("请输入汽车牌号:\n");
scanf("%d",&car->carNumber);
}
bool carWaitParkEmpty(CarWaitPark *carWaitPark)
{
return carWaitPark->front == carWaitPark->rear;
}
bool carparkEmpty(Carpark *carpark)
{
return carpark->top == -1 ;
}
double parkingFee(Car car)
{
time_t now ;
time(&now);
double parkingTime = difftime(now, car.enCarpark);
double rate = 0.00278 ;
return parkingTime * rate ;
}
void displayCarpark(Carpark carpark,int *choice)
{
Car car ;
int carCount = 0;
printf("-------------停车场列表--------------\n");
while (!carparkEmpty(&carpark))
{
carCount ++ ;
popCarpark(&carpark, &car);
printf("%d.编号%d\n",carCount ,car.carNumber);
}
if (carCount == 0)
{
printf("停车场无车\n");
*choice = -1 ;
}
else
{
reInput:
printf("请输入出库车序号:\n");
int carSerial ;
scanf("%d",&carSerial);
if (carSerial <= 0 || carSerial > carCount)
{
printf("输入无效,请重新输入\n");
goto reInput;
}
*choice = carSerial;
}
}
bool popCarpark(Carpark *carpark, Car *car)
{
if (carpark->top == -1)
{
return false ;
}
else
{
*car = carpark->data[carpark->top];
carpark->top -- ;
return true ;
}
}
void adjustCarForOut(Carpark *carpark ,int choice,CarWaitPark *carWaitPark)
{
Carpark *carTempPark = initCarpark() ;
int count = 1 ;
Car car ;
while (choice > count)
{
popCarpark(carpark, &car);
double free = parkingFee(car);
printf("汽车%d临时性出库,支付%f元\n",car.carNumber,free);
pushCarpark(carTempPark, car);
count ++ ;
}
popCarpark(carpark, &car);
double free = parkingFee(car);
printf("汽车%d出库,支付%f元\n",car.carNumber,free);
while (!carparkEmpty(carTempPark)) {
popCarpark(carTempPark, &car);
printf("汽车%d临时性进库\n",car.carNumber);
time(&car.enCarpark);
pushCarpark(carpark, car);
}
while (carpark->top < MaxCarparkCapacity-1 && !carWaitParkEmpty(carWaitPark)) {
out_waitingP(carWaitPark, &car);
time(&car.enCarpark);
pushCarpark(carpark, car);
}
}
bool pushCarpark(Carpark *carpark,Car car)
{
if (carpark->top == MaxCarparkCapacity -1)
{
return false ;
}
else
{
carpark->top ++ ;
carpark->data[carpark->top] = car ;
return true ;
}
}