1 #include<iostream> 2 #include <fstream> 3 #include <string> 4 #include <iomanip> 5 #include<vector> 6 #include<conio.h> 7 #define max 9999 8 const int num = 40; 9 using namespace std; 10 void jiemian(); 11 class point 12 { 13 public: 14 friend istream& operator>>(istream& in, point& s) 15 { 16 in >> s.code >> s.name >> s.comments; 17 return in; 18 } 19 friend class nuist; 20 protected: 21 int code; 22 string name; 23 string comments; 24 string into; 25 26 }; 27 class nuist :public point 28 { 29 public: 30 nuist(); 31 ~nuist(); 32 int numberofpoints(); 33 int numberofedges(); 34 point getvalue(int i); 35 int getLength(int v1, int v2); 36 int getati(int code); 37 void setcnc(); 38 void setedge(); 39 void show(int i); 40 void Dijkstra(int v); 41 void showmin(int v, int x); 42 int findminpath(int v, int x); 43 void showmany(); 44 void increase(); 45 virtual void search(); 46 virtual void choose(); 47 void Map(); 48 void insertcomment(); 49 void weihu(); 50 private: 51 int maxpoint; 52 int numedges; 53 int numpoints; 54 point* pointlist; 55 int edges[40][40]; 56 int* path; 57 int* dist; 58 }; 59 nuist::nuist() 60 { 61 maxpoint = 40; 62 numedges = 0; numpoints = 0; 63 pointlist = new point[maxpoint]; 64 for (int i = 0; i < 40; i++) 65 for (int j = 0; j < 40; j++) 66 { 67 if (i == j) 68 edges[i][j] = 0; 69 else 70 edges[i][j] = max; 71 } 72 setcnc(); 73 setedge(); 74 } 75 nuist::~nuist() 76 { 77 delete pointlist; 78 delete path; 79 delete dist; 80 } 81 int nuist::numberofpoints() 82 { 83 return numpoints; 84 } 85 int nuist::numberofedges() 86 { 87 return numedges; 88 } 89 point nuist::getvalue(int i) 90 { 91 return pointlist[i]; 92 } 93 int nuist::getLength(int v1, int v2) 94 { 95 return edges[v1][v2]; 96 }; 97 int nuist::getati(int code) 98 { 99 for (int i = 0; i < numpoints; i++) 100 if (pointlist[i].code == code) 101 { 102 return i; 103 } 104 return -1; 105 }; 106 107 void nuist::setcnc() 108 { 109 if (numpoints == maxpoint) 110 { 111 cout << "景点已达到最大值,无法在添加景点。" << endl; 112 } 113 point t; 114 ifstream in; 115 in.open("nuist.txt"); 116 if (!in.is_open()) 117 cout << "false to open nuist.txt"; 118 vector<point> x[40]; 119 while (in >> t) 120 { 121 x->push_back(t); 122 pointlist[numpoints].code = x->at(numpoints).code; 123 pointlist[numpoints].name = x->at(numpoints).name; 124 pointlist[numpoints].comments = x->at(numpoints).comments; 125 numpoints++; 126 } 127 } 128 void nuist::setedge() 129 { 130 edges[0][1] = edges[1][0] = 150; 131 edges[0][2] = edges[2][0] = 300; 132 edges[1][5] = edges[5][1] = 50; 133 edges[5][6] = edges[6][5] = 50; 134 edges[6][7] = edges[7][6] = 100; 135 edges[7][3] = edges[3][7] = 200; 136 edges[3][4] = edges[4][3] = 50; 137 edges[7][8] = edges[8][7] = 100; 138 edges[8][9] = edges[9][8] = 50; 139 edges[4][9] = edges[9][4] = 30; 140 edges[8][10] = edges[10][8] = 30; 141 edges[10][11] = edges[11][10] = 40; 142 edges[11][12] = edges[12][11] = 50; 143 edges[12][15] = edges[15][12] = 20; 144 edges[15][16] = edges[16][15] = 50; 145 edges[16][17] = edges[17][16] = 50; 146 edges[17][13] = edges[13][17] = 50; 147 edges[13][14] = edges[14][13] = 100; 148 edges[18][13] = edges[13][18] = 100; 149 edges[19][16] = edges[16][19] = 100; 150 edges[20][14] = edges[14][20] = 200; 151 edges[20][21] = edges[21][20] = 200; 152 edges[14][21] = edges[21][14] = 200; 153 edges[21][22] = edges[22][21] = 200; 154 edges[22][23] = edges[23][22] = 150; 155 edges[21][23] = edges[23][21] = 100; 156 edges[23][24] = edges[24][23] = 50; 157 edges[24][25] = edges[25][24] = 50; 158 edges[24][26] = edges[26][24] = 100; 159 edges[18][26] = edges[26][18] = 300; 160 edges[20][26] = edges[26][20] = 50; 161 edges[26][27] = edges[27][26] = 50; 162 edges[27][29] = edges[29][27] = 100; 163 edges[28][29] = edges[29][28] = 100; 164 edges[29][26] = edges[26][29] = 150; 165 edges[18][29] = edges[29][18] = 200; 166 } 167 void nuist::show(int i) 168 { 169 170 cout << "\t\t欢迎来到南信大校园导航" << endl; 171 cout << " 景点编号:" << pointlist[i].code << endl; 172 cout << " 景点名称:" << pointlist[i].name << endl; 173 cout << " 景点介绍:" << pointlist[i].comments << endl; 174 cout << "*******************************************" << endl; 175 } 176 void nuist::Dijkstra(int v) 177 { 178 int n = numberofpoints(); 179 dist = new int[n]; 180 path = new int[n]; 181 bool* S = new bool[n]; 182 int i, j, k, w, min; 183 for (i = 0; i < n; i++) 184 { 185 dist[i] = getLength(v, i); 186 S[i] = false; 187 if (i != v && dist[i] < max) 188 path[i] = v; 189 else 190 path[i] = -1; 191 } 192 S[v] = true; 193 dist[v] = 0; 194 for (i = 0; i < n - 1; i++) 195 { 196 min = max; 197 int u = v; 198 for (j = 0; j < n; j++) 199 if (S[j] == false && dist[j] < min) 200 { 201 u = j; 202 min = dist[j]; 203 } 204 S[u] = true; 205 for (k = 0; k < n; k++) 206 { 207 w = getLength(u, k); 208 if (S[k] == false && w < max && dist[u] + w < dist[k]) 209 { 210 dist[k] = dist[u] + w; 211 path[k] = u; 212 } 213 } 214 } 215 delete[] S; 216 } 217 void nuist::showmin(int v, int x) 218 { 219 int j, k, n; 220 n = numberofpoints(); 221 int* d = new int[n]; 222 { 223 j = x; 224 k = 0; 225 while (j != v) 226 { 227 d[k++] = j; 228 j = path[j]; 229 } 230 cout << getvalue(v).name << "到" << getvalue(x).name << "的最短路径为:" << endl << getvalue(v).name; 231 while (k > 0) 232 { 233 cout << "-->" << getvalue(d[--k]).name; 234 } 235 cout << endl << "最短路径长度为:" << dist[x] << endl; 236 237 } 238 delete[] d; 239 } 240 int nuist::findminpath(int v, int x) 241 { 242 int j, k, n; 243 n = numberofedges(); 244 int* d = new int[n]; 245 { 246 j = x; 247 k = 0; 248 while (j != v) 249 { 250 d[k++] = j; 251 j = path[j]; 252 } 253 while (k > 0) 254 { 255 cout << "-->" << getvalue(d[--k]).name; 256 } 257 } 258 259 delete[] d; 260 261 return dist[x]; 262 } 263 void nuist::showmany() 264 { 265 Map(); 266 int i, j, sum, x, a[num]; 267 int c[num]; 268 for (i = 0;; i++) 269 { 270 cout << "请输入你要参观的第" << i + 1 << "个景点(输入-1结束): "; 271 cin >> c[i]; 272 if (c[i] == -1) 273 break; 274 while (1) 275 { 276 a[i] = getati(c[i]); 277 if (a[i] == -1) 278 { 279 cout << "输入错误,请重新输入" << endl; 280 cout << "请输入你要参观的第" << i + 1 << "个景点(输入-1结束): "; 281 getchar(); 282 cin >> c[i]; 283 } 284 else 285 { 286 break; 287 } 288 } 289 } 290 cout << getvalue(a[0]).name; 291 for (j = 0, sum = 0; j < i - 1; j++) 292 { 293 Dijkstra(a[j]); 294 x = findminpath(a[j], a[j + 1]); 295 sum += x; 296 } 297 cout << endl << "最短路径长度为:" << sum << endl; 298 cout << "按任意键继续"; 299 system("pause"); 300 getchar(); 301 } 302 void nuist::Map() 303 { 304 system("cls"); 305 cout << endl << endl << endl << endl; 306 cout << "*******************************************************************************************" << endl; 307 cout << "***景点编号如下: ***" << endl; 308 for (int i = 1; i <= numpoints; i++) 309 { 310 cout << i-1 << std::left << setw(15) << pointlist[i - 1].name; 311 312 if (i % 4 == 0) 313 cout << endl; 314 else 315 cout << "\t"; 316 } 317 cout << endl; 318 cout << 319 endl << "*******************************************************************************************" << endl; 320 } 321 void nuist::choose() 322 { 323 system("cls"); 324 Map(); 325 int v1, v2; 326 int code1, code2; 327 cout << "编号如上图,请输入您要查询的两个景点的编号:" << endl; 328 cout << "起始景点:"; 329 cin >> code1; 330 cout << "终止景点:"; 331 cin >> code2; 332 while (1) 333 { 334 v1 = getati(code1); 335 v2 = getati(code2); 336 if (v1 == -1 || v2 == -1) 337 { 338 cout << "输入错误,请重新输入" << endl; 339 cout << "起始景点:"; 340 cin >> code1; 341 cout << "终止景点:"; 342 cin >> code2; 343 } 344 else 345 { 346 Dijkstra(v1); 347 showmin(v1, v2); 348 break; 349 } 350 } 351 cout << "按任意键继续"; 352 system("pause"); 353 jiemian(); 354 } 355 void nuist::increase() 356 { 357 Map(); 358 int code; 359 string name; 360 string comment; 361 cout << "请输入要增加的景点的编号:"; 362 cin >> code; numpoints += 1; 363 while (1) 364 { 365 int f = 0; 366 for (int i = 0; i < numpoints; i++) 367 { 368 if (code == getvalue(i).code) 369 { 370 cout << "已有该编号请重新输入" << endl; 371 f = 1; 372 break; 373 } 374 } 375 if (f == 1) 376 { 377 cout << "请输入要增加的景点的编号:"; 378 cin >> code; 379 } 380 else 381 { 382 break; 383 } 384 } 385 cout << "要添加景点的名称:"; 386 cin >> name; 387 pointlist[code].code = code; 388 pointlist[code].name = name; 389 cout << "请输入" << pointlist[code].name << "的介绍:"; 390 cin >> comment; 391 pointlist[code].comments = comment; 392 int code1, code2 = -1, length, z = 0; 393 cout << "请添加该地点的道路" << endl; 394 while (z != -1) 395 { 396 cout << "请输入" << pointlist[code].name << "到其他地方的编号:"; 397 cin >> code1; 398 if (code2 == code1) 399 { 400 cout << "这条路径已经添加过"; 401 continue; 402 } 403 if (getati(code1) == -1) 404 { 405 cout << "输入错误" << endl; 406 continue; 407 } 408 cout << "请输入" << pointlist[code].name << "到" << pointlist[code1].name << "的距离:"; 409 cin >> length; 410 edges[code][code1] = edges[code1][code] = length; 411 cout << "添加成功(不再添加请输入-1) "; 412 cin >> z; 413 code2 = code1; 414 } 415 416 } 417 void nuist::insertcomment() 418 { 419 int i; string s; 420 Map(); 421 cout << "请输入修改景点简介的编号:"; 422 cin >> i; 423 cout << "请输入" << pointlist[i].name << "的简介:"; 424 cin >> s; 425 pointlist[i].comments = s; 426 cout << "添加成功" << endl; 427 } 428 void nuist::search() 429 { 430 int i; 431 int code; 432 while (1) 433 { 434 435 Map(); 436 cout << "请输入要查询的景点编号(输入-1退出景点信息查询):"; 437 cin >> code; 438 if (code == -1) 439 { 440 jiemian(); 441 break; 442 } 443 i = code; 444 if (i == -1) 445 { 446 cout << "输入错误,请重新输入" << endl; 447 } 448 else 449 { 450 show(i); 451 system("pause"); 452 cout << "按任意键继续"; 453 } 454 } 455 } 456 void nuist::weihu() 457 { 458 Map(); 459 int ssi; 460 cout <<"请输入需要维护的景区:"; 461 cin >> ssi; 462 pointlist[ssi].name = "正在维护中"; 463 for (int i = 0; i < numpoints;i++) 464 { 465 edges[ssi][i] = max; 466 } 467 cout << "设置维护成功(此路不通)"; 468 cout << endl << "按任意建继续"; 469 system("pause"); 470 system("cls"); 471 Map(); 472 473 } 474 class visitor 475 { 476 protected: 477 string username; 478 string password; 479 public: 480 visitor() 481 { 482 this->username = username; 483 this->password = password; 484 } 485 visitor(string username, string password) 486 { 487 this->username = username; 488 this->password = password; 489 } 490 void creatuser(); 491 }; 492 void visitor::creatuser() 493 { 494 cout << "请输入用户名(账号):"; cin >> username; 495 int i = 0; 496 cout << "请输入密码: "; 497 while (true) 498 { 499 char ch = _getch(); 500 501 if (ch == 13) 502 { 503 password[i] = '\0'; 504 break; 505 } 506 else if (ch == 8) 507 { 508 if (i > 0) 509 { 510 cout << "\b \b"; 511 i--; 512 } 513 } 514 else 515 { 516 password[i] = ch; 517 cout << "*"; 518 i++; 519 } 520 } 521 } 522 class mangi :public visitor,public nuist 523 { 524 private: 525 string pinglun; 526 string ss; 527 public: 528 mangi(const string& username="nuistmangi", const string& password = "1258963074") 529 { 530 this->username = username; 531 this->password = password; 532 } 533 bool yesno() 534 { 535 if ( username=="nuistmangi" && password=="1258963074") 536 return 1; 537 else 538 return 0; 539 } 540 friend istream& operator>>(istream& in, mangi& s) 541 { 542 in>>s.ss >> s.pinglun; 543 return in; 544 } 545 void xiugaiinto(); 546 void sss(vector<mangi> x[50],int i) 547 { 548 for (; i >= 0; i--) 549 { 550 cout << x->at(i).ss << ":" << x->at(i).pinglun << endl; 551 } 552 } 553 }; 554 void mangi::xiugaiinto() 555 { 556 Map(); 557 mangi ts; 558 mangi tt; int zxc = -1; 559 ifstream in; 560 cout<<1; 561 in.open("ccpl.txt"); 562 if (!in.is_open()) 563 cout << "fail to open ccpl.txt."; 564 else 565 { 566 cout<<2; 567 vector<mangi> zx[15]; 568 cout<<3; 569 while (in >>tt ) 570 { 571 zx->push_back(tt); 572 zxc++; 573 } 574 cout<<4; 575 sss(zx,zxc); 576 in.close(); 577 } 578 579 } 580 581 void start() 582 { 583 printf("\n\n\n\n\n"); 584 printf("\t\t\t ******************************************************\n"); 585 printf("\t\t\t *----------------------------------------------------*\n"); 586 printf("\t\t\t *----------------------------------------------------*\n"); 587 printf("\t\t\t *---------------------Welcome!-----------------------*\n"); 588 printf("\t\t\t *----------------------------------------------------*\n"); 589 printf("\t\t\t *----------------南信大校园导航系统------------------*\n"); 590 printf("\t\t\t *----------------------------------------------------*\n"); 591 printf("\t\t\t *-------------------按任意键继续---------------------*\n"); 592 printf("\t\t\t *----------------------------------------------------*\n"); 593 printf("\t\t\t *----------------------------------------------------*\n"); 594 printf("\t\t\t ******************************************************\n"); 595 system("pause"); 596 } 597 598 void doad() 599 { 600 printf("\n\n\n\n\n"); 601 printf("\t\t\t ******************************************************\n"); 602 printf("\t\t\t *----------------------------------------------------*\n"); 603 printf("\t\t\t *----------------------------------------------------*\n"); 604 printf("\t\t\t *------------------1.游客注册------------------------*\n"); 605 printf("\t\t\t *----------------------------------------------------*\n"); 606 printf("\t\t\t *------------------2.管理员登录----------------------*\n"); 607 printf("\t\t\t *----------------------------------------------------*\n"); 608 printf("\t\t\t *------------------3.退出导航------------------------*\n"); 609 printf("\t\t\t *----------------------------------------------------*\n"); 610 printf("\t\t\t *----------------------------------------------------*\n"); 611 printf("\t\t\t ******************************************************\n"); 612 } 613 void jiemian() 614 { 615 system("cls"); 616 cout << "*****************************************************************************************************************" << endl; 617 cout << "*** 南信大校园导航 ***" << endl; 618 cout << "*****************************************************************************************************************" << endl; 619 cout << "*** ***" << endl; 620 cout << "*** 1.执行景点信息查询 ***" << endl; 621 cout << "*** 2.查询两个景点间的最佳路径 ***" << endl; 622 cout << "*** 3.查询多个景点间的最佳路径 ***" << endl; 623 cout << "*** 4.添加景点(管理员功能) ***" << endl; 624 cout << "*** 5.添加景点介绍(管理员功能) ***" << endl; 625 cout << "*** 6.设置维护景点(管理员功能) ***" << endl; 626 cout << "*** 7.执行退出系统 ***" << endl; 627 cout << "*** ***" << endl; 628 cout << "*****************************************************************************************************************" << endl; 629 630 } 631 int main() 632 { 633 visitor z; mangi m; 634 start(); 635 system("cls"); 636 int cose, sz = 1, tiao = 1; 637 string username; string password; 638 doad(); 639 while (tiao) 640 { 641 cout << "请输入您要执行功能的编号:(请输入数字)"; 642 cin >> cose; 643 if (cose < 0 || cose>3) 644 { 645 cout << "输入错误,请重新输入!" << endl; 646 } 647 switch (cose) 648 { 649 case 1: 650 z.creatuser(); 651 tiao = 0; 652 jiemian(); 653 break; 654 case 2: 655 m.creatuser(); 656 if (m.yesno()) 657 { 658 cout << "登录成功"; sz++; tiao = 0; 659 jiemian(); 660 break; 661 } 662 else 663 { 664 cout << "登录失败" << endl; 665 break; 666 } 667 case 3: 668 system("cls"); 669 return 0; 670 } 671 } 672 nuist g; int ssi = 1; 673 while (ssi) 674 { 675 jiemian(); 676 int c; 677 cout << "请输入您要执行功能的编号:(请输入数字)"; 678 cin >> c; 679 680 if (c < 0 || c>8) { cout << "输入错误,请重新输入!" << endl; } 681 switch (c) 682 { 683 case 1: 684 g.search(); 685 break; 686 case 2: 687 g.choose(); 688 break; 689 case 3: 690 g.showmany(); 691 break; 692 case 4: 693 if (sz >= 1) { g.increase(); break; } 694 else { cout << "您没有该权限" << endl; break; } 695 case 5: 696 if (sz >= 1) { g.insertcomment(); break; } 697 else { cout << "您没有该权限" << endl; break; } 698 case 6: 699 g.weihu(); 700 break; 701 case 7: 702 cout << "谢谢您的使用!" << endl; 703 ssi -= 1; 704 break; 705 case 8: 706 m.xiugaiinto(); 707 } 708 } 709 return 0; 710 }