-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicketDemo.cpp
More file actions
1138 lines (850 loc) · 32.8 KB
/
TicketDemo.cpp
File metadata and controls
1138 lines (850 loc) · 32.8 KB
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>//头文件
#include <stdlib.h>
#include <string.h>
#include <graphics.h>
#include <math.h>
#define ArraySize 50//注意数组编号从0开始(0~stationnumber-1)
typedef struct Line//储存各线路信息
{
char station[ArraySize][ArraySize];//储存各站名称,(0~stationnumber-1)
int stationnumber;//储存该线路总站数
int interchange[ArraySize][3];//储存该线路的换乘站,0列存站编号,1列存换乘另一线路编号,2列存该换乘站在另一线路的编号,同站多换乘会储存在不同行
int internumber;//储存换乘站数,多换乘站会重复
}LINE;
void Welcome()//欢迎界面
{
int height = 0, width = 0;//字符串的绘制长宽
setbkcolor(LIGHTBLUE);//设置背景色
setlinecolor(LIGHTBLUE);
setfillcolor(LIGHTBLUE);
cleardevice();
LPCSTR string;//字符串
string = "---Welcome to the subway automatic ticket system---";//欢迎信息
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 200 - height / 2, string);
string = "@Copyright Matrix Inc -- All rights reserved";//版权信息
height = textheight(string);
width = textwidth(string);
outtextxy(640 - width, 480 - height, string);
string = "Loading . . .";//模拟加载
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 300 - height / 2, string);
Sleep(3000);//延时
printf("---Welcome to the subway automatic ticket system---\n\n");
printf("@Copyright Matrix Inc.\nAll rights reserved\n\n");
}
int Menu()//主菜单选项
{
int selection = 0;
int height = 0, width = 0;//字符串的绘制长宽
LPCSTR string;//字符串
cleardevice();//清屏
string = "---Main menu---";//欢迎信息
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 160 - height / 2, string);
string = "@Copyright Matrix Inc -- All rights reserved";//版权信息
height = textheight(string);
width = textwidth(string);
outtextxy(640 - width, 480 - height, string);
string = "1-Buy a ticket";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 250 - height / 2, string);
string = "2-View the map";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 280 - height / 2, string);
string = "3-Quit";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 310 - height / 2, string);
printf("\n\n--Main menu--");
printf("\n1-Buy a ticket\n2-View the map\n3-Quit");
while (1)
{
printf("\n\nPlease select an option:");
scanf_s("%d", &selection);
getchar();
if (selection >= 1 && selection <= 3)
{
break;
}
else
{
printf("\n\nInvalid Number! Please try again!\a\n\n");
string = "Invalid Number! Please try again!";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 450 - height / 2, string);
Sleep(2000);
fillrectangle(320 - width / 2, 450 - height / 2, 320 + width / 2, 450 + height / 2);
}
}
return selection;
}
int Map(LINE StationLine[ArraySize],int linenumber)//地图查询函数
{
int number = 0, charac = 0, line = 0, selection = 0, temp = 0;
int height = 0, width = 0;
LPCSTR string;
cleardevice();
IMAGE pic = NULL;//读取地图
loadimage(&pic, "D:\\VSproject\\TicketDemo\\Map.jpg", 320, 240);
putimage(160, 80, &pic);
string = "@Copyright Matrix Inc -- All rights reserved";//版权信息
height = textheight(string);
width = textwidth(string);
outtextxy(640 - width, 480 - height, string);
string = "---Map---";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 50 - height / 2, string);
printf("\n\n--Map--\n");
for (line = 0;line <= linenumber-1;line++)//输出所有线路的站点
{
printf("\n\n--Line %d Stations:", line + 1);
for (number = 0;number <= StationLine[line].stationnumber - 1;number++)
{
printf("\n\n%d.", number + 1);
for (charac = 0;StationLine[line].station[number][charac] != '\0';charac++)
{
printf("%c", StationLine[line].station[number][charac]);
}
for (temp = 0;temp <= StationLine[line].internumber - 1;temp++)//检测该站是否为换乘站,若是则显示换乘线路
{
if (StationLine[line].interchange[temp][0] == number)
{
printf(" #Interchange with Line %d", StationLine[line].interchange[temp][1] + 1);
}
}
}
}
printf("\n\n-----\n0-Main menu\n1-Buy a ticket\n2-Quit\n3-Search for a station");
{
string = "0-Main menu";
outtextxy(160, 360, string);
string = "1-Buy a ticket";
outtextxy(320, 360, string);
string = "2-Quit";
outtextxy(160, 420, string);
string = "3-Search for a station";
outtextxy(320, 420, string);
}
while (1)
{
printf("\n\nPlease select an option:");
scanf_s("%d", &selection);
getchar();
if (selection >= 0 && selection <= 4)
{
if (selection == 2 || selection == 3)//选项编号一致化
{
selection++;
}
break;
}
else
{
printf("\n\nInvalid Number! Please try again!\a\n\n");
string = "Invalid Number! Please try again!";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 450 - height / 2, string);
Sleep(2000);
fillrectangle(320 - width / 2, 450 - height / 2, 320 + width / 2, 450 + height / 2);
}
}
return selection;
}
int Search(LINE StationLine[ArraySize],int linenumber)//站点查找函数
{
int find = 0, number = 0, line = 0, temp = 0, selection = 0;
int lines[ArraySize] = {0};//搜索站名所在线路(换乘站会多于一个)
char search[ArraySize];//储存输入待搜索的站名
int height = 0, width = 0;
LPCSTR string;
char strtemp[ArraySize];
cleardevice();
string = "@Copyright Matrix Inc -- All rights reserved";//版权信息
height = textheight(string);
width = textwidth(string);
outtextxy(640 - width, 480 - height, string);
string = "---Search for a station---";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 50 - height / 2, string);
string = "Input:";
outtextxy(220, 240, string);
printf("\n\n--Station Search--");
printf("\n\nPlease input a station by name:");
fgets(search, ArraySize, stdin);//输入搜索的站名
fflush(stdin);
for (temp = 0;temp <= ArraySize;temp++)
{
if (search[temp] == '\n')
{
search[temp] = '\0';//去除回车符
break;
}
}
string = search;
outtextxy(280, 240, string);
for (line = 0;line <= linenumber - 1;line++)//搜寻站点所在线路
{
for (number = 0;number <= StationLine[line].stationnumber - 1;number++)
{
if (strcmp(search, StationLine[line].station[number]) == 0)
{
lines[find] = line;
find++;
}
}
}
if (find == 0)//搜索无结果
{
string = "Station Not found!";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 280 - height / 2, string);
printf("\nStation Not found!\a\n");
}
if (find == 1)//非换乘站
{
sprintf_s(strtemp, "Found this station in Line %d", lines[find - 1] + 1);
string = strtemp;
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 280 - height / 2, string);
printf("\nFound this station in Line %d\n", lines[find - 1] + 1);
}
if (find > 1)//换乘站
{
string = "This station is the interchange of Line ";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 280 - height / 2, string);
printf("\nThis station is the interchange of Line ");
for (number = 0;number <= find - 1;number++)
{
printf("%d", lines[number] + 1);
sprintf_s(strtemp, "%d", lines[number] + 1);
string = strtemp;
outtextxy(442 + number * 16, 280 - height / 2, string);
if (number < find - 1)
{
sprintf_s(strtemp, ", ");
string = strtemp;
outtextxy(442 + number * 16 + 16, 280 - height / 2, string);
printf(", ");
}
else
{
putchar('\n');
}
}
}
string = "1-Retry";
outtextxy(160, 360, string);
string = "0-Main menu";
outtextxy(320, 360, string);
printf("\n0-Main menu\n1-Retry");
while (1)
{
printf("\nPlease select an option:");//提示重新输入或回到主菜单
scanf_s("%d", &selection);
getchar();
if (selection == 0)
{
return 0;
}
else if (selection == 1)
{
return 4;
}
else
{
printf("\n\nInvalid Number! Please try again!\a\n\n");
string = "Invalid Number! Please try again!";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 450 - height / 2, string);
Sleep(2000);
fillrectangle(320 - width / 2, 450 - height / 2, 320 + width / 2, 450 + height / 2);
}
}
return 0;//跳回主菜单
}
void ComplexRoute(LINE StationLine[ArraySize], int Terln[ArraySize][3], int Avoid[ArraySize], int selectline, int prinum, int pass, int flagsolution, int *solutionA, int *solutionB)
//线路相关信息 终到站所在线路 递归已经过的线路 本次递归线路 初始站号 已经过线路数 目前经过总站数 方案指针
{
int inter = 0, temp = 0, tempsolution = 0, endline = 0;
//printf("\n\n Line %d", selectline+1);
Avoid[pass] = selectline;//新输入现在处于的线路
pass++;//已经过线路数加一,注意此时pass-1才指向当前线路
//printf("\n\nPass=%d", pass);//调试用:输出换乘
for (endline = 0;endline <= Terln[0][2] - 1;endline++)//递归终点
{
if (selectline == Terln[endline][0])//一但当前线路位于目标线路之内
{
tempsolution = flagsolution + abs(prinum - Terln[endline][1]);
//printf("\n\n\t\t\t\tPrinum:%d,Last PASS:%d,Since PASS:%d,FINAL:%d!",prinum, prinum - Terln[endline][1],flagsolution ,tempsolution);
if (*solutionA == 0)//方案A赋值
{
*solutionA = tempsolution;
}
if (*solutionB == 0)//方案B赋值
{
*solutionB = tempsolution;
}
if (*solutionA != 0 && *solutionB != 0)//清空较差方案,为后面的方案空出位置
{
if (*solutionA >= *solutionB)
{
*solutionA = 0;
}
else
{
*solutionB = 0;
}
}
return;
}
}
for (inter = 0; inter <= StationLine[selectline].internumber - 1; inter ++)//在现有线路选定一个换乘站
{
for (temp = 0;temp <= pass - 2;temp++)//避开曾经过的线路,-2是因为pass-1指向当前线路
{
if (StationLine[selectline].interchange[inter][1] == Avoid[temp])
{
inter ++;
if (inter > StationLine[selectline].internumber - 1)
{
return;
}
else
{
temp = -1;
}
}
}
tempsolution = flagsolution + abs(prinum - StationLine[selectline].interchange[inter][0]);//计算该段线路过站数(截至目前)
//printf("\n\n\t\tChosen Inter:%d,Since PASS:%d",StationLine[selectline].interchange[inter],tempsolution);
ComplexRoute(StationLine, Terln, Avoid, StationLine[selectline].interchange[inter][1], StationLine[selectline].interchange[inter][2], pass, tempsolution, solutionA, solutionB);
}
return;
}
int Ticket(int totalpass)
{
int ferror = 0;
int temp = 0;//临时计数变量
int singleprice = 0;//最终价格(一张票)
int price = 0;//总价格
int ticketnum = 0;//票数
int inserted = 0;//本次投入金额
int paid = 0;//已投入总金额
int stairprice[ArraySize][3];//0列存价格,1列存此级价格的最远站数,[0][2]存阶梯级数
int height = 0, width = 0;
LPCSTR string;
char strtemp[ArraySize];
cleardevice();
string = "@Copyright Matrix Inc -- All rights reserved";//版权信息
height = textheight(string);
width = textwidth(string);
outtextxy(640 - width, 480 - height, string);
string = "---Payment---";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 50 - height / 2, string);
FILE* fp = NULL;//文件编写格式:阶梯级数,(间两行),价格1,(间一行),价格最远站数1,(间两行)...
ferror = fopen_s(&fp, "D:\\VSproject\\TicketDemo\\Price.txt", "r");//打开文件
if (ferror != NULL)//读取文件失败,退回主菜单
{
printf("File opening failed!\a");
return 0;
}
fscanf_s(fp, "%d", &stairprice[0][2]);//读取阶梯级数
for (temp = 0;temp <= stairprice[0][2] - 1;temp++)//读取各阶梯价格及最远站数
{
fgetc(fp);
fgetc(fp);
fgetc(fp);
fscanf_s(fp, "%d", &stairprice[temp][0]);
fgetc(fp);
fgetc(fp);
fscanf_s(fp, "%d", &stairprice[temp][1]);
}
ferror = fclose(fp);//关闭文件
if (ferror != NULL)//文件关闭失败,退回主菜单
{
printf("File closing failed!\a");
return 0;
}
for (temp = 0;temp <= stairprice[0][2] - 1;temp++)//检测所选路线的价格区间,确定最终价格
{
if (totalpass <= stairprice[temp][1])
{
singleprice = stairprice[temp][0];
}
if (temp == stairprice[0][2] - 1)//防止未考虑最高票价的情况(所选线路超过了定义的最大票价站数),按定义最高区间收取
{
singleprice = stairprice[temp][0];
}
}
string = "Please input number of tickets to buy:";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 120 - height / 2, string);
while (1)
{
printf("\n\nPlease input number of tickets to buy:");//购票数
scanf_s("%d", &ticketnum);
getchar();
sprintf_s(strtemp, "%d", ticketnum);
string = strtemp;
height = textheight(string);
width = textwidth(string);
outtextxy(450 - width / 2, 120 - height / 2, string);
if (ticketnum >= 1 && ticketnum <= 1000)//不可买超过一千张
{
price = ticketnum * singleprice;//总价格
break;
}
else
{
fillrectangle(450 - width / 2, 120 - height / 2, 450 + width / 2, 120 + height / 2);//覆盖输入数字
string = "Invalid Number! Please try again!";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 450 - height / 2, string);
Sleep(2000);
fillrectangle(320 - width / 2, 450 - height / 2, 320 + width / 2, 450 + height / 2);
printf("\n\nInvalid Number! Please try again!\a\n\n");
}
}
sprintf_s(strtemp, "The total price is $%d", price);
string = strtemp;
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 150 - height / 2, string);
printf("\n\n---The total price is $%d", price);
string = "Please insert your cash ($1,$2,$10):";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 240 - height / 2, string);
while (1)
{
inserted = 0;
printf("\n\nPlease insert your cash($1,$2,$10):");//模拟投币
scanf_s("%d", &inserted);
getchar();
sprintf_s(strtemp, "%d", inserted);
string = strtemp;
height = textheight(string);
width = textwidth(string);
outtextxy(448 - width / 2, 240 - height / 2, string);
if (inserted == 1 || inserted == 2 || inserted == 10)
{
paid = paid + inserted;
if (paid >= price)
{
sprintf_s(strtemp, "Here is your change $%d", paid - price);
string = strtemp;
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 400 - height / 2, string);
string = "Have a nice trip!";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 420 - height / 2, string);
printf("\n\nHere is your change $%d", paid - price);
printf("\n\nHave a nice trip!");
Sleep(5000);
break;
}
else
{
Sleep(500);
fillrectangle(448 - width / 2, 240 - height / 2, 448 + width / 2, 240 + height / 2);//清除输入数字
printf("\n\n$%d to be paid!\nPlease continue to insert.\a", price - paid);
sprintf_s(strtemp, "$%d to be paid! Please continue to insert.", price - paid);
string = strtemp;
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 450 - height / 2, string);
Sleep(2000);
fillrectangle(320 - width / 2, 450 - height / 2, 320 + width / 2, 450 + height / 2);
}
}
else
{
printf("\n\nInvalid Number! Please try again!\a\n\n");
string = "Invalid Number! Please try again!";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 450 - height / 2, string);
Sleep(2000);
fillrectangle(320 - width / 2, 450 - height / 2, 320 + width / 2, 450 + height / 2);
}
}
return 0;
}
int Path(LINE StationLine[ArraySize], int linenumber)
{
int temp1 = 0, temp2 = 0, temp3 = 0, selection = 0;//临时计数变量
int situation = 0;//三种不同的情况
int line = 0;//当前循环线路
int number = 0;//当前循环站数
int solutionA = 0, solutionB = 0, finalsolution = 0;//两种解决方案(经过总站数)及最优解
int tempsolution = 0;//临时方案储存
char Departure[ArraySize];//起始站名
char Terminal[ArraySize];//终到站名
int Depln[ArraySize][3];//0列为可能起始线路,1列为该站编号(换乘站会有多个),可能线路数存到[0][2]
int Terln[ArraySize][3];//0列为可能终到线路,1列为该站编号(换乘站会有多个),可能线路数存到[0][2]
int Avoid[ArraySize];//循迹规避线路号
int pass = 0;//pass+1表示已经可以回避的线路数
int height = 0, width = 0;
LPCSTR string;
char strtemp[ArraySize];
cleardevice();
string = "@Copyright Matrix Inc -- All rights reserved";//版权信息
height = textheight(string);
width = textwidth(string);
outtextxy(640 - width, 480 - height, string);
string = "---Ticket---";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 50 - height / 2, string);
string = "Departure Input:";
outtextxy(220, 200, string);
string = "Terminal Input:";
outtextxy(220, 240, string);
printf("\n\n--Ticket--");
printf("\n\nPlease input a departure station by name:");
fgets(Departure, ArraySize, stdin);//输入起始站名
for (temp1 = 0;temp1 <= ArraySize;temp1++)//去除回车符
{
if (Departure[temp1] == '\n')
{
Departure[temp1] = '\0';
break;
}
}
string = Departure;
outtextxy(340, 200, string);
printf("\n\nPlease input a terminal station by name:");
fgets(Terminal, ArraySize, stdin);//输入终到站名
fflush(stdin);
for (temp1 = 0;temp1 <= ArraySize;temp1++)//去除回车符
{
if (Terminal[temp1] == '\n')
{
Terminal[temp1] = '\0';
break;
}
}
string = Terminal;
outtextxy(338, 240, string);
temp1 = 0, temp2 = 0;
for (line = 0;line <= linenumber - 1;line++)//检测输入车站所在线路及站号
{
for (number = 0;number <= StationLine[line].stationnumber - 1;number++)
{
if (strcmp(Departure, StationLine[line].station[number]) == 0)
{
Depln[temp1][0] = line;
Depln[temp1][1] = number;
temp1++;
}
if (strcmp(Terminal, StationLine[line].station[number]) == 0)
{
Terln[temp2][0] = line;
Terln[temp2][1] = number;
temp2++;
}
}
}
Depln[0][2] = temp1;//写入起始终到站的可能线路数
Terln[0][2] = temp2;
if (Depln[0][2] == 0 || Terln[0][2] == 0 || strcmp(Departure, Terminal) == 0)//三种出错情况
{
if (Depln[0][2] == 0)//起始站未找到
{
string = "Departure station was not found!";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 280 - height / 2, string);
printf("\n\nDeparture station was not found!");
}
if (Terln[0][2] == 0)//终点站未找到
{
string = "Terminal station was not found!";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 300 - height / 2, string);
printf("\n\nTerminal station was not found!");
}
if (strcmp(Departure, Terminal) == 0)//输入两站为同一站
{
string = "This is your terminal station. Turn around and find an exit!";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 320 - height / 2, string);
printf("\n\nThis is your terminal station. Turn around and find an exit!");
}
string = "1-Retry";
outtextxy(160, 360, string);
string = "0-Main menu";
outtextxy(320, 360, string);
printf("\n\n0-Main menu\n1-Retry");
while (1)
{
printf("\nPlease select an option:");//提示重新输入或回到主菜单
scanf_s("%d", &selection);
getchar();
if (selection == 0)
{
return 0;//表示退回主菜单
}
else if (selection == 1)
{
return 1;//表示重试(退出重新加载函数)
}
else
{
printf("\n\nInvalid Number! Please try again!\a\n\n");
string = "Invalid Number! Please try again!";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 450 - height / 2, string);
Sleep(2000);
fillrectangle(320 - width / 2, 450 - height / 2, 320 + width / 2, 450 + height / 2);
}
}
}
if (situation == 0)
{
for (temp1 = 0;temp1 <= Depln[0][2] - 1;temp1++)//第一种情况,起始终点站在同一线路,一层循环检验起始线路,二层循环检验终到线路
{
for (temp2 = 0;temp2 <= Terln[0][2] - 1;temp2++)
{
if (Depln[temp1][0] == Terln[temp2][0])
{
situation = 1;//第一种情况成立
tempsolution = abs(Terln[temp2][1] - Depln[temp1][1]);//方案
if (solutionA == 0)//方案A赋值
{
solutionA = tempsolution;
}
if (solutionB == 0)//方案B赋值
{
solutionB = tempsolution;
}
if (solutionA != 0 && solutionB != 0)//清空较差方案,为后面的方案空出位置
{
if (solutionA >= solutionB)
{
solutionA = 0;
}
else
{
solutionB = 0;
}
}
}
}
}
}
if (situation == 0)
{
for (temp1 = 0;temp1 <= Depln[0][2] - 1;temp1++)//temp1循环选定起始线路
{
for (pass = 0;pass <= Depln[0][2] - 2;pass++)//pass+1表示已经可以回避的线路数,当前选定的线路留到子函数再输入,-2是因为从0开始且跳过当前线路
{
temp2 = 0;//temp2指向可能起始线路在原数组的序号
if (Depln[temp2][0] != Depln[temp1][0])//暂时跳开当前线路录入
{
Avoid[pass] = Depln[temp2][0];//首先回避可能起初线路
}
temp2 ++;
}
ComplexRoute(StationLine, Terln, Avoid, Depln[temp1][0], Depln[temp1][1], pass, tempsolution, &solutionA, &solutionB);
}
}
if (solutionA != 0 || solutionB != 0)//最后输出决定最优解
{
if (solutionA >= solutionB)
{
printf("\n\nThere are %d stations on your way", solutionA);
finalsolution = solutionA;
}
else
{
printf("\n\nThere are %d stations on your way", solutionB);
finalsolution = solutionB;
}
}
sprintf_s(strtemp, "There are %d stations on your way", finalsolution);
string = strtemp;
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 280 - height / 2, string);
if (situation == 0 || 1)//准备购票
{
string = "Buy a ticket now?";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 300 - height / 2, string);
string = "0-Yes";
outtextxy(160, 360, string);
string = "1-No";
outtextxy(320, 360, string);
printf("\n\nBuy a ticket now?");
printf("\n\n0-Yes\n1-No");
while (1)
{
printf("\nPlease select an option:");//提示立即购票或回到主菜单
scanf_s("%d", &selection);
getchar();
if (selection == 0)
{
Ticket(finalsolution);
break;
}
else if (selection == 1)
{
return 0;//表示退回主菜单
}
else
{
printf("\n\nInvalid Number! Please try again!\a\n\n");
string = "Invalid Number! Please try again!";
height = textheight(string);
width = textwidth(string);
outtextxy(320 - width / 2, 450 - height / 2, string);
Sleep(2000);
fillrectangle(320 - width / 2, 450 - height / 2, 320 + width / 2, 450 + height / 2);
}
}
}
return 0;
}
void Quit()
{
int height = 0, width = 0;//字符串的绘制长宽
LPCSTR string;//字符串
cleardevice();//清屏
string = "---Thank you for using!---";//感谢使用
height = textheight(string);