标签:public ade == 参数 ati 显示 判断 ptime using
链表:
要求:
定义一个包含学生信息(学号,姓名,成绩)的顺序表和链表,使其具有如下功能:
(1) 根据指定学生个数,逐个输入学生信息;
(2) 逐个显示学生表中所有学生的相关信息;
(3) 根据姓名进行查找,返回此学生的学号和成绩;
(4) 根据指定的位置可返回相应的学生信息(学号,姓名,成绩);
(5) 给定一个学生信息,插入到表中指定的位置;
(6) 删除指定位置的学生记录;
(7) 统计表中学生个数。
typedef struct { char no[8]; //8位学号 char name[20]; //姓名 int score; //成绩 }Student;
1 typedef struct LNode{ 2 Student data; //数据域 3 struct LNode *next; //指针域 4 }LNode,*LinkList;
完整代码:
1 /* 2 * 一个简单的学生信息处理程序 3 */ 4 5 #include <iostream> 6 #include <cstdio> 7 #include <fstream> 8 #include <unistd.h> 9 #include <cstdlib> 10 #include <cstddef> 11 #include <string> 12 using std::cin; 13 using std::cout; 14 using std::endl; 15 using std::ofstream; 16 using std::string; 17 18 struct StuData //学生信息 19 { 20 string stuID; //学生学号 21 string stuName; //学生姓名 22 double stuScore; //学生成绩 23 }; 24 25 class LinkList 26 { 27 public: 28 LinkList( ) { } //构造函数,对成员变量初始化 29 LinkList(StuData info, LinkList *previous, LinkList *next) 30 : stuInfo(info), previousLink(previous), nextLink(next) 31 { } 32 LinkList *getPreviousLink( ) //取值 33 { 34 return previousLink; 35 } 36 LinkList *getNextLink( ) 37 { 38 return nextLink; 39 } 40 StuData &getData( ) 41 { 42 return stuInfo; 43 } 44 void setPreviousLink(LinkList *pointer) //赋值 45 { 46 previousLink = pointer; 47 } 48 void setNextLink(LinkList *pointer) 49 { 50 nextLink = pointer; 51 } 52 private: 53 StuData stuInfo; //学生信息 54 LinkList *previousLink; //指向前节点 55 LinkList *nextLink; //指向后节点 56 }; 57 typedef LinkList* linkPtr; 58 59 void welcome(unsigned &choose); 60 /* 功能:在控制台输出欢迎界面 61 * 参数:choose的引用 62 * 返回值:无 63 */ 64 65 void headInsert(linkPtr &head, StuData &info); 66 /* 67 * 功能:在链表头部插入节点 68 * 参数:head:链表头节点的引用 info:需要插入学生信息 69 * 返回值:无 70 */ 71 72 void insert(linkPtr &head, StuData &info, int order); 73 /* 74 * 功能:在指定的位置插入学生信息 75 * 参数:head:链表头节点的引用 info:需要插入的学生信息 order: 插入到链表中的位置】 76 * 返回值: 无 77 */ 78 79 void add(linkPtr &head, StuData &info); 80 /* 81 * 功能:(在尾部)增加学生信息 82 * 参数:head:链表头节点的引用 info: 需要增加的学生信息 83 * 返回值: 无 84 */ 85 86 linkPtr search(linkPtr &head, string &target); 87 /* 88 * 功能:对于输入的ID/Name, 遍历查找各数据元素中是否存在相应的数据项 89 * 参数:head:链表头节点的引用 target: 需要查找的ID/Name 90 * 返回值:如果存在对应的匹配,返回其所在的节点指针,否则返回NULL 91 */ 92 93 bool isContain(const linkPtr &head, const string &target); 94 /* 95 * 功能:判断输入的ID是否已经存在,增强健壮性 96 * 参数:head:链表头节点的指针的引用 target:输入的学号 97 * 返回值:若已经存在相应的学号,返回true, 否则返回false 98 */ 99 void deleteNode(linkPtr &head, linkPtr &discard); 100 /* 101 * 功能:删除指定的数据项 102 * 参数:head:链表头节点指针的引用 discard: 需要删除的数据项的指针的引用 103 * 返回值:无 104 */ 105 106 void print(linkPtr p, char type=‘o‘); 107 /* 108 * 功能:打印单个或所有信息 109 * 参数:p: 指向某个数据项 type: 打印类型,默认输出一个数据元素,为‘a‘时 110 * 表示输出p后所有数据项 111 */ 112 void save(linkPtr &head, ofstream &allStuData); 113 /* 114 * 功能:将输入的所有数据项保存到文件中 115 * 参数:head: 链表头节点指针的引用 allStuData: 文件输出流,保存到其指向的文件 116 * 返回值:无 117 */ 118 119 int main(void) 120 { 121 linkPtr head = NULL; 122 StuData info; 123 unsigned choose; 124 125 while (true) 126 { 127 fflush(stdin); 128 system("color 2F"); 129 char ans = ‘y‘; 130 welcome(choose); 131 switch (choose) 132 { 133 case 1: 134 { 135 system("color 8F"); 136 while (ans == ‘y‘) 137 { 138 printf("Please enter data:\n"); 139 printf("ID: "); 140 cin >> info.stuID; 141 printf("Name: "); 142 getchar(); 143 getline(cin, info.stuName); 144 printf("Score: "); 145 cin >> info.stuScore; 146 add(head, info); 147 fflush(stdin); 148 printf("Continue![y]: "); 149 ans = getchar( ); 150 if (ans==‘\n‘) 151 { 152 ans = ‘y‘; 153 } 154 system("cls"); 155 } 156 157 break; 158 } 159 case 2: 160 { 161 system("color 6F"); 162 string ID_Name; 163 linkPtr locate; 164 while (ans == ‘y‘) 165 { 166 printf("Please give the stu‘s ID/Name: "); 167 getchar(); 168 getline(cin, ID_Name); 169 locate = search(head, ID_Name); 170 if (locate != NULL) 171 { 172 putchar(‘\n‘); 173 print(locate, ‘o‘); 174 } 175 else 176 { 177 printf("\nWarning: You entered information is not exist!\n"); 178 } 179 putchar(‘\n‘); 180 fflush(stdin); 181 printf("Continue![y]: "); 182 scanf("%c", &ans); 183 if (ans==‘\n‘) 184 { 185 ans = ‘y‘; 186 } 187 system("cls"); 188 } 189 break; 190 } 191 case 3: 192 { 193 system("color B4"); 194 linkPtr discard; 195 string litter; 196 printf("Please input the ID/Name of the student you want to delete: "); 197 fflush(stdin); 198 getline(cin, litter); 199 discard = search(head, litter); 200 if (discard != NULL) 201 { 202 deleteNode(head, discard); 203 printf("\aDelete succeed!\n"); 204 } 205 getchar( ); 206 system("cls"); 207 208 break; 209 } 210 case 4: 211 { 212 system("color DF"); 213 print(head, ‘a‘); 214 system("cls"); 215 break; 216 } 217 case 5: 218 { 219 ofstream allStuData; 220 allStuData.open("stuInformation.dat"); 221 save(head, allStuData); 222 allStuData.close(); 223 system("cls"); 224 break; 225 } 226 case 6: 227 { 228 printf("\a\n\n\t\tBye Bye!\n"); 229 sleep(1); 230 system("color 0F"); 231 exit(0); 232 break; 233 } 234 235 default : 236 { 237 printf("\a\n\tSorry! I haven‘t been creat this operation!\n"); 238 sleep(2); 239 system("cls"); 240 break; 241 } 242 } 243 } 244 245 return 0; 246 } 247 248 249 void welcome(unsigned &choose) 250 { 251 printf("\n\n\n\n\n WELCOME\n"); 252 string reticule = "-------------------------------------------------------\n"; 253 string prompt1 = "-- 1.增加或建立学生信息\n"; 254 string prompt2 = "-- 2.根据姓名或学号找人\n"; 255 string prompt3 = "-- 3.删除不必要学生信息\n"; 256 string prompt4 = "-- 4.显示所有的学生信息\n"; 257 string prompt5 = "-- 5.保存\n"; 258 string prompt6 = "-- 6.退出\n\n"; 259 unsigned sleepTime = 900; 260 261 cout << reticule << endl; 262 for (unsigned i=0; i<prompt1.length(); i++) 263 { 264 usleep(sleepTime); 265 putchar(prompt1[i]); 266 fflush(stdout); 267 } 268 for (unsigned i=0; i<prompt2.length(); i++) 269 { 270 usleep(sleepTime); 271 putchar(prompt2[i]); 272 fflush(stdout); 273 } 274 for (unsigned i=0; i<prompt3.length(); i++) 275 { 276 usleep(sleepTime); 277 putchar(prompt3[i]); 278 fflush(stdout); 279 } 280 for (unsigned i=0; i<prompt4.length(); i++) 281 { 282 usleep(sleepTime); 283 putchar(prompt4[i]); 284 fflush(stdout); 285 } 286 for (unsigned i=0; i<prompt5.length(); i++) 287 { 288 usleep(sleepTime); 289 putchar(prompt5[i]); 290 fflush(stdout); 291 } 292 for (unsigned i=0; i<prompt6.length(); i++) 293 { 294 usleep(sleepTime); 295 putchar(prompt6[i]); 296 fflush(stdout); 297 } 298 cout << reticule << endl; 299 printf("你想进行什么操作: "); 300 scanf("%u", &choose); 301 system("cls"); 302 } 303 304 void headInsert(linkPtr &head, StuData &info) 305 { 306 linkPtr newHead = new LinkList(info, NULL, head); 307 newHead->setPreviousLink(newHead); 308 head = newHead; 309 } 310 311 void insert(linkPtr &head, StuData &info, int order) 312 { 313 linkPtr p = head; 314 for (int i=0; i<order; i++) 315 { 316 p = p->getNextLink( ); 317 } 318 linkPtr next = p->getNextLink( ); 319 p->setNextLink(new LinkList(info, p, p->getNextLink( ))); 320 p = p->getNextLink( ); 321 next->setPreviousLink(p); 322 } 323 324 void add(linkPtr &head, StuData &info) 325 { 326 static unsigned int count = 0; 327 if (isContain(head, info.stuID)) 328 { 329 printf("\a\n\n\t\t!!!Warning: This stu‘s ID have already exist!!!‘\n"); 330 return; 331 } 332 else 333 { 334 if (head == NULL) 335 { 336 head = new LinkList(info, NULL, NULL); 337 } 338 else 339 { 340 static linkPtr point = head; 341 while (point->getNextLink() != NULL) 342 { 343 point = point->getNextLink( ); 344 } 345 point->setNextLink(new LinkList(info, point, NULL)); 346 point = point->getNextLink( ); 347 } 348 count++; 349 printf("\nAdd succeed!\t\t\tstu‘s num %u\n", count); 350 } 351 352 } 353 354 linkPtr search(linkPtr &head, string &target) 355 { 356 linkPtr here = head; 357 if (here == NULL) 358 { 359 printf("\a\n\t\t!!! Warning: NO data exist !!!\n\n"); 360 return NULL; 361 } 362 else 363 { 364 while (here != NULL) 365 { 366 StuData ans = here->getData( ); 367 if (ans.stuID == target || ans.stuName == target) 368 { 369 return here; 370 } 371 here = here->getNextLink( ); 372 } 373 return NULL; 374 } 375 } 376 377 bool isContain(const linkPtr &head, const string &target) 378 { 379 linkPtr here = head; 380 while (here != NULL) 381 { 382 StuData ans = here->getData( ); 383 if (ans.stuID == target || ans.stuName == target) 384 { 385 return true; 386 } 387 else 388 { 389 here = here->getNextLink( ); 390 } 391 } 392 return false; 393 } 394 395 void deleteNode(linkPtr &head, linkPtr &discard) 396 { 397 if (head == discard) 398 { 399 head = head->getNextLink( ); 400 if (head != NULL) 401 { 402 head->setPreviousLink(NULL); 403 } 404 405 } 406 else 407 { 408 linkPtr prev = discard->getPreviousLink( ); 409 linkPtr next = discard->getNextLink( ); 410 prev->setNextLink(next); 411 if (next != NULL) 412 { 413 next->setPreviousLink(prev); 414 } 415 } 416 417 delete discard; 418 } 419 420 void print(linkPtr p, char type) 421 { 422 switch (type) 423 { 424 case ‘o‘: 425 { 426 StuData ans = p->getData( ); 427 cout << "- "; 428 cout << ans.stuID; 429 cout << "\t\t"; 430 cout << ans.stuName; 431 cout << "\t\t"; 432 cout << ans.stuScore; 433 cout << endl; 434 break; 435 } 436 case ‘a‘: 437 { 438 linkPtr point = p; 439 printf("All of the students are:\n"); 440 printf(" ID Name Score\n"); 441 printf(" -------------------------------------------------\n"); 442 if (p == NULL) 443 { 444 printf("\n\tError: No data exist!\n"); 445 } 446 else 447 { 448 unsigned count=0; 449 while (point != NULL) 450 { 451 printf("%3d- ", ++count); 452 StuData ans = point->getData( ); 453 cout << ans.stuID; 454 cout << "\t\t"; 455 cout << ans.stuName; 456 cout << "\t\t\t"; 457 cout << ans.stuScore; 458 cout << endl; 459 point = point->getNextLink( ); 460 } 461 } 462 printf("\n -------------------------------------------------\n"); 463 getchar(); 464 getchar(); 465 break; 466 } 467 } 468 } 469 470 void save(linkPtr &head, ofstream &allStuData) 471 { 472 linkPtr point = head; 473 allStuData << "All of the students are:\n"; 474 allStuData << " ID Name Score\n"; 475 allStuData << " ----------------------------------------------\n"; 476 if (point == NULL) 477 { 478 allStuData << "\n\tError: No data exist!\n"; 479 } 480 else 481 { 482 int count=0; 483 while (point != NULL) 484 { 485 StuData ans = point->getData( ); 486 allStuData << ""; 487 allStuData << (++count) << " - "; 488 allStuData << ans.stuID; 489 allStuData << "\t\t"; 490 allStuData << ans.stuName; 491 allStuData << "\t\t"; 492 allStuData << ans.stuScore; 493 allStuData << endl; 494 point = point->getNextLink( ); 495 } 496 } 497 allStuData << "\a\n ---------------------------------------------\n"; 498 printf("\a\n\n\t\tSaving successfully!\n"); 499 sleep(1); 500 }
标签:public ade == 参数 ati 显示 判断 ptime using
原文地址:http://www.cnblogs.com/lymboy/p/7750257.html