1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
| #include <bits/stdc++.h>
using namespace std;
struct BorrowedBook // 书 { string bookNumber; string deadLine; BorrowedBook *next; };
struct Student // 学生 { string studentNumber; BorrowedBook *firstBook; };
struct StudentBorrowedBook // 学生借书 { Student *student; int sum; };
typedef struct Book { string name; string bookNumber; string author; int numNow; int numTotal; int BF; Book *lChild, *rChild; } Book, *AVLBook;
int InsertBook(AVLBook &book, string bookNumber, bool &flag);
void addStudent(StudentBorrowedBook &stu, string studentNumber); bool IsExistStu(StudentBorrowedBook stu, string numberStu); bool findStudent(StudentBorrowedBook stu, string numberStu, Student &res); int SearchTreeBorrow(AVLBook book, StudentBorrowedBook &stu, Student &student, string bookNumber, string time); int SearchTreeReturn(AVLBook book, StudentBorrowedBook &stu, Student &student, string bookNumber);
void display(StudentBorrowedBook stu); void InOrderTraverse(AVLBook T); void printT(AVLBook T, int n);
int main() { AVLBook book = NULL; StudentBorrowedBook stu; stu.sum = 0;
while (true) { cout << "请问您要做什么,入库书籍请按P,借书请按B,还书请按R,退出系统请按Q:" << endl; char ch; cin >> ch; if (ch == 'Q') { cout << "退出程序成功!" << endl; break; } switch (ch) { case 'P': { bool flag; int bookSum; cout << "请问您要入库几本书:" << endl; cin >> bookSum; for (int i = 0; i < bookSum; i++) { cout << "请输入第" << i + 1 << "本书的书号:" << endl; string bookNumber; getchar(); getline(cin,bookNumber); InsertBook(book, bookNumber, flag); } break; } case 'B': { cout << "请输入你的借书证号:" << endl; string studentNum; cin >> studentNum; getchar(); cout << "请输入你要借的书的书号:" << endl; string bookNumber; getline(cin,bookNumber);
if (!IsExistStu(stu, studentNum)) { addStudent(stu, studentNum); } Student nowStu; if(findStudent(stu, studentNum,nowStu)) { SearchTreeBorrow(book, stu, nowStu, bookNumber, "9"); }
break; } case 'R': { cout << "请输入你的借书证号:" << endl; string studentNum; cin >> studentNum; getchar(); cout << "请输入你要还的书的书号:" << endl; string bookNumber; getline(cin,bookNumber);
Student nowStu; if(findStudent(stu, studentNum,nowStu)) { SearchTreeReturn(book, stu, nowStu, bookNumber); } else{ cout << "查无此人,无法还书!" << endl; }
break; } default: cout << "输入的功能代码错误,请重新输入!" << endl; } } cout << "\n借阅人的信息有:" << endl; display(stu); cout << "中序遍历平衡二叉树:" << endl; InOrderTraverse(book); cout << "\n凹入表打印平衡二叉树:" << endl; printT(book, 0); return 0; }
void R_Rotate(AVLBook &book) { AVLBook tmp = book->lChild; book->lChild = tmp->rChild; tmp->rChild = book; book = tmp; }
void L_Rotate(AVLBook &book) { AVLBook tmp = book->rChild; book->rChild = tmp->lChild; tmp->lChild = book; book = tmp; }
void leftBalance(AVLBook &book) { AVLBook lchild = book->lChild; AVLBook tmpRightChild = NULL; switch (lchild->BF) { case 1: book->BF = lchild->BF = 0; R_Rotate(book); break; case -1: tmpRightChild = lchild->rChild; switch (tmpRightChild->BF) { case 1: book->BF = -1; lchild->BF = 0; break; case 0: book->BF = lchild->BF = 0; break; case -1: book->BF = 0; lchild->BF = 1; break; } tmpRightChild->BF = 0; L_Rotate(book->lChild); R_Rotate(book); break; } }
void rightBalance(AVLBook &book) { AVLBook rightchild = book->rChild; AVLBook tmpChild = NULL; switch (rightchild->BF) { case -1: book->BF = rightchild->BF = 0; L_Rotate(book); break; case 1: tmpChild = rightchild->lChild; switch (tmpChild->BF) { case 1: book->BF = 0; rightchild->BF = -1; break; case 0: book->BF = rightchild->BF = 0; break; case -1: book->BF = 0; rightchild->BF = 1; break; } tmpChild->BF = 0; R_Rotate(book->rChild); L_Rotate(book); break; } }
AVLBook createBook(string bookNumber) { AVLBook newBook = new Book;
cout << "请输入书名:" << endl; cin >> newBook->name; newBook->bookNumber = bookNumber; cout << "请输入作者:" << endl; cin >> newBook->author; cout << "请输入总存量:" << endl; cin >> newBook->numTotal;
newBook->numNow = newBook->numTotal; newBook->BF = 0; newBook->lChild = NULL; newBook->rChild = NULL; return newBook; }
int InsertBook(AVLBook &book, string bookNumber, bool &flag) { if (book == NULL) { book = createBook(bookNumber); flag = true; } else if (book->bookNumber == bookNumber) { book->numNow++; book->numTotal++; flag = false; return 0; } else if (bookNumber < book->bookNumber) { if (!InsertBook(book->lChild, bookNumber, flag)) return 0; if (flag) { switch (book->BF) { case 1: leftBalance(book); flag = false; break; case -1: book->BF = 0; flag = false; break; case 0: book->BF = 1; flag = true; break; } } } else { if (!InsertBook(book->rChild, bookNumber, flag)) return 0; if (flag) { switch (book->BF) { case 1: book->BF = 0; flag = false; break; case -1: rightBalance(book); flag = false; break; case 0: book->BF = -1; flag = true; break; } } } return 1; }
void addStudent(StudentBorrowedBook &stu, string studentNumber) { Student *newStu = new Student[stu.sum + 1]; Student *oldStu = stu.student;
for (int i = 0; i < stu.sum; i++) { newStu[i] = stu.student[i]; } newStu[stu.sum].studentNumber = studentNumber; newStu[stu.sum].firstBook = new BorrowedBook; newStu[stu.sum].firstBook->next = NULL;
stu.student = newStu; stu.sum++; }
bool IsExistStu(StudentBorrowedBook stu, string numberStu) { for (int i = 0; i < stu.sum; i++) { if (stu.student[i].studentNumber == numberStu) { return true; } } return false; }
bool findStudent(StudentBorrowedBook stu, string numberStu, Student &res) { for (int i = 0; i < stu.sum; i++) { if (stu.student[i].studentNumber == numberStu) { res = stu.student[i]; return true; } } return false; }
bool IsBook(Student stu, string bookNumber) { BorrowedBook *p = stu.firstBook->next; while (p) { if (p->bookNumber == bookNumber) { cout << "此学生借过此书了!" << endl; return true; } p = p->next; } return false; }
bool IsOverTime(BorrowedBook *book, string time) { return time < book->deadLine; }
bool IsStuBookOverTime(Student stu, string time) { BorrowedBook *p = stu.firstBook->next; while (p) { if (IsOverTime(p, time)) { cout << "有超时未归还的图书,不允许借书!" << endl; return true; } p = p->next; } return false; }
void BorrowBook(Student &stu, string bookNumber) { cout << "请输入归还日期:" << endl; string deadLine; cin >> deadLine;
BorrowedBook *newBook = new BorrowedBook; newBook->bookNumber = bookNumber; newBook->deadLine = deadLine;
newBook->next = stu.firstBook->next; stu.firstBook->next = newBook; }
bool ReturnBook(Student &stu, string bookNumber) { BorrowedBook *delBook, *p = stu.firstBook->next, *q = stu.firstBook; while (p) { if (p->bookNumber == bookNumber) { q->next = p->next; return true; } q = p; p = p->next; } return false; }
void deleteStudent(StudentBorrowedBook &stu, string studentNum) { for (int i = 0; i < stu.sum; i++) { bool flag = true; if (stu.student[i].studentNumber == studentNum) { flag = false; for (int j = i + 1; j < stu.sum; j++) { stu.student[j - 1] = stu.student[j]; } } if (!flag) { break; } } stu.sum--; }
int SearchTreeBorrow(AVLBook book, StudentBorrowedBook &stu, Student &student, string bookNumber, string time) { if (book->bookNumber == bookNumber) {
if (book->numNow > 0) { if (!IsStuBookOverTime(student, time)) { if (!IsBook(student, book->bookNumber)) { BorrowBook(student, book->bookNumber); book->numNow--; cout << "学生" << student.studentNumber << "借了一本《" << book->name << "》,作者为" << book->author; cout << ",书号为:" << book->bookNumber << "。库存" << book->numTotal << "本,现存"; cout << book->numNow << "本" << endl; } } } else { cout << "此书已经被借阅完,无余书!" << endl; } return 1; } else if (bookNumber > book->bookNumber && book->rChild) { return SearchTreeBorrow(book->rChild, stu,student, bookNumber, time); } else if (bookNumber < book->bookNumber && book->lChild) { return SearchTreeBorrow(book->lChild, stu,student, bookNumber, time); } else { cout << "查无此书!" << endl; if (student.firstBook->next == NULL) { deleteStudent(stu, student.studentNumber); } return 0; } }
int SearchTreeReturn(AVLBook book, StudentBorrowedBook &stu, Student &student, string bookNumber) { if (book->bookNumber == bookNumber) {
if (ReturnBook(student, book->bookNumber)) { book->numNow++; cout << "学生" << student.studentNumber << "还了一本《" << book->name << "》,作者为" << book->author; cout << ",书号为:" << book->bookNumber << "。库存" << book->numTotal << "本,现存"; cout << book->numNow << "本" << endl;
if (student.firstBook->next == NULL) { deleteStudent(stu, student.studentNumber); } } else { cout << "此学生未借此书,无法归还!" << endl; }
return 1; } else if (bookNumber > book->bookNumber && book->rChild) { return SearchTreeReturn(book->rChild, stu, student, bookNumber); } else if (bookNumber < book->bookNumber && book->lChild) { return SearchTreeReturn(book->lChild, stu, student, bookNumber); } else { cout << "查无此书!" << endl; return 0; } }
void display(StudentBorrowedBook stu) { for (int i = 0; i < stu.sum; i++) { BorrowedBook *p = stu.student[i].firstBook->next; cout << "此人借书号为:" << stu.student[i].studentNumber << " 所借书籍的书号有: "; while (p) { cout << p->bookNumber << " "; p = p->next; } cout << endl; } }
void InOrderTraverse(AVLBook T) { if (T == NULL) { return; }
InOrderTraverse(T->lChild); cout << "书号:" << T->bookNumber << " 现存量:" << T->numNow << " "; InOrderTraverse(T->rChild); }
void printT(AVLBook T, int n) { if (T) { printT(T->rChild, n + 1); for (int i = 0; i < n; i++) { cout << " "; } if (n >= 0) { cout << T->bookNumber << endl; } printT(T->lChild, n + 1); } }
|