├── .gitattributes ├── 01_2152085_孙亦菲 ├── 01_2152085_孙亦菲.cpp ├── 01_2152085_孙亦菲.doc ├── 01_2152085_孙亦菲.exe ├── 01_2152085_孙亦菲_Linux └── makefile ├── 02_2152085_孙亦菲 ├── 02_2152085_孙亦菲.cpp ├── 02_2152085_孙亦菲.doc ├── 02_2152085_孙亦菲.exe ├── 02_2152085_孙亦菲_Linux └── makefile ├── 03_2152085_孙亦菲 ├── 03_2152085_孙亦菲.cpp ├── 03_2152085_孙亦菲.doc ├── 03_2152085_孙亦菲.exe ├── 03_2152085_孙亦菲_Linux └── makefile ├── 04_2152085_孙亦菲 ├── 04_2152085_孙亦菲.cpp ├── 04_2152085_孙亦菲.doc ├── 04_2152085_孙亦菲.exe ├── 04_2152085_孙亦菲_Linux └── makefile ├── 05_2152085_孙亦菲 ├── 05_2152085_孙亦菲.cpp ├── 05_2152085_孙亦菲.doc ├── 05_2152085_孙亦菲.exe ├── 05_2152085_孙亦菲_Linux └── makefile ├── 06_2152085_孙亦菲 ├── 06_2152085_孙亦菲.cpp ├── 06_2152085_孙亦菲.doc ├── 06_2152085_孙亦菲.exe ├── 06_2152085_孙亦菲_Linux └── makefile ├── 07_2152085_孙亦菲 ├── 07_2152085_孙亦菲.cpp ├── 07_2152085_孙亦菲.doc ├── 07_2152085_孙亦菲.exe ├── 07_2152085_孙亦菲_Linux └── makefile ├── 08_2152085_孙亦菲 ├── 08_2152085_孙亦菲.cpp ├── 08_2152085_孙亦菲.doc ├── 08_2152085_孙亦菲.exe ├── 08_2152085_孙亦菲_Linux └── makefile ├── 09_2152085_孙亦菲 ├── 09_2152085_孙亦菲.cpp ├── 09_2152085_孙亦菲.doc ├── 09_2152085_孙亦菲.exe ├── 09_2152085_孙亦菲_Linux └── makefile ├── 10_2152085_孙亦菲 ├── 10_2152085_孙亦菲.cpp ├── 10_2152085_孙亦菲.doc ├── 10_2152085_孙亦菲.exe ├── 10_2152085_孙亦菲_Linux └── makefile ├── 2152085孙亦菲.docx └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /01_2152085_孙亦菲/01_2152085_孙亦菲.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | #include 5 | 6 | #define DefaultSize 32 7 | 8 | using namespace std; 9 | 10 | //自定义实现String类 11 | class String 12 | { 13 | friend istream& operator>>(istream&, String& Str); 14 | friend ostream& operator <<(ostream&, const String& Str); 15 | private: 16 | int currentLength; 17 | int maxLength; 18 | char* ch; 19 | public: 20 | String(int size = DefaultSize); 21 | 22 | String(const char* init); 23 | 24 | String(const String& ob); 25 | 26 | ~String() 27 | { 28 | delete[]ch; 29 | } 30 | 31 | int length() 32 | { 33 | return currentLength; 34 | } 35 | 36 | String operator()(int pos, int length); 37 | 38 | bool operator==(const String& ob) 39 | { 40 | return strcmp(ch, ob.ch) == 0; 41 | } 42 | 43 | bool operator!=(const String& ob) 44 | { 45 | return strcmp(ch, ob.ch) != 0; 46 | } 47 | 48 | String& operator=(const String& ob); 49 | 50 | String& operator=(const char* ch); 51 | 52 | String& operator+=(const String& ob); 53 | 54 | char operator[](int num); 55 | 56 | int fastFind(String& pat, int pos); 57 | 58 | }; 59 | String::String(int size) 60 | { 61 | maxLength = size; 62 | ch = new char[size + 1]; 63 | if (ch == NULL) 64 | { 65 | cerr << "Allocation Error!\n"; 66 | } 67 | currentLength = 0; ch[0] = '\0'; 68 | } 69 | String::String(const char* init) 70 | { 71 | int length = strlen(init); 72 | maxLength = length > DefaultSize ? length : DefaultSize; 73 | ch = new char[maxLength + 1]; 74 | if (ch == NULL) 75 | { 76 | cerr << "Allocation Error!\n"; 77 | } 78 | currentLength = length; 79 | strcpy(ch, init); 80 | } 81 | String::String(const String& ob) 82 | { 83 | int length = ob.currentLength; 84 | maxLength = length > DefaultSize ? length : DefaultSize; 85 | ch = new char[maxLength + 1]; 86 | if (ch == NULL) 87 | { 88 | cerr << "Allocation Error!\n"; 89 | } 90 | currentLength = length; 91 | strcpy(ch, ob.ch); 92 | } 93 | 94 | String String::operator()(int pos, int length) 95 | { 96 | String s; 97 | if (pos >= 0 && pos < currentLength && length>0 && pos + length - 1 < currentLength) 98 | { 99 | for (int i = 0; i < length; i++) 100 | { 101 | s.ch[i] = ch[pos + i]; 102 | } 103 | s.ch[length] = '\0'; 104 | } 105 | return s; 106 | } 107 | 108 | String& String:: operator=(const String& ob) 109 | { 110 | if (this != &ob) 111 | { 112 | delete[]ch; 113 | ch = new char[ob.maxLength + 1]; 114 | if (ch == NULL) 115 | { 116 | cerr << "Allocation Error!\n"; 117 | } 118 | maxLength = ob.maxLength; 119 | currentLength = ob.currentLength; 120 | strcpy(ch, ob.ch); 121 | } 122 | return *this; 123 | } 124 | 125 | String& String::operator=(const char* init) 126 | { 127 | int length = strlen(init); 128 | maxLength = length > DefaultSize ? length : DefaultSize; 129 | ch = new char[maxLength + 1]; 130 | if (ch == NULL) 131 | { 132 | cerr << "Allocation Error!\n"; 133 | } 134 | currentLength = length; 135 | strcpy(ch, init); 136 | return *this; 137 | } 138 | String& String::operator+=(const String& ob) 139 | { 140 | char* temp = ch; 141 | int length = currentLength + ob.currentLength; 142 | maxLength = maxLength > length ? maxLength : length; 143 | ch = new char[maxLength + 1]; 144 | if (ch == NULL) 145 | { 146 | cerr << "Allocation Error!\n"; 147 | exit(1); 148 | } 149 | strcpy(ch, temp); 150 | strcat(ch, ob.ch); 151 | currentLength = length; 152 | delete[]temp; 153 | return *this; 154 | } 155 | char String::operator[](int num) 156 | { 157 | if (num < 0 || num >= currentLength) 158 | { 159 | cerr << "out of range!\n"; 160 | exit(1); 161 | } 162 | return ch[num]; 163 | } 164 | int String::fastFind(String& pat, int pos) 165 | { 166 | int pLength = pat.length(); 167 | int* next = new int[pLength]; 168 | next[0] = -1; 169 | int j = 0, k = -1; 170 | while (j < pLength - 1) 171 | { 172 | if (k == -1 || ch[j] == ch[k]) 173 | { 174 | j++; k++; 175 | next[j] = k; 176 | } 177 | else 178 | { 179 | k = next[k]; 180 | } 181 | } 182 | int tPos = 0; 183 | int pPos = 0; 184 | while (tPos < currentLength && pPos < pLength) 185 | { 186 | if (pPos == -1 || ch[tPos] == ch[pPos]) 187 | { 188 | tPos++; 189 | pPos++; 190 | } 191 | else 192 | { 193 | pPos = next[pPos]; 194 | } 195 | } 196 | delete[]next; 197 | if (pPos < pLength) 198 | { 199 | return -1; 200 | } 201 | else 202 | { 203 | return tPos - pLength; 204 | } 205 | } 206 | istream& operator>>(istream& cin, String& Str) 207 | { 208 | cin >> Str.ch; 209 | 210 | return cin; 211 | } 212 | 213 | ostream& operator<<(ostream& cout, const String& Str) 214 | { 215 | cout << Str.ch; 216 | 217 | return cout; 218 | } 219 | 220 | 221 | //学生类 222 | class Student 223 | { 224 | friend istream& operator>>(istream&, Student&); 225 | friend ostream& operator<<(ostream&, const Student&); 226 | private: 227 | String m_ID; 228 | String m_Name; 229 | String m_Sex; 230 | int m_Age; 231 | String m_Category; 232 | 233 | public: 234 | Student(String id = "", String name = "", String sex = "", int age = 0, String category = ""); 235 | 236 | Student& operator=(const Student& Stu); 237 | 238 | bool operator==(const Student& Stu) 239 | { 240 | return m_ID == Stu.m_ID; 241 | } 242 | 243 | String getID() 244 | { 245 | return m_ID; 246 | } 247 | 248 | }; 249 | 250 | Student::Student(String id, String name, String sex, int age, String category) : 251 | m_ID(id), m_Name(name), m_Sex(sex), m_Age(age), m_Category(category) {} 252 | 253 | Student& Student::operator=(const Student& Stu) 254 | { 255 | m_ID = Stu.m_ID; 256 | m_Name = Stu.m_Name; 257 | m_Sex = Stu.m_Sex; 258 | m_Age = Stu.m_Age; 259 | m_Category = Stu.m_Category; 260 | return *this; 261 | } 262 | 263 | istream& operator>>(istream& cin, Student& Stu) 264 | { 265 | cin >> Stu.m_ID >> Stu.m_Name >> Stu.m_Sex >> Stu.m_Age >> Stu.m_Category; 266 | 267 | return cin; 268 | } 269 | 270 | ostream& operator<<(ostream& cout, const Student& Stu) 271 | { 272 | cout << Stu.m_ID << '\t' << Stu.m_Name << '\t' << Stu.m_Sex << '\t' << Stu.m_Age << '\t' << 273 | Stu.m_Category; 274 | 275 | return cout; 276 | } 277 | 278 | //节点类 279 | template 280 | class LinkNode 281 | { 282 | private: 283 | T data; 284 | 285 | public: 286 | LinkNode* link; 287 | 288 | LinkNode(LinkNode* ptr = NULL)//已知Link初始化LinkNode 289 | { 290 | link = ptr; 291 | } 292 | 293 | LinkNode(const T& item, LinkNode* ptr = NULL)//已知data Link 初始化LinkNode 294 | { 295 | data = item; 296 | link = ptr; 297 | } 298 | 299 | T getData()//得到LinkNode的Data 300 | { 301 | return data; 302 | } 303 | 304 | void setData(const T& item) 305 | { 306 | data = item; 307 | } 308 | 309 | }; 310 | 311 | template 312 | class List 313 | { 314 | private: 315 | LinkNode* first; 316 | public: 317 | List(); 318 | 319 | List(List& L); 320 | 321 | ~List(); 322 | 323 | void makeEmpty();// 324 | 325 | LinkNode* getHead()const 326 | { 327 | return first; 328 | } 329 | 330 | LinkNode* getTail()const; 331 | 332 | int length()const;//get valid length 333 | 334 | void setHead(LinkNode* p);//set firsr LinkNode 335 | 336 | LinkNode* search(T& x);// 337 | 338 | LinkNode* locate(int& num); 339 | 340 | bool getData(int num, T& x);//x得到第num个数据,成功返回true,失败返回false 341 | 342 | bool setData(int num, const T& x);//x设置第num个数据,成功返回true,失败返回false 343 | 344 | bool insert(int num, T& x);//第num个数据后插入x,成功返回true,失败返回false 345 | 346 | bool remove(int num, T& x);//删除第num个数据,x得到该数据 347 | 348 | bool remove(T& x);//删除为x的数据 349 | 350 | bool isEmpty()const//Link是否为空 351 | { 352 | return first->link == NULL ? true : false; 353 | } 354 | 355 | bool isFull()const//Link是否为满 356 | { 357 | return false; 358 | } 359 | 360 | void output();//输出整个Link数据 361 | 362 | List& operator=(List& L); 363 | 364 | void inputFront(T endTag);//前插输入Link,endTag为结束符号 365 | 366 | void inputFront(const int& num);//前插输入Link,有效节点num个 367 | 368 | void inputRear(T endTag);//后插输入Link,endTag为结束符号 369 | 370 | void inputRear(const int& num);//后插输入Link,有效节点num个 371 | }; 372 | 373 | template 374 | List::List() 375 | { 376 | first = new LinkNode; 377 | } 378 | 379 | template 380 | List::List(List& L) 381 | { 382 | LinkNode* srcptr = L.getHead(); 383 | LinkNode* desptr = first = new LinkNode; 384 | while (srcptr->link != NULL) 385 | { 386 | desptr->link = new LinkNode(srcptr->link->data); 387 | desptr = desptr->link; 388 | srcptr = srcptr->link; 389 | } 390 | } 391 | 392 | template 393 | List::~List() 394 | { 395 | makeEmpty(); delete first; 396 | } 397 | 398 | template 399 | void List::makeEmpty() 400 | { 401 | LinkNode* current; 402 | while (first->link != NULL) 403 | { 404 | current = first->link; 405 | first->link = current->link; 406 | delete current; 407 | } 408 | 409 | } 410 | 411 | template 412 | LinkNode* List::getTail()const 413 | { 414 | LinkNode* last = first; 415 | LinkNode* current = first; 416 | while (current != NULL) 417 | { 418 | last = current; 419 | current = current->link; 420 | } 421 | return last; 422 | } 423 | 424 | template 425 | int List::length()const 426 | { 427 | int i = 0; 428 | LinkNode* current = first; 429 | while (current->link != NULL) 430 | { 431 | i++; 432 | current = current->link; 433 | } 434 | return i; 435 | } 436 | 437 | template 438 | void List::setHead(LinkNode* p) 439 | { 440 | if (first->link != NULL) 441 | { 442 | p->link = first->link; 443 | } 444 | first = p; 445 | } 446 | 447 | template 448 | LinkNode* List::search(T& x) 449 | { 450 | LinkNode* current = first->link; 451 | while (current != NULL) 452 | { 453 | if (current->getData() == x) 454 | { 455 | break; 456 | } 457 | current = current->link; 458 | } 459 | return current; 460 | } 461 | 462 | template 463 | LinkNode* List::locate(int& num) 464 | { 465 | int i = 0; 466 | LinkNode* current = first; 467 | while (current != NULL && i < num) 468 | { 469 | i++; 470 | current = current->link; 471 | } 472 | return current; 473 | } 474 | 475 | template 476 | bool List::getData(int num, T& x) 477 | { 478 | if (x <= 0) 479 | { 480 | return false; 481 | } 482 | int i = 0; 483 | LinkNode* current = first; 484 | while (current != NULL && i < x) 485 | { 486 | i++; 487 | current = current->link; 488 | } 489 | if (current == NULL) 490 | { 491 | return false;; 492 | } 493 | x = current->getData(); 494 | } 495 | 496 | template 497 | bool List::setData(int num, const T& x) 498 | { 499 | if (num <= 0) 500 | { 501 | return false; 502 | } 503 | int i = 0; 504 | LinkNode* current = first; 505 | while (current != NULL && i < num) 506 | { 507 | i++; 508 | current = current->link; 509 | } 510 | if (current == NULL) 511 | { 512 | return false; 513 | } 514 | current->data = x; 515 | return true; 516 | } 517 | 518 | template 519 | bool List::insert(int num, T& x) 520 | { 521 | if (num <= 0) 522 | { 523 | // 524 | return false; 525 | } 526 | int i = 1; 527 | LinkNode* last = first; 528 | while (last != NULL && i < num) 529 | { 530 | i++; 531 | last = last->link; 532 | } 533 | if (last == NULL) 534 | { 535 | // 536 | return false; 537 | } 538 | LinkNode* next = last->link; 539 | LinkNode* newNode = new LinkNode(x, next); 540 | last->link = newNode; 541 | return true; 542 | } 543 | 544 | template 545 | bool List::remove(int num, T& x) 546 | { 547 | if (num < 0) 548 | { 549 | // 550 | return false; 551 | } 552 | LinkNode* last = first; 553 | LinkNode* current = first; 554 | int i = 0; 555 | while (current != NULL && i < num) 556 | { 557 | i++; 558 | last = current; 559 | current = current->link; 560 | } 561 | if (current == NULL) 562 | { 563 | // 564 | return false; 565 | } 566 | x = current->data; 567 | last->link = current->link; 568 | delete current; 569 | return true; 570 | } 571 | 572 | template 573 | bool List::remove(T& x) 574 | { 575 | bool flag = false; 576 | LinkNode* last = first; 577 | LinkNode* current = first; 578 | while (last->link != NULL) 579 | { 580 | current = last->link; 581 | if (current->getData() == x) 582 | { 583 | last->link = current->link; 584 | flag = true; 585 | } 586 | last = last->link; 587 | } 588 | return flag; 589 | } 590 | 591 | template 592 | void List::output() 593 | { 594 | LinkNode* current = first->link; 595 | while (current != NULL) 596 | { 597 | cout << current->getData() << "\n"; 598 | current = current->link; 599 | } 600 | } 601 | 602 | template 603 | List& List::operator=(List& L) 604 | { 605 | makeEmpty(); 606 | LinkNode* srcptr = L.getHead()->link; 607 | LinkNode* desptr = first = new LinkNode; 608 | while (srcptr != NULL) 609 | { 610 | LinkNode* newNode = new LinkNode(srcptr.data); 611 | srcptr = srcptr->link; 612 | desptr->link = newNode; 613 | desptr = desptr->link; 614 | } 615 | return *this; 616 | } 617 | 618 | template 619 | void List::inputFront(T endTag) 620 | { 621 | makeEmpty(); 622 | LinkNode* newNode; 623 | T val; 624 | cin >> val; 625 | while (!(val == endTag)) 626 | { 627 | newNode = new LinkNode(val); 628 | if (newNode == NULL) 629 | { 630 | // 631 | exit(1); 632 | } 633 | newNode->link = first->link; 634 | first->link = newNode; 635 | cin >> val; 636 | } 637 | } 638 | 639 | template 640 | void List::inputFront(const int& num) 641 | { 642 | makeEmpty(); 643 | LinkNode* newNode; 644 | 645 | for (int i = 1; i <= num; i++) 646 | { 647 | T val; 648 | cin >> val; 649 | newNode = new LinkNode(val); 650 | if (newNode == NULL) 651 | { 652 | // 653 | exit(1); 654 | } 655 | newNode->link = first->link; 656 | first->link = newNode; 657 | } 658 | } 659 | 660 | template 661 | void List::inputRear(T endTag) 662 | { 663 | makeEmpty(); 664 | LinkNode* newNode; 665 | LinkNode* last = first; 666 | T val; 667 | cin >> val; 668 | while (!(val == endTag)) 669 | { 670 | newNode = new LinkNode(val); 671 | if (newNode == NULL) 672 | { 673 | // 674 | exit(1); 675 | } 676 | last->link = newNode; 677 | last = newNode; 678 | cin >> val; 679 | } 680 | } 681 | 682 | template 683 | void List::inputRear(const int& num) 684 | { 685 | makeEmpty(); 686 | LinkNode* newNode; 687 | LinkNode* last = first; 688 | 689 | for (int i = 1; i <= num; i++) 690 | { 691 | T val; 692 | cin >> val; 693 | newNode = new LinkNode(val); 694 | if (newNode == NULL) 695 | { 696 | // 697 | exit(1); 698 | } 699 | last->link = newNode; 700 | last = newNode; 701 | } 702 | 703 | } 704 | 705 | class TestRegistrationSystem 706 | { 707 | private: 708 | List StudentList; 709 | public: 710 | TestRegistrationSystem(); 711 | 712 | LinkNode* findById(const String& id);//按考号查找考生 713 | 714 | bool appendStudent(); 715 | 716 | bool popStudent(); 717 | 718 | bool findStudent(); 719 | 720 | bool changeStudent(); 721 | 722 | void displayStudents(); 723 | 724 | }; 725 | 726 | TestRegistrationSystem::TestRegistrationSystem() 727 | { 728 | cout << "首先请建立考生信息系统\n"; 729 | cout << "请输入考生人数:"; 730 | int num; 731 | cin >> num; 732 | while (cin.fail() || num < 1) 733 | { 734 | cout << "输入人数有误,请重新输入!\n"; 735 | cin.clear(); 736 | cin.ignore(numeric_limits::max(), '\n'); 737 | cout << "请输入考生人数:"; 738 | cin >> num; 739 | } 740 | cout << "请依次输入考生的考号,姓名,性别,年龄及报考类别!\n"; 741 | StudentList.inputRear(num); 742 | displayStudents(); 743 | } 744 | 745 | LinkNode* TestRegistrationSystem::findById(const String& id) 746 | { 747 | Student stu = Student(id); 748 | return StudentList.search(stu); 749 | } 750 | 751 | bool TestRegistrationSystem::appendStudent() 752 | { 753 | cout << "请输入你要插入的考生位置:"; 754 | int position = 0; 755 | cin >> position; 756 | while (cin.fail() || position<1 || position>StudentList.length() + 1) 757 | { 758 | cout << "输入非法(插入位置应为正整数,且不超过当前人数+1)" << endl; 759 | cout << "请重新输入插入位置:"; 760 | cin.clear(); 761 | cin.ignore(numeric_limits::max(), '\n'); 762 | cin >> position; 763 | } 764 | Student stu; 765 | cout << "请依次输入考生的考号,姓名,性别,年龄及报考类别!" << endl; 766 | 767 | cin >> stu; 768 | 769 | while (findById(stu.getID()) != NULL) 770 | { 771 | cout << "插入失败,该考号已被使用,请重新输入" << endl; 772 | cout << "请依次输入考生的考号,姓名,性别,年龄及报考类别!" << endl; 773 | cin.clear(); 774 | cin.ignore(numeric_limits::max(), '\n'); 775 | cin >> stu; 776 | } 777 | if (StudentList.insert(position, stu)) 778 | { 779 | cout << "插入成功!" << endl; 780 | displayStudents(); 781 | return true; 782 | } 783 | return false; 784 | } 785 | 786 | bool TestRegistrationSystem::popStudent() 787 | { 788 | String id = ""; 789 | cout << "请输入要删除的考生的考号:" << endl; 790 | cin >> id; 791 | while (findById(id) == NULL) 792 | { 793 | cout << "该考生号对应的考生不存在,请重新输入考生号" << endl; 794 | cin >> id; 795 | } 796 | LinkNode* node = findById(id); 797 | cout << "你删除的考生信息是:" << node->getData() << endl; 798 | Student stu = Student(id); 799 | if (StudentList.remove(stu)) 800 | { 801 | cout << "删除成功!" << endl; 802 | displayStudents(); 803 | return true; 804 | } 805 | cout << "删除失败!" << endl; 806 | return false; 807 | } 808 | 809 | bool TestRegistrationSystem::findStudent() 810 | { 811 | String id; 812 | cout << "请输入要查找的考生的考号:" << endl; 813 | cin >> id; 814 | if (findById(id) == NULL) 815 | { 816 | cout << "该考生号对应的考生不存在!" << endl; 817 | return false; 818 | } 819 | LinkNode* node = findById(id); 820 | cout << "你查找的考生信息是:" << "考号\t姓名\t性别\t年龄\t报考类别\t" << 821 | node->getData() << endl; 822 | return true; 823 | } 824 | 825 | bool TestRegistrationSystem::changeStudent() 826 | { 827 | String id; 828 | cout << "请输入要修改的考生的考号:" << endl; 829 | cin >> id; 830 | if (findById(id) == NULL) 831 | { 832 | cout << "该考生号对应的考生不存在!" << endl; 833 | return false; 834 | } 835 | LinkNode* node = findById(id); 836 | cout << "查找成功,请依次输入修改后的考号,姓名,性别,年龄及报考类别:" << endl; 837 | Student stu; 838 | cin >> stu; 839 | if (findById(stu.getID()) != NULL) 840 | { 841 | cout << "修改失败,该考生考号已存在!" << endl; 842 | return false; 843 | } 844 | node->setData(stu); 845 | cout << "修改成功!" << endl; 846 | displayStudents(); 847 | return true; 848 | 849 | } 850 | 851 | void TestRegistrationSystem::displayStudents() 852 | { 853 | cout << "考号\t姓名\t性别\t年龄\t报考类别\t" << endl; 854 | StudentList.output(); 855 | cout << endl; 856 | } 857 | 858 | int main() 859 | { 860 | TestRegistrationSystem system; 861 | cout << "请输入你要进行的操作(1为插入,2为删除,3为查找,4为修改,5为统计,0为取消操作)" << endl; 862 | int option = 0; 863 | do 864 | { 865 | cout << "请输入你要进行的操作:"; 866 | cin >> option; 867 | switch (option) 868 | { 869 | case 1: 870 | system.appendStudent(); 871 | break; 872 | case 2: 873 | system.popStudent(); 874 | break; 875 | case 3: 876 | system.findStudent(); 877 | break; 878 | case 4: 879 | system.changeStudent(); 880 | break; 881 | case 5: 882 | system.displayStudents(); 883 | break; 884 | } 885 | } while (option); 886 | cout << "谢谢使用!" << endl; 887 | } 888 | -------------------------------------------------------------------------------- /01_2152085_孙亦菲/01_2152085_孙亦菲.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/01_2152085_孙亦菲/01_2152085_孙亦菲.doc -------------------------------------------------------------------------------- /01_2152085_孙亦菲/01_2152085_孙亦菲.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/01_2152085_孙亦菲/01_2152085_孙亦菲.exe -------------------------------------------------------------------------------- /01_2152085_孙亦菲/01_2152085_孙亦菲_Linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/01_2152085_孙亦菲/01_2152085_孙亦菲_Linux -------------------------------------------------------------------------------- /01_2152085_孙亦菲/makefile: -------------------------------------------------------------------------------- 1 | 01_2152085_孙亦菲_Linux:01_2152085_孙亦菲.o 2 | g++ *.o -o $@ 3 | %.o:%.c 4 | g++ -c $< -o $@ 5 | 6 | clean: 7 | rm -f *.o 01_2152085_孙亦菲_Linux 8 | 9 | -------------------------------------------------------------------------------- /02_2152085_孙亦菲/02_2152085_孙亦菲.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | //节点类 6 | template 7 | class LinkNode 8 | { 9 | private: 10 | T data; 11 | 12 | public: 13 | LinkNode* link; 14 | 15 | LinkNode(LinkNode* ptr = NULL)//已知Link初始化LinkNode 16 | { 17 | link = ptr; 18 | } 19 | 20 | LinkNode(const T& item, LinkNode* ptr = NULL)//已知data Link 初始化LinkNode 21 | { 22 | data = item; 23 | link = ptr; 24 | } 25 | 26 | T getData()//得到LinkNode的Data 27 | { 28 | return data; 29 | } 30 | 31 | void setData(const T& item) 32 | { 33 | data = item; 34 | } 35 | 36 | }; 37 | 38 | 39 | template 40 | class List 41 | { 42 | private: 43 | LinkNode* first; 44 | public: 45 | List(); 46 | 47 | List(const List& L); 48 | 49 | ~List(); 50 | 51 | void makeEmpty();// 52 | 53 | LinkNode* getHead()const 54 | { 55 | return first; 56 | } 57 | 58 | LinkNode* getTail()const; 59 | 60 | int length()const;//get valid length 61 | 62 | void setHead(LinkNode* p);//set firsr LinkNode 63 | 64 | LinkNode* search(T& x);// 65 | 66 | LinkNode* locate(int& num); 67 | 68 | bool getData(int num, T& x);//x得到第num个数据,成功返回true,失败返回false 69 | 70 | bool setData(int num, const T& x);//x设置第num个数据,成功返回true,失败返回false 71 | 72 | bool insert(int num, T& x);//第num个数据后插入x,成功返回true,失败返回false 73 | 74 | bool remove(int num, T& x);//删除第num个数据,x得到该数据 75 | 76 | bool remove(T& x);//删除为x的数据 77 | 78 | bool isEmpty()const//Link是否为空 79 | { 80 | return first->link == NULL ? true : false; 81 | } 82 | 83 | bool isFull()const//Link是否为满 84 | { 85 | return false; 86 | } 87 | 88 | void output();//输出整个Link数据 89 | 90 | List& operator=(List L); 91 | 92 | void inputFront(T endTag);//前插输入Link,endTag为结束符号 93 | 94 | void inputFront(const unsigned int& num);//前插输入Link,有效节点num个 95 | 96 | void inputRear(T endTag);//后插输入Link,endTag为结束符号 97 | 98 | void inputRear(const unsigned int& num);//后插输入Link,有效节点num个 99 | 100 | List getIntersection(const List& l2);//求与另一个链表的交集 101 | }; 102 | 103 | template 104 | List::List() 105 | { 106 | first = new LinkNode; 107 | } 108 | 109 | template 110 | List::List(const List& L) 111 | { 112 | LinkNode* srcptr = L.getHead(); 113 | LinkNode* desptr = first = new LinkNode; 114 | while (srcptr->link != NULL) 115 | { 116 | desptr->link = new LinkNode(srcptr->link->getData()); 117 | desptr = desptr->link; 118 | srcptr = srcptr->link; 119 | } 120 | } 121 | 122 | template 123 | List::~List() 124 | { 125 | makeEmpty(); delete first; 126 | } 127 | 128 | template 129 | void List::makeEmpty() 130 | { 131 | LinkNode* current; 132 | while (first->link != NULL) 133 | { 134 | current = first->link; 135 | first->link = current->link; 136 | delete current; 137 | } 138 | 139 | } 140 | 141 | template 142 | LinkNode* List::getTail()const 143 | { 144 | LinkNode* last = first; 145 | LinkNode* current = first; 146 | while (current != NULL) 147 | { 148 | last = current; 149 | current = current->link; 150 | } 151 | return last; 152 | } 153 | 154 | template 155 | int List::length()const 156 | { 157 | int i = 0; 158 | LinkNode* current = first; 159 | while (current->link != NULL) 160 | { 161 | i++; 162 | current = current->link; 163 | } 164 | return i; 165 | } 166 | 167 | template 168 | void List::setHead(LinkNode* p) 169 | { 170 | if (first->link != NULL) 171 | { 172 | p->link = first->link; 173 | } 174 | first = p; 175 | } 176 | 177 | template 178 | LinkNode* List::search(T& x) 179 | { 180 | LinkNode* current = first->link; 181 | while (current != NULL) 182 | { 183 | if (current->getData() == x) 184 | { 185 | break; 186 | } 187 | current = current->link; 188 | } 189 | return current; 190 | } 191 | 192 | template 193 | LinkNode* List::locate(int& num) 194 | { 195 | int i = 0; 196 | LinkNode* current = first; 197 | while (current != NULL && i < num) 198 | { 199 | i++; 200 | current = current->link; 201 | } 202 | return current; 203 | } 204 | 205 | template 206 | bool List::getData(int num, T& x) 207 | { 208 | if (x <= 0) 209 | { 210 | return false; 211 | } 212 | int i = 0; 213 | LinkNode* current = first; 214 | while (current != NULL && i < x) 215 | { 216 | i++; 217 | current = current->link; 218 | } 219 | if (current == NULL) 220 | { 221 | return false;; 222 | } 223 | x = current->getData(); 224 | } 225 | 226 | template 227 | bool List::setData(int num, const T& x) 228 | { 229 | if (num <= 0) 230 | { 231 | return false; 232 | } 233 | int i = 0; 234 | LinkNode* current = first; 235 | while (current != NULL && i < num) 236 | { 237 | i++; 238 | current = current->link; 239 | } 240 | if (current == NULL) 241 | { 242 | return false; 243 | } 244 | current->data = x; 245 | return true; 246 | } 247 | 248 | template 249 | bool List::insert(int num, T& x) 250 | { 251 | if (num <= 0) 252 | { 253 | // 254 | return false; 255 | } 256 | int i = 1; 257 | LinkNode* last = first; 258 | while (last != NULL && i < num) 259 | { 260 | i++; 261 | last = last->link; 262 | } 263 | if (last == NULL) 264 | { 265 | // 266 | return false; 267 | } 268 | LinkNode* next = last->link; 269 | LinkNode* newNode = new LinkNode(x, next); 270 | last->link = newNode; 271 | return true; 272 | } 273 | 274 | template 275 | bool List::remove(int num, T& x) 276 | { 277 | if (num < 0) 278 | { 279 | // 280 | return false; 281 | } 282 | LinkNode* last = first; 283 | LinkNode* current = first; 284 | int i = 0; 285 | while (current != NULL && i < num) 286 | { 287 | i++; 288 | last = current; 289 | current = current->link; 290 | } 291 | if (current == NULL) 292 | { 293 | // 294 | return false; 295 | } 296 | x = current->data; 297 | last->link = current->link; 298 | delete current; 299 | return true; 300 | } 301 | 302 | template 303 | bool List::remove(T& x) 304 | { 305 | bool flag = false; 306 | LinkNode* last = first; 307 | LinkNode* current = first; 308 | while (last->link != NULL) 309 | { 310 | current = last->link; 311 | if (current->getData() == x) 312 | { 313 | last->link = current->link; 314 | flag = true; 315 | } 316 | last = last->link; 317 | } 318 | return flag; 319 | } 320 | 321 | template 322 | void List::output() 323 | { 324 | LinkNode* current = first->link; 325 | if (current != NULL) 326 | { 327 | cout << current->getData(); 328 | current = current->link; 329 | } 330 | 331 | while (current != NULL) 332 | { 333 | cout << " " << current->getData(); 334 | current = current->link; 335 | } 336 | } 337 | 338 | template 339 | List& List::operator=(List L) 340 | { 341 | makeEmpty(); 342 | LinkNode* srcptr = L.getHead()->link; 343 | LinkNode* desptr = first = new LinkNode; 344 | while (srcptr != NULL) 345 | { 346 | LinkNode* newNode = new LinkNode(srcptr->data); 347 | srcptr = srcptr->link; 348 | desptr->link = newNode; 349 | desptr = desptr->link; 350 | } 351 | return *this; 352 | } 353 | 354 | template 355 | void List::inputFront(T endTag) 356 | { 357 | makeEmpty(); 358 | LinkNode* newNode; 359 | T val; 360 | cin >> val; 361 | while (!(val == endTag)) 362 | { 363 | newNode = new LinkNode(val); 364 | if (newNode == NULL) 365 | { 366 | // 367 | exit(1); 368 | } 369 | newNode->link = first->link; 370 | first->link = newNode; 371 | cin >> val; 372 | } 373 | } 374 | 375 | template 376 | void List::inputFront(const unsigned int& num) 377 | { 378 | makeEmpty(); 379 | LinkNode* newNode; 380 | 381 | for (int i = 1; i <= num; i++) 382 | { 383 | T val; 384 | cin >> val; 385 | newNode = new LinkNode(val); 386 | if (newNode == NULL) 387 | { 388 | // 389 | exit(1); 390 | } 391 | newNode->link = first->link; 392 | first->link = newNode; 393 | } 394 | } 395 | 396 | template 397 | void List::inputRear(T endTag) 398 | { 399 | makeEmpty(); 400 | LinkNode* newNode; 401 | LinkNode* last = first; 402 | T val; 403 | cin >> val; 404 | while (!(val == endTag)) 405 | { 406 | newNode = new LinkNode(val); 407 | if (newNode == NULL) 408 | { 409 | // 410 | exit(1); 411 | } 412 | last->link = newNode; 413 | last = newNode; 414 | cin >> val; 415 | } 416 | } 417 | 418 | template 419 | void List::inputRear(const unsigned int& num) 420 | { 421 | makeEmpty(); 422 | LinkNode* newNode; 423 | LinkNode* last = first; 424 | 425 | for (int i = 1; i <= num; i++) 426 | { 427 | T val; 428 | cin >> val; 429 | newNode = new LinkNode(val); 430 | if (newNode == NULL) 431 | { 432 | cerr << "Allocation Error!"; 433 | exit(1); 434 | } 435 | last->link = newNode; 436 | last = newNode; 437 | } 438 | 439 | } 440 | 441 | template 442 | List List::getIntersection(const List& l2) 443 | { 444 | List l3; 445 | LinkNode* currentnode1 = first->link; 446 | LinkNode* currentnode2 = l2.first->link; 447 | LinkNode* currentnode3 = l3.first; 448 | while (currentnode1 != NULL && currentnode2 != NULL) 449 | { 450 | if (currentnode1->getData() == currentnode2->getData()) 451 | { 452 | currentnode3->link = new LinkNode(currentnode1->getData()); 453 | currentnode1 = currentnode1->link; 454 | currentnode2 = currentnode2->link; 455 | currentnode3 = currentnode3->link; 456 | } 457 | else if (currentnode1->getData() < currentnode2->getData()) 458 | { 459 | currentnode1 = currentnode1->link; 460 | } 461 | else 462 | { 463 | currentnode2 = currentnode2->link; 464 | } 465 | } 466 | return l3; 467 | } 468 | 469 | int main() 470 | { 471 | List l1, l2; 472 | l1.inputRear(-1); 473 | l2.inputRear(-1); 474 | List l3 = l1.getIntersection(l2); 475 | 476 | if (l3.isEmpty()) 477 | { 478 | cout << "NULL\n"; 479 | } 480 | else 481 | { 482 | l3.output(); 483 | } 484 | 485 | return 0; 486 | } -------------------------------------------------------------------------------- /02_2152085_孙亦菲/02_2152085_孙亦菲.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/02_2152085_孙亦菲/02_2152085_孙亦菲.doc -------------------------------------------------------------------------------- /02_2152085_孙亦菲/02_2152085_孙亦菲.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/02_2152085_孙亦菲/02_2152085_孙亦菲.exe -------------------------------------------------------------------------------- /02_2152085_孙亦菲/02_2152085_孙亦菲_Linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/02_2152085_孙亦菲/02_2152085_孙亦菲_Linux -------------------------------------------------------------------------------- /02_2152085_孙亦菲/makefile: -------------------------------------------------------------------------------- 1 | 02_2152085_孙亦菲_Linux:02_2152085_孙亦菲.o 2 | g++ *.o -o $@ 3 | %.o:%.c 4 | g++ -c $< -o $@ 5 | 6 | clean: 7 | rm -f *.o 02_2152085_孙亦菲_Linux 8 | 9 | -------------------------------------------------------------------------------- /03_2152085_孙亦菲/03_2152085_孙亦菲.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAZESIZE 7 4 | using namespace std; 5 | 6 | //节点类 7 | template 8 | class LinkNode 9 | { 10 | private: 11 | T data; 12 | 13 | public: 14 | LinkNode* link; 15 | 16 | LinkNode(LinkNode* ptr = NULL)//已知Link初始化LinkNode 17 | { 18 | link = ptr; 19 | } 20 | 21 | LinkNode(const T& item, LinkNode* ptr = NULL)//已知data Link 初始化LinkNode 22 | { 23 | data = item; 24 | link = ptr; 25 | } 26 | 27 | T getData()//得到LinkNode的Data 28 | { 29 | return data; 30 | } 31 | 32 | void setData(const T& item) 33 | { 34 | data = item; 35 | } 36 | 37 | }; 38 | 39 | 40 | template 41 | class List 42 | { 43 | private: 44 | LinkNode* first; 45 | public: 46 | List(); 47 | 48 | List(const List& L); 49 | 50 | ~List(); 51 | 52 | void makeEmpty();// 53 | 54 | LinkNode* getHead()const 55 | { 56 | return first; 57 | } 58 | 59 | LinkNode* getTail()const; 60 | 61 | int length()const;//get valid length 62 | 63 | void setHead(LinkNode* p);//set firsr LinkNode 64 | 65 | LinkNode* search(T& x);// 66 | 67 | LinkNode* locate(int& num); 68 | 69 | bool getData(int num, T& x);//x得到第num个数据,成功返回true,失败返回false 70 | 71 | bool setData(int num, const T& x);//x设置第num个数据,成功返回true,失败返回false 72 | 73 | bool insert(int num, T& x);//第num个数据后插入x,成功返回true,失败返回false 74 | 75 | bool insertAtTail(T x);//insert item at tail 76 | 77 | bool remove(int num, T& x);//删除第num个数据,x得到该数据 78 | 79 | bool remove(T& x);//删除为x的数据 80 | 81 | bool removeAtTail();//remove item at tail 82 | 83 | bool isEmpty()const//Link是否为空 84 | { 85 | return first->link == NULL ? true : false; 86 | } 87 | 88 | bool isFull()const//Link是否为满 89 | { 90 | return false; 91 | } 92 | 93 | void output();//输出整个Link数据 94 | 95 | List& operator=(List L); 96 | 97 | void inputFront(T endTag);//前插输入Link,endTag为结束符号 98 | 99 | void inputFront(const unsigned int& num);//前插输入Link,有效节点num个 100 | 101 | void inputRear(T endTag);//后插输入Link,endTag为结束符号 102 | 103 | void inputRear(const unsigned int& num);//后插输入Link,有效节点num个 104 | 105 | List getIntersection(const List& l2);//求与另一个链表的交集 106 | }; 107 | 108 | template 109 | List::List() 110 | { 111 | first = new LinkNode; 112 | } 113 | 114 | template 115 | List::List(const List& L) 116 | { 117 | LinkNode* srcptr = L.getHead(); 118 | LinkNode* desptr = first = new LinkNode; 119 | while (srcptr->link != NULL) 120 | { 121 | desptr->link = new LinkNode(srcptr->link->getData()); 122 | desptr = desptr->link; 123 | srcptr = srcptr->link; 124 | } 125 | } 126 | 127 | template 128 | List::~List() 129 | { 130 | makeEmpty(); delete first; 131 | } 132 | 133 | template 134 | void List::makeEmpty() 135 | { 136 | LinkNode* current; 137 | while (first->link != NULL) 138 | { 139 | current = first->link; 140 | first->link = current->link; 141 | delete current; 142 | } 143 | 144 | } 145 | 146 | template 147 | LinkNode* List::getTail()const 148 | { 149 | LinkNode* last = first; 150 | LinkNode* current = first; 151 | while (current != NULL) 152 | { 153 | last = current; 154 | current = current->link; 155 | } 156 | return last; 157 | } 158 | 159 | template 160 | int List::length()const 161 | { 162 | int i = 0; 163 | LinkNode* current = first; 164 | while (current->link != NULL) 165 | { 166 | i++; 167 | current = current->link; 168 | } 169 | return i; 170 | } 171 | 172 | template 173 | void List::setHead(LinkNode* p) 174 | { 175 | if (first->link != NULL) 176 | { 177 | p->link = first->link; 178 | } 179 | first = p; 180 | } 181 | 182 | template 183 | LinkNode* List::search(T& x) 184 | { 185 | LinkNode* current = first->link; 186 | while (current != NULL) 187 | { 188 | if (current->getData() == x) 189 | { 190 | break; 191 | } 192 | current = current->link; 193 | } 194 | return current; 195 | } 196 | 197 | template 198 | LinkNode* List::locate(int& num) 199 | { 200 | int i = 0; 201 | LinkNode* current = first; 202 | while (current != NULL && i < num) 203 | { 204 | i++; 205 | current = current->link; 206 | } 207 | return current; 208 | } 209 | 210 | template 211 | bool List::getData(int num, T& x) 212 | { 213 | if (x <= 0) 214 | { 215 | return false; 216 | } 217 | int i = 0; 218 | LinkNode* current = first; 219 | while (current != NULL && i < x) 220 | { 221 | i++; 222 | current = current->link; 223 | } 224 | if (current == NULL) 225 | { 226 | return false;; 227 | } 228 | x = current->getData(); 229 | } 230 | 231 | template 232 | bool List::setData(int num, const T& x) 233 | { 234 | if (num <= 0) 235 | { 236 | return false; 237 | } 238 | int i = 0; 239 | LinkNode* current = first; 240 | while (current != NULL && i < num) 241 | { 242 | i++; 243 | current = current->link; 244 | } 245 | if (current == NULL) 246 | { 247 | return false; 248 | } 249 | current->data = x; 250 | return true; 251 | } 252 | 253 | template 254 | bool List::insert(int num, T& x) 255 | { 256 | if (num <= 0) 257 | { 258 | // 259 | return false; 260 | } 261 | int i = 1; 262 | LinkNode* last = first; 263 | while (last != NULL && i < num) 264 | { 265 | i++; 266 | last = last->link; 267 | } 268 | if (last == NULL) 269 | { 270 | // 271 | return false; 272 | } 273 | LinkNode* next = last->link; 274 | LinkNode* newNode = new LinkNode(x, next); 275 | last->link = newNode; 276 | return true; 277 | } 278 | 279 | template 280 | bool List::insertAtTail(T x) 281 | { 282 | LinkNode* tail = getTail(); 283 | //cout << tail->getData(); cout << endl; 284 | LinkNode* newnode = new LinkNode(x, NULL); 285 | tail->link = newnode; 286 | return true; 287 | } 288 | 289 | template 290 | bool List::remove(int num, T& x) 291 | { 292 | if (num < 0) 293 | { 294 | // 295 | return false; 296 | } 297 | LinkNode* last = first; 298 | LinkNode* current = first; 299 | int i = 0; 300 | while (current != NULL && i < num) 301 | { 302 | i++; 303 | last = current; 304 | current = current->link; 305 | } 306 | if (current == NULL) 307 | { 308 | // 309 | return false; 310 | } 311 | x = current->data; 312 | last->link = current->link; 313 | delete current; 314 | return true; 315 | } 316 | 317 | template 318 | bool List::remove(T& x) 319 | { 320 | bool flag = false; 321 | LinkNode* last = first; 322 | LinkNode* current = first; 323 | while (last->link != NULL) 324 | { 325 | current = last->link; 326 | if (current->getData() == x) 327 | { 328 | last->link = current->link; 329 | flag = true; 330 | } 331 | last = last->link; 332 | } 333 | return flag; 334 | } 335 | 336 | template 337 | bool List::removeAtTail() 338 | { 339 | if (isEmpty()) 340 | { 341 | return false; 342 | } 343 | LinkNode* current = first; 344 | while (current->link != NULL && current->link->link != NULL) 345 | { 346 | current = current->link; 347 | } 348 | current->link = NULL; 349 | 350 | return true; 351 | } 352 | 353 | template 354 | void List::output() 355 | { 356 | LinkNode* current = first->link; 357 | if (current != NULL) 358 | { 359 | cout << current->getData(); 360 | current = current->link; 361 | } 362 | 363 | while (current != NULL) 364 | { 365 | cout << " " << current->getData(); 366 | current = current->link; 367 | } 368 | } 369 | 370 | template 371 | List& List::operator=(List L) 372 | { 373 | makeEmpty(); 374 | LinkNode* srcptr = L.getHead()->link; 375 | LinkNode* desptr = first = new LinkNode; 376 | while (srcptr != NULL) 377 | { 378 | LinkNode* newNode = new LinkNode(srcptr.data); 379 | srcptr = srcptr->link; 380 | desptr->link = newNode; 381 | desptr = desptr->link; 382 | } 383 | return *this; 384 | } 385 | 386 | template 387 | void List::inputFront(T endTag) 388 | { 389 | makeEmpty(); 390 | LinkNode* newNode; 391 | T val; 392 | cin >> val; 393 | while (!(val == endTag)) 394 | { 395 | newNode = new LinkNode(val); 396 | if (newNode == NULL) 397 | { 398 | // 399 | exit(1); 400 | } 401 | newNode->link = first->link; 402 | first->link = newNode; 403 | cin >> val; 404 | } 405 | } 406 | 407 | template 408 | void List::inputFront(const unsigned int& num) 409 | { 410 | makeEmpty(); 411 | LinkNode* newNode; 412 | 413 | for (int i = 1; i <= num; i++) 414 | { 415 | T val; 416 | cin >> val; 417 | newNode = new LinkNode(val); 418 | if (newNode == NULL) 419 | { 420 | // 421 | exit(1); 422 | } 423 | newNode->link = first->link; 424 | first->link = newNode; 425 | } 426 | } 427 | 428 | template 429 | void List::inputRear(T endTag) 430 | { 431 | makeEmpty(); 432 | LinkNode* newNode; 433 | LinkNode* last = first; 434 | T val; 435 | cin >> val; 436 | while (!(val == endTag)) 437 | { 438 | newNode = new LinkNode(val); 439 | if (newNode == NULL) 440 | { 441 | // 442 | exit(1); 443 | } 444 | last->link = newNode; 445 | last = newNode; 446 | cin >> val; 447 | } 448 | } 449 | 450 | template 451 | void List::inputRear(const unsigned int& num) 452 | { 453 | makeEmpty(); 454 | LinkNode* newNode; 455 | LinkNode* last = first; 456 | 457 | for (int i = 1; i <= num; i++) 458 | { 459 | T val; 460 | cin >> val; 461 | newNode = new LinkNode(val); 462 | if (newNode == NULL) 463 | { 464 | // 465 | exit(1); 466 | } 467 | last->link = newNode; 468 | last = newNode; 469 | } 470 | 471 | } 472 | 473 | template 474 | List List::getIntersection(const List& l2) 475 | { 476 | List l3; 477 | LinkNode* currentnode1 = first->link; 478 | LinkNode* currentnode2 = l2.first->link; 479 | LinkNode* currentnode3 = l3.first; 480 | while (currentnode1 != NULL && currentnode2 != NULL) 481 | { 482 | if (currentnode1->getData() == currentnode2->getData()) 483 | { 484 | currentnode3->link = new LinkNode(currentnode1->getData()); 485 | currentnode1 = currentnode1->link; 486 | currentnode2 = currentnode2->link; 487 | currentnode3 = currentnode3->link; 488 | } 489 | else if (currentnode1->getData() < currentnode2->getData()) 490 | { 491 | currentnode1 = currentnode1->link; 492 | } 493 | else 494 | { 495 | currentnode2 = currentnode2->link; 496 | } 497 | } 498 | return l3; 499 | } 500 | 501 | class point 502 | { 503 | friend ostream& operator<<(ostream& cout, const point& p); 504 | private: 505 | int row, col; 506 | public: 507 | point() {}; 508 | point(int r, int c) 509 | { 510 | row = r; 511 | col = c; 512 | } 513 | int getRow()const 514 | { 515 | return row; 516 | } 517 | int getCol()const 518 | { 519 | return col; 520 | } 521 | }; 522 | 523 | ostream& operator<<(ostream& cout, const point& p) 524 | { 525 | cout << "(" << p.row << "," << p.col << ")"; 526 | return cout; 527 | } 528 | 529 | class Maze 530 | { 531 | private: 532 | char map[MAZESIZE][MAZESIZE] = 533 | { 534 | {'#','#','#','#','#','#','#'}, 535 | {'#','S','#','0','0','0','#'}, 536 | {'#','0','#','0','#','#','#'}, 537 | {'#','0','#','0','#','0','#'}, 538 | {'#','0','0','0','0','0','#'}, 539 | {'#','0','#','0','#','E','#'}, 540 | {'#','#','#','#','#','#','#'}, 541 | }; 542 | 543 | char pathmap[MAZESIZE][MAZESIZE]; 544 | 545 | int visit[MAZESIZE][MAZESIZE] = { 0 }; 546 | 547 | int move[4][2] = { {-1,0}, {1,0}, {0,-1}, {0,1} }; 548 | 549 | List pathList; 550 | //展示地图上的路径 551 | void showpathMaze(); 552 | //找通路 553 | bool findPath(const point& start); 554 | //输出路径坐标 555 | void showPath(const point& start); 556 | //初始化地图 557 | void initpathmap(); 558 | public: 559 | //展示地图 560 | void showMaze(); 561 | 562 | //统一解决问题 563 | void solve(); 564 | }; 565 | 566 | void Maze::initpathmap() 567 | { 568 | for (int i = 0; i < MAZESIZE; i++) 569 | { 570 | for (int j = 0; j < MAZESIZE; j++) 571 | { 572 | pathmap[i][j] = map[i][j]; 573 | } 574 | } 575 | } 576 | 577 | void Maze::showMaze() 578 | { 579 | //output table-header 580 | cout << "迷宫地图(#代表不可通行,0代表可以通行,S为起点,E为终点):\n\n"; 581 | cout << "\t"; 582 | for (int j = 0; j < MAZESIZE; j++) 583 | { 584 | cout << j << "列\t"; 585 | } 586 | cout << endl; 587 | for (int i = 0; i < MAZESIZE; i++) 588 | { 589 | cout << i << "行\t"; 590 | for (int j = 0; j < MAZESIZE; j++) 591 | { 592 | cout << map[i][j] << "\t"; 593 | } 594 | cout << endl << endl; 595 | } 596 | } 597 | 598 | void Maze::showpathMaze() 599 | { 600 | cout << "行走地图如下(X代表走的路径);\n"; 601 | //output table-header 602 | cout << "\t"; 603 | for (int j = 0; j < MAZESIZE; j++) 604 | { 605 | cout << j << "列\t"; 606 | } 607 | cout << endl; 608 | for (int i = 0; i < MAZESIZE; i++) 609 | { 610 | cout << i << "行\t"; 611 | for (int j = 0; j < MAZESIZE; j++) 612 | { 613 | cout << pathmap[i][j] << "\t"; 614 | } 615 | cout << endl << endl; 616 | } 617 | } 618 | 619 | bool Maze::findPath(const point& start) 620 | { 621 | point currentPos = start; 622 | int row = currentPos.getRow(); 623 | int col = currentPos.getCol(); 624 | visit[row][col] = 1; 625 | pathList.insertAtTail(point(row, col)); 626 | pathmap[row][col] = 'X'; 627 | if (map[row][col] == 'E') 628 | { 629 | return true; 630 | } 631 | 632 | bool flag = false; 633 | for (int i = 0; i < 4; i++) 634 | { 635 | int nxtrow = row + move[i][0]; 636 | int nxtcol = col + move[i][1]; 637 | if (nxtrow <= 0 || nxtcol <= 0 || nxtrow >= MAZESIZE || 638 | nxtcol >= MAZESIZE || visit[nxtrow][nxtcol] || map[nxtrow][nxtcol] == '#') 639 | { 640 | continue; 641 | } 642 | if (findPath(point(nxtrow, nxtcol))) 643 | { 644 | flag = true; 645 | } 646 | } 647 | if (!flag) 648 | { 649 | pathmap[row][col] = '0'; 650 | pathList.removeAtTail(); 651 | visit[row][col] = 0; 652 | } 653 | return flag; 654 | } 655 | 656 | void Maze::showPath(const point& start) 657 | { 658 | if (findPath(start)) 659 | { 660 | showpathMaze(); 661 | cout << "迷宫路径\n"; 662 | LinkNode* current = pathList.getHead()->link; 663 | cout << current->getData(); 664 | current = current->link; 665 | while (current != NULL) 666 | { 667 | cout << " ---> " << current->getData(); 668 | current = current->link; 669 | } 670 | cout << "\n\n"; 671 | return; 672 | } 673 | else 674 | { 675 | pathmap[start.getRow()][start.getCol()] = 'S'; 676 | showpathMaze(); 677 | cout << "从当前位置无法走到迷宫出口!" << endl; 678 | return; 679 | } 680 | } 681 | 682 | void Maze::solve() 683 | { 684 | cout << "按任意键显示路径" << endl; 685 | getchar(); 686 | for (int i = 0; i < MAZESIZE; i++) 687 | { 688 | for (int j = 0; j < MAZESIZE; j++) 689 | { 690 | if (map[i][j] == 'S') 691 | { 692 | memset(visit, 0, sizeof(visit)); 693 | pathList.makeEmpty(); 694 | initpathmap(); 695 | showPath(point(i, j)); 696 | } 697 | } 698 | } 699 | } 700 | 701 | int main() 702 | { 703 | Maze maze; 704 | maze.showMaze(); 705 | maze.solve(); 706 | 707 | return 0; 708 | } 709 | -------------------------------------------------------------------------------- /03_2152085_孙亦菲/03_2152085_孙亦菲.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/03_2152085_孙亦菲/03_2152085_孙亦菲.doc -------------------------------------------------------------------------------- /03_2152085_孙亦菲/03_2152085_孙亦菲.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/03_2152085_孙亦菲/03_2152085_孙亦菲.exe -------------------------------------------------------------------------------- /03_2152085_孙亦菲/03_2152085_孙亦菲_Linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/03_2152085_孙亦菲/03_2152085_孙亦菲_Linux -------------------------------------------------------------------------------- /03_2152085_孙亦菲/makefile: -------------------------------------------------------------------------------- 1 | 03_2152085_孙亦菲_Linux:03_2152085_孙亦菲.o 2 | g++ *.o -o $@ 3 | %.o:%.c 4 | g++ -c $< -o $@ 5 | 6 | clean: 7 | rm -f *.o 03_2152085_孙亦菲_Linux 8 | 9 | -------------------------------------------------------------------------------- /04_2152085_孙亦菲/04_2152085_孙亦菲.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | #include 5 | #define DefaultSize 20 6 | using namespace std; 7 | 8 | class String 9 | { 10 | friend istream& operator>>(istream&, String& Str); 11 | friend ostream& operator <<(ostream&, const String& Str); 12 | private: 13 | int currentLength; 14 | int maxLength; 15 | char* ch; 16 | public: 17 | String(int size = DefaultSize); 18 | 19 | String(const char* init); 20 | 21 | String(const String& ob); 22 | 23 | ~String() 24 | { 25 | delete[]ch; 26 | } 27 | 28 | int length()const 29 | { 30 | return currentLength; 31 | } 32 | 33 | String operator()(int pos, int length); 34 | 35 | bool operator==(const String& ob) 36 | { 37 | return strcmp(ch, ob.ch) == 0; 38 | } 39 | 40 | bool operator!=(const String& ob) 41 | { 42 | return strcmp(ch, ob.ch) != 0; 43 | } 44 | 45 | String& operator=(const String& ob); 46 | 47 | String& operator+=(const String& ob); 48 | 49 | String& operator+=(const char c); 50 | 51 | char& operator[](int num)const; 52 | 53 | int fastFind(String& pat, int pos); 54 | 55 | 56 | }; 57 | String::String(int size) 58 | { 59 | maxLength = size; 60 | ch = new char[size + 1]; 61 | if (ch == NULL) 62 | { 63 | cerr << "Allocation Error!\n"; 64 | } 65 | currentLength = 0; ch[0] = '\0'; 66 | } 67 | String::String(const char* init) 68 | { 69 | int length = strlen(init); 70 | maxLength = length > DefaultSize ? length : DefaultSize; 71 | ch = new char[maxLength + 1]; 72 | if (ch == NULL) 73 | { 74 | cerr << "Allocation Error!\n"; 75 | } 76 | currentLength = length; 77 | strcpy(ch, init); 78 | } 79 | String::String(const String& ob) 80 | { 81 | int length = ob.currentLength; 82 | maxLength = length > DefaultSize ? length : DefaultSize; 83 | ch = new char[maxLength + 1]; 84 | if (ch == NULL) 85 | { 86 | cerr << "Allocation Error!\n"; 87 | } 88 | currentLength = length; 89 | strcpy(ch, ob.ch); 90 | } 91 | 92 | String String::operator()(int pos, int length) 93 | { 94 | String s; 95 | if (pos >= 0 && pos < currentLength && length>0 && pos + length - 1 < currentLength) 96 | { 97 | for (int i = 0; i < length; i++) 98 | { 99 | s.ch[i] = ch[pos + i]; 100 | } 101 | s.ch[length] = '\0'; 102 | } 103 | s.currentLength=strlen(s.ch); 104 | return s; 105 | } 106 | 107 | String& String:: operator=(const String& ob) 108 | { 109 | if (this != &ob) 110 | { 111 | delete[]ch; 112 | ch = new char[ob.maxLength+ 1] ; 113 | maxLength = ob.maxLength; 114 | currentLength = ob.currentLength; 115 | strcpy(ch, ob.ch); 116 | } 117 | return *this; 118 | } 119 | String& String::operator+=(const String& ob) 120 | { 121 | char* temp = ch; 122 | int length = currentLength + ob.currentLength; 123 | maxLength = maxLength > length ? maxLength : length; 124 | ch = new char[maxLength + 1]; 125 | if (ch == NULL) 126 | { 127 | cerr << "Allocation Error!\n"; 128 | exit(1); 129 | } 130 | strcpy(ch, temp); 131 | strcat(ch, ob.ch); 132 | currentLength = length; 133 | delete[]temp; 134 | return *this; 135 | } 136 | 137 | String& String::operator+=(const char c) 138 | { 139 | char* temp = ch; 140 | int length = currentLength + 1; 141 | maxLength = maxLength > length ? maxLength : length; 142 | ch = new char[maxLength + 1]; 143 | if (ch == NULL) 144 | { 145 | cerr << "Allocation Error!\n"; 146 | exit(1); 147 | } 148 | ch[currentLength] = c; 149 | currentLength = length; 150 | delete[]temp; 151 | return *this; 152 | } 153 | char& String::operator[](int num)const 154 | { 155 | if (num < 0 || num >= currentLength) 156 | { 157 | cerr << "Out of Range!\n"; 158 | exit(1); 159 | } 160 | return ch[num]; 161 | } 162 | int String::fastFind(String& pat, int pos) 163 | { 164 | int pLength = pat.length(); 165 | int* next = new int[pLength]; 166 | next[0] = -1; 167 | int j = 0, k = -1; 168 | while (j < pLength - 1) 169 | { 170 | if (k == -1 || ch[j] == ch[k]) 171 | { 172 | j++; k++; 173 | next[j] = k; 174 | } 175 | else 176 | { 177 | k = next[k]; 178 | } 179 | } 180 | int tPos = 0; 181 | int pPos = 0; 182 | while (tPos < currentLength && pPos < pLength) 183 | { 184 | if (pPos == -1 || ch[tPos] == ch[pPos]) 185 | { 186 | tPos++; 187 | pPos++; 188 | } 189 | else 190 | { 191 | pPos = next[pPos]; 192 | } 193 | } 194 | delete[]next; 195 | if (pPos < pLength) 196 | { 197 | return -1; 198 | } 199 | else 200 | { 201 | return tPos - pLength; 202 | } 203 | } 204 | istream& operator>>(istream& cin, String& Str) 205 | { 206 | char* init = new char[DefaultSize * 5]; 207 | cin.getline(init, DefaultSize * 5, '\n'); 208 | int length = strlen(init); 209 | delete[]Str.ch; 210 | Str.ch = new char[length + 1]; 211 | Str.currentLength = length; 212 | strcpy(Str.ch, init); 213 | 214 | return cin; 215 | } 216 | 217 | ostream& operator<<(ostream& cout, const String& Str) 218 | { 219 | cout << Str.ch; 220 | 221 | return cout; 222 | } 223 | 224 | //节点类 225 | template 226 | class LinkNode 227 | { 228 | private: 229 | T data; 230 | 231 | public: 232 | LinkNode* link; 233 | //已知Link初始化LinkNode 234 | LinkNode(LinkNode* ptr = NULL) 235 | { 236 | link = ptr; 237 | } 238 | //已知data Link 初始化LinkNode 239 | LinkNode(const T& item, LinkNode* ptr = NULL) 240 | { 241 | data = item; 242 | link = ptr; 243 | } 244 | //得到LinkNode的Data 245 | T getData() 246 | { 247 | return data; 248 | } 249 | //设置LinkNode的Data 250 | void setData(const T& item) 251 | { 252 | data = item; 253 | } 254 | 255 | }; 256 | 257 | template 258 | class Stack 259 | { 260 | private: 261 | LinkNode* top; 262 | public: 263 | Stack() :top(NULL) {}; 264 | ~Stack() 265 | { 266 | makeEmpty(); 267 | delete top; 268 | } 269 | Stack(const Stack& s); 270 | 271 | Stack& operator=(const Stack& s); 272 | 273 | void makeEmpty(); 274 | 275 | void Push(const T x); 276 | 277 | bool Pop(); 278 | 279 | bool isEmpty()const 280 | { 281 | return top == NULL; 282 | } 283 | T getTop() 284 | { 285 | return top->getData(); 286 | } 287 | int getSize()const; 288 | 289 | Stack reverse()const; 290 | 291 | void output()const; 292 | 293 | }; 294 | 295 | template 296 | Stack::Stack(const Stack& s) 297 | { 298 | top = NULL; 299 | makeEmpty(); 300 | 301 | T memory[10 * DefaultSize]; 302 | int cnt = -1; 303 | LinkNode* p = s.top; 304 | while (p != NULL) 305 | { 306 | memory[++cnt] = p->getData(); 307 | p = p->link; 308 | } 309 | for (int i = cnt; i >= 0; i--) 310 | { 311 | Push(memory[i]); 312 | } 313 | } 314 | 315 | template 316 | Stack& Stack::operator=(const Stack& s) 317 | { 318 | makeEmpty(); 319 | 320 | T memory[10 * DefaultSize]; 321 | int cnt = -1; 322 | LinkNode* p = s.top; 323 | while (p != NULL) 324 | { 325 | memory[++cnt] = p->getData(); 326 | p = p->link; 327 | } 328 | for (int i = cnt; i >= 0; i--) 329 | { 330 | Push(memory[i]); 331 | } 332 | return *this; 333 | } 334 | 335 | 336 | template 337 | void Stack::makeEmpty() 338 | { 339 | LinkNode* p; 340 | while (top != NULL) 341 | { 342 | p = top; 343 | top = top->link; 344 | delete p; 345 | } 346 | } 347 | 348 | template 349 | void Stack::Push(const T x) 350 | { 351 | top = new LinkNode(x, top); 352 | if (top == NULL) 353 | { 354 | cerr << "Allocation Error!\n"; 355 | exit(1); 356 | } 357 | } 358 | 359 | template 360 | bool Stack::Pop() 361 | { 362 | if (isEmpty()) 363 | { 364 | return false; 365 | } 366 | LinkNode* p = top; 367 | top = top->link; 368 | 369 | delete p; 370 | return true; 371 | } 372 | 373 | template 374 | int Stack::getSize()const 375 | { 376 | int k = 0; 377 | LinkNode* p = top; 378 | while (p != NULL) 379 | { 380 | k++; 381 | p = p->link; 382 | } 383 | return k; 384 | } 385 | 386 | template 387 | Stack Stack::reverse()const 388 | { 389 | Stack s; 390 | LinkNode* p = top; 391 | while (p != NULL) 392 | { 393 | s.Push(p->getData()); 394 | p = p->link; 395 | } 396 | return s; 397 | } 398 | 399 | template 400 | void Stack::output()const 401 | { 402 | LinkNode* p = top; 403 | while (p!=NULL) 404 | { 405 | cout << p->getData(); 406 | p = p->link; 407 | if (p != NULL) 408 | { 409 | cout << " "; 410 | } 411 | } 412 | } 413 | 414 | 415 | int priorLevel(char c) 416 | { 417 | switch (c) 418 | { 419 | case '(': 420 | return 0; 421 | break; 422 | case '+': 423 | return 1; 424 | break; 425 | case '-': 426 | return 1; 427 | break; 428 | case '*': 429 | return 2; 430 | break; 431 | case '/': 432 | return 2; 433 | break; 434 | default: 435 | return -1; 436 | } 437 | } 438 | 439 | inline void error() 440 | { 441 | cout << "中缀表达式输入有误,程序结束!" << endl; 442 | exit(0); 443 | } 444 | 445 | bool numCheck(const String& s) 446 | { 447 | bool pointflag = 0;//此时是否能出现小数点 448 | bool existpointflag = 0;//此时是否已经有过小数点 449 | for (int i = 0; i < s.length(); i++) 450 | { 451 | if (s[i] >= '0' && s[i] <= '9') 452 | { 453 | pointflag = 1; 454 | continue; 455 | } 456 | else if (s[i] == '.') 457 | { 458 | if (!pointflag||existpointflag) 459 | { 460 | return false; 461 | } 462 | existpointflag = 1; 463 | continue; 464 | } 465 | else 466 | { 467 | return false; 468 | } 469 | } 470 | return true; 471 | } 472 | 473 | void spaceRecord(int spaceplace[], const String& s, int& spacecnt) 474 | { 475 | spaceplace[0] = -1; 476 | 477 | int sum = 0; 478 | for (int i = 0; i < s.length(); i++) 479 | { 480 | if (s[i] == ' ') 481 | { 482 | spaceplace[++spacecnt] = i; 483 | continue; 484 | } 485 | sum++; 486 | if (sum > DefaultSize) 487 | { 488 | error(); 489 | } 490 | 491 | } 492 | spaceplace[++spacecnt] = s.length(); 493 | } 494 | 495 | int main() 496 | { 497 | Stack ansStack;//答案栈 498 | Stack operatorStack;//运算符栈 499 | int operatorflag = 0;//当前是否能输入运算符 500 | int numberflag = 1;//当前是否能输入数字 501 | int leftparentheseNum = 0;//左括号个数 502 | 503 | String InputString; 504 | cin >> InputString; 505 | 506 | int spaceplace[20] = {0}; 507 | int spacecnt = 0; 508 | 509 | //记录空格位置 510 | spaceRecord(spaceplace, InputString, spacecnt); 511 | 512 | String nowS; 513 | for(int i=0;i 1) 529 | { 530 | flag = 0; 531 | } 532 | else 533 | { 534 | operatorStack.Push(nowS); 535 | leftparentheseNum++; 536 | operatorflag = 0; 537 | numberflag = 1; 538 | } 539 | } 540 | //输入了右括号 541 | else if (ch == ')') 542 | { 543 | if (!leftparentheseNum) 544 | { 545 | flag = 0; 546 | } 547 | else 548 | { 549 | leftparentheseNum--; 550 | while (1) 551 | { 552 | String temp = operatorStack.getTop(); 553 | operatorStack.Pop(); 554 | if (temp == "(") 555 | { 556 | break; 557 | } 558 | ansStack.Push(temp); 559 | } 560 | operatorflag = 1; 561 | numberflag = 0; 562 | } 563 | } 564 | //输入了数字 565 | else if (numberflag&&ch >= '0' && ch <= '9') 566 | { 567 | flag = numCheck(nowS); 568 | ansStack.Push(nowS); 569 | operatorflag = 1; 570 | numberflag = 0; 571 | } 572 | //输入了带符号的数字 573 | else if (numberflag && (ch == '+' || ch == '-') && nowS.length() > 1) 574 | { 575 | flag = numCheck(nowS(1, nowS.length()-1)); 576 | if (ch == '+') 577 | { 578 | ansStack.Push(nowS(1, nowS.length() - 1)); 579 | } 580 | else 581 | { 582 | ansStack.Push(nowS); 583 | } 584 | operatorflag = 1; 585 | numberflag = 0; 586 | } 587 | //输入了运算符 588 | else if (operatorflag &&(ch == '+' || ch == '-'|| ch == '*' || ch == '/') && nowS.length() ==1) 589 | { 590 | flag = 1; 591 | operatorflag = 0; 592 | while (!operatorStack.isEmpty()) 593 | { 594 | if (priorLevel(ch) > priorLevel(operatorStack.getTop()[0])) 595 | { 596 | break; 597 | } 598 | else 599 | { 600 | ansStack.Push((operatorStack.getTop())); 601 | operatorStack.Pop(); 602 | } 603 | } 604 | operatorStack.Push(nowS); 605 | operatorflag = 0; 606 | numberflag = 1; 607 | } 608 | //以上情况都不符,错误 609 | else 610 | { 611 | flag = 0; 612 | } 613 | //如果有错误情况,进入error 614 | if (!flag) 615 | { 616 | error(); 617 | } 618 | } 619 | //将运算符栈中剩余运算符进入答案栈 620 | while (!operatorStack.isEmpty()) 621 | { 622 | String temp = operatorStack.getTop(); 623 | operatorStack.Pop(); 624 | ansStack.Push(temp); 625 | } 626 | //如果左括号剩余/还需输入数字/答案栈为空 ,则error 627 | if (leftparentheseNum||numberflag||ansStack.isEmpty()) 628 | { 629 | error(); 630 | } 631 | 632 | //翻转栈 633 | ansStack = ansStack.reverse(); 634 | //输出栈 635 | ansStack.output(); 636 | cout< 2 | using namespace std; 3 | 4 | 5 | template 6 | class LinkNode 7 | { 8 | private: 9 | T data; 10 | 11 | public: 12 | LinkNode* link; 13 | //已知Link初始化LinkNode 14 | LinkNode(LinkNode* ptr = NULL) 15 | { 16 | link = ptr; 17 | } 18 | //已知data Link 初始化LinkNode 19 | LinkNode(const T& item, LinkNode* ptr = NULL) 20 | { 21 | data = item; 22 | link = ptr; 23 | } 24 | //得到LinkNode的Data 25 | T getData() 26 | { 27 | return data; 28 | } 29 | //设置LinkNode的Data 30 | void setData(const T& item) 31 | { 32 | data = item; 33 | } 34 | 35 | }; 36 | 37 | template 38 | class LinkedQueue 39 | { 40 | private: 41 | LinkNode* first, * front, * rear; 42 | public: 43 | LinkedQueue(); 44 | 45 | ~LinkedQueue(); 46 | 47 | void makeEmpty(); 48 | 49 | bool isEmpty() 50 | { 51 | return first->link == NULL; 52 | } 53 | 54 | bool Push(const T x); 55 | 56 | T Front() 57 | { 58 | return front->getData(); 59 | } 60 | 61 | void Pop(); 62 | }; 63 | 64 | template 65 | LinkedQueue::LinkedQueue() 66 | { 67 | first = new LinkNode; 68 | front = rear = NULL; 69 | } 70 | 71 | template 72 | LinkedQueue::~LinkedQueue() 73 | { 74 | makeEmpty(); 75 | delete first; 76 | } 77 | 78 | template 79 | void LinkedQueue::makeEmpty() 80 | { 81 | LinkNode* current; 82 | while (first->link != NULL) 83 | { 84 | current = first->link; 85 | first->link = current->link; 86 | delete current; 87 | } 88 | } 89 | 90 | template 91 | bool LinkedQueue::Push(const T x) 92 | { 93 | LinkNode* current = new LinkNode(x); 94 | if (current == NULL) 95 | { 96 | // 97 | return false; 98 | } 99 | if (front == NULL) 100 | { 101 | front = rear = current; 102 | first->link = front; 103 | return true; 104 | } 105 | rear->link = current; 106 | rear = current; 107 | return true; 108 | } 109 | 110 | template 111 | void LinkedQueue::Pop() 112 | { 113 | LinkNode* p = front; 114 | front = front->link; 115 | first->link = front; 116 | delete p; 117 | } 118 | 119 | int main() 120 | { 121 | int n; 122 | while (1) 123 | { 124 | cin >> n; 125 | if (cin.good() && (n > 0 && n <= 1000)) 126 | { 127 | break; 128 | } 129 | if (cin.fail()) 130 | { 131 | cin.clear(); 132 | cin.ignore(1024, '\n'); 133 | } 134 | cout << "error,input again" << endl; 135 | } 136 | LinkedQueue Qa, Qb; 137 | 138 | for (int i = 0; i < n; i++) 139 | { 140 | int x; 141 | cin >> x; 142 | if (x % 2) 143 | { 144 | Qa.Push(x); 145 | } 146 | else 147 | { 148 | Qb.Push(x); 149 | } 150 | } 151 | int tg = 0; 152 | while (!Qa.isEmpty() || !Qb.isEmpty()) 153 | { 154 | int a = 2, b = 1;//A窗口每处理完2个顾客是,B窗口处理完1个顾客 155 | while (a-- && !Qa.isEmpty()) 156 | { 157 | int x = Qa.Front(); 158 | Qa.Pop(); 159 | if (tg) 160 | { 161 | cout << " "; 162 | } 163 | cout << x ; 164 | tg = 1; 165 | } 166 | while (b-- && !Qb.isEmpty()) 167 | { 168 | int x = Qb.Front(); 169 | Qb.Pop(); 170 | if (tg) 171 | { 172 | cout << " "; 173 | } 174 | cout << x; 175 | tg = 1; 176 | } 177 | } 178 | cout< 3 | #include 4 | using namespace std; 5 | #define DefaultSize 20 6 | 7 | class String 8 | { 9 | friend istream& operator>>(istream&, String& Str); 10 | friend ostream& operator <<(ostream&, const String& Str); 11 | private: 12 | int currentLength; 13 | int maxLength; 14 | char* ch; 15 | public: 16 | String(int size = DefaultSize); 17 | 18 | String(const char* init); 19 | 20 | String(const String& ob); 21 | 22 | ~String() 23 | { 24 | delete[]ch; 25 | } 26 | 27 | int length()const 28 | { 29 | return currentLength; 30 | } 31 | 32 | String operator()(int pos, int length); 33 | 34 | bool operator==(const String& ob) 35 | { 36 | return strcmp(ch, ob.ch) == 0; 37 | } 38 | 39 | bool operator!=(const String& ob) 40 | { 41 | return strcmp(ch, ob.ch) != 0; 42 | } 43 | 44 | String& operator=(const String& ob); 45 | 46 | String& operator+=(const String& ob); 47 | 48 | String& operator+=(const char c); 49 | 50 | char& operator[](int num)const; 51 | 52 | int fastFind(String& pat, int pos); 53 | 54 | 55 | }; 56 | String::String(int size) 57 | { 58 | maxLength = size; 59 | ch = new char[size + 1]; 60 | if (ch == NULL) 61 | { 62 | cerr << "Allocation Error!\n"; 63 | } 64 | currentLength = 0; ch[0] = '\0'; 65 | } 66 | String::String(const char* init) 67 | { 68 | int length = strlen(init); 69 | maxLength = length > DefaultSize ? length : DefaultSize; 70 | ch = new char[maxLength + 1]; 71 | if (ch == NULL) 72 | { 73 | cerr << "Allocation Error!\n"; 74 | } 75 | currentLength = length; 76 | strcpy(ch, init); 77 | } 78 | String::String(const String& ob) 79 | { 80 | int length = ob.currentLength; 81 | maxLength = length > DefaultSize ? length : DefaultSize; 82 | ch = new char[maxLength + 1]; 83 | if (ch == NULL) 84 | { 85 | cerr << "Allocation Error!\n"; 86 | } 87 | currentLength = length; 88 | strcpy(ch, ob.ch); 89 | } 90 | 91 | String String::operator()(int pos, int length) 92 | { 93 | String s; 94 | if (pos >= 0 && pos < currentLength && length>0 && pos + length - 1 < currentLength) 95 | { 96 | for (int i = 0; i < length; i++) 97 | { 98 | s.ch[i] = ch[pos + i]; 99 | } 100 | s.ch[length] = '\0'; 101 | } 102 | s.currentLength = strlen(s.ch); 103 | return s; 104 | } 105 | 106 | String& String:: operator=(const String& ob) 107 | { 108 | if (this != &ob) 109 | { 110 | delete[]ch; 111 | ch = new char[ob.maxLength + 1]; 112 | maxLength = ob.maxLength; 113 | currentLength = ob.currentLength; 114 | strcpy(ch, ob.ch); 115 | } 116 | return *this; 117 | } 118 | String& String::operator+=(const String& ob) 119 | { 120 | char* temp = ch; 121 | int length = currentLength + ob.currentLength; 122 | maxLength = maxLength > length ? maxLength : length; 123 | ch = new char[maxLength + 1]; 124 | if (ch == NULL) 125 | { 126 | cerr << "Allocation Error!\n"; 127 | exit(1); 128 | } 129 | strcpy(ch, temp); 130 | strcat(ch, ob.ch); 131 | currentLength = length; 132 | delete[]temp; 133 | return *this; 134 | } 135 | 136 | String& String::operator+=(const char c) 137 | { 138 | char* temp = ch; 139 | int length = currentLength + 1; 140 | maxLength = maxLength > length ? maxLength : length; 141 | ch = new char[maxLength + 1]; 142 | if (ch == NULL) 143 | { 144 | cerr << "Allocation Error!\n"; 145 | exit(1); 146 | } 147 | ch[currentLength] = c; 148 | currentLength = length; 149 | delete[]temp; 150 | return *this; 151 | } 152 | char& String::operator[](int num)const 153 | { 154 | if (num < 0 || num >= currentLength) 155 | { 156 | cerr << "Out of Range!\n"; 157 | exit(1); 158 | } 159 | return ch[num]; 160 | } 161 | int String::fastFind(String& pat, int pos) 162 | { 163 | int pLength = pat.length(); 164 | int* next = new int[pLength]; 165 | next[0] = -1; 166 | int j = 0, k = -1; 167 | while (j < pLength - 1) 168 | { 169 | if (k == -1 || ch[j] == ch[k]) 170 | { 171 | j++; k++; 172 | next[j] = k; 173 | } 174 | else 175 | { 176 | k = next[k]; 177 | } 178 | } 179 | int tPos = 0; 180 | int pPos = 0; 181 | while (tPos < currentLength && pPos < pLength) 182 | { 183 | if (pPos == -1 || ch[tPos] == ch[pPos]) 184 | { 185 | tPos++; 186 | pPos++; 187 | } 188 | else 189 | { 190 | pPos = next[pPos]; 191 | } 192 | } 193 | delete[]next; 194 | if (pPos < pLength) 195 | { 196 | return -1; 197 | } 198 | else 199 | { 200 | return tPos - pLength; 201 | } 202 | } 203 | istream& operator>>(istream& cin, String& Str) 204 | { 205 | char* init = new char[DefaultSize * 5]; 206 | cin >> init; 207 | int length = strlen(init); 208 | delete[]Str.ch; 209 | Str.ch = new char[length + 1]; 210 | Str.currentLength = length; 211 | strcpy(Str.ch, init); 212 | 213 | return cin; 214 | } 215 | 216 | ostream& operator<<(ostream& cout, const String& Str) 217 | { 218 | cout << Str.ch; 219 | 220 | return cout; 221 | } 222 | 223 | template 224 | class LinkNode 225 | { 226 | private: 227 | T data; 228 | 229 | public: 230 | LinkNode* link; 231 | //已知Link初始化LinkNode 232 | LinkNode(LinkNode* ptr = NULL) 233 | { 234 | link = ptr; 235 | } 236 | //已知data Link 初始化LinkNode 237 | LinkNode(const T item, LinkNode* ptr = NULL) 238 | { 239 | data = item; 240 | link = ptr; 241 | } 242 | //得到LinkNode的Data 243 | T getData() 244 | { 245 | return data; 246 | } 247 | //设置LinkNode的Data 248 | void setData(const T item) 249 | { 250 | data = item; 251 | } 252 | 253 | }; 254 | 255 | template 256 | class LinkedQueue 257 | { 258 | private: 259 | LinkNode* first, * front, * rear; 260 | public: 261 | LinkedQueue(); 262 | 263 | ~LinkedQueue(); 264 | 265 | void makeEmpty(); 266 | 267 | bool isEmpty() 268 | { 269 | return first->link == NULL; 270 | } 271 | 272 | bool Push(const T x); 273 | 274 | T Front() 275 | { 276 | return front->getData(); 277 | } 278 | 279 | void Pop(); 280 | }; 281 | 282 | template 283 | LinkedQueue::LinkedQueue() 284 | { 285 | first = new LinkNode; 286 | front = rear = NULL; 287 | } 288 | 289 | template 290 | LinkedQueue::~LinkedQueue() 291 | { 292 | makeEmpty(); 293 | delete first; 294 | } 295 | 296 | template 297 | void LinkedQueue::makeEmpty() 298 | { 299 | LinkNode* current; 300 | while (first->link != NULL) 301 | { 302 | current = first->link; 303 | first->link = current->link; 304 | delete current; 305 | } 306 | } 307 | 308 | template 309 | bool LinkedQueue::Push(const T x) 310 | { 311 | LinkNode* current = new LinkNode(x); 312 | if (current == NULL) 313 | { 314 | cerr << "Allocation Error!!"; 315 | return false; 316 | } 317 | if (front == NULL) 318 | { 319 | front = rear = current; 320 | first->link = front; 321 | return true; 322 | } 323 | rear->link = current; 324 | rear = current; 325 | return true; 326 | } 327 | 328 | template 329 | void LinkedQueue::Pop() 330 | { 331 | LinkNode* p = front; 332 | front = front->link; 333 | first->link = front; 334 | delete p; 335 | } 336 | 337 | template 338 | class Tree; 339 | 340 | template 341 | class TreeNode 342 | { 343 | friend class Tree; 344 | private: 345 | T data; 346 | TreeNode* firstChild, * nextSibiling; 347 | TreeNode(T value, TreeNode* fc=NULL, TreeNode* ns=NULL) 348 | :data(value), firstChild(fc), nextSibiling(ns) {}; 349 | public: 350 | T getData() 351 | { 352 | return data; 353 | } 354 | void setData(T x) 355 | { 356 | data = x; 357 | } 358 | }; 359 | 360 | template 361 | class Tree 362 | { 363 | private: 364 | TreeNode* root, * current; 365 | //从一个节点开始找,找到置current 366 | bool find(TreeNode* p, T value); 367 | //移除一个子树 368 | void removeSubtree(TreeNode*& p); 369 | //寻找双亲并置其为current 370 | bool findParent(TreeNode* t, T x); 371 | public: 372 | Tree() 373 | { 374 | root = current = NULL; 375 | } 376 | ~Tree() 377 | { 378 | removeSubtree(root); 379 | } 380 | //置root为current 381 | bool Root() 382 | { 383 | if (root == NULL) 384 | { 385 | return false; 386 | } 387 | else 388 | { 389 | current = root; 390 | return true; 391 | } 392 | } 393 | //设置root 394 | void setRoot(T x) 395 | { 396 | TreeNode* p = new TreeNode(x); 397 | current=root = p; 398 | } 399 | //现在指向的是否是根 400 | bool isRoot() 401 | { 402 | return root == current; 403 | } 404 | //置current为目前current的双亲 405 | bool parent(T x) 406 | { 407 | return findParent(root, x); 408 | } 409 | //置current为目前current的长子 410 | bool firstChild(); 411 | //置current为目前current兄弟 412 | bool nextSibling(); 413 | //寻找值为x的节点,设为current 414 | bool find(const T x) 415 | { 416 | return find(root, x); 417 | } 418 | //添加孩子 419 | bool addChild(T parent, T child); 420 | //改变current的值 421 | void changeData(T x) 422 | { 423 | current->setData(x); 424 | } 425 | //移除一个子树 426 | void removeSubtree() 427 | { 428 | removeSubtree(current->firstChild); 429 | } 430 | //访问current的数据 431 | T visit()const 432 | { 433 | return current->getData(); 434 | } 435 | }; 436 | 437 | template 438 | void Tree::removeSubtree(TreeNode*& p) 439 | { 440 | if (p == NULL) 441 | { 442 | return; 443 | } 444 | TreeNode* q = p->firstChild,*t; 445 | while (q != NULL) 446 | { 447 | t = q->nextSibiling; 448 | removeSubtree(q); 449 | q =t; 450 | } 451 | delete p; 452 | p = NULL; 453 | } 454 | 455 | template 456 | bool Tree::firstChild() 457 | { 458 | if (current->firstChild != NULL) 459 | { 460 | current = current->firstChild; 461 | return true; 462 | } 463 | else 464 | { 465 | return false; 466 | } 467 | } 468 | 469 | template 470 | bool Tree::nextSibling() 471 | { 472 | if (current->nextSibiling != NULL) 473 | { 474 | current = current->nextSibiling; 475 | return true; 476 | } 477 | else 478 | { 479 | return false; 480 | } 481 | } 482 | 483 | template 484 | bool Tree::addChild(T parent, T child) 485 | { 486 | if (find(parent) == false) 487 | { 488 | return false; 489 | } 490 | TreeNode* p = new TreeNode(child); 491 | if (firstChild() == false) 492 | { 493 | TreeNode* p = new TreeNode(child); 494 | current->firstChild = p; 495 | } 496 | else 497 | { 498 | while (nextSibling() == true); 499 | current->nextSibiling = p; 500 | } 501 | return true; 502 | } 503 | 504 | template 505 | bool Tree::findParent(TreeNode* p, T x) 506 | { 507 | TreeNode* q = p->firstChild; 508 | while (q != NULL) 509 | { 510 | if (q->getData() == x) 511 | { 512 | current = p; 513 | return true; 514 | } 515 | if (findParent(q, x)) 516 | { 517 | return true; 518 | } 519 | q = q->nextSibiling; 520 | } 521 | return false; 522 | } 523 | 524 | template 525 | bool Tree::find(TreeNode* p, T value) 526 | { 527 | if (p->data == value) 528 | { 529 | current = p; 530 | return true; 531 | } 532 | TreeNode* q = p->firstChild; 533 | while (q != NULL) 534 | { 535 | if (find(q, value)) 536 | { 537 | return true; 538 | } 539 | q = q->nextSibiling; 540 | } 541 | return false; 542 | } 543 | 544 | 545 | class Genealogy 546 | { 547 | private: 548 | Tree gTree; 549 | void menu(); 550 | public: 551 | Genealogy(); 552 | //输出族长 553 | void getHeader(); 554 | //输出s的所有子孙,按第几代分类输出 555 | void getChildren(String s); 556 | //组建家庭 557 | void bulidfamily(); 558 | //添加子女 559 | void addChild(); 560 | //解散家庭 561 | void disbandfamily(); 562 | //改变姓名 563 | void changeName(); 564 | //使用 565 | void use(); 566 | }; 567 | 568 | void Genealogy::getHeader() 569 | { 570 | if (gTree.Root() == false) 571 | { 572 | cout << "此家族已不存在!" << endl;; 573 | } 574 | cout << "此家谱的祖先是:"; 575 | cout < q; 604 | q.Push(child(0, gTree.visit())); 605 | int tag = 0; 606 | while (!q.isEmpty()) 607 | { 608 | child tc = q.Front(); 609 | q.Pop(); 610 | int gen = tc.gen; 611 | if (gen > nowgen) 612 | { 613 | nowgen = gen; 614 | if (tag) 615 | { 616 | cout << "\n"; 617 | } 618 | cout << s << "的第"; 619 | if (nowgen / 10) 620 | { 621 | cout << str[nowgen / 10 - 1]; 622 | } 623 | if (1 + (nowgen - 1) % 10) 624 | { 625 | cout << str[(nowgen - 1) % 10]; 626 | } 627 | cout << "代子孙是:"; 628 | } 629 | if (tag) 630 | { 631 | cout << tc.name << " "; 632 | } 633 | tag = 1; 634 | gTree.find(tc.name); 635 | if (gTree.firstChild() == true) 636 | { 637 | q.Push(child(gen+1, gTree.visit())); 638 | while (gTree.nextSibling() == true) 639 | { 640 | q.Push(child(gen, gTree.visit())); 641 | } 642 | } 643 | } 644 | cout << "\n"; 645 | } 646 | 647 | void Genealogy::bulidfamily() 648 | { 649 | cout << "请输入要建立家庭的人的姓名:"; 650 | String s; 651 | cin >> s; 652 | cin.ignore(1024, '\n'); 653 | if (gTree.find(s) == false) 654 | { 655 | cout << "家族中无此人!" << endl; 656 | return; 657 | } 658 | if (gTree.firstChild() == true) 659 | { 660 | cout << "此人已建立家庭!" << endl; 661 | return; 662 | } 663 | int num = 0; 664 | while (1) 665 | { 666 | cout << "请输入" << s << "的儿女人数:"; 667 | cin >> num; 668 | if (cin.good() && num > 0) 669 | { 670 | break; 671 | } 672 | cout << "输入有误,请重新输入" << endl; 673 | if (cin.fail()) 674 | { 675 | cin.clear(); 676 | cin.ignore(1024, '\n'); 677 | } 678 | } 679 | cin.ignore(1024, '\n'); 680 | cout << "请依次输入" << s << "的儿女的姓名:"; 681 | for (int i = 0; i < num; i++) 682 | { 683 | String t; 684 | cin >> t; 685 | gTree.addChild(s, t); 686 | } 687 | cin.ignore(1024, '\n'); 688 | getChildren(s); 689 | } 690 | 691 | void Genealogy::addChild() 692 | { 693 | cout << "请输入要添加儿子(或女儿)的人的姓名:"; 694 | String s; 695 | cin >> s; 696 | if (gTree.find(s) == false) 697 | { 698 | cout << "家族中无此人!" << endl; 699 | return; 700 | } 701 | if (gTree.firstChild() == false) 702 | { 703 | cout << "此人还没有组建家庭!" << endl; 704 | return; 705 | } 706 | cin.ignore(1024, '\n'); 707 | gTree.find(s); 708 | cout << "请输入" << s << "要添加儿子(或女儿)的人的姓名:"; 709 | String t; 710 | cin >> t; 711 | cin.ignore(1024, '\n'); 712 | gTree.addChild(s, t); 713 | getChildren(s); 714 | } 715 | 716 | void Genealogy::disbandfamily() 717 | { 718 | cout << "请输入要解散家庭的人的姓名:"; 719 | String s; 720 | cin >> s; 721 | if (gTree.find(s) == false) 722 | { 723 | cout << "家族中无此人!" << endl; 724 | return; 725 | } 726 | 727 | cin.ignore(1024, '\n'); 728 | cout << "要解散家庭的人是:" << s << endl; 729 | getChildren(s); 730 | gTree.find(s); 731 | 732 | gTree.removeSubtree(); 733 | } 734 | 735 | void Genealogy::changeName() 736 | { 737 | cout << "请输入要更改姓名的人的目前姓名:"; 738 | String s; 739 | cin >> s; 740 | if (gTree.find(s) == false) 741 | { 742 | cout << "家族中无此人!" << endl; 743 | return; 744 | } 745 | cin.ignore(1024, '\n'); 746 | cout << "请输入更改后的姓名:"; 747 | String t; 748 | cin >> t; 749 | cin.ignore(1024, '\n'); 750 | gTree.changeData(t); 751 | cout << s << "已更名为" << t << endl; 752 | } 753 | 754 | void Genealogy::use() 755 | { 756 | int op; 757 | while (1) 758 | { 759 | cout << "\n请选择要执行的操作:"; 760 | op = getchar(); 761 | cin.ignore(1024, '\n'); 762 | if (op < 'A' || op>'E') 763 | { 764 | cout << "没有此项操作!" << endl; 765 | continue; 766 | } 767 | if (op == 'E') 768 | { 769 | break; 770 | } 771 | switch (op) 772 | { 773 | case 'A': 774 | bulidfamily(); 775 | break; 776 | case 'B': 777 | addChild(); 778 | break; 779 | case 'C': 780 | disbandfamily(); 781 | break; 782 | case 'D': 783 | changeName(); 784 | break; 785 | } 786 | } 787 | cout << "谢谢使用!" << endl; 788 | } 789 | 790 | Genealogy::Genealogy() 791 | { 792 | menu(); 793 | cout << "首先建立一个家谱!\n"; 794 | cout << "请输入祖先的姓名:"; 795 | String s; 796 | cin >> s; 797 | cin.ignore(1024, '\n'); 798 | gTree.setRoot(s); 799 | getHeader(); 800 | } 801 | 802 | 803 | void Genealogy::menu() 804 | { 805 | cout << "** 家谱管理系统 **" << endl; 806 | cout << "====================================================" << endl; 807 | cout << "** 请选择要执行的操作 : **" << endl; 808 | cout << "** A --- 完善家庭 **" << endl; 809 | cout << "** B --- 添加家庭成员 **" << endl; 810 | cout << "** C --- 解散局部家庭 **" << endl; 811 | cout << "** D --- 更改家庭成员姓名 **" << endl; 812 | cout << "** E --- 退出程序 **" << endl; 813 | cout << "====================================================" << endl; 814 | } 815 | 816 | int main() 817 | { 818 | Genealogy g; 819 | g.use(); 820 | return 0; 821 | } 822 | -------------------------------------------------------------------------------- /06_2152085_孙亦菲/06_2152085_孙亦菲.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/06_2152085_孙亦菲/06_2152085_孙亦菲.doc -------------------------------------------------------------------------------- /06_2152085_孙亦菲/06_2152085_孙亦菲.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/06_2152085_孙亦菲/06_2152085_孙亦菲.exe -------------------------------------------------------------------------------- /06_2152085_孙亦菲/06_2152085_孙亦菲_Linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/06_2152085_孙亦菲/06_2152085_孙亦菲_Linux -------------------------------------------------------------------------------- /06_2152085_孙亦菲/makefile: -------------------------------------------------------------------------------- 1 | 06_2152085_孙亦菲_Linux:06_2152085_孙亦菲.o 2 | g++ *.o -o $@ 3 | %.o:%.c 4 | g++ -c $< -o $@ 5 | 6 | clean: 7 | rm -f *.o 06_2152085_孙亦菲_Linux 8 | 9 | -------------------------------------------------------------------------------- /07_2152085_孙亦菲/07_2152085_孙亦菲.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define DefaultSize 10005 4 | 5 | template 6 | class PQueue 7 | { 8 | private: 9 | T* arr; 10 | int maxSize; 11 | int currentSize; 12 | void shiftdown(int k); 13 | void shiftup(int k); 14 | public: 15 | PQueue(int sz = 10005); 16 | ~PQueue(); 17 | bool Push(T x); 18 | bool Pop(); 19 | bool isEmpty()const 20 | { 21 | return currentSize == 0; 22 | } 23 | T Front()const 24 | { 25 | return arr[0]; 26 | } 27 | int Size()const 28 | { 29 | return currentSize; 30 | } 31 | void show() 32 | { 33 | for (int i = 0; i < currentSize; i++) 34 | { 35 | cout << arr[i] << " "; 36 | } 37 | cout << endl; 38 | } 39 | }; 40 | 41 | template 42 | PQueue::PQueue(int sz) 43 | { 44 | arr = new T[sz]; 45 | currentSize = 0; 46 | maxSize = sz; 47 | } 48 | 49 | template 50 | PQueue::~PQueue() 51 | { 52 | delete[]arr; 53 | } 54 | 55 | template 56 | void PQueue::shiftdown(int k) 57 | { 58 | T temp = arr[k]; 59 | for (int i = 2 * k + 1; i < currentSize; i = 2 * i + 1) 60 | { 61 | if (i + 1 < currentSize && arr[i + 1] < arr[i]) 62 | { 63 | i++; 64 | } 65 | if (temp < arr[i]) 66 | { 67 | break; 68 | } 69 | arr[k] = arr[i]; 70 | k = i; 71 | } 72 | arr[k] = temp; 73 | } 74 | 75 | template 76 | void PQueue::shiftup(int k) 77 | { 78 | T temp = arr[k]; 79 | int i = k; 80 | while (i > 0) 81 | { 82 | i = (i - 1) / 2; 83 | if (temp > arr[i]) 84 | { 85 | break; 86 | } 87 | arr[k] = arr[i]; 88 | k = i; 89 | } 90 | arr[k] = temp; 91 | } 92 | 93 | template 94 | bool PQueue::Push(T x) 95 | { 96 | if (currentSize + 1 > maxSize) 97 | { 98 | return false; 99 | } 100 | arr[currentSize] = x; 101 | currentSize++; 102 | shiftup(currentSize - 1); 103 | return true; 104 | } 105 | 106 | template 107 | bool PQueue::Pop() 108 | { 109 | if (currentSize == 0) 110 | { 111 | return false; 112 | } 113 | arr[0] = arr[currentSize - 1]; 114 | currentSize--; 115 | shiftdown(0); 116 | return true; 117 | } 118 | 119 | int main() 120 | { 121 | int N; 122 | while (1) 123 | { 124 | cin >> N; 125 | if (cin.good() && N > 0) 126 | { 127 | break; 128 | } 129 | if (cin.fail()) 130 | { 131 | cin.clear(); 132 | cin.ignore(1024, '\n'); 133 | } 134 | cout << "error,input again" << endl; 135 | } 136 | PQueue pQ; 137 | for (int i = 0; i < N; i++) 138 | { 139 | int temp; 140 | cin >> temp; 141 | pQ.Push(temp); 142 | } 143 | int ans = 0; 144 | while (pQ.Size() > 1) 145 | { 146 | int t1 = pQ.Front(); 147 | pQ.Pop(); 148 | int t2 = pQ.Front(); 149 | pQ.Pop(); 150 | ans += t1 + t2; 151 | pQ.Push(t1 + t2); 152 | } 153 | cout << ans< 2 | using namespace std; 3 | #define DefaultVertices 100 4 | 5 | template 6 | class PQueue 7 | { 8 | private: 9 | T* arr; 10 | int maxSize; 11 | int currentSize; 12 | void shiftdown(int k); 13 | void shiftup(int k); 14 | public: 15 | PQueue(int sz = 10005); 16 | ~PQueue(); 17 | bool Push(T x); 18 | bool Pop(); 19 | bool isEmpty()const 20 | { 21 | return currentSize == 0; 22 | } 23 | T Front()const 24 | { 25 | return arr[0]; 26 | } 27 | int Size()const 28 | { 29 | return currentSize; 30 | } 31 | }; 32 | 33 | template 34 | PQueue::PQueue(int sz) 35 | { 36 | arr = new T[sz]; 37 | currentSize = 0; 38 | maxSize = sz; 39 | } 40 | 41 | template 42 | PQueue::~PQueue() 43 | { 44 | delete[]arr; 45 | } 46 | 47 | template 48 | void PQueue::shiftdown(int k) 49 | { 50 | T temp = arr[k]; 51 | for (int i = 2 * k + 1; i < currentSize; i = 2 * i + 1) 52 | { 53 | if (i + 1 < currentSize && arr[i + 1] < arr[i]) 54 | { 55 | i++; 56 | } 57 | if (temp < arr[i]) 58 | { 59 | break; 60 | } 61 | arr[k] = arr[i]; 62 | k = i; 63 | } 64 | arr[k] = temp; 65 | } 66 | 67 | template 68 | void PQueue::shiftup(int k) 69 | { 70 | T temp = arr[k]; 71 | int i = k; 72 | while (i > 0) 73 | { 74 | i = (i - 1) / 2; 75 | if (arr[i] 86 | bool PQueue::Push(T x) 87 | { 88 | if (currentSize + 1 > maxSize) 89 | { 90 | return false; 91 | } 92 | arr[currentSize] = x; 93 | currentSize++; 94 | shiftup(currentSize - 1); 95 | return true; 96 | } 97 | 98 | template 99 | bool PQueue::Pop() 100 | { 101 | if (currentSize == 0) 102 | { 103 | return false; 104 | } 105 | arr[0] = arr[currentSize - 1]; 106 | currentSize--; 107 | shiftdown(0); 108 | return true; 109 | } 110 | 111 | template 112 | class Graph 113 | { 114 | public: 115 | Graph(int sz = DefaultVertices) {}; 116 | virtual ~Graph() {}; 117 | bool GraphEmpty()const 118 | { 119 | return numEdges == 0; 120 | } 121 | bool GraphFull()const 122 | { 123 | return numVertices == maxVertices || numEdges == (maxVertices) * (maxVertices - 1) / 2; 124 | } 125 | int NumberOfVertices() 126 | { 127 | return numVertices; 128 | } 129 | int NumberOfEdges() 130 | { 131 | return numEdges; 132 | } 133 | 134 | virtual int getFirstNeighbor(int v) = 0; 135 | virtual int getNextNeighbor(int v, int w) = 0; 136 | virtual bool insertVertex(const T vertex) = 0; 137 | virtual bool insertEdge(int v1, int v2, E cost) = 0; 138 | //virtual bool removeVertex(int v); 139 | virtual bool removeEdge(int v1, int v2) = 0; 140 | virtual int getVertexPos(T vertex) = 0; 141 | 142 | protected: 143 | int maxVertices; 144 | int numEdges; 145 | int numVertices; 146 | }; 147 | 148 | 149 | 150 | template 151 | class Edge 152 | { 153 | public: 154 | int fr; 155 | int dest; 156 | E cost; 157 | Edge* link; 158 | Edge() {}; 159 | Edge(int num, E weight) :dest(num), cost(weight), link(NULL) {}; 160 | bool operator!=(Edge& R)const 161 | { 162 | return dest != R.dest; 163 | } 164 | bool operator<(Edge& R)const 165 | { 166 | return this->cost < R.cost; 167 | } 168 | }; 169 | 170 | template 171 | class Vertex 172 | { 173 | public: 174 | T data; 175 | Edge* adj; 176 | }; 177 | 178 | template 179 | class Graphlnk :public Graph 180 | { 181 | public: 182 | Graphlnk(int sz = DefaultVertices); 183 | ~Graphlnk(); 184 | //第一个邻接顶点 185 | int getFirstNeighbor(int v); 186 | //下一个邻接顶点 187 | int getNextNeighbor(int v, int w); 188 | //编号为i的顶点的值 189 | T getValue(int i); 190 | //顶点v1,v2间边的权值 191 | E getWeight(int v1, int v2); 192 | //指向顶点v1,v2间边的指针 193 | Edge* getEdge(int v1, int v2); 194 | //插入顶点 195 | bool insertVertex(T vertex); 196 | //无向图移除顶点 197 | bool undirremoveVertex(int v); 198 | //有向图移除顶点 199 | bool dirremoveVertex(int v); 200 | //插入边 201 | bool insertEdge(int v1, int v2, E weight); 202 | //移除边 203 | bool removeEdge(int v1, int v2); 204 | //得到某元素在图中的位置 205 | int getVertexPos(const T vertex) 206 | { 207 | for (int i = 0; i < this->maxVertices; i++) 208 | { 209 | if (NodeTable[i].data == vertex) 210 | { 211 | return i; 212 | } 213 | } 214 | return -1; 215 | } 216 | private: 217 | Vertex* NodeTable; 218 | }; 219 | 220 | 221 | template 222 | Graphlnk::Graphlnk(int sz) 223 | { 224 | this->maxVertices = sz; this->numVertices = 0; this->numEdges = 0; 225 | NodeTable = new Vertex[this->maxVertices]; 226 | for (int i = 0; i < this->maxVertices; i++) 227 | { 228 | NodeTable[i].adj = NULL; 229 | } 230 | } 231 | 232 | template 233 | Graphlnk::~Graphlnk() 234 | { 235 | for (int i = 0; i < this->maxVertices; i++) 236 | { 237 | Edge* p = NodeTable[i].adj; 238 | 239 | while (p != NULL) 240 | { 241 | NodeTable[i].adj = p->link; 242 | delete p; 243 | p = NodeTable[i].adj; 244 | } 245 | } 246 | delete[]NodeTable; 247 | } 248 | 249 | template 250 | T Graphlnk::getValue(int i) 251 | { 252 | return NodeTable[i].data; 253 | } 254 | 255 | template 256 | int Graphlnk::getFirstNeighbor(int v) 257 | { 258 | if (v < 0 || v >= this->numVertices) 259 | { 260 | return -1; 261 | } 262 | if (NodeTable[v].adj != NULL) 263 | { 264 | return NodeTable[v].adj->dest; 265 | } 266 | else 267 | { 268 | return -1; 269 | } 270 | } 271 | 272 | template 273 | int Graphlnk::getNextNeighbor(int v, int w) 274 | { 275 | if (v < 0 || v >= this->numVertices) 276 | { 277 | return -1; 278 | } 279 | Edge* p = NodeTable[v].adj; 280 | while (p != NULL && p->dest != w) 281 | { 282 | p = p->link; 283 | } 284 | if (p != NULL && p->link != NULL) 285 | { 286 | return p->link->dest; 287 | } 288 | return -1; 289 | } 290 | 291 | template 292 | Edge* Graphlnk::getEdge(int v1, int v2) 293 | { 294 | Edge < T, E>* p = NodeTable[v1].adj; 295 | while (p != NULL && p->dest != v2) 296 | { 297 | p = p->link; 298 | } 299 | return p; 300 | } 301 | 302 | template 303 | E Graphlnk::getWeight(int v1, int v2) 304 | { 305 | Edge < T, E>* p = NodeTable[v1].adj; 306 | while (p != NULL && p->dest != v2) 307 | { 308 | p = p->link; 309 | } 310 | if (p != NULL) 311 | { 312 | return p->cost; 313 | } 314 | return 0; 315 | } 316 | 317 | template 318 | bool Graphlnk::insertVertex(T vertex) 319 | { 320 | if (this->numVertices == this->maxVertices) 321 | { 322 | return false; 323 | } 324 | for (int i = 0; i < this->numVertices; i++) 325 | { 326 | if (vertex == NodeTable[i].data) 327 | { 328 | return false; 329 | } 330 | } 331 | NodeTable[this->numVertices].data = vertex; 332 | this->numVertices++; 333 | return true; 334 | } 335 | 336 | /*无向图的办法*/ 337 | template 338 | bool Graphlnk::undirremoveVertex(int v) 339 | { 340 | if (v < 0 || v >= this->numVertices) 341 | { 342 | return false; 343 | } 344 | Edge* p, * s, * t; 345 | int k; 346 | while (NodeTable[v].adj != NULL) 347 | { 348 | p = NodeTable[v].adj; k = p->dest; 349 | s = NodeTable[k].adj; 350 | t = NULL; 351 | while (s != NULL && s->dest != v) 352 | { 353 | t = s; s = s->link; 354 | } 355 | if (s != NULL) 356 | { 357 | if (t == NULL) 358 | { 359 | NodeTable[k].adj = s->link; 360 | } 361 | else 362 | { 363 | t->link = s->link; 364 | } 365 | delete s; 366 | } 367 | NodeTable[v].adj = p->link; 368 | delete p; 369 | this->numEdges--; 370 | } 371 | this->numVertices--; 372 | NodeTable[v].data = NodeTable[this->numVertices].data; 373 | p = NodeTable[v].adj = NodeTable[this->numVertices].adj; 374 | while (p != NULL) 375 | { 376 | s = NodeTable[p->dest].adj; 377 | while (s != NULL) 378 | { 379 | if (s->dest == this->numVertices) 380 | { 381 | s->dest = v; 382 | break; 383 | } 384 | else 385 | { 386 | s = s->link; 387 | } 388 | } 389 | p = p->link; 390 | } 391 | return true; 392 | } 393 | 394 | /*有向图的办法*/ 395 | template 396 | bool Graphlnk::dirremoveVertex(int v) 397 | { 398 | if (v < 0 || v >= this->numVertices) 399 | { 400 | return false; 401 | } 402 | Edge* p, * q; 403 | p = NodeTable[v].adj; 404 | while (p != NULL) 405 | { 406 | NodeTable[v].adj = p->link; 407 | delete p; 408 | p = NodeTable[v].adj; 409 | } 410 | for (int i = 0; i < this->numVertices; i++) 411 | { 412 | if (i == v) 413 | { 414 | continue; 415 | } 416 | p = NodeTable[i].adj; 417 | q = p; 418 | while (p != NULL) 419 | { 420 | if (p->dest == v) 421 | { 422 | q->link = p->link; 423 | delete p; 424 | p = q->link; 425 | } 426 | else 427 | { 428 | q = p; 429 | p = p->link; 430 | } 431 | } 432 | } 433 | this->numVertices--; 434 | NodeTable[v].data = NodeTable[this->numVertices].data; 435 | p = NodeTable[v].adj = NodeTable[this->numVertices].adj; 436 | while (p != NULL) 437 | { 438 | Edge* s = NodeTable[p->dest].adj; 439 | while (s != NULL) 440 | { 441 | if (s->dest == this->numVertices) 442 | { 443 | s->dest = v; 444 | break; 445 | } 446 | else 447 | { 448 | s = s->link; 449 | } 450 | } 451 | p = p->link; 452 | } 453 | return true; 454 | } 455 | 456 | template 457 | bool Graphlnk::insertEdge(int v1, int v2, E weight) 458 | { 459 | if (v1 < 0 || v1 >= this->numVertices || v2 < 0 || v2 >= this->numVertices) 460 | { 461 | return false; 462 | } 463 | Edge* p = NodeTable[v1].adj; 464 | while (p != NULL && p->dest != v2) 465 | { 466 | p = p->link; 467 | } 468 | if (p != NULL) 469 | { 470 | return false; 471 | } 472 | p = new Edge; 473 | p->dest = v2; p->cost = weight; p->link = NodeTable[v1].adj; NodeTable[v1].adj = p; 474 | this->numEdges++; 475 | return true; 476 | } 477 | 478 | template 479 | bool Graphlnk::removeEdge(int v1, int v2) 480 | { 481 | if (v1 < 0 || v1 >= this->numVertices || v2 < 0 || v2 >= this->numVertices) 482 | { 483 | return false; 484 | } 485 | Edge* p = NodeTable[v1].adj, * q = NULL; 486 | while (p != NULL && p->dest != v2) 487 | { 488 | q = p; p = p->link; 489 | } 490 | if (p == NULL) 491 | { 492 | return true; 493 | } 494 | else 495 | { 496 | if (q == NULL) 497 | { 498 | NodeTable[v1].adj = p->link; 499 | } 500 | else 501 | { 502 | q->link = p->link; 503 | } 504 | delete p; 505 | } 506 | return true; 507 | } 508 | 509 | class PowerGridSystem 510 | { 511 | private: 512 | Graphlnk G;//存储电网信息的图 513 | Edge* mintreeedge;//存放最小生成树边的mintreeedge数组 514 | //菜单 515 | void menu(); 516 | //建立顶点 517 | void buildvertex(); 518 | //增加边 519 | void addedge(); 520 | //用Prin算法生成最小生成树 521 | void buildmintreebyprim(); 522 | //展示最小生成树 523 | void showmintreebyprim(); 524 | 525 | public: 526 | PowerGridSystem() 527 | { 528 | mintreeedge = NULL; 529 | } 530 | ~PowerGridSystem() 531 | { 532 | if (mintreeedge != NULL) 533 | { 534 | delete[]mintreeedge; 535 | } 536 | } 537 | //使用系统 538 | void use(); 539 | }; 540 | 541 | void PowerGridSystem::menu() 542 | { 543 | cout << "** 电网造假模拟系统 **" << endl; 544 | cout << "====================================================" << endl; 545 | cout << "** A --- 创建电网顶点 **" << endl; 546 | cout << "** B --- 添加电网的边 **" << endl; 547 | cout << "** C --- 构造最小生成树 **" << endl; 548 | cout << "** D --- 显示最小生成树 **" << endl; 549 | cout << "** E --- 退出程序 **" << endl; 550 | cout << "====================================================" << endl; 551 | } 552 | 553 | void PowerGridSystem::buildvertex() 554 | { 555 | int n; 556 | while (1) 557 | { 558 | cout << "请输入顶点的个数:"; 559 | cin >> n; 560 | if (cin.good() && n > 0) 561 | { 562 | break; 563 | } 564 | if (cin.fail()) 565 | { 566 | cin.clear(); 567 | cin.ignore(1024, '\n'); 568 | } 569 | cout << "输入有误,请重新输入" << endl; 570 | } 571 | cout << "请依次输入电网各顶点的名称:" << endl; 572 | for (int i = 0; i < n; i++) 573 | { 574 | char c; 575 | cin >> c; 576 | if (G.getVertexPos(c) != -1) 577 | { 578 | cout << "顶点" << c << "重复输入" << endl; 579 | continue; 580 | } 581 | G.insertVertex(c); 582 | } 583 | cin.ignore(1024, '\n'); 584 | } 585 | void PowerGridSystem::addedge() 586 | { 587 | int n; 588 | while (1) 589 | { 590 | cout << "请输入添加边的个数:"; 591 | cin >> n; 592 | if (cin.good() && n > 0) 593 | { 594 | break; 595 | } 596 | if (cin.fail()) 597 | { 598 | cin.clear(); 599 | cin.ignore(1024, '\n'); 600 | } 601 | cout << "输入有误,请重新输入" << endl; 602 | } 603 | for (int i = 0; i < n; i++) 604 | { 605 | char c1, c2; 606 | int v1, v2, co; 607 | while (1) 608 | { 609 | cout << "请输入两个顶点及边:"; 610 | cin >> c1 >> c2 >> co; 611 | if (cin.fail()) 612 | { 613 | cin.clear(); 614 | cin.ignore(1024,'\n'); 615 | cout << "输入有误,请重新输入" << endl; 616 | } 617 | else 618 | { 619 | break; 620 | } 621 | } 622 | v1 = G.getVertexPos(c1); 623 | v2 = G.getVertexPos(c2); 624 | if (v1 == -1 || v2 == -1) 625 | { 626 | cout << "顶点不存在!" << endl; 627 | continue; 628 | } 629 | if (co < 0) 630 | { 631 | cout << "边权值不合理!" << endl; 632 | continue; 633 | } 634 | if (G.insertEdge(v1, v2, co) == 0) 635 | { 636 | cout << "该两顶点间已存在边!" << endl; 637 | continue; 638 | } 639 | G.insertEdge(v2, v1, co); 640 | } 641 | } 642 | 643 | bool check(int arr[], int n) 644 | { 645 | for (int i = 0; i < n; i++) 646 | { 647 | if (arr[i] == 0) 648 | { 649 | return false; 650 | } 651 | } 652 | return true; 653 | } 654 | 655 | 656 | void PowerGridSystem::buildmintreebyprim() 657 | { 658 | char c; 659 | int fr; 660 | while (1) 661 | { 662 | cout << "请输入起始顶点:" ; 663 | cin >> c; 664 | fr = G.getVertexPos(c); 665 | if (fr == -1) 666 | { 667 | cin.clear(); 668 | cin.ignore(1024, '\n'); 669 | cout << "顶点不存在!" << endl; 670 | } 671 | else 672 | { 673 | break; 674 | } 675 | } 676 | if (mintreeedge != NULL) 677 | { 678 | delete[]mintreeedge; 679 | } 680 | int size = G.NumberOfVertices(); 681 | size = size * (size - 1) / 2; 682 | mintreeedge = new Edge[size]; 683 | int cnt = -1; 684 | 685 | int* connected = new int[size]; 686 | for (int i = 0; i < size; i++) 687 | { 688 | connected[i] = 0; 689 | } 690 | connected[fr] = 1; 691 | 692 | PQueue> pQ; 693 | for (int i = G.getFirstNeighbor(fr); i != -1; i = G.getNextNeighbor(fr, i)) 694 | { 695 | Edge* ptr = G.getEdge(fr, i); 696 | ptr->fr = fr; 697 | pQ.Push(*ptr); 698 | } 699 | int to; 700 | Edge e; 701 | while (!pQ.isEmpty()) 702 | { 703 | int endflag = 1; 704 | while (!pQ.isEmpty()) 705 | { 706 | e = pQ.Front(); 707 | pQ.Pop(); 708 | to = e.dest; 709 | if (connected[to] == 0) 710 | { 711 | endflag = 0;//如果此时有边可以被加入,则不结束 712 | break; 713 | } 714 | } 715 | if (endflag) 716 | { 717 | break; 718 | } 719 | connected[to] = 1; 720 | fr = to; 721 | mintreeedge[++cnt] = e; 722 | if (check(connected, G.NumberOfVertices())) 723 | { 724 | break; 725 | } 726 | for (int i = G.getFirstNeighbor(fr); i != -1; i = G.getNextNeighbor(fr, i)) 727 | { 728 | Edge* ptr = G.getEdge(fr, i); 729 | ptr->fr = fr; 730 | pQ.Push(*ptr); 731 | } 732 | } 733 | 734 | if (check(connected, G.NumberOfVertices())) 735 | { 736 | cout << "生成Prim最小生成树!" << endl; 737 | } 738 | else 739 | { 740 | cout << "生成Prim最小生成树失败!" << endl; 741 | delete[] mintreeedge; 742 | mintreeedge = NULL; 743 | } 744 | delete[]connected; 745 | 746 | } 747 | 748 | void PowerGridSystem::showmintreebyprim() 749 | { 750 | if (mintreeedge == NULL) 751 | { 752 | cout << "无最小生成树" << endl; 753 | return; 754 | } 755 | cout << "最小生成树的顶点及边为:" << endl; 756 | int size = G.NumberOfVertices()-1; 757 | for (int i = 0; i < size; i++) 758 | { 759 | Edge e = mintreeedge[i]; 760 | int v1 = e.fr; 761 | int v2 = e.dest; 762 | char c1 = G.getValue(v1); 763 | char c2 = G.getValue(v2); 764 | int co = e.cost; 765 | cout << c1 << "-<" << co << ">-" << c2 << "\t"; 766 | } 767 | cout << endl; 768 | } 769 | 770 | void PowerGridSystem::use() 771 | { 772 | menu(); 773 | char op; 774 | while (1) 775 | { 776 | while (1) 777 | { 778 | cout << "\n请选择操作:"; 779 | cin >> op; 780 | if (op >= 'A' && op <='E') 781 | { 782 | break; 783 | } 784 | cout << "输入有误,请重新输入" << endl; 785 | } 786 | if (op == 'E') 787 | { 788 | break; 789 | } 790 | switch (op) 791 | { 792 | case 'A': 793 | buildvertex(); 794 | break; 795 | case 'B': 796 | addedge(); 797 | break; 798 | case 'C': 799 | buildmintreebyprim(); 800 | break; 801 | case 'D': 802 | showmintreebyprim(); 803 | break; 804 | } 805 | } 806 | cout << "谢谢使用!" << endl; 807 | } 808 | 809 | int main() 810 | { 811 | PowerGridSystem sys; 812 | sys.use(); 813 | return 0; 814 | } 815 | 816 | /* 817 | a b 8 818 | b c 7 819 | c d 5 820 | d a 11 821 | a c 18 822 | b d 12 823 | 824 | */ -------------------------------------------------------------------------------- /08_2152085_孙亦菲/08_2152085_孙亦菲.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/08_2152085_孙亦菲/08_2152085_孙亦菲.doc -------------------------------------------------------------------------------- /08_2152085_孙亦菲/08_2152085_孙亦菲.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/08_2152085_孙亦菲/08_2152085_孙亦菲.exe -------------------------------------------------------------------------------- /08_2152085_孙亦菲/08_2152085_孙亦菲_Linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/08_2152085_孙亦菲/08_2152085_孙亦菲_Linux -------------------------------------------------------------------------------- /08_2152085_孙亦菲/makefile: -------------------------------------------------------------------------------- 1 | 08_2152085_孙亦菲_Linux:08_2152085_孙亦菲.o 2 | g++ *.o -o $@ 3 | %.o:%.c 4 | g++ -c $< -o $@ 5 | 6 | clean: 7 | rm -f *.o 08_2152085_孙亦菲_Linux 8 | 9 | -------------------------------------------------------------------------------- /09_2152085_孙亦菲/09_2152085_孙亦菲.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define DefaultVertices 100 4 | 5 | template 6 | class Graph 7 | { 8 | public: 9 | Graph(int sz = DefaultVertices) {}; 10 | virtual ~Graph() {}; 11 | bool GraphEmpty()const 12 | { 13 | return numEdges == 0; 14 | } 15 | bool GraphFull()const 16 | { 17 | return numVertices == maxVertices || numEdges == (maxVertices) * (maxVertices - 1) / 2; 18 | } 19 | int NumberOfVertices() 20 | { 21 | return numVertices; 22 | } 23 | int NumberOfEdges() 24 | { 25 | return numEdges; 26 | } 27 | 28 | virtual int getFirstNeighbor(int v) = 0; 29 | virtual int getNextNeighbor(int v, int w) = 0; 30 | virtual bool insertVertex(const T vertex) = 0; 31 | virtual bool insertEdge(int v1, int v2, E cost) = 0; 32 | //virtual bool removeVertex(int v); 33 | virtual bool removeEdge(int v1, int v2) = 0; 34 | virtual int getVertexPos(T vertex) = 0; 35 | 36 | protected: 37 | int maxVertices; 38 | int numEdges; 39 | int numVertices; 40 | }; 41 | 42 | 43 | 44 | template 45 | class Edge 46 | { 47 | public: 48 | int fr; 49 | int dest; 50 | E cost; 51 | Edge* link; 52 | Edge() {}; 53 | Edge(int num, E weight) :dest(num), cost(weight), link(NULL) {}; 54 | bool operator!=(Edge& R)const 55 | { 56 | return dest != R.dest; 57 | } 58 | bool operator<(Edge& R)const 59 | { 60 | return this->cost < R.cost; 61 | } 62 | }; 63 | 64 | template 65 | class Vertex 66 | { 67 | public: 68 | T data; 69 | Edge* adj; 70 | }; 71 | 72 | template 73 | class Graphlnk :public Graph 74 | { 75 | public: 76 | Graphlnk(int sz = DefaultVertices); 77 | ~Graphlnk(); 78 | //第一个邻接顶点 79 | int getFirstNeighbor(int v); 80 | //下一个邻接顶点 81 | int getNextNeighbor(int v, int w); 82 | //编号为i的顶点的值 83 | T getValue(int i); 84 | //顶点v1,v2间边的权值 85 | E getWeight(int v1, int v2); 86 | //指向顶点v1,v2间边的指针 87 | Edge* getEdge(int v1, int v2); 88 | //插入顶点 89 | bool insertVertex(T vertex); 90 | //无向图移除顶点 91 | bool undirremoveVertex(int v); 92 | //有向图移除顶点 93 | bool dirremoveVertex(int v); 94 | //插入边 95 | bool insertEdge(int v1, int v2, E weight); 96 | //移除边 97 | bool removeEdge(int v1, int v2);//得到某元素在图中的位置 98 | int getVertexPos(const T vertex) 99 | { 100 | for (int i = 0; i < this->maxVertices; i++) 101 | { 102 | if (NodeTable[i].data == vertex) 103 | { 104 | return i; 105 | } 106 | } 107 | return -1; 108 | } 109 | private: 110 | Vertex* NodeTable; 111 | }; 112 | 113 | 114 | template 115 | Graphlnk::Graphlnk(int sz) 116 | { 117 | this->maxVertices = sz; this->numVertices = 0; this->numEdges = 0; 118 | NodeTable = new Vertex[this->maxVertices]; 119 | for (int i = 0; i < this->maxVertices; i++) 120 | { 121 | NodeTable[i].adj = NULL; 122 | } 123 | } 124 | 125 | template 126 | Graphlnk::~Graphlnk() 127 | { 128 | for (int i = 0; i < this->maxVertices; i++) 129 | { 130 | Edge* p = NodeTable[i].adj; 131 | 132 | while (p != NULL) 133 | { 134 | NodeTable[i].adj = p->link; 135 | delete p; 136 | p = NodeTable[i].adj; 137 | } 138 | } 139 | delete[]NodeTable; 140 | } 141 | 142 | template 143 | T Graphlnk::getValue(int i) 144 | { 145 | return NodeTable[i].data; 146 | } 147 | 148 | template 149 | int Graphlnk::getFirstNeighbor(int v) 150 | { 151 | if (v < 0 || v >= this->numVertices) 152 | { 153 | return -1; 154 | } 155 | if (NodeTable[v].adj != NULL) 156 | { 157 | return NodeTable[v].adj->dest; 158 | } 159 | else 160 | { 161 | return -1; 162 | } 163 | } 164 | 165 | template 166 | int Graphlnk::getNextNeighbor(int v, int w) 167 | { 168 | if (v < 0 || v >= this->numVertices) 169 | { 170 | return -1; 171 | } 172 | Edge* p = NodeTable[v].adj; 173 | while (p != NULL && p->dest != w) 174 | { 175 | p = p->link; 176 | } 177 | if (p != NULL && p->link != NULL) 178 | { 179 | return p->link->dest; 180 | } 181 | return -1; 182 | } 183 | 184 | template 185 | Edge* Graphlnk::getEdge(int v1, int v2) 186 | { 187 | Edge < T, E>* p = NodeTable[v1].adj; 188 | while (p != NULL && p->dest != v2) 189 | { 190 | p = p->link; 191 | } 192 | return p; 193 | } 194 | 195 | template 196 | E Graphlnk::getWeight(int v1, int v2) 197 | { 198 | Edge < T, E>* p = NodeTable[v1].adj; 199 | while (p != NULL && p->dest != v2) 200 | { 201 | p = p->link; 202 | } 203 | if (p != NULL) 204 | { 205 | return p->cost; 206 | } 207 | return 0; 208 | } 209 | 210 | template 211 | bool Graphlnk::insertVertex(T vertex) 212 | { 213 | if (this->numVertices == this->maxVertices) 214 | { 215 | return false; 216 | } 217 | for (int i = 0; i < this->numVertices; i++) 218 | { 219 | if (vertex == NodeTable[i].data) 220 | { 221 | return false; 222 | } 223 | } 224 | NodeTable[this->numVertices].data = vertex; 225 | this->numVertices++; 226 | return true; 227 | } 228 | 229 | /*无向图的办法*/ 230 | template 231 | bool Graphlnk::undirremoveVertex(int v) 232 | { 233 | if (v < 0 || v >= this->numVertices) 234 | { 235 | return false; 236 | } 237 | Edge* p, * s, * t; 238 | int k; 239 | while (NodeTable[v].adj != NULL) 240 | { 241 | p = NodeTable[v].adj; k = p->dest; 242 | s = NodeTable[k].adj; 243 | t = NULL; 244 | while (s != NULL && s->dest != v) 245 | { 246 | t = s; s = s->link; 247 | } 248 | if (s != NULL) 249 | { 250 | if (t == NULL) 251 | { 252 | NodeTable[k].adj = s->link; 253 | } 254 | else 255 | { 256 | t->link = s->link; 257 | } 258 | delete s; 259 | } 260 | NodeTable[v].adj = p->link; 261 | delete p; 262 | this->numEdges--; 263 | } 264 | this->numVertices--; 265 | NodeTable[v].data = NodeTable[this->numVertices].data; 266 | p = NodeTable[v].adj = NodeTable[this->numVertices].adj; 267 | while (p != NULL) 268 | { 269 | s = NodeTable[p->dest].adj; 270 | while (s != NULL) 271 | { 272 | if (s->dest == this->numVertices) 273 | { 274 | s->dest = v; 275 | break; 276 | } 277 | else 278 | { 279 | s = s->link; 280 | } 281 | } 282 | p = p->link; 283 | } 284 | return true; 285 | } 286 | 287 | /*有向图的办法*/ 288 | template 289 | bool Graphlnk::dirremoveVertex(int v) 290 | { 291 | if (v < 0 || v >= this->numVertices) 292 | { 293 | return false; 294 | } 295 | Edge* p, * q; 296 | p = NodeTable[v].adj; 297 | while (p != NULL) 298 | { 299 | NodeTable[v].adj = p->link; 300 | delete p; 301 | p = NodeTable[v].adj; 302 | } 303 | for (int i = 0; i < this->numVertices; i++) 304 | { 305 | if (i == v) 306 | { 307 | continue; 308 | } 309 | p = NodeTable[i].adj; 310 | q = p; 311 | while (p != NULL) 312 | { 313 | if (p->dest == v) 314 | { 315 | q->link = p->link; 316 | delete p; 317 | p = q->link; 318 | } 319 | else 320 | { 321 | q = p; 322 | p = p->link; 323 | } 324 | } 325 | } 326 | this->numVertices--; 327 | NodeTable[v].data = NodeTable[this->numVertices].data; 328 | p = NodeTable[v].adj = NodeTable[this->numVertices].adj; 329 | while (p != NULL) 330 | { 331 | Edge* s = NodeTable[p->dest].adj; 332 | while (s != NULL) 333 | { 334 | if (s->dest == this->numVertices) 335 | { 336 | s->dest = v; 337 | break; 338 | } 339 | else 340 | { 341 | s = s->link; 342 | } 343 | } 344 | p = p->link; 345 | } 346 | return true; 347 | } 348 | 349 | template 350 | bool Graphlnk::insertEdge(int v1, int v2, E weight) 351 | { 352 | if (v1 < 0 || v1 >= this->numVertices || v2 < 0 || v2 >= this->numVertices) 353 | { 354 | return false; 355 | } 356 | Edge* p = NodeTable[v1].adj; 357 | while (p != NULL && p->dest != v2) 358 | { 359 | p = p->link; 360 | } 361 | if (p != NULL) 362 | { 363 | return false; 364 | } 365 | p = new Edge; 366 | p->dest = v2; p->cost = weight; p->link = NodeTable[v1].adj; NodeTable[v1].adj = p; 367 | this->numEdges++; 368 | return true; 369 | } 370 | 371 | template 372 | bool Graphlnk::removeEdge(int v1, int v2) 373 | { 374 | if (v1 < 0 || v1 >= this->numVertices || v2 < 0 || v2 >= this->numVertices) 375 | { 376 | return false; 377 | } 378 | Edge* p = NodeTable[v1].adj, * q = NULL; 379 | while (p != NULL && p->dest != v2) 380 | { 381 | q = p; p = p->link; 382 | } 383 | if (p == NULL) 384 | { 385 | return true; 386 | } 387 | else 388 | { 389 | if (q == NULL) 390 | { 391 | NodeTable[v1].adj = p->link; 392 | } 393 | else 394 | { 395 | q->link = p->link; 396 | } 397 | delete p; 398 | } 399 | return true; 400 | } 401 | 402 | 403 | class AOE 404 | { 405 | private: 406 | GraphlnkG; 407 | //入度数组 408 | int* incount; 409 | //出度数组 410 | int* outcount; 411 | //拓扑排序检验有无环存在 412 | int TopologicalSort(); 413 | public: 414 | AOE(); 415 | ~AOE() 416 | { 417 | if (incount != NULL) 418 | { 419 | delete[]incount; 420 | } 421 | } 422 | //求关键路径 423 | void CriticalPath(); 424 | }; 425 | 426 | AOE::AOE() 427 | { 428 | int n; 429 | cin >> n; 430 | //插入顶点 431 | for (int i = 1; i <= n; i++) 432 | { 433 | G.insertVertex(i); 434 | } 435 | 436 | incount = new int[n]; 437 | for (int i = 0; i < n; i++) 438 | { 439 | incount[i] = 0; 440 | } 441 | 442 | outcount = new int[n]; 443 | for (int i = 0; i < n; i++) 444 | { 445 | outcount[i] = 0; 446 | } 447 | 448 | int m; 449 | cin >> m; 450 | //插入边 451 | for (int i = 0; i < m; i++) 452 | { 453 | int fr, to, co; 454 | cin >> fr >> to >> co; 455 | //每个编号代表顶点的下标位置恰好为编号减1 456 | G.insertEdge(fr - 1, to - 1, co); 457 | outcount[fr - 1]++; 458 | incount[to - 1]++; 459 | } 460 | } 461 | int AOE::TopologicalSort() 462 | { 463 | int size = G.NumberOfVertices(); 464 | 465 | int top = -1;//栈顶 466 | 467 | int* count = new int[size];//复制入度数组兼入度为0顶点栈 468 | 469 | for (int i = 0; i < size; i++) 470 | { 471 | count[i] = incount[i]; 472 | if ( count[i] == 0) 473 | { 474 | count[i] = top; 475 | top = i; 476 | } 477 | } 478 | 479 | int u, v; 480 | 481 | for (int cnt = 0; cnt < size; cnt++) 482 | { 483 | if (top == -1)//提前栈空,有结点从没进来过,存在环 484 | { 485 | delete[]count; 486 | return false; 487 | } 488 | else 489 | { 490 | u = top; 491 | top = count[top]; 492 | for (v = G.getFirstNeighbor(u); v != -1; v = G.getNextNeighbor(u, v)) 493 | { 494 | count[v]--; 495 | if ( count[v] == 0)//压入入度为0的栈 496 | { 497 | count[v] = top; 498 | top = v; 499 | } 500 | } 501 | } 502 | } 503 | delete[]count; 504 | return true; 505 | } 506 | void AOE::CriticalPath() 507 | { 508 | if (TopologicalSort() == 0) 509 | { 510 | cout << "0"; 511 | return; 512 | } 513 | 514 | int n = G.NumberOfVertices(); 515 | int* Ve = new int[n];//某节点最早开始时间 516 | int* Vl = new int[n];//某节点最晚开始时间 517 | 518 | for (int i = 0; i < n; i++) 519 | { 520 | Ve[i] = 0; 521 | Vl[i] = 0x7fffffff; 522 | } 523 | 524 | //将正向遍历时的队列看作一个容器,反向遍历时当作一个栈来反向弹栈即可 525 | int* vqueue = new int[n]; 526 | int hptr=0,tptr =0;//队列头,队列尾 527 | 528 | 529 | for (int i = 0; i < n; i++) 530 | { 531 | if (incount[i] == 0) 532 | { 533 | vqueue[tptr++] = i; 534 | } 535 | } 536 | 537 | for (int i = 0; i < n; i++) 538 | { 539 | int u = vqueue[hptr++]; 540 | for (int v = G.getFirstNeighbor(u); v != -1; v = G.getNextNeighbor(u, v)) 541 | { 542 | int co = G.getWeight(u, v); 543 | if (Ve[u] + co > Ve[v]) 544 | { 545 | Ve[v] = Ve[u] + co; 546 | } 547 | incount[v]--; 548 | if (incount[v] == 0) 549 | { 550 | vqueue[tptr++] = v; 551 | } 552 | } 553 | } 554 | //得到最早结束时间,总时间 555 | int time = 0; 556 | for (int i = 0; i < n; i++) 557 | { 558 | if (outcount[i] == 0) 559 | { 560 | if (Ve[i] > time) 561 | { 562 | time = Ve[i]; 563 | } 564 | } 565 | } 566 | cout << time << endl; 567 | //所有汇点的Vl为time 568 | for (int i = 0; i < n; i++) 569 | { 570 | if (outcount[i] == 0) 571 | { 572 | Vl[i] = time; 573 | } 574 | } 575 | //根据相反的入队顺序遍历AOE图 576 | while(tptr--) 577 | { 578 | int u = vqueue[tptr]; 579 | for (int v = G.getFirstNeighbor(u); v != -1; v = G.getNextNeighbor(u, v)) 580 | { 581 | int co = G.getWeight(u, v); 582 | if (Vl[v] -co < Vl[u]) 583 | { 584 | Vl[u] = Vl[v] - co; 585 | } 586 | } 587 | } 588 | delete[]vqueue; 589 | class path 590 | { 591 | public: 592 | int u, v; 593 | path() {}; 594 | path(int uu, int vv) 595 | { 596 | u = uu; v = vv; 597 | } 598 | bool operator<(path& p) 599 | { 600 | return this->u < p.u; 601 | } 602 | 603 | }; 604 | 605 | path* p = new path[n]; 606 | int pcnt = 0; 607 | 608 | for (int i = 0; i < n; i++) 609 | { 610 | int u = i; 611 | for (int v = G.getFirstNeighbor(u); v != -1; v = G.getNextNeighbor(u, v)) 612 | { 613 | int Ae = Ve[u]; 614 | int Al = Vl[v] - G.getWeight(u, v); 615 | if (Ae == Al) 616 | { 617 | p[pcnt++] = path(u, v); 618 | } 619 | } 620 | } 621 | 622 | //简单冒泡排序,保证起点编号小的在前且稳定 623 | for (int i = 0; i < pcnt ; i++) 624 | { 625 | for (int j = 0; j < pcnt -1- i; j++) 626 | { 627 | if (p[j + 1] < p[j]) 628 | { 629 | path tp = p[j + 1]; 630 | p[j + 1] = p[j]; 631 | p[i] = tp; 632 | } 633 | } 634 | } 635 | 636 | for (int i = 0; i < pcnt; i++) 637 | { 638 | cout << p[i].u+1 << "->" << p[i].v+1 << endl; 639 | } 640 | 641 | delete[]p; 642 | delete[]Ve; 643 | delete[]Vl; 644 | } 645 | 646 | int main() 647 | { 648 | AOE A; 649 | A.CriticalPath(); 650 | return 0; 651 | } -------------------------------------------------------------------------------- /09_2152085_孙亦菲/09_2152085_孙亦菲.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/09_2152085_孙亦菲/09_2152085_孙亦菲.doc -------------------------------------------------------------------------------- /09_2152085_孙亦菲/09_2152085_孙亦菲.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/09_2152085_孙亦菲/09_2152085_孙亦菲.exe -------------------------------------------------------------------------------- /09_2152085_孙亦菲/09_2152085_孙亦菲_Linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/09_2152085_孙亦菲/09_2152085_孙亦菲_Linux -------------------------------------------------------------------------------- /09_2152085_孙亦菲/makefile: -------------------------------------------------------------------------------- 1 | 09_2152085_孙亦菲_Linux:09_2152085_孙亦菲.o 2 | g++ *.o -o $@ 3 | %.o:%.c 4 | g++ -c $< -o $@ 5 | 6 | clean: 7 | rm -f *.o 09_2152085_孙亦菲_Linux 8 | 9 | -------------------------------------------------------------------------------- /10_2152085_孙亦菲/10_2152085_孙亦菲.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void show(int a[], int n) 6 | { 7 | for (int i = 0; i < n; i++) 8 | { 9 | cout << a[i] << " "; 10 | } 11 | cout << endl; 12 | } 13 | template 14 | class sortSystem 15 | { 16 | private: 17 | void menu(); 18 | void message(char* str, int time, long long change, long long comp); 19 | long long change; 20 | long long comp; 21 | 22 | void bubbleSort(T arr[], int n); 23 | void selectSort(T arr[], int n); 24 | void insertSort(T arr[], int n); 25 | void shellSort(T arr[], int n); 26 | 27 | void quickSort(T arr[], int n,int low,int high); 28 | int partition(T arr[], int low, int high); 29 | 30 | void heapSort(T arr[], int n); 31 | void buildmaxheap(T arr[], int n); 32 | void headadjust(T arr[], int k, int n); 33 | 34 | void merge(T arr[],int n, int low, int mid, int high); 35 | void mergeSort(T arr[],int n, int low,int high, int firstflag); 36 | 37 | void radixSort(int arr[], int n); 38 | public: 39 | void use(); 40 | }; 41 | 42 | template 43 | void sortSystem::menu() 44 | { 45 | cout << "** 排序算法比较 **" << endl; 46 | cout << "====================================================" << endl; 47 | cout << "** 1 --- 冒泡排序 **" << endl; 48 | cout << "** 2 --- 选择排序 **" << endl; 49 | cout << "** 3 --- 直接插入排序 **" << endl; 50 | cout << "** 4 --- 希尔排序 **" << endl; 51 | cout << "** 5 --- 快速排序 **" << endl; 52 | cout << "** 6 --- 堆排序 **" << endl; 53 | cout << "** 7 --- 归并排序 **" << endl; 54 | cout << "** 8 --- 基数排序 **" << endl; 55 | cout << "** 9 --- 退出程序 **" << endl; 56 | cout << "====================================================" << endl; 57 | } 58 | 59 | template 60 | void sortSystem::message(char*str,int time,long long change,long long comp) 61 | { 62 | cout << endl; 63 | cout << str << "所用时间:\t" << time <<"\t毫秒" << endl; 64 | cout << str << "交换次数:\t" << change << "\t次" << endl; 65 | cout << str << "比较次数:\t" < 69 | void sortSystem::bubbleSort(T arr[],int n) 70 | { 71 | clock_t start,end; 72 | int flag ; 73 | start = clock(); 74 | 75 | change = 0; 76 | comp = 0; 77 | 78 | for (int i = 0; i < n; i++) 79 | { 80 | flag = 1; 81 | for (int j = 0; j < n -1- i; j++) 82 | { 83 | comp++; 84 | if (arr[j] > arr[j + 1]) 85 | { 86 | int temp = arr[j + 1]; 87 | arr[j + 1] = arr[j]; 88 | arr[j] = temp; 89 | flag = 0; 90 | change++; 91 | } 92 | } 93 | if (flag) 94 | { 95 | break; 96 | } 97 | } 98 | end = clock(); 99 | char str[20] = "冒泡排序"; 100 | message(str, end - start, change, comp); 101 | } 102 | 103 | template 104 | void sortSystem::selectSort(T arr[], int n) 105 | { 106 | clock_t start, end; 107 | int minpl; 108 | change = 0; 109 | comp = 0; 110 | start = clock(); 111 | for (int i = 0; i < n; i++) 112 | { 113 | minpl = i; 114 | for (int j = i + 1; j < n; j++) 115 | { 116 | comp++; 117 | if (arr[j] < arr[minpl]) 118 | { 119 | minpl = j; 120 | } 121 | } 122 | int temp = arr[i]; 123 | arr[i] = arr[minpl]; 124 | arr[minpl] = temp; 125 | change++; 126 | } 127 | end = clock(); 128 | char str[20] = "选择排序"; 129 | message(str, end - start, change, comp); 130 | } 131 | 132 | template 133 | void sortSystem::insertSort(T arr[], int n) 134 | { 135 | clock_t start, end; 136 | change = 0; 137 | comp = 0; 138 | start = clock(); 139 | for (int i = 1; i < n; i++) 140 | { 141 | int temp = arr[i]; 142 | int j = i - 1; 143 | for ( ; j >= 0; j--) 144 | { 145 | comp++; 146 | if (temp < arr[j]) 147 | { 148 | change++; 149 | arr[j + 1] = arr[j]; 150 | } 151 | else 152 | { 153 | break; 154 | } 155 | } 156 | change++; 157 | arr[j + 1]= temp; 158 | } 159 | end = clock(); 160 | char str[20] = "插入排序"; 161 | message(str, end - start, change, comp); 162 | } 163 | 164 | template 165 | void sortSystem::shellSort(T arr[], int n) 166 | { 167 | clock_t start, end; 168 | start = clock(); 169 | change = 0; 170 | comp = 0; 171 | for (int gap = n / 2; gap >= 1; gap = gap / 2) 172 | { 173 | for (int i = gap; i < n; i++) 174 | { 175 | int temp = arr[i]; 176 | int j = i - gap; 177 | for (; j >= 0; j = j - gap) 178 | { 179 | comp++; 180 | if (temp < arr[j]) 181 | { 182 | change++; 183 | arr[j + gap] = arr[j]; 184 | } 185 | else 186 | { 187 | break; 188 | } 189 | } 190 | change++; 191 | arr[j + gap] = temp; 192 | } 193 | } 194 | 195 | end = clock(); 196 | char str[20] = "希尔排序"; 197 | message(str, end - start, change, comp); 198 | } 199 | 200 | template 201 | int sortSystem::partition(T arr[], int low, int high) 202 | { 203 | T pivot = arr[low]; 204 | while (low < high) 205 | { 206 | comp++; 207 | while (low < high && arr[high] >= pivot) 208 | { 209 | high--; 210 | comp++; 211 | } 212 | arr[low] = arr[high]; 213 | comp++; 214 | while (low < high && arr[low] <= pivot) 215 | { 216 | low++; 217 | comp++; 218 | } 219 | arr[high] = arr[low]; 220 | change++; 221 | } 222 | arr[low] = pivot; 223 | change++; 224 | return low; 225 | } 226 | 227 | //=快速排序 228 | template 229 | void sortSystem::quickSort(T arr[], int firstflag_n,int low,int high) 230 | { 231 | clock_t begin, end; 232 | if (firstflag_n) 233 | { 234 | comp = 0; 235 | change = 0; 236 | low = 0; 237 | high = firstflag_n - 1; 238 | begin = clock(); 239 | } 240 | if (low < high) 241 | { 242 | int pivot = partition(arr, low, high); 243 | quickSort(arr, 0,low, pivot - 1); 244 | quickSort(arr,0,pivot + 1, high); 245 | } 246 | if (firstflag_n) 247 | { 248 | end = clock(); 249 | char str[20] = "快速排序"; 250 | message(str, end - begin, change, comp); 251 | } 252 | } 253 | 254 | template 255 | void sortSystem::buildmaxheap(T arr[], int n) 256 | { 257 | for (int i = (n - 1) / 2; i >= 0; i--) 258 | { 259 | headadjust(arr, i, n); 260 | } 261 | } 262 | 263 | //数组,需shiftdown的元素,当前元素个数 264 | template 265 | void sortSystem::headadjust(T arr[], int k, int n) 266 | { 267 | int temp = arr[k]; 268 | for (int i = 2 * k+1; i < n; i = 2*k+1) 269 | { 270 | comp++; 271 | if (i + 1 < n && arr[i] < arr[i + 1]) 272 | { 273 | i++; 274 | } 275 | comp++; 276 | if (temp > arr[i]) 277 | { 278 | break; 279 | } 280 | change++; 281 | arr[k] = arr[i]; 282 | k = i; 283 | } 284 | arr[k] = temp; 285 | change++; 286 | } 287 | 288 | template 289 | void sortSystem::heapSort(T arr[], int n) 290 | { 291 | comp = 0; 292 | change = 0; 293 | clock_t begin, end; 294 | begin = clock(); 295 | buildmaxheap(arr, n); 296 | for (int i = n - 1; i > 0; i--) 297 | { 298 | int temp = arr[0]; 299 | arr[0]=arr[i]; 300 | arr[i] = temp; 301 | change++; 302 | headadjust(arr, 0, i); 303 | } 304 | end = clock(); 305 | char str[20] = "堆排序"; 306 | message(str, end - begin, change, comp); 307 | } 308 | 309 | template 310 | void sortSystem::merge(T arr[], int n,int low,int mid, int high) 311 | { 312 | T* tarr = new T[n]; 313 | for (int i = low; i <= high; i++) 314 | { 315 | tarr[i] = arr[i]; 316 | } 317 | int i, j, k; 318 | for (i = low, j = mid + 1, k = low; i <= mid && j <= high; k++) 319 | { 320 | comp++; 321 | change++; 322 | if (tarr[i] <= tarr[j]) 323 | { 324 | arr[k] = tarr[i]; 325 | i++; 326 | } 327 | else 328 | { 329 | arr[k] = tarr[j]; 330 | j++; 331 | } 332 | } 333 | while (i <= mid) 334 | { 335 | arr[k] = tarr[i]; 336 | k++; 337 | i++; 338 | change++; 339 | } 340 | while (j <= high) 341 | { 342 | arr[k] = tarr[j]; 343 | k++; 344 | j++; 345 | change++; 346 | } 347 | delete[]tarr; 348 | } 349 | 350 | template 351 | void sortSystem::mergeSort(T arr[], int n,int low, int high,int firstflag) 352 | { 353 | clock_t begin, end = 0; 354 | if (firstflag) 355 | { 356 | comp = 0; 357 | change = 0; 358 | begin = clock(); 359 | } 360 | if (low < high) 361 | { 362 | int mid = (low + high) / 2; 363 | mergeSort(arr, n, low, mid, 0); 364 | mergeSort(arr, n, mid + 1, high, 0); 365 | merge(arr, n, low, mid, high); 366 | } 367 | if (firstflag) 368 | { 369 | end = clock(); 370 | char str[20] = "归并排序"; 371 | message(str, end - begin, change, comp); 372 | } 373 | } 374 | 375 | template 376 | void sortSystem::radixSort(int arr[], int n) 377 | { 378 | clock_t begin, end; 379 | comp = 0; 380 | change = 0; 381 | begin = clock(); 382 | 383 | int radix = 2048; 384 | int move[3] = { 0,11,22 }; 385 | int* temparr = new int[n]; 386 | int* count = new int[radix]; 387 | 388 | for (int i = 0; i < 3; i++) 389 | { 390 | for (int i = 0; i < radix; i++) 391 | { 392 | count[i] = 0; 393 | } 394 | for (int k = 0; k < n; k++) 395 | { 396 | count[(arr[k] >> move[i]) & 0x7ff]++; 397 | } 398 | for (int j = 1; j < radix; j++) 399 | { 400 | count[j] += count[j - 1]; 401 | } 402 | for (int k = n-1; k >=0; k--) 403 | { 404 | int index = count[(arr[k] >> move[i]) & 0x7ff] - 1; 405 | temparr[index] = arr[k]; 406 | count[(arr[k] >> move[i]) & 0x7ff] --; 407 | } 408 | for (int k = 0; k < n; k++) 409 | { 410 | change++; 411 | arr[k] = temparr[k]; 412 | } 413 | //show(arr, n); 414 | } 415 | end = clock(); 416 | delete[]temparr; 417 | delete[]count; 418 | char str[20] = "基数排序"; 419 | message(str, end - begin, change, comp); 420 | } 421 | 422 | void buildrandomarr(int arr[], int n) 423 | { 424 | for (int i = 0; i < n; i++) 425 | { 426 | arr[i] = rand(); 427 | } 428 | } 429 | 430 | 431 | 432 | template 433 | void sortSystem::use() 434 | { 435 | menu(); 436 | int n; 437 | while (1) 438 | { 439 | cout << "请输入要产生的随机数个数:"; 440 | cin >> n; 441 | if (cin.good() && n > 0) 442 | { 443 | break; 444 | } 445 | if (cin.fail()) 446 | { 447 | cin.clear(); 448 | cin.ignore(1024, '\n'); 449 | } 450 | cout << "输入有误,请重新输入" << endl; 451 | } 452 | int* oriarr = new int[n]; 453 | buildrandomarr(oriarr, n); 454 | int* arr = new int[n]; 455 | int op; 456 | //测试4.3使用 457 | //int tag = 1; 458 | 459 | while (1) 460 | { 461 | while (1) 462 | { 463 | cout << "\n请选择排序算法:"; 464 | cin >> op; 465 | if(cin.good() && op > 0 && op < 10) 466 | { 467 | break; 468 | } 469 | if (cin.fail()) 470 | { 471 | cin.clear(); 472 | cin.ignore(1024, '\n'); 473 | } 474 | cout << "输入有误,请重新输入" << endl; 475 | } 476 | if (op == 9) 477 | { 478 | break; 479 | } 480 | for (int i = 0; i < n; i++) 481 | { 482 | arr[i] = oriarr[i]; 483 | } 484 | //show(arr, n); 485 | switch (op) 486 | { 487 | case 1: 488 | bubbleSort(arr, n); 489 | break; 490 | case 2: 491 | selectSort(arr, n); 492 | break; 493 | case 3: 494 | insertSort(arr, n); 495 | break; 496 | case 4: 497 | shellSort(arr, n); 498 | break; 499 | case 5: 500 | quickSort(arr, n,0,n-1); 501 | break; 502 | case 6: 503 | heapSort(arr, n); 504 | break; 505 | case 7: 506 | mergeSort(arr, n,0,n-1,1); 507 | break; 508 | case 8: 509 | radixSort(arr, n); 510 | break; 511 | } 512 | //show(arr, n); 513 | //测试4.3使用 514 | /*if (tag) 515 | { 516 | for (int i = 0; i < n; i++) 517 | { 518 | oriarr[i] = arr[i]; 519 | } 520 | tag = 0; 521 | }*/ 522 | } 523 | cout << "谢谢使用!" << endl; 524 | delete[]oriarr; 525 | delete[]arr; 526 | } 527 | 528 | 529 | int main() 530 | { 531 | /* 生成伪随机数的种子,只需在程序开始时执行一次即可 */ 532 | srand((unsigned int)(time(0))); 533 | 534 | sortSystem T; 535 | T.use(); 536 | return 0; 537 | } -------------------------------------------------------------------------------- /10_2152085_孙亦菲/10_2152085_孙亦菲.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/10_2152085_孙亦菲/10_2152085_孙亦菲.doc -------------------------------------------------------------------------------- /10_2152085_孙亦菲/10_2152085_孙亦菲.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/10_2152085_孙亦菲/10_2152085_孙亦菲.exe -------------------------------------------------------------------------------- /10_2152085_孙亦菲/10_2152085_孙亦菲_Linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/10_2152085_孙亦菲/10_2152085_孙亦菲_Linux -------------------------------------------------------------------------------- /10_2152085_孙亦菲/makefile: -------------------------------------------------------------------------------- 1 | 10_2152085_孙亦菲_Linux:10_2152085_孙亦菲.o 2 | g++ *.o -o $@ 3 | %.o:%.c 4 | g++ -c $< -o $@ 5 | 6 | clean: 7 | rm -f *.o 10_2152085_孙亦菲_Linux 8 | 9 | -------------------------------------------------------------------------------- /2152085孙亦菲.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guangnianyuji/DataStructureDesign/9bf1283afeabcaac851b1380b697cbc1c1046cb0/2152085孙亦菲.docx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 同济大学2022年秋数据结构课程设计DataStructureDesign 2 | --------------------------------------------------------------------------------