数据结构课设--火车售票系统

用C/C++写了一个火车售票系统,一起来看看吧:P

先看看简单的运行吧 :P

如果你觉得不错那就继续往下看吧……

首先新建一个文件为ticket.txt,和代码放到一起,保存列车的信息,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*不要复制注释
普铁:1,动车:0
列车号
出发地
出发站台
目的地
目的站台
出发时间
到达时间
站票|一等座
硬座|二等座
硬卧
*/
1 G5 北京 南站 上海 站 07:00 11:40 939 558
1 G1 北京 南站 上海 虹桥站 09:00 13:28 933 553
1 G57 北京 南站 杭州 东站 07:15 13:05 907 538.5
0 Z97 北京 西站 广州 东站 12:40 10:04+1 251 251 426
1 G1976 成都 东站 上海 虹桥站 10:57 22:25 1516 932.5
0 K284 成都 站 上海 站 18:22 07:06+2 273.5 273.5 463.5
0 K1363 北京 西站 成都 站 22:06 07:37+2 240 240 408
0 K1092 深圳 东站 成都 东站 17:26 09:26 289.5 289.5 501.5

接着就是贴代码了:

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstring>
#include <iomanip>
#define MAX_LINE 1024 //文件缓冲读取一行大小为1024
#define TICKET_NUM 11 //存储文件读取的数据,最大11个数据
using namespace std;

int TICKET_SUM; //可选择的车票总数

struct ticketMoney //车票价格
{
double stand; //站票
double hardSeat; //硬座
double hardSleeper; //硬卧
double firstSeat; //一等座
double secondSeat; //二等座
int standNum; //站票的数量
int hardSeatNum; //硬座的数量
int hardSleeperNum; //硬卧的数量
int firstSeatNum; //一等座的数量
int secondSeatNum; //二等座的数量
//默认构造方法
ticketMoney(){};
//获取普铁的车票价格
ticketMoney(double stand,double hardSeat,double hardSleeper):
stand(stand),
hardSeat(hardSeat),
hardSleeper(hardSleeper){};
//获取动车的车票价格
ticketMoney(double firstSeat,double secondSeat):
firstSeat(firstSeat),
secondSeat(secondSeat){};

};

struct ticketInform //车票信息
{
string flag; //0:普铁 1:动车
string trainNum; //车次
string fromPlace; //出发地
string fromStation; //出发站台
string toPlace; //目的地
string toStation; //目的站台
string fromTime; //出发时间
string toTime; //到站时间
ticketMoney money; //价格

ticketInform(){};
//获取火车的信息
ticketInform(string flag,string trainNum, string fromPlace, string fromStation, string toPlace,
string toStation, string fromTime, string toTime):
flag(flag),
trainNum(trainNum),
fromPlace(fromPlace),
fromStation(fromStation),
toPlace(toPlace),
toStation(toStation),
fromTime(fromTime),
toTime(toTime){};
//打印车票的提示信息
void printTrip()
{
printf("| 车次 | 出发地 | 目的地 | 出发时间 | 到达时间 |");
if (flag == "0")
{
printf(" 站票 | 硬座 | 硬卧 |\n");
}
else
{
printf(" 一等座 | 二等座 |\n");
}
}
//打印车票的内容
void printInform()
{
printTrip();
cout << setw(8) << trainNum << setw(8) << fromPlace << fromStation << setw(8) << toPlace <<
toStation << setw(10) << fromTime << setw(13) << toTime << setw(11);
if (flag == "0")
{
cout << money.stand << setw(9) <<money.hardSeat << setw(9) << money.hardSleeper;
}
else
{
cout << money.firstSeat << setw(10) << money.secondSeat;
}
cout << endl;
}
};

struct ticketList //全部车票
{
ticketInform ticket;
ticketList *next;
};

struct userInform //用户信息
{
string name; //用户名
string fromPlace; //出发地
string toPlace; //目的地
string fromStation; //出发站台
string toStation; //目的站台
string fromTime; //出发时间
double money; //票价
int seat; //座位
string trainNum; //车次
string seaTpye; //座位类型

userInform(){};
//获取用户出发地和目的地
userInform(string name, string fromPlace, string toPlace):
name(name),
fromPlace(fromPlace),
toPlace(toPlace){};
//获取用户想购买的车次相关票
userInform(string name, string trainNum):
name(name),
trainNum(trainNum){};
};

struct userList //用户车票
{
userInform user;
userList *next;
};

struct ticketOS
{
void printInterface(); //显示主界面
void userViewInterface(); //显示用户操作界面
ticketInform ticketInsert(string pp[]); //车票初始化
ticketList *ticketInquire(); //从文件获取车票
void printTicketInform(ticketList *L); //显示车票信息
void userVoteByPlace(userInform &user); //用户通过地点选票
void userVoteByNum(userInform &user); //用户通过车次选票
void userOperation(ticketList *L, userInform &user, ticketOS tos); //用户操作
bool userPrintTicketInform(ticketList *L, userInform user, int choose); //显示用户可选择的车票
bool checkByPlace(ticketList *L, userInform user); //判断地址是否存在
bool checkById(ticketList *L, userInform user); //判断车次是否存在
void userChooseTicket(userInform user, userList *userL,ticketList *L, int choose); //用户具体选择的票
bool judgeTicket(userList *userL, string Seat, double ticketMoney, int *ticketSum); //判断车票是否存在
void userVoteResult(userList *userL, string name); //用户选票结果
};

void ticketOS::printInterface()
{
printf(" ********************************************************************\n");
printf(" *** 火车售票系统 ***\n");
printf(" *** 欢迎您使用本软件,祝您旅途愉快!!! ***\n");
printf(" ********************************************************************\n");
}

void ticketOS::userViewInterface()
{
printf(" ****************用户操作**************\n");
printf(" *** 1:地点选票 ***\n");
printf(" *** 2:车次选票 ***\n");
printf(" *** 3:今日车票 ***\n");
printf(" *** 4:车票打印 ***\n");
printf(" *** 0:退出系统 ***\n");
printf(" **************************************\n");
}

ticketInform ticketOS::ticketInsert(string pp[])
{
ticketInform tif;
tif = ticketInform(pp[0], pp[1], pp[2], pp[3], pp[4], pp[5], pp[6], pp[7]);
//区别普铁和动车,给车票赋值
if (tif.flag == "0")
{
tif.money = ticketMoney(atof(pp[8].c_str()), atof(pp[9].c_str()), atof(pp[10].c_str()));
tif.money.firstSeat = tif.money.secondSeat = -1;
}
else
{
tif.money = ticketMoney(atof(pp[8].c_str()), atof(pp[9].c_str()));
tif.money.stand = tif.money.hardSeat = tif.money.hardSleeper = -1;
}
//随机车票数量
srand((unsigned)time(NULL));
tif.money.standNum = rand()%5+5;
tif.money.hardSeatNum = rand()%5+5;
tif.money.hardSleeperNum = rand()%5+5;
tif.money.firstSeatNum = rand()%5+5;
tif.money.secondSeatNum = rand()%5+5;
return tif;
}

ticketList *ticketOS::ticketInquire()
{
char buf[MAX_LINE]; //缓冲区
FILE *fp; //文件指针
int len; //行字符个数
if ((fp = fopen("ticket.txt","r")) == NULL)
{
perror("系统加载错误");
exit (1) ;
}
ticketList *L, *temp;
L = new (ticketList);
temp = L;
temp->next = NULL;
while (fgets(buf,MAX_LINE,fp) != NULL)
{
ticketList *t = new(ticketList);
char *p;
if (strlen(buf)==1) continue;
p = strtok(buf, " ");
string pp[TICKET_NUM];
int len = 0;
while(p)
{
pp[len++] = p;
p = strtok(NULL, " ");
}
t->ticket = ticketInsert(pp);
temp->next = t;
temp = temp->next;
}
temp->next = NULL;
return L;
}

void ticketOS::printTicketInform(ticketList *L)
{
printf("-------------------------------------售票信息-------------------------------------\n");
ticketList *t = L->next;
while (t != NULL)
{
t->ticket.printInform();
t = t->next;
}
printf("----------------------------------以上为今日售票信息----------------------------------\n");
}

void ticketOS::userVoteByPlace(userInform &user)
{
string name;
string fromPlace;
string toPlace;
printf("请输入您的姓名:");
cin >> name;
printf("请输入您的乘车地点:");
cin >> fromPlace;
printf("请输入您的目的地地点:");
cin >> toPlace;
user = userInform(name, fromPlace, toPlace);
}

void ticketOS::userVoteByNum(userInform &user)
{
string name;
string trainNum;
printf("请输入您的姓名:");
cin >> name;
printf("请输入您想要乘坐的车次:");
cin >> trainNum;
user = userInform(name, trainNum);
}

bool ticketOS::checkByPlace(ticketList *L, userInform user)
{
if (L->ticket.fromPlace == user.fromPlace && L->ticket.toPlace == user.toPlace)
return true;
return false;
}

bool ticketOS::checkById(ticketList *L, userInform user)
{
if (L->ticket.trainNum == user.trainNum)
return true;
return false;
}

bool ticketOS::userPrintTicketInform(ticketList *L, userInform user, int choose)
{
ticketList *temp = L->next;
ticketList *LL= new (ticketList);
ticketList *tt = LL;
tt->next = NULL;
TICKET_SUM = 0;
bool flag;
while (temp)
{
if (choose&1) flag = checkByPlace(temp, user);
else flag = checkById(temp, user);
if (flag)
{
ticketList *t = new (ticketList);
t->ticket = temp->ticket;
tt->next = t;
tt = tt->next;
printf("可选择的第%d张票:\n", TICKET_SUM+1);
temp->ticket.printInform();
TICKET_SUM++;
}
temp = temp->next;
}
tt->next = NULL;
if (!TICKET_SUM)
{
printf("对不起,没有该列车的相关信息!\n");
return false;
}
return true;
}

bool ticketOS::judgeTicket(userList *userL, string seat, double ticketMoney, int *ticketSum)
{
if (ticketMoney == -1)
{
printf("对不起,选票失败!\n");
return false;
}
if ( ticketSum <= 0)
{
printf("对不起,该列车票已经售空!\n");
return false;
}

userL->user.money = ticketMoney;
userL->user.seaTpye = seat;
userL->user.seat = *ticketSum;
*ticketSum = *ticketSum-1;
return true;
}

void ticketOS::userChooseTicket(userInform user, userList *userL,ticketList *L, int choose)
{
ticketList *temp = L->next;
userList *t = userL;
printf("请选择您的火车序号[1,%d]:", TICKET_SUM);
int x;
int k=0;
string seat;
bool flag = true;
scanf("%d", &x);
while(t->next) t = t->next;
if (x > TICKET_SUM && x < 1)
{
printf("对不起,没有该序号的火车,购票失败!!!\n");
return;
}
while(temp && choose&1)
{
if (checkByPlace(temp, user))
{
k++;
}
if (k == x) break;
temp = temp->next;
}
while(temp && choose&2)
{
if (checkById(temp, user))
{
k++;
}
if (k == x) break;
temp = temp->next;
}
printf("请选择您想要的座位(普通:站票|硬座|硬卧,动车:一等座|二等座):");
cin >> seat;
printf("\n");
userList *tt = new (userList);
if (seat == "站票")
{
flag = judgeTicket(tt, seat, temp->ticket.money.stand, &(temp->ticket.money.standNum));
}
else if (seat == "硬座")
{
flag = judgeTicket(tt, seat, temp->ticket.money.hardSeat, &(temp->ticket.money.hardSeatNum));
}
else if (seat == "硬卧")
{
flag = judgeTicket(tt, seat, temp->ticket.money.hardSleeper, &(temp->ticket.money.hardSleeperNum));
}
else if (seat == "一等座")
{
flag = judgeTicket(tt, seat, temp->ticket.money.firstSeat, &(temp->ticket.money.firstSeatNum));
}
else if (seat == "二等座")
{
flag = judgeTicket(tt, seat, temp->ticket.money.secondSeat, &(temp->ticket.money.secondSeatNum));
}
if (flag)
{
tt->user.name = user.name;
tt->user.trainNum = temp->ticket.trainNum;
tt->user.fromPlace = temp->ticket.fromPlace;
tt->user.fromStation = temp->ticket.fromStation;
tt->user.toPlace = temp->ticket.toPlace;
tt->user.toStation = temp->ticket.toStation;
tt->user.fromTime = temp->ticket.fromTime;
t->next = tt;
t = t->next;
}
if (flag) printf("恭喜您,选票成功!\n");
temp = temp->next;
t->next = NULL;
}

void ticketOS::userVoteResult(userList *userL, string name)
{
userList *temp = userL->next;
time_t rawtime;
struct tm *pt;
time(&rawtime);
pt = localtime(&rawtime);
while(temp)
{
userInform user= temp->user;
printf("---------------------------------------------\n");
cout << "| " << user.fromPlace << ' ' << user.fromStation << " " << user.trainNum << " " << user.toPlace << ' ' << user.toStation << " |\n";
printf("|%d年%02d月%02d日", pt->tm_year+1900, pt->tm_mon+1, pt->tm_mday);
cout << user.fromTime << "开" << " ";
cout.fill('0');
if (user.seaTpye == "站票") cout << "1车" << setw(2) << user.seat << "站票 |\n";
if (user.seaTpye == "硬座") cout << "1车" << setw(2) << user.seat << "号 硬座 |\n";
if (user.seaTpye == "一等座") cout << "1车" << setw(2) << user.seat << "号 一等座 |\n";
if (user.seaTpye == "硬卧") cout << "2车" << setw(2) << user.seat << "号 硬卧 |\n";
if (user.seaTpye == "二等座") cout << "2车" << setw(2) << user.seat << "号 二等座 |\n";
printf("|限乘当日当次车 |\n");
printf("| |\n");
cout << "| ****************** " << user.name << " |\n";
printf("---------------------------------------------\n");
cout.fill(' ');
temp = temp->next;
}
}

void ticketOS::userOperation(ticketList *L, userInform &user, ticketOS tos)
{
int num;
bool flag = true;
userList *userL = new (userList);
userL->next = NULL;
while(true)
{
printf("请输入您的选项:");
cin >> num;
if (num == 1)
{
userVoteByPlace(user);
if (!tos.userPrintTicketInform(L, user, 1)) continue;
tos.userChooseTicket(user, userL, L, 1);
}else if (num == 2)
{
userVoteByNum(user);
if (!tos.userPrintTicketInform(L, user, 2)) continue;
tos.userChooseTicket(user, userL, L, 2);
}else if (num == 3)
{
tos.printTicketInform(L);
}else if (num == 4)
{
tos.userVoteResult(userL,user.name);
}else if (num == 0)
{
printf("欢迎下次使用!\n");
break;
}else
{
printf("请输入有效操作!!!\n");
}
}
}

int main()
{
ticketOS tos;
tos.printInterface();
ticketList *L = tos.ticketInquire();
tos.printTicketInform(L);
tos.userViewInterface();
userInform user;
tos.userOperation(L,user,tos);
return 0;
}

我尽自己最大努力去写了,不知道这课设能不能过。

但还是有遗憾。没用搜索查票。

因为我们买票,很多时候不能一票到站,往往还要几次中转。

想在地点查询加搜索,但想了半天,写出来太难了。

我只能来说说我想要做的这个思路吧:

先开一个二维数组 a[n][m]存储找出可达的车票路径。

n:第i的可购买的车票。

m:出发地。

比如我们想从A到B地,假如可选择的票是A->B,A->C->D->B和A->E->B。

我的数组这样存 a[0][A] = B。

a[1][A] = C,a[1][C]= D,a[1][D] = B。

a[2][A] = E,a[2][E] = B。

数组里面存的是数字,但我给的是字符串,还要在初始化的时候map…

搜索的话,我觉得DFS就行,在加一个book[n]标记是否查询过。

但每次都链表查询,额,这效率…

而且输出也麻烦,先查a数组,在查链表…

谢谢您对我的支持
0%