-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.h
1895 lines (1875 loc) · 57.4 KB
/
User.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
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
#pragma once
#include <iostream>
#define NOMINMAX
#include <windows.h>
#include <conio.h>
#include "AML.h"
#include "Subway.h"
#include "voice_func.h"
#include "MyVector.h"
using namespace std;
class User;
User* currUser;
// 密码加密函数
string encrype(string str) {
//创建一个哈希器对象以对字符串的内容进行哈希处理
hash<string> hasher;
//计算字符串的哈希值
size_t hash = hasher(str);
//将哈希值转换为字符串表示形式
return to_string(hash);
}
void loading() {
for (int i = 0; i <= 10; i++) {
printf("加载中: [");
for (int j = 0; j < i; j++) {
printf("██");
}
for (int j = i; j < 10; j++) {
printf(" ");
}
printf("] %d%%", i * 10);
if (i < 5) {
Sleep(100);
}
else if (i < 8) {
Sleep(200);
}
else {
Sleep(300);
}
//光标回退
for (int i = 0; i < 50; i++) {
printf("\b");
}
}
printf("Done!");
Sleep(200);
return;
}
void printMenu() {
system("cls");
cout << "\t\t\t\t==========================" << endl;
cout << "\t\t\t\t[ 北京地铁线路小助手 ]" << endl;
cout << "\t\t\t\t==========================" << endl;
cout << "\t\t\t\t[ [ ]查看所有站点 ]" << endl;
cout << "\t\t\t\t[ [ ]查看地铁线路 ]" << endl;
cout << "\t\t\t\t[ [ ]路线规划 ]" << endl;
cout << "\t\t\t\t[ [ ]价格计算 ]" << endl;
cout << "\t\t\t\t[ [ ]站点查询 ]" << endl;
cout << "\t\t\t\t[ [ ]打开线路图 ]" << endl;
cout << "\t\t\t\t[ [ ]随机路线 ]" << endl;
cout << "\t\t\t\t[ [ ]地铁AI助手 ]" << endl;
cout << "\t\t\t\t[ [ ]修改账号密码 ]" << endl;
cout << "\t\t\t\t[ [ ]注销通行证 ]" << endl;
cout << "\t\t\t\t[ [ ]路线规划历史 ]" << endl;
cout << "\t\t\t\t[ [ ]退出登录 ]" << endl;
cout << "\t\t\t\t==========================" << endl;
cout << "\t\t\t\t[ Jerry Coding With Love ]" << endl;
cout << "\t\t\t\t[ 按\"V\"键可切换语音模式 ]" << endl;
cout << "\t\t\t\t==========================";
return;
}
void printUnloginedMenu() {
system("cls");
cout << "\t\t\t\t==========================" << endl;
cout << "\t\t\t\t[ 北京地铁线路小助手 ]" << endl;
cout << "\t\t\t\t==========================" << endl;
cout << "\t\t\t\t[ [ ]查看地铁线路 ]" << endl;
cout << "\t\t\t\t[ [ ]路线规划 ]" << endl;
cout << "\t\t\t\t[ [ ]站点查询 ]" << endl;
cout << "\t\t\t\t[ [ ]打开线路图 ]" << endl;
cout << "\t\t\t\t[ [ ]退出此页面 ]" << endl;
cout << "\t\t\t\t==========================" << endl;
cout << "\t\t\t\t[ 登录享受更多功能 ]" << endl;
cout << "\t\t\t\t==========================";
return;
}
void printManageMenu() {
system("cls");
cout << "\t\t\t\t==========================" << endl;
cout << "\t\t\t\t[ 北京地铁线路维护后台 ]" << endl;
cout << "\t\t\t\t==========================" << endl;
cout << "\t\t\t\t[ [ ]查看所有站点 ]" << endl;
cout << "\t\t\t\t[ [ ]查看地铁线路 ]" << endl;
cout << "\t\t\t\t[ [ ]添加站点 ]" << endl;
cout << "\t\t\t\t[ [ ]添加站点路线 ]" << endl;
cout << "\t\t\t\t[ [ ]添加地铁线路 ]" << endl;
cout << "\t\t\t\t[ [ ]切换站点状态 ]" << endl;
cout << "\t\t\t\t[ [ ]删除站点 ]" << endl;
cout << "\t\t\t\t[ [ ]删除站点路线 ]" << endl;
cout << "\t\t\t\t[ [ ]删除地铁线路 ]" << endl;
cout << "\t\t\t\t[ [ ]修改站点名称 ]" << endl;
cout << "\t\t\t\t[ [ ]修改线路名称 ]" << endl;
cout << "\t\t\t\t[ [ ]DFS遍历测试 ]" << endl;
cout << "\t\t\t\t[ [ ]查看通行证 ]" << endl;
cout << "\t\t\t\t[ [ ]修改权限组 ]" << endl;
cout << "\t\t\t\t[ [ ]通行证改密 ]" << endl;
cout << "\t\t\t\t[ [ ]删除通行证 ]" << endl;
cout << "\t\t\t\t[ [ ]退出维护后台 ]" << endl;
cout << "\t\t\t\t==========================" << endl;
cout << "\t\t\t\t[ Jerry Coding With Love ]" << endl;
cout << "\t\t\t\t==========================";
return;
}
// 按位置打印
void printSelection(short x, short y, bool isClear = false) {
CONSOLE_SCREEN_BUFFER_INFO cursorInfo;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hOut, &cursorInfo);
COORD bakPos = cursorInfo.dwCursorPosition;
COORD pos = { x,y };
SetConsoleCursorPosition(hOut, pos);
if (isClear) {
cout << " ";
}
else {
cout << "@";
}
SetConsoleCursorPosition(hOut, bakPos);
return;
}
// 保存map.txt文件
bool saveMap(AMLGraph<string, double>& graph, Subway& subway) {
ofstream file("map.txt");
if (!file.is_open()) {
cout << "map.txt文件读取失败" << endl;
return false;
}
string suffix = "[CLOSED]";
// 外循环,循环的是subway对象中的所有线路
for (int i = 0; i < subway.getLineNum(); i++) {
int stationNum = subway.getStationNum(subway.getLineName(i));
string* stations = subway.getStations(subway.getLineName(i));
// 内循环,将末尾存在[CLOSED]的站名消除掉[CLOSED]
for (int j = 0; j < stationNum; j++) {
if (stations[j].rfind(suffix) == (stations[j].size() - suffix.size())) {
// 删除 "[CLOSED]"
stations[j] = stations[j].substr(0, stations[j].size() - suffix.size());
}
}
// 正式输出线路信息
file << "#" << subway.getLineName(i) << "\t0.00" << endl;
// 内循环,输出非最后站点的站名和到下一站的权值
for (int j = 0; j < stationNum - 1; j++) {
file << stations[j] << "\t" << graph.getEdgeInfo(graph.getVexIndex(stations[j]), graph.getVexIndex(stations[j + 1])) << endl;
}
// 最后一站的权值是0
file << stations[stationNum - 1] << "\t0.00" << endl;
}
file.close();
return true;
}
class User {
private:
string username;
string password;
public:
//构造和析构
User(string username = "UNKNWON", string password = "UNKNWON") {
this->username = username;
this->password = password;
}
~User() {}
//功能实现
void signUp(MyVector<User*>& data);
int signIn(MyVector<User*>& data);
virtual string getType() = 0;//获取用户类型,返回不同类型
virtual int enterMenu(AMLGraph<string, double>& graph, double**& dist, int**& path, Subway& bjsubway, MyVector<User*>& data) = 0;//不同用户进入不同菜单
int checkInfo(string username, string password) {
if (this->getUsername() == username && this->getPassword() == password) {
return 1;
}
else {
return 0;
}
}
void deleteHistory() {
string line;
ifstream readFile("history.txt");
//如果history.txt文件不存在,则调用ofstream进行新建文件
if (!readFile.good()) {
ofstream createFile("history.txt");
if (!createFile.is_open()) {
cout << "\t\t\t\t无法创建history.txt文件!" << endl;
Sleep(2000);
return;
}
createFile.close();
readFile.open("history.txt");
}
MyVector<string> usernames, starts, ends;
while (getline(readFile, line)) {
string username, startStation, endStation;
istringstream iss(line);
iss >> username >> startStation >> endStation;
usernames.push_back(username);
starts.push_back(startStation);
ends.push_back(endStation);
}
readFile.close();
//覆盖文件
ofstream saveFile("history.txt");
if (!saveFile.is_open()) {
cout << "文件打开失败!" << endl;
Sleep(2000);
return;
}
string currUserName = this->getUsername();
for (int i = 0; i < usernames.size(); i++) {
if (usernames[i] != currUserName) {
saveFile << usernames[i] << ' ' << starts[i] << ' ' << ends[i] << endl;
}
}
saveFile.close();
return;
}
string getUsername() {
return this->username;
}
string getPassword() {
return this->password;
}
void setPassword(string newPassword) {
this->password = newPassword;
}
void setType(string usertype, MyVector<User*>& data);
virtual void printUser(MyVector<User*>& data) = 0;//只有Root能查看
virtual int deleteUser(MyVector<User*>& data) = 0;//非Root只能删自己
virtual int fixType(MyVector<User*>& data) = 0;//只有Root能修改用户类型
virtual void fixPassword(MyVector<User*>& data) = 0;//非Root只能改自己的密码
//运算符重载
bool operator==(User& obj) {
if (this->username == obj.username && this->password == obj.password) {
return true;
}
else {
return false;
}
}
friend ostream& operator<< (ostream& out, User& t) {
out << setw(7) << t.getType() << setw(8) << t.getUsername();
return out;
}
friend istream& operator>> (istream& in, User& t) {
cin >> t.username >> t.password;
return in;
}
};
// 保存用户
void saveUser(MyVector<User*>& users) {
//在功能实现中需要调用saveNode,所以必须要传头节点指针,否则用当前用户的节点就错了!
//新建文件输出流
ofstream saveFile("user.txt");
if (!saveFile.is_open()) {
cout << "文件打开失败!" << endl;
Sleep(2000);
return;
}
//因为调用saveNode的对象只有头节点,所以this指针就是头节点指针,赋给temp进行遍历
for (int i = 0; i < users.size(); i++) {
//把内容输出到文件
saveFile << users[i]->getType() << ' ' << users[i]->getUsername() << ' ' << users[i]->getPassword() << endl;
}
saveFile.close();
return;
}
//================================================================================//
class Root : public User {
public:
Root(string username, string password) : User(username, password) { }
string getType() {
return "Root";
}
void printUser(MyVector<User*>& data) {
system("cls");
cout << "\t\t\t\t========================================" << endl;
cout << "\t\t\t\t[ 管理员 查看通行证 ]" << endl;
cout << "\t\t\t\t========================================" << endl;
cout << "\t\t\t\t 权限组 用户名" << endl;
for (int i = 0; i < data.size(); i++) {
cout << "\t\t\t\t" << setw(7) << data[i]->getType() << setw(8) << data[i]->getUsername() << endl;
}
cout << "\t\t\t\t";
system("pause");
return;
}
int deleteUser(MyVector<User*>& data) {
system("cls");
cout << "\t\t\t\t========================================" << endl;
cout << "\t\t\t\t[ 通行证 删除用户 ]" << endl;
cout << "\t\t\t\t========================================" << endl;
string currUsername = currUser->getUsername();
cout << "\t\t\t\t当前的用户数据如下:" << endl;
cout << "\t\t\t\t 权限组 用户名" << endl;
for (int i = 0; i < data.size(); i++) {
cout << "\t\t\t\t" << setw(7) << data[i]->getType() << setw(8) << data[i]->getUsername() << endl;
}
string content;
while (1) {
int returnValue = 0;
cout << "\t\t\t\t请输入您要删除的用户:";
cin >> content;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
//判断要删除的用户是否为当前登录用户,以便执行自动退出登录功能
if (currUser->getUsername() == content) {
returnValue = 1;
}
else {
returnValue = 0;
}
for (int i = 0; i < data.size(); i++) {
if (data[i]->getUsername() == content) {
if (returnValue == 0 && data[i]->getType() == "Root") {
cout << "\t\t\t\t您无法删除除了自己的其他管理员,删除失败。" << endl;
Sleep(1500);
return 0;
}
if (returnValue == 1) {
cout << "\t\t\t\t您删除的是当前管理用户,即将退出登录。" << endl;
}
if (data[i]->getType() == "Common") {
data[i]->deleteHistory();
}
//调用函数模板,删除节点
data.erase(i);
cout << "\t\t\t\t用户删除成功!" << endl;
saveUser(data);
Sleep(3000);
return returnValue;
}
}
cout << "\t\t\t\t找不到用户,请重新输入用户名!" << endl;
Sleep(2000);
continue;
}
return 0;
}
//管理员修改权限组
int fixType(MyVector<User*>& data) {
system("cls");
cout << "\t\t\t\t========================================" << endl;
cout << "\t\t\t\t[ 管理员 修改权限组 ]" << endl;
cout << "\t\t\t\t========================================" << endl;
cout << "\t\t\t\t当前的用户数据如下:" << endl;
cout << "\t\t\t\t 权限组 用户名" << endl;
for (int i = 0; i < data.size(); i++) {
cout << "\t\t\t\t" << setw(7) << data[i]->getType() << setw(8) << data[i]->getUsername() << endl;
}
string content;
while (1) {
cout << "\t\t\t\t请输入您要修改的用户:";
cin >> content;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
//定义returnValue,最终实现判断修改完成后是否需要自动退出账号!
int returnValue = 0;
//如果修改的是当前用户,那么最终自动退出登录
if (currUser->getUsername() == content) {
returnValue = 1;
}
for (int i = 0; i < data.size(); i++) {
if (data[i]->getUsername() == content) {
if (returnValue == 0 && data[i]->getType() == "Root") {
cout << "\t\t\t\t您无法修改除了自己的其他管理员的权限组,修改失败。" << endl;
Sleep(1500);
return 0;
}
int input;
cout << "\t\t\t\t[ [1]普通用户 [2]管理员 ]" << endl;
while (1) {
cout << "\t\t\t\t请选择要修改为的权限组:";
if (!(cin >> input) || input < 0 || input > 2) {
cout << "\t\t\t\t数据输入错误,请重新输入!" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Sleep(2000);
continue;
}
if (input == 2) {
data[i]->setType("Root", data);
}
else {
data[i]->setType("Common", data);
}
cout << "\t\t\t\t修改完成!";
saveUser(data);
if (returnValue == 1) {
cout << "您修改的是当前用户的权限组,即将自动退出登录。" << endl;
}
else {
cout << endl;
}
Sleep(3000);
return returnValue;
}
}
}
cout << "\t\t\t\t找不到用户,请重新输入用户名!" << endl;
Sleep(2000);
continue;
}
}
void fixPassword(MyVector<User*>& data) {
system("cls");
cout << "\t\t\t\t========================================" << endl;
cout << "\t\t\t\t[ 通行证 修改密码 ]" << endl;
cout << "\t\t\t\t========================================" << endl;
cout << "\t\t\t\t当前的用户数据如下:" << endl;
cout << "\t\t\t\t 权限组 用户名" << endl;
for (int i = 0; i < data.size(); i++) {
cout << "\t\t\t\t" << setw(7) << data[i]->getType() << setw(8) << data[i]->getUsername() << endl;
}
string content;
while (1) {
cout << "\t\t\t\t请输入您要修改的用户:";
cin >> content;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int i = 0; i < data.size(); i++) {
if (data[i]->getUsername() == content) {
if (data[i]->getType() == "Root" && currUser->getUsername() != content) {
cout << "\t\t\t\t您无法修改除了自己的其他管理员的密码,修改失败。" << endl;
Sleep(1500);
return;
}
string ipassword;
while (1) {
cout << "\t\t\t\t找到数据,请输入用户 " << content << " 的新的密码:";
//密码输入
int inputCount = 0;
char pwdChar;
while (1) {
pwdChar = _getch();
if (pwdChar == 13) {
cout << endl;
break;
}
if (pwdChar == 8 && inputCount > 0) {
cout << "\b \b";
ipassword.pop_back();
inputCount--;
}
else if (pwdChar != 8 && inputCount < 20) {
//密显功能
cout << "*";
ipassword.push_back(pwdChar);
inputCount++;
}
}
if (ipassword.length() == 0) {
cout << "\t\t\t\t密码不合法,请重新输入!" << endl;
ipassword = "";
Sleep(2000);
continue;
}
break;
}
ipassword = encrype(ipassword);
//重设密码
data[i]->setPassword(ipassword);
cout << "\t\t\t\t修改成功!" << endl;
saveUser(data);
Sleep(2000);
return;
}
}
cout << "\t\t\t\t找不到用户,请重新输入用户名!" << endl;
continue;
}
}
int enterMenu(AMLGraph<string, double>& graph, double**& dist, int**& path, Subway& bjsubway, MyVector<User*>& data) {
short defaultX = 38;
short defaultY = 3;
int currChoice = 0;
char inputChar;
char charinput;
while (1) {
printManageMenu();
printSelection(defaultX, defaultY);
while (1) {
inputChar = _getch();
if (inputChar == 13 or inputChar == '5') {
// 循环条件:输入的字符不等于回车
break;
}
if ((inputChar == 'w' or inputChar == '8') and currChoice > 0) {
// 箭头向上移动
printSelection(defaultX, defaultY, true);
printSelection(defaultX, --defaultY);
currChoice--;
}
else if ((inputChar == 's' or inputChar == '2') and currChoice < 16) {
// 箭头向下移动
printSelection(defaultX, defaultY, true);
printSelection(defaultX, ++defaultY);
currChoice++;
}
}
cout << endl;
// 在switch-case中输入的变量都在这里定义
string stationName, lineName, begin, end, command, oldLineName, oldStationName;
string* stations = nullptr;
stringstream stationInfo, lineInfo;
int stationIndex = -1, beginIndex = -1, endIndex = -1, addType = 0, stationNum = 0, oldStationIndex = -1, flag = 0;
double s, s2;
switch (currChoice) {
case 0:
cout << "所有地铁站如下:" << endl;
graph.printGraph();
system("pause");
break;
case 1:
cout << "当前最新版地图包含以下线路:" << endl;
bjsubway.printLineNames();
while (1) {
cout << "请按照以上提示输入您要查询的地铁线路:";
if (!(cin >> lineName)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
break;
}
if (bjsubway.printLineInfo(lineName)) {
while (1) {
cout << "是否查看该线路的更多信息(y/n):";
if (!(cin >> charinput) or (charinput != 'y' and charinput != 'n')) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue;
}
break;
}
if (charinput == 'y') {
if (lineName == "17号线北段") {
lineName = "17号线";
lineInfo << "start " << "https://baike.baidu.com/item/北京地铁" << lineName;
}
else if (lineName == "亦庄T1线") {
lineName = "亦庄新城现代有轨电车T1线";
lineInfo << "start " << "https://baike.baidu.com/item/" << lineName;
}
else {
lineInfo << "start " << "https://baike.baidu.com/item/北京地铁" << lineName;
}
command = lineInfo.str();
if (!system(command.c_str())) {
cout << "已成功打开北京地铁" << lineName << "的在线信息。" << endl;
}
else {
cout << "无法打开网页。" << endl;
}
Sleep(2000);
}
}
else {
cout << "您输入的线路不存在!" << endl;
}
system("pause");
break;
case 2:
while (1) {
cout << "请输入您要添加的站点名称:";
if (!(cin >> stationName)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
break;
}
graph.freeDistAndPath(dist, path);
graph.addVex(stationName);
graph.calculateFloyd(dist, path);
cout << "添加完成" << endl;
system("pause");
break;
case 3:
cout << "添加站点路线规则:输入三个站点,三个站点相邻,第一个和第三个站点存在,第二个站点为新站点,从而让新站点插入到旧的站点之中。\n您可以选择是否输入第一个站点和第三个站点,进而达到前插和后插的效果。" << endl;
cout << "当前最新版地图包含以下线路:" << endl;
bjsubway.printLineNames();
while (1) {
cout << "请输入您要操作的线路:";
if (!(cin >> lineName)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
if (bjsubway.lineIndex(lineName) == -1) {
cout << "您输入的线路不存在,请重新输入!" << endl;
continue;
}
break;
}
bjsubway.printLineInfo(lineName);
while (1) {
while (1) {
cout << "请输入新的地铁站名:";
if (!(cin >> stationName)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
stationIndex = graph.getVexIndex(stationName);
if (stationIndex == -1) {
cout << "不存在您输入的地铁站,请重新输入!" << endl;
continue;
}
if (!graph.getVexEnabled(stationIndex)) {
cout << "您输入的地铁站为关闭状态,无法对此进行操作,请重新输入!" << endl;
continue;
}
break;
}
while (1) {
cout << "请输入前一个地铁站名(输入n为忽略此选项):";
if (!(cin >> begin)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
if (begin != "n") {
beginIndex = graph.getVexIndex(begin);
if (beginIndex == -1) {
cout << "不存在您输入的地铁站,请重新输入!" << endl;
continue;
}
if (!graph.getVexEnabled(stationIndex)) {
cout << "您输入的地铁站为关闭状态,无法对此进行操作,请重新输入!" << endl;
continue;
}
}
break;
}
while (1) {
cout << "请输入后一个地铁站名(输入n为忽略此选项):";
if (!(cin >> end)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
if (end != "n") {
endIndex = graph.getVexIndex(end);
if (endIndex == -1) {
cout << "不存在您输入的地铁站,请重新输入!" << endl;
continue;
}
if (!graph.getVexEnabled(stationIndex)) {
cout << "您输入的地铁站为关闭状态,无法对此进行操作,请重新输入!" << endl;
continue;
}
}
break;
}
if (begin == "n" and end == "n") {
cout << "您不能同时忽略输入前后两个站点!请重新输入站点。" << endl;
system("pause");
continue;
}
else if (begin == "n" and end != "n") {
// 前插
addType = 1;
}
else if (begin != "n" and end == "n") {
// 后插
addType = 2;
}
else {
if (abs(bjsubway.stationIndex(begin, lineName) - bjsubway.stationIndex(end, lineName)) != 1) {
cout << "您输入的站点不相邻,请重新输入!" << endl;
system("pause");
continue;
}
// 默认中间插
}
if (addType == 0) {
while (1) {
cout << "请输入新站点与前一站点间的距离:";
if (!(cin >> s)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
break;
}
while (1) {
cout << "请输入新站点与后一站点间的距离:";
if (!(cin >> s2)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
break;
}
}
else {
while (1) {
cout << "请输入已输入的两站点间的距离:";
if (!(cin >> s)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
break;
}
}
break;
}
graph.freeDistAndPath(dist, path);
// 中间插入
if (addType == 0) {
bjsubway.insertStation(begin, stationName, lineName);
graph.deleteEdge(beginIndex, endIndex);
graph.addEdge(beginIndex, stationIndex, s);
graph.addEdge(endIndex, stationIndex, s2);
}
// 前插
else if (addType == 1) {
bjsubway.insertStation(lineName, stationName, lineName);
graph.addEdge(endIndex, stationIndex, s);
}
// 后插
else {
bjsubway.addStation(stationName, lineName);
graph.addEdge(beginIndex, stationIndex, s);
}
graph.calculateFloyd(dist, path);
cout << "站点路线添加成功!" << endl;
system("pause");
break;
case 4:
cout << "当前最新版地图包含以下线路:" << endl;
bjsubway.printLineNames();
while (1) {
cout << "请输入您要新增的线路:";
if (!(cin >> lineName)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
if (bjsubway.lineIndex(lineName) != -1) {
cout << "您输入的线路已存在,请重新输入!" << endl;
continue;
}
break;
}
bjsubway.addLine(lineName);
cout << "地铁线路添加完成,请从已有站点中为线路添加一个初始站点" << endl;
while (1) {
cout << "请输入新的地铁站名:";
if (!(cin >> stationName)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
stationIndex = graph.getVexIndex(stationName);
if (stationIndex == -1) {
cout << "不存在您输入的地铁站,请重新输入!" << endl;
continue;
}
break;
}
bjsubway.addStation(stationName, lineName);
cout << "初始站点添加完成" << endl;
system("pause");
break;
case 5:
while (1) {
cout << "请输入要切换状态地铁站名:";
if (!(cin >> stationName)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
stationIndex = graph.getVexIndex(stationName);
if (stationIndex == -1) {
cout << "不存在您输入的地铁站,请重新输入!" << endl;
continue;
}
break;
}
graph.freeDistAndPath(dist, path);
if (graph.changeVexStatus(stationIndex) == 0) {
cout << "当前站点状态为停用,已切换为启用" << endl;
bjsubway.modifyStationNameAllLine(stationName + "[CLOSED]", stationName);
graph.updateVexData(stationIndex, stationName);
}
else {
cout << "当前站点状态为启用,已切换为停用" << endl;
bjsubway.modifyStationNameAllLine(stationName, stationName + "[CLOSED]");
graph.updateVexData(stationIndex, stationName + "[CLOSED]");
}
graph.calculateFloyd(dist, path);
system("pause");
break;
case 6:
while (1) {
cout << "请输入要删除的地铁站名:";
if (!(cin >> stationName)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
stationIndex = graph.getVexIndex(stationName);
if (stationIndex == -1) {
cout << "不存在您输入的地铁站,请重新输入!" << endl;
continue;
}
break;
}
graph.freeDistAndPath(dist, path);
if (graph.getVexEnabled(stationIndex)) {
bjsubway.deleteStationAllLine(stationName);
}
else {
bjsubway.deleteStationAllLine(stationName + "[CLOSED]");
}
graph.deleteVex(stationIndex);
graph.calculateFloyd(dist, path);
cout << "已删除" << stationName << "站" << endl;
system("pause");
break;
case 7:
cout << "删除站点路线规则:输入三个站点,三个站点相邻,第一个和第三个站点存在,第二个站点为要删除的站点。\n您可以选择是否输入第一个站点和第三个站点,进而达到前删和后删的效果。" << endl;
cout << "当前最新版地图包含以下线路:" << endl;
bjsubway.printLineNames();
while (1) {
cout << "请输入您要操作的线路:";
if (!(cin >> lineName)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
if (bjsubway.lineIndex(lineName) == -1) {
cout << "您输入的线路不存在,请重新输入!" << endl;
continue;
}
break;
}
bjsubway.printLineInfo(lineName);
while (1) {
while (1) {
cout << "请输入要删除的地铁站名:";
if (!(cin >> stationName)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
stationIndex = graph.getVexIndex(stationName);
if (stationIndex == -1) {
cout << "不存在您输入的地铁站,请重新输入!" << endl;
continue;
}
if (!graph.getVexEnabled(stationIndex)) {
cout << "您输入的地铁站为关闭状态,无法对此进行操作,请重新输入!" << endl;
continue;
}
break;
}
while (1) {
cout << "请输入前一个地铁站名(输入n为忽略此选项):";
if (!(cin >> begin)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
if (begin != "n") {
beginIndex = graph.getVexIndex(begin);
if (beginIndex == -1) {
cout << "不存在您输入的地铁站,请重新输入!" << endl;
continue;
}
if (!graph.getVexEnabled(stationIndex)) {
cout << "您输入的地铁站为关闭状态,无法对此进行操作,请重新输入!" << endl;
continue;
}
}
break;
}
while (1) {
cout << "请输入后一个地铁站名(输入n为忽略此选项):";
if (!(cin >> end)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
if (end != "n") {
endIndex = graph.getVexIndex(end);
if (endIndex == -1) {
cout << "不存在您输入的地铁站,请重新输入!" << endl;
continue;
}
if (!graph.getVexEnabled(stationIndex)) {
cout << "您输入的地铁站为关闭状态,无法对此进行操作,请重新输入!" << endl;
continue;
}
}
break;
}
if (begin == "n" and end == "n") {
cout << "您不能同时忽略输入前后两个站点!请重新输入站点。" << endl;
system("pause");
continue;
}
else if (begin == "n" and end != "n") {
// 前删
addType = 1;
}
else if (begin != "n" and end == "n") {
// 后删
addType = 2;
}
else {
if (abs(bjsubway.stationIndex(begin, lineName) - bjsubway.stationIndex(end, lineName)) != 2) {
cout << "您输入的站点不相邻,请重新输入!" << endl;
system("pause");
continue;
}
while (1) {
cout << "请输入删除之后前后两站点间的距离:";
if (!(cin >> s)) {
cout << "数据输入错误,请重新输入!" << endl;
//清空输入缓存区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
continue;
}
break;