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
| #include "generation.h" #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue>
void newLeft(BiTree& p, Info One) { BitNode* q = new BitNode; q->person = One; q->lc = q->rc = NULL; p->lc = q; }
void newRight(BiTree& p, Info One) { BitNode* q = new BitNode; q->person = One; q->lc = q->rc = NULL; p->rc = q; }
BiTree createTree(Info Human[]) { BiTree Bt; Bt = new BitNode; Bt->person = Human[0]; Bt->lc = Bt->rc = NULL; newLeft(Bt, Human[1]); newRight(Bt, Human[2]); newLeft(Bt->lc, Human[3]); newRight(Bt->lc, Human[4]); newLeft(Bt->rc, Human[5]); newRight(Bt->rc, Human[6]); newLeft(Bt->lc->lc, Human[7]); newRight(Bt->lc->lc, Human[8]); newLeft(Bt->lc->rc, Human[9]); newRight(Bt->lc->rc, Human[10]); newRight(Bt->rc->lc, Human[11]); return Bt; }
void outputTitle() { printf("%-10s", "姓名"); printf("%-5s", "性别"); printf("%-10s", "出生日期"); printf("%-5s", "婚否"); printf("%-12s", "地址"); printf("%-5s", "健在"); printf("%s\n", "死亡日期"); }
void outputInfo(BiTree p) { if (p) { printf("%-10s", p->person.name); if (p->person.sex == 'M') printf("%-6s", "男"); else printf("%-6s", "女"); printf("%-10s", p->person.birth); if (p->person.wedding) printf("%-6s", "是"); else printf("%-6s", "否"); printf("%-12s", p->person.address); if (p->person.alive) printf("%-6s", "是"); else printf("%-6s", "否"); printf("%s\n", p->person.death); } else printf("查无此人\n"); }
void levelTrav(BiTree Tree) { BiTree Q[20]; int front = 0, rear = 0; outputTitle(); if (Tree) { rear++; Q[rear] = Tree; while (front != rear) { front = (front + 1) % 20; BitNode* p = Q[front]; outputInfo(p); if ((rear + 1) % 20 != front && p->lc != NULL) { rear = (rear + 1) % 20; Q[rear] = p->lc; } if ((rear + 1) % 20 != front && p->rc != NULL) { rear = (rear + 1) % 20; Q[rear] = p->rc; } } } }
void showNGene(BiTree p, int n) { static int depth = 0; depth++; if (p) { if (depth == n) outputInfo(p); showNGene(p->lc, n); showNGene(p->rc, n); } depth--; }
void showFamily(BiTree Tree) { std::queue<BiTree> q1, q2; int i = 0; if (Tree) { q1.push(Tree); while (!q1.empty()) { while (!q1.empty()) { q2.push(q1.front()); q1.pop(); } i++; switch (i) { case 1: printf("第%d代: ", i); break; case 2: printf("第%d代: ", i); break; case 3: printf("第%d代: ", i); break; case 4: printf("第%d代:", i); break; default: break; } while (!q2.empty()) { BitNode* tmp = q2.front(); q2.pop(); printf("%s ", tmp->person.name); if (tmp->lc) q1.push(tmp->lc); if (tmp->rc) q1.push(tmp->rc); } printf("\n"); } } }
BiTree findParent(BiTree BT, BitNode* p) { BiTree L_result, R_result; if (!BT || BT == p) return NULL; if (BT->lc == p || BT->rc == p) return BT; else { L_result = findParent(BT->lc, p); R_result = findParent(BT->rc, p); return L_result ? L_result : (R_result ? R_result : NULL); } }
BiTree searchName(BiTree BT, char* str) { BiTree L_result, R_result; if (!BT) return NULL; if (strcmp(BT->person.name, str) == 0) return BT; else { L_result = searchName(BT->lc, str); R_result = searchName(BT->rc, str); return L_result ? L_result : (R_result ? R_result : NULL); } }
void findChild(BiTree p, BiTree *child) { child[0] = p->lc ? p->lc : NULL; child[1] = p->rc ? p->rc : NULL; }
void searchThreeGeneration(BiTree Tree) { char name[20]; BiTree parent, trnode, child[2]; printf("请输入被查找成员的姓名:"); scanf_s("%s", name, 20); trnode = searchName(Tree, name); if (!trnode) printf("查无此人\n"); else { parent = findParent(Tree, trnode); findChild(trnode, child); printf("此人信息为:\n"); outputTitle(); outputInfo(trnode); printf("他的父亲结点的信息为:\n"); if (parent) outputTitle(); outputInfo(parent); printf("他的左孩子的信息为:\n"); if (child[0]) outputTitle(); outputInfo(child[0]); printf("他的右孩子的信息为:\n"); if (child[1]) outputTitle(); outputInfo(child[1]); } }
BiTree findBirthday(BiTree BT, char* birth) { BiTree L_result, R_result; if (!BT) return NULL; if (strcmp(BT->person.birth, birth) == 0) return BT; else { L_result = findBirthday(BT->lc, birth); R_result = findBirthday(BT->rc, birth); return L_result ? L_result : (R_result ? R_result : NULL); } }
void searchBirthday(BiTree Tree) { char birth[10]; printf("请输入要查找的成员的生日:"); scanf_s("%s", birth, 10); BitNode* p = findBirthday(Tree, birth); if (!p) { printf("查无此人\n"); return; } outputTitle(); outputInfo(p); }
bool isSameFather(BiTree BT, BitNode* p, BitNode* q) { BiTree f1 = findParent(BT, p), f2 = findParent(BT, q); if (f1 == f2) return true; else return false; }
void judgeRelationship(BiTree Tree) { char name1[20], name2[20]; printf("请输入第一个人的姓名:"); scanf_s("%s", name1, 20); printf("请输入第二个人的姓名:"); scanf_s("%s", name2, 20); BiTree s1 = searchName(Tree, name1), s2 = searchName(Tree, name2); if (isSameFather(Tree, s1, s2)) printf("两人是亲兄弟\n"); else { BiTree f1 = findParent(Tree, s1), f2 = findParent(Tree, s2); if (isSameFather(Tree, f1, f2)) printf("两人是堂兄弟\n"); else if (s1->lc == s2) printf("%s是%s的左孩子\n", s2->person.name, s1->person.name); else if (s2->lc == s1) printf("%s是%s的左孩子\n", s1->person.name, s2->person.name); else if (s1->rc == s2) printf("%s是%s的右孩子\n", s2->person.name, s1->person.name); else if (s2->rc == s1) printf("%s是%s的右孩子\n", s1->person.name, s2->person.name); else printf("关系有点远,年长就叫老祖宗,同龄人就叫美女帅哥吧~\n"); } }
void addChild(BiTree& Tree) { char name_get_child[20]; Info new_Child; printf("请输入要为其添加孩子的人的名字:"); scanf_s("%s", name_get_child, 20); BitNode* p = searchName(Tree, name_get_child); if (p == NULL) { printf("查无此人\n"); return; } if (p->lc && p->rc) printf("此人已有两个孩子,添加失败!\n"); else { printf("请输入孩子的姓名:"); scanf_s("%s", new_Child.name, 20); printf("请输入孩子的出生日期:"); scanf_s("%s", new_Child.birth, 10); printf("如果孩子结婚了,请输入1, 否则输入0:"); int i; scanf_s("%d", &i); if (i != 1 && i != 0) printf("您输入了非法数字,系统将自动处理!\n"); new_Child.wedding = i % 2; printf("请输入孩子的地址:"); scanf_s("%s", new_Child.address, 20); printf("如果孩子健在,请输入1, 否则输入0:"); scanf_s("%d", &i); if (i != 1 && i != 0) printf("您输入了非法数字,系统将自动处理!\n"); new_Child.alive = i % 2; if (!i) { printf("请输入孩子的死亡时间:"); scanf_s("%s", new_Child.death, 10); } else strcpy_s(new_Child.death, "null"); if (p->lc) newLeft(p, new_Child); else newRight(p, new_Child); printf("添加成功!\n"); } }
void deletePerson(BiTree& Tree) { char name_be_deleted[20]; printf("请输入要删除的人的名字:"); scanf_s("%s", name_be_deleted, 20); BitNode* p = searchName(Tree, name_be_deleted); if (p == NULL) { printf("查无此人\n"); return; } if (p->lc || p->rc) { printf("请注意,此人的后代也将一并删除!\n确认删除吗?(Y/N) "); char ch; scanf_s(" %c", &ch, 1); if (ch == 'N' || ch == 'n') return; } BitNode* f = findParent(Tree, p); if (f) { if (f->lc == p) { f->lc = NULL; delete p; } if (f->rc == p) { f->rc = NULL; delete p; } } else Tree = NULL; printf("删除成功!\n"); }
void updateInfo(BiTree Tree) { char name_change[20]; printf("请输入要修改信息的成员的姓名:"); scanf_s("%s", name_change, 20); BitNode* p = searchName(Tree, name_change); if (p == NULL) { printf("查无此人\n"); return; } char tmp[20]; printf("请输入姓名,0为不修改:"); scanf_s("%s", tmp, 20); if (tmp[0] != '0') strcpy_s(p->person.name, tmp); printf("请输入出生日期,no为不修改:"); scanf_s("%s", tmp, 20); if (tmp[0] != 'n') strcpy_s(p->person.birth, tmp); printf("如果结婚了,请输入1, 否则输入0,输入-1则为不修改:"); int i; scanf_s("%d", &i); if (i != -1) { if (i != 1 && i != 0) printf("您输入了非法数字,系统将自动处理!\n"); p->person.wedding = i % 2; } printf("请输入地址,0为不修改:"); scanf_s("%s", tmp, 20); if (tmp[0] != '0') strcpy_s(p->person.address, tmp); printf("如果健在,请输入1, 否则输入0,输入-1则为不修改:"); scanf_s("%d", &i); if (i != -1) { if (i != 1 && i != 0) printf("您输入了非法数字,系统将自动处理!\n"); p->person.alive = i % 2; } if (!i) { printf("请输入死亡时间,no为不修改:"); scanf_s("%s", tmp, 20); if (tmp[0] != 'n') strcpy_s(p->person.death, tmp); } else strcpy_s(p->person.death, "null");
printf("更新成功!\n"); }
void transportBirth(BiTree BT, char bir[][20], int& i) { if (BT) { strcpy_s(bir[i++], BT->person.birth); transportBirth(BT->lc, bir, i); transportBirth(BT->rc, bir, i); } }
void sortBirthday(BiTree Tree, int n) { char birth[20][20]; transportBirth(Tree, birth, n); for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (strcmp(birth[i], birth[j]) > 0) { char tmp[20]; strcpy_s(tmp, birth[i]); strcpy_s(birth[i], birth[j]); strcpy_s(birth[j], tmp); } outputTitle(); for (int i = 0; i < n; i++) { BitNode* p = findBirthday(Tree, birth[i]); outputInfo(p); } printf("\n"); }
|