https://blog.csdn.net/yanxiaolx/article/details/53393437
題目:用c++模擬實現一個學生成績的信息管理系統,要求能添加、刪除、修改、查看和保存學生的信息等功能
源代碼如下:
- #define??_CRT_SECURE_NO_WARNINGS??
- ??
- #include<iostream>??
- using?namespace?std;??
- #include<string.h>??
- #include<fstream>??
- ??
- class?student??
- {??
- private:??
- ????student*?next;??
- public:??
- ????char?stu_num[15];??????????????????//學號??
- ????char?stu_name[30];????????????????//姓名??
- ????float?stu_score;??????????????????????//成績??
- ??
- ????void?afterInsert(student?*p);//在該節點后插入一個節點??
- ????void?afterDelete();//在該節點后刪除一個節點??
- ??????
- ????student?*getNext()//獲得下一個節點的指針??
- ????{???
- ????????return?next;???
- ????}??
- ??
- ????/***********查詢學生信息************/??
- ????void?getMage();??
- ??
- ????/******學生信息修改******/??
- ????void?changeMage(int?n,?char?*ptr);??
- ????void?changegrade(float?p);??
- ??
- ????/******構造*****/??
- ????student(char?*num,?char?*name,?float?score);??
- ????student();??
- };??
- ??
- void?student::changegrade(float?p)??
- {??
- ????stu_score?=?p;??
- }??
- ??
- student::student()???????????//構造??
- {??
- ????strcpy(stu_num,?"\0");??
- ????strcpy(stu_name,?"\0");??
- ????stu_score?=?0;??
- ????next?=?'\0';??
- }??
- ??
- student::student(char?*num,?char?*name,?float?score)??
- {??
- ????strcpy(stu_num,?num);??
- ????strcpy(stu_name,?name);??
- ????stu_score?=?score;??
- ????next?=?'\0';??
- }??
- ??
- void?student::afterInsert(student?*p)//插入節點??
- {??
- ????p->next?=?next;??
- ????next?=?p;??
- }??
- ??
- void?student::afterDelete()????????//刪除節點??
- {??
- ????student?*p?=?next;??
- ????next?=?p->next;??
- ????delete?p;??
- }??
- ??
- void?student::getMage()?????????????//獲得信息??
- {??
- ????cout?<<?"學號:"?<<?stu_num?<<?"??????姓名:"?<<?stu_name;??
- ????cout?<<?"??????c++成績:"?<<?stu_score?<<?endl;??
- }??
- ??
- void?student::changeMage(int?n,?char?*ptr)??
- {??
- ????switch?(n)??
- ????{??
- ????case?1:?strcpy(stu_num,?ptr);???
- ????????break;??
- ????case?2:?strcpy(stu_name,?ptr);??
- ????}??
- }??
- ??
- //建立鏈表函數??
- void??construct_list(student?*tail)??
- {??
- ????student?*p?=?new?student;??
- ????char?very[20];??
- ????float?achieve;??
- ????cout?<<?"請輸入學號:"?<<?endl;??
- ????cin?>>?very;??
- ????p->changeMage(1,?very);??
- ????cout?<<?"請輸入姓名:"?<<?endl;??
- ????cin?>>?very;??
- ????p->changeMage(2,?very);??
- ????cout?<<?"請輸入c++成績:"?<<?endl;??
- ????cin?>>?achieve;??
- ????p->changegrade(achieve);??
- ????system("cls");??
- ????cout?<<?"信息輸入完畢"?<<?endl;??
- ??
- ????for?(;?tail->getNext()?!=?'\0';)??
- ????{??
- ????????tail?=?tail->getNext();??
- ????}??
- ??
- ????tail->afterInsert(p);??
- }??
- ??
- /*********查詢信息*********/??
- student?*findmege(student?*head)??
- {??
- loop:??
- ????cout?<<?"1--按姓名查詢???????????2--按學號查詢??????????????q--返回上一級菜單"?<<?endl;??
- ????char?p[5],?ptr[20];??
- ????student?*mid?=?head;??
- ????cin?>>?p;??
- ??
- ????if?(p[0]?!=?'1'&&p[0]?!=?'2'&&p[0]?!=?'q'?||?strlen(p)>1)??
- ????{??
- ????????system("cls");??
- ????????cout?<<?"對不起,你的輸入有誤,請重新輸入!"?<<?endl;??
- ????????goto?loop;??
- ????}??
- ??
- ????switch?(p[0])??
- ????{??
- ????case?'1':??
- ????{??
- ????????system("cls");??
- ????????cout?<<?"請輸入要查找的姓名:"?<<?endl;??
- ????????cin?>>?ptr;??
- ??
- ????????for?(;?strcmp(ptr,?mid->stu_name)?!=?0;?mid?=?mid->getNext())??
- ????????{??
- ????????????if?(mid->getNext()?==?'\0')??
- ????????????{??
- ????????????????cout?<<?"對不起,你要查找的人不存在,請確認你的輸入是否正確!"?<<?endl;??
- ????????????????goto?loop;??
- ????????????}??
- ????????}??
- ????????return?mid;??
- ????}??
- ????case?'2':??
- ????{??
- ????????system("cls");??
- ????????cout?<<?"請輸入您要查找的學號:"?<<?endl;??
- ????????cin?>>?ptr;??
- ????????for?(;?strcmp(ptr,?mid->stu_num)?!=?0;?mid?=?mid->getNext())??
- ????????{??
- ????????????if?(mid->getNext()?==?'\0')??
- ????????????{??
- ????????????????cout?<<?"對不起,您要查找的內容不存在,請確認您的輸入是否正確!"?<<?endl;??
- ????????????????goto?loop;??
- ????????????}??
- ????????}??
- ????????return?mid;??
- ????}??
- ????case?'q':???
- ????{??
- ????????return?'\0';??
- ????}??
- ????default:??
- ????{??
- ????????system("cls");??
- ????????cout?<<?"對不起,您的輸入有誤,請重新輸入!"?<<?endl;??
- ????????goto?loop;??
- ????}??
- ????}??
- }??
- ??
- /******************刪除鏈表?節點***********************/??
- void?delete_list(student?*head)??
- {??
- ????student?*p?=?'\0';??
- ????char?selet[4];??
- ????system("cls");??
- ????cout?<<?"在刪除前,系統會根據您的提示找到您要刪除的學生信息!"?<<?endl;??
- ????p?=?findmege(head);??
- ????if?(p?!=?'\0')??
- ????{??
- ????????cout?<<?"確認要刪除嗎(yes/任意鍵返回)"?<<?endl;??
- ????????cin?>>?selet;??
- ??
- ????????if?(strcmp(selet,?"yes")?==?0)??
- ????????{??
- ????????????for?(;?head->getNext()?!=?p;?head?=?head->getNext());??
- ????????????head->afterDelete();??
- ????????????system("cls");??
- ????????????cout?<<?"該信息刪除成功!"?<<?endl;??
- ????????}??
- ????}??
- }??
- ??
- /*******************修改節點信息********************/??
- void?change_info(student?*head)??
- {??
- ????system("cls");??
- ????cout?<<?"在您修改前,系統會根據您提供的信息找的您要修改的信息:"?<<?endl;??
- ????student?*p?=?'\0';??
- ??
- ????float?achieve;??
- ????p?=?findmege(head);??
- ????if?(p?!=?'\0')??
- ????{??
- ????????cout?<<?"請輸入c++成績:"?<<?endl;??
- ????????cin?>>?achieve;??
- ????????p->changegrade(achieve);??
- ????????system("cls");??
- ????????cout?<<?"修改成功!"?<<?endl;??
- ????}??
- ??
- }??
- ??
- /**************輸出學生成績信息**************/??
- void?output(student?*head)??
- {??
- ????system("cls");??
- ????cout?<<?"1-查看指定學生信息;2-查看所有學生信息;3-分段輸出學生信息"?<<?endl;??
- ????char?ch;??
- ????int?n?=?0;??
- ????head?=?head->getNext();??
- ????cin?>>?ch;??
- ????switch?(ch)??
- ????{??
- ????case?'1':???
- ????????head?=?findmege(head);??
- ????????if?(head?==?'\0')??
- ????????{??
- ????????????break;??
- ????????}??
- ????????head->getMage();??
- ????????break;??
- ????case?'2':???
- ????while?(head)??
- ????{??
- ????????head->getMage();??
- ????????head?=?head->getNext();??
- ????}??
- ????break;??
- ????case?'3':???
- ????????cout?<<?"a-60分以下;b-60~70分之間;c-70~80分之間;d-80~90分之間;e-90~100分之間:"?<<?endl;??
- ????????cin?>>?ch;??
- ????????switch?(ch)??
- ????????{??
- ????????case?'a':??
- ????????while?(head)??
- ????????{??
- ????????????if?(head->stu_score?<=?60)??
- ????????????{??
- ????????????????head->getMage();??
- ????????????????n++;??
- ????????????}??
- ????????????head?=?head->getNext();??
- ????????}??
- ?????????break;??
- ????????case?'b':???
- ????????while?(head)??
- ????????{??
- ????????????if?(head->stu_score>60?&&?head->stu_score?<=?70)???
- ????????????{???
- ????????????????head->getMage();??
- ????????????????n++;???
- ????????????}??
- ????????????head?=?head->getNext();??
- ????????}??
- ????????break;??
- ????????case?'c':???
- ????????while?(head)??
- ????????{??
- ????????????if?(head->stu_score>70?&&?head->stu_score?<=?80)??
- ????????????{???
- ????????????????head->getMage();???
- ????????????????n++;???
- ????????????}??
- ????????????head?=?head->getNext();??
- ????????}??
- ????????break;??
- ????????case?'d':???
- ????????while?(head)??
- ????????{??
- ????????????if?(head->stu_score>80?&&?head->stu_score?<=?90)??
- ????????????{??
- ????????????????head->getMage();??
- ????????????????n++;??
- ????????????}??
- ????????????head?=?head->getNext();??
- ????????}??
- ????????break;??
- ????????case?'e':???
- ????????while?(head)??
- ????????{??
- ????????????if?(head->stu_score>90?&&?head->stu_score?<=?100)??
- ????????????{???
- ????????????????head->getMage();??
- ????????????????n++;??
- ????????????}??
- ????????????head?=?head->getNext();??
- ????????}??
- ????????}??
- ????????if?(n?==?0)??
- ????????{??
- ????????????cout?<<?"該分段內沒有您要找的學生信息"?<<?endl;??
- ????????}??
- ????}??
- }??
- ??
- /*****************主菜單************************/??
- void?mainmenu(student?*head)??
- {??
- ????char?selet[10];??
- ????int?n?=?1;??
- ????ofstream?outfile;??
- ????ifstream?infile;??
- ????student?*p,?*ptr;??
- ????student?*test?=?head,?*mid;??
- ????cout?<<?"*************************歡迎進入學生信息管理系統*************************"?<<?endl;??
- ????do?{??
- ????????cout?<<?"**************************************************************************"?<<?endl;??
- ????????cout?<<?"1.插入信息;???2.刪除信息;??3.修改信息;?4.查看信息;?5.保存??"?<<?endl;??
- ????????cout?<<?"按'q'鍵退出??????"?<<?endl;??
- ????????cout?<<?"**************************************************************************"?<<?endl;??
- ????????cin?>>?selet;??
- ????????if?(((selet[0]<'1'?||?selet[0]>'6')?&&?selet[0]?!=?'q')?||?strlen(selet)>1)??
- ????????{??
- ????????????system("cls");??
- ????????????cout?<<?"您的輸入有誤,請重新輸入!"?<<?endl;??
- ????????????break;??
- ????????}??
- ????????switch?(selet[0])??
- ????????{??
- ??
- ????????case?'1':??
- ????????????construct_list(head);??
- ????????????break;???
- ????????case?'2':???
- ????????????delete_list(head);???
- ????????????break;??
- ????????case?'3':???
- ????????????change_info(head);??
- ????????????break;??
- ????????case?'4':???
- ????????????output(head);??
- ????????????break;??
- ????????case?'5':????
- ????????????outfile.open("students.txt",?ios::out?|?ios::app);??
- ????????????for?(p?=?head->getNext();?p?!=?'\0';?p?=?p->getNext())??
- ????????????{??
- ????????????????outfile?<<?p->stu_name?<<?'?';??
- ????????????????outfile?<<?p->stu_num?<<?'?';??
- ????????????????outfile?<<?p->stu_score?<<?'?';??
- ????????????????outfile?<<?endl;??
- ????????????}??
- ????????????outfile.close();??
- ????????????system("cls");??
- ????????????cout?<<?"保存成功!"?<<?endl;??
- ????????????break;??
- ????????case?'q':???
- ????????????break;??
- ????????}??
- ????}?while?(selet[0]?!=?'q');??
- }??
- ??
- void?main()??
- {??
- ????student?head;??
- ????mainmenu(&head);??
- }??