简易的学生成绩管理系统(C++实现)
最近浅显的学习了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 }
下面是程序运行的结果:
初始界面:
插入信息:
连续插入后显示:
修改信息:
修改后结果:
如有错误,欢迎批评指针哈~
少一些功利主义的追求,多一些不为什么的坚持!