最近淺顯的學習了C++的基礎知識,想來練練手,於是就用單鏈表寫了最經典的小項目,存粹學習,所以就在控制台下寫了,寫的有點簡陋,碼了大概400多行。
下面上代碼:
1 #include <cstdlib> 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 6 #define null NULL 7 8 class student 9 { 10 private: 11 friend class studentMessage; 12 student *next; //節點指針 13 string name; //學生姓名 14 int age; //年紀 15 int id; //學號 16 double score[3]; //三科成績 17 double total; //總分 18 double average; //平均成績 19 public: 20 student(string _name,int _age,int _id,double *_score) 21 { 22 name = _name; 23 age = _age; 24 id = _id; 25 score[0] = _score[0]; 26 score[1] = _score[1]; 27 score[2] = _score[2]; 28 total = score[0]+score[1]+score[2]; 29 average = total/3; 30 next = NULL; 31 } 32 student() //為studentMessage初始化頭結點用 33 { 34 name = "null"; 35 age = 0; 36 id = 0; 37 score[0]=score[1]=score[2]=0; 38 total = 0; 39 average = 0; 40 next = NULL; 41 } 42 ~student(){} 43 void swap(student*); 44 }; 45 46 47 void student::swap(student *q) 48 { 49 string _name; 50 int _age,_id; 51 double _score[3],_total,_average; 52 53 _name = name; 54 name = q->name; 55 q->name = _name; 56 57 _age = age; 58 age = q->age; 59 q->age = _age; 60 61 _id = id; 62 id = q->id; 63 q->id = _id; 64 65 _score[0] = score[0]; 66 score[0] = q->score[0]; 67 q->score[0] = _score[0]; 68 69 _score[1] = score[1]; 70 score[1] = q->score[1]; 71 q->score[1] = _score[1]; 72 73 _score[2] = score[2]; 74 score[2] = q->score[2]; 75 q->score[2] = _score[2]; 76 77 _total = total; 78 total = q->total; 79 q->total = _total; 80 81 _average = average; 82 average = q->average; 83 q->average = _average; 84 } 85 86 87 88 89 90 91 class studentMessage 92 { 93 private: 94 student *first; //頭指針 95 int num; //信息中的學生人數 96 public: 97 studentMessage() 98 { 99 num = 0; //初始化學生人數為0 100 first = new student; //初始化頭結點 101 } 102 ~studentMessage(){delete first;} 103 104 /*管理系統常規操作*/ 105 void Insret(void); //插入 106 void Display(void); //顯示 107 void Delete(void); //刪除 108 void Search(void); //搜索 109 void Change(void); //改動 110 void sortByLesson1(void); //按成績一來排序 111 void sortByLesson2(void); //按成績二來排序 112 void sortByLesson3(void); //按成績三來排序 113 void sortByTotal(void); //按總分來排序 114 void SearchByid(void); //按照學號查找 115 void SearchByname(void); //按照姓名查找 116 int menu(void); //初始的菜單 117 void clear(void); //清空列表 118 }; 119 120 121 int studentMessage::menu(void) 122 { 123 int ch; 124 cout<<"**********************************************************************"<<endl; 125 cout<<"======================================================================"<<endl; 126 cout<<"***************************學生成績管理系統***************************"<<endl;cout<<endl; 127 cout<<"1.顯示所有學生成績"<<endl; 128 cout<<"2.添加學生信息"<<endl; 129 cout<<"3.查詢學生信息"<<endl; 130 cout<<"4.修改學生信息"<<endl; 131 cout<<"5.刪除最下面一個學生信息"<<endl; 132 cout<<"6.刪除所有信息"<<endl; 133 cout<<"0.退出系統"<<endl;cout<<endl; 134 cout<<"********************Copyright@ By Jeaven Wong**************************"<<endl; 135 cout<<"======================================================================="<<endl; 136 cout<<"***********************************************************************"<<endl; 137 cin >> ch; 138 cout<<"\n\n\n"<<endl; 139 return ch; 140 } 141 142 143 144 //插入 145 void studentMessage::Insret(void) 146 { 147 string name; 148 int age; 149 int id; 150 double score[3]; 151 cout<<"請輸入學生姓名: "; 152 cin>>name; 153 cout<<"請輸入學生年齡: "; 154 cin>>age; 155 cout<<"請輸入學生學號: "; 156 cin>>id; 157 cout<<"下面請輸入學生三門課程成績: "; 158 cout<<endl; 159 cout<<"請輸入第一門課的成績: ";cin>>score[0]; 160 cout<<"請輸入第二門課的成績: ";cin>>score[1]; 161 cout<<"請輸入第三門課的成績: ";cin>>score[2]; 162 cout<<endl; 163 164 165 student *newstu = new student(name,age,id,score); 166 student *p = first; 167 while(p->next != NULL) 168 { 169 p = p->next; 170 } 171 p->next = newstu; 172 newstu->next = null; 173 num++; 174 } 175 176 177 178 void studentMessage::Display(void) 179 { 180 if(num == 0) 181 { 182 cout<<"當前記錄中無學生..."<<endl; 183 } 184 185 else 186 { 187 student *p = first->next; 188 while(p != null) 189 { 190 cout<<"姓名:"<<p->name<<" "; 191 cout<<"年齡:"<<p->age<<" "; 192 cout<<"學號:"<<p->id<<" "; 193 cout<<"三門課程成績: "<<p->score[0]<<" "<<p->score[1]<<" "<<p->score[2]<<" "; 194 cout<<"總分:"<<p->total<<" "; 195 cout<<"平均分:"<<p->average<<endl; 196 p = p->next; 197 } 198 } 199 } 200 201 202 void studentMessage::Delete(void) 203 { 204 student *p = first; 205 student *pre = first; 206 while(p->next != NULL) 207 { 208 pre = p; 209 p = p->next; 210 } 211 pre->next = NULL; 212 delete p; 213 num--; 214 } 215 216 217 void studentMessage::Search(void) 218 { 219 int temp = 0; 220 cout<<"請輸入查找的條件,有如下選項..."<<endl; 221 cout<<"按照學號查找(請輸入【1】) 按照姓名查找(請輸入【2】)"<<endl; 222 cin>>temp; 223 switch(temp) 224 { 225 case 1: SearchByid(); break; 226 case 2: SearchByname(); break; 227 default: cout<<"輸入有誤..."<<endl; 228 } 229 } 230 231 void studentMessage::SearchByid(void) 232 { 233 int _id; 234 int flag = 0; 235 cout<<"請輸入待查找學生的學號:"; 236 cin >> _id; 237 student *p = first->next; 238 while(p != null) 239 { 240 if(p->id == _id) 241 { 242 flag = 1; 243 cout<<"下面是查找匹配結果:"<<endl; 244 cout<<"姓名:"<<p->name<<" "; 245 cout<<"年齡:"<<p->age<<" "; 246 cout<<"學號:"<<p->id<<" "; 247 cout<<"三門課程成績: "<<p->score[0]<<" "<<p->score[1]<<" "<<p->score[2]<<" "; 248 cout<<"總分:"<<p->total<<" "; 249 cout<<"平均分:"<<p->average<<endl; 250 } 251 p = p->next; 252 } 253 if(flag == 0) 254 { 255 cout<<"抱歉,記錄中沒有查找匹配項..."<<endl; 256 } 257 } 258 259 void studentMessage::SearchByname(void) 260 { 261 string _name; 262 int flag = 0; 263 cout<<"請輸入待查找的學生姓名: "; 264 cin >> _name; 265 student *p = first->next; 266 while(p != null) 267 { 268 if(p->name == _name) 269 { 270 flag = 1; 271 cout<<"下面是查找匹配結果:"<<endl; 272 cout<<"姓名:"<<p->name<<" "; 273 cout<<"年齡:"<<p->age<<" "; 274 cout<<"學號:"<<p->id<<" "; 275 cout<<"三門課程成績: "<<p->score[0]<<" "<<p->score[1]<<" "<<p->score[2]<<" "; 276 cout<<"總分:"<<p->total<<" "; 277 cout<<"平均分:"<<p->average<<endl; 278 } 279 p = p->next; 280 } 281 282 if(flag == 0) 283 { 284 cout<<"抱歉,記錄中沒有查找匹配項..."<<endl; 285 } 286 } 287 288 289 290 void studentMessage::Change(void) 291 { 292 string _name; 293 int flag = 0,temp; 294 int _id,_age; 295 int course = 0; 296 cout<<"請輸入需要改動信息的學生的姓名: "; 297 cin >> _name; 298 student *p = first->next; 299 while(p != null) 300 { 301 if(p->name == _name) 302 { 303 flag = 1; 304 cout<<"該學生當前信息如下:"<<endl; 305 cout<<"姓名:"<<p->name<<" "; 306 cout<<"年齡:"<<p->age<<" "; 307 cout<<"學號:"<<p->id<<" "; 308 cout<<"三門課程成績: "<<p->score[0]<<" "<<p->score[1]<<" "<<p->score[2]<<" "; 309 cout<<"總分:"<<p->total<<" "; 310 cout<<"平均分:"<<p->average<<endl; 311 cout<<"請指明哪一項需要修改..."<<endl; 312 cout<<"修改學號(輸入【1】) 修改年齡(輸入【2】)修改成績(輸入【3】)"<<endl; 313 cin >> temp; 314 switch(temp) 315 { 316 case 1: 317 { 318 cout<<"請輸入新的學號:";cin>>_id; 319 p->id = _id; 320 } 321 break; 322 case 2: 323 { 324 cout<<"請輸入新的年齡:";cin>>_age; 325 p->age = _age; 326 } 327 break; 328 case 3: 329 { 330 cout<<"請按指示修改課程成績..."<<endl; 331 cout<<"是否需要修改第一門課程成績?(需要輸入【1】不需要輸入【0】)"<<endl; 332 cin >> course; 333 if(course == 1) 334 { 335 cout<<"請輸入修改后的第一門課的成績:"; cin >> p->score[0]; 336 } 337 course = 0; 338 339 cout<<"是否需要修改第二門課程成績?(需要輸入【1】不需要輸入【0】)"<<endl; 340 cin >> course; 341 if(course == 1) 342 { 343 cout<<"請輸入修改后的第二門課的成績:"; cin >> p->score[1]; 344 } 345 course = 0; 346 347 cout<<"是否需要修改第三門課程成績?(需要輸入【1】不需要輸入【0】)"<<endl; 348 cin >> course; 349 if(course == 1) 350 { 351 cout<<"請輸入修改后的第三門課的成績:"; cin >> p->score[2]; 352 } 353 course = 0; 354 355 p->total = p->score[0]+p->score[1]+p->score[2]; 356 p->average = p->total/3; 357 358 cout<<"修改后的信息如下: "<<endl; 359 cout<<"姓名:"<<p->name<<" "; 360 cout<<"年齡:"<<p->age<<" "; 361 cout<<"學號:"<<p->id<<" "; 362 cout<<"三門課程成績: "<<p->score[0]<<" "<<p->score[1]<<" "<<p->score[2]<<" "; 363 cout<<"總分:"<<p->total<<" "; 364 cout<<"平均分:"<<p->average<<endl; 365 } 366 break; 367 default: cout<<"輸入有誤..."<<endl; 368 } 369 } 370 p = p->next; 371 } 372 if(flag == 0) 373 cout<<"當前記錄中沒有次學生..."<<endl; 374 } 375 376 /*排序我均采用冒泡排序法,均是從小到大排序*/ 377 378 379 //按照科目一排序 380 void studentMessage::sortByLesson1(void) 381 { 382 student *p = first->next; 383 while(p->next != null) 384 { 385 student *q = p->next; 386 while(q != null) 387 { 388 if(p->score[0] > q->score[0]) 389 { 390 p->swap(q); 391 } 392 q = q->next; 393 } 394 p = p->next; 395 } 396 } 397 398 //按照科目二排序 399 void studentMessage::sortByLesson2(void) 400 { 401 student *p = first->next; 402 while(p->next != null) 403 { 404 student *q = p->next; 405 while(q != null) 406 { 407 if(p->score[1] > q->score[1]) 408 { 409 p->swap(q); 410 } 411 q = q->next; 412 } 413 p = p->next; 414 } 415 } 416 417 //按照科目三排序 418 void studentMessage::sortByLesson3(void) 419 { 420 student *p = first->next; 421 while(p->next != null) 422 { 423 student *q = p->next; 424 while(q != null) 425 { 426 if(p->score[2] > q->score[2]) 427 { 428 p->swap(q); 429 } 430 q = q->next; 431 } 432 p = p->next; 433 } 434 } 435 436 //按照總成績排序 437 void studentMessage::sortByTotal(void) 438 { 439 student *p = first->next; 440 while(p->next != null) 441 { 442 student *q = p->next; 443 while(q != null) 444 { 445 if(p->total > q->total) 446 { 447 p->swap(q); 448 } 449 q = q->next; 450 } 451 p = p->next; 452 } 453 } 454 455 void studentMessage::clear(void) 456 { 457 student *p = first->next; 458 while(p != null) 459 { 460 first->next = p->next; 461 p->next = null; 462 delete p; 463 p = first->next; 464 } 465 } 466 467 468 469 int main() 470 { 471 studentMessage stulist; 472 int ch; 473 while(ch = stulist.menu()) 474 { 475 switch(ch) 476 { 477 case 1: stulist.Display(); break; 478 case 2: stulist.Insret(); break; 479 case 3: stulist.Search(); break; 480 case 4: stulist.Change(); break; 481 case 5: stulist.Delete(); break; 482 case 6: stulist.clear(); break; 483 default: cout<<"請按要求輸入..."<<endl; 484 } 485 } 486 return 0; 487 }
下面是程序運行的結果:
初始界面:

插入信息:


連續插入后顯示:

修改信息:

修改后結果:

如有錯誤,歡迎批評指針哈~
