c++面向对象的课程设计
题目:学生成绩管理系统
输入一个班级的学生基本信息(包括学号,姓名,性别,科目),对N门考试的成绩进行管理(例N=5) 要求 用户录入每个学生每门课程的分数; 能够计算每个学生的各门功课总分和平均分,并按总分将成绩排序,显示每个学生的总分和排名;计算全班各门功课的平均分,显示每门课程中低于平均分的每一个学生的学号,姓名,性别,科目,成绩等信息; 显示每门科目中,成绩在90分以上的学生信息。另外还输出每门科目中不及格的学生信息; 能按姓名或者学号查找,增加,删除和保存各个学生的信息。
程序基本功能
1.能够连续的录入学生的基本信息(包括学号,姓名,性别,各科成绩)
2能够计算出统计的每个学生的各门科目成绩的总分和平均分,并按总分排序
3显示每门科目中成绩在90分以上同学的相关信息
4显示每门科目中成绩在60分以下同学的相关信息
5能够按照学号查找,增加,删除和保存各个学生的信息
6.计算全班各门科目成绩的平均分,显示低于平均分同学的相关信息
程序设计方案和原理
1.设计student基类与science派生类,包含学号、姓名、性别,与各科成绩
2.以菜单的形式呈现以上程序功能,输入命令能够连续执行。
结构图如下:
详细代码如下
#include <iostream>#include <string>#include <ctime>#include <windows.h>using namespace std;# define N 5//班级原始人数# define increment 5 //班级人数增量class Student{protected:int number;//学号string name;//姓名string sex;//性别int chinese;//语文成绩int math;//数学成绩int english;//英语成绩int Total_score;//总分int Total_score_order;//总分顺序public:Student(int number=1705110001,string name="noname",string sex="nosex",int chinese=0,int math=0,int english=0,int Total_score=0){this->number=number; this->name=name; this->sex=sex;this->chinese=chinese; this->math=math; this->english=english;}Student(Student &s){this->number=s.number; this->name=s.name; this->sex=s.sex;this->chinese=s.chinese; this->math=s.math; this->english=s.english;}virtual ~Student(){}virtual void display(){cout<<"学号:"<<number<<endl<<"姓名:"<<name<<endl<<"性别:"<<sex<<endl<<"语文成绩:"<<chinese<<endl<<"英语成绩:"<<english<<endl;}};//Studentclass Sscience:public Student{protected:int physics;//物理成绩int chemistry;//化学成绩public:Sscience(){this->physics=0;this->chemistry=0;}Sscience(int number,string name,string sex,int chinese,int math,int english,int Total_score,int physics,int chemistry):Student(number,name,sex,chinese,math,english,Total_score){this->physics=physics; this->chemistry=chemistry;}Sscience(Sscience &s):Student(s){this->physics=s.physics;this->chemistry=s.chemistry; }virtual ~Sscience(){}void display(){cout<<"学号:"<<number<<endl<<"姓名:"<<name<<endl<<"性别:"<<sex<<endl<<"语文成绩:"<<chinese<<endl<<"数学成绩:"<<math<<endl<<"英语成绩:"<<english<<endl<<"物理成绩:"<<physics<<endl<<"化学成绩:"<<chemistry<<endl;}void setall();int getnumber(){return this->number;}string getname(){return this->name;}int getchinese(){return this->chinese;}int getmath(){return this->math;}int getenglish(){return this->english;}int getphysics(){return this->physics;}int getchemistry(){return this->chemistry;}int getTotal_score(){this->Total_score=this->math+this->chinese+this->english+this->chemistry+this->physics;return this->Total_score;}void resetname(){string n;cout<<"请输入新的名字:"<<endl;cin>>n;this->name=n;}void findname(){string n;cout<<"请输入需要查找的姓名:"<<endl;cin>>n;int flag = 0; // 0表示未找到 if(n==this->name){flag = 1;cout << "该同学在成绩管理系统中存在" << endl;} // 判断是否未找到该元素 if(flag==0){cout << "该同学在成绩管理系统中不存在" << endl;}}void resetsex(){string s;cout<<"请输入新的性别:"<<endl;cin>>s;this->sex=s;}void findsex(){string s;cout<<"请输入需要查找的性别:"<<endl;cin>>s;int flag = 0; // 0表示未找到 if(s==this->sex){flag = 1;cout << "该同学在成绩管理系统中存在" << endl;} // 判断是否未找到该元素 if(flag==0){cout << "该同学在成绩管理系统中不存在" << endl;}}void resetchinese(){int c;cout<<"请输入新的语文成绩:"<<endl;cin>>c;this->chinese=c;}void findchinese(){int c;cout<<"请输入需要查找的语文成绩:"<<endl;cin>>c;int flag = 0; // 0表示未找到 if(c==this->chinese){flag = 1;cout << "该成绩与成绩管理系统中存在的语文成绩匹配" << endl;} // 判断是否未找到该元素 if(flag==0){cout << "该成绩与成绩管理系统中存在的语文成绩不匹配" << endl;}}void resetmath(){int m;cout<<"请输入新的数学成绩:"<<endl;cin>>m;this->math=m;}void findmath(){int m;cout<<"请输入需要查找的数学成绩:"<<endl;cin>>m;int flag = 0; // 0表示未找到 if(m==this->math){flag = 1;cout << "该成绩与成绩管理系统中存在的数学成绩匹配" << endl;} // 判断是否未找到该元素 if(flag==0){cout << "该成绩与成绩管理系统中存在的数学成绩不匹配!" << endl;}}void resetenglish(){int e;cout<<"请输入新的英语成绩:"<<endl;cin>>e;this->english=e;}void findenglish(){int e;cout<<"请输入需要查找的英语成绩:"<<endl;cin>>e;int flag = 0; // 0表示未找到 if(e==this->english){flag = 1;cout << "该成绩与成绩管理系统中存在的英语成绩匹配" << endl;} // 判断是否未找到该元素 if(flag==0){cout << "该成绩与成绩管理系统中存在的英语成绩不匹配" << endl;}}void resetchemistry(){int ch;cout<<"请输入新的化学成绩:"<<endl;cin>>ch;this->chemistry=ch;}void findchemistry(){int ch;cout<<"请输入需要查找的化学成绩:"<<endl;cin>>ch;int flag = 0; // 0表示未找到 if(ch==this->chemistry){flag = 1;cout << "该成绩与成绩管理系统中存在的化学成绩匹配" << endl;} // 判断是否未找到该元素 if(flag==0){cout << "该成绩与成绩管理系统中存在的化学成绩不匹配" << endl;}}void resetphysics(){int p;cout<<"请输入新的物理成绩:"<<endl;cin>>p;this->physics=p;}void findphysics(){int p;cout<<"请输入需要查找的物理成绩:"<<endl;cin>>p;int flag = 0; // 0表示未找到 if(p==this->physics){flag = 1;cout << "该成绩与成绩管理系统中存在的物理成绩匹配" << endl;} // 判断是否未找到该元素 if(flag==0){cout << "该成绩与成绩管理系统中存在的物理成绩不匹配" << endl;}}};//Ssciencevoid Sscience::setall(){cout<<"输入学号:"; cin>>number;cout<<"输入姓名:"; cin>>name;cout<<"输入性别:"; cin>>sex;cout<<"输入语文成绩:"; cin>>chinese;cout<<"输入数学成绩:"; cin>>math;cout<<"输入英语成绩:"; cin>>english;cout<<"输入物理成绩:"; cin>>physics;cout<<"输入化学成绩:"; cin>>chemistry;}void informationentry(Sscience *headptr/*对象指针*/,int &count,int &capacity){if(count==capacity){capacity+=increment;Sscience *p=headptr;Sscience *newheadptr = new Sscience[capacity];Sscience *q=newheadptr;for(int i=0;i<count;i++){*q=*p;p++;q++;}p=headptr;headptr=newheadptr;delete[] p;}(headptr+count)->setall();count++;}void informationmodify(Sscience *headptr,int count){int x=0,control=0;cout<<"请输入所要修改学生信息的学号:"<<endl;cin>>x;for(int i=0;i<count;i++){if((headptr+i)->getnumber()==x){do{cout<<"选择要修改的信息编号:"<<endl<<"\t1.姓名\n"<<"\t2.性别\n"<<"\t3.语文成绩\n"<<"\t4.数学成绩\n"<<"\t5.英语成绩\n"<<"\t6.物理成绩\n"<<"\t7.化学成绩\n"<<"\t0.放弃本次修改\n\t";cout<<"请输入你的指令"<<endl;cin>>control;switch(control){case 1:(headptr+i)->resetname();cout<<"修改成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 2:(headptr+i)->resetsex();cout<<"修改成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 3:(headptr+i)->resetchinese();cout<<"修改成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 4:(headptr+i)->resetmath();cout<<"修改成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 5:(headptr+i)->resetenglish();cout<<"修改成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 6:(headptr+i)->resetphysics();cout<<"修改成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 7:(headptr+i)->resetchemistry();cout<<"修改成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 0:cout<<"放弃修改!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;default:cout<<"输入错误!请重新输入:"<<endl;Sleep(1000);system("cls");}}while(control==0);}}cout<<"操作完成!"<<endl;Sleep(1000);}void informationdelete(Sscience *headptr,int &count){int x=0,ctrl=0;cout<<"请输入要删除学生信息的学号:";cin>>x;for(int i=0;i<count;i++){if((headptr+i)->getnumber()==x){for(int j=1;j<count-i;j++){*(headptr+i+j-1)=*(headptr+i+j);}count--;ctrl++;}}if(ctrl==0){cout<<"没有找到该条信息!返回主界面";Sleep(1000);system("cls");}elsecout<<"删除成功!"<<endl;}void informationfind(Sscience *headptr,int count){int x=0,control=0;cout<<"请输入所要查找学生信息的学号:"<<endl;cin>>x;for(int i=0;i<count;i++){if((headptr+i)->getnumber()==x){do{cout<<"选择要查找的信息编号:"<<endl<<"\t1.姓名\n"<<"\t2.性别\n"<<"\t3.语文成绩\n"<<"\t4.数学成绩\n"<<"\t5.英语成绩\n"<<"\t6.物理成绩\n"<<"\t7.化学成绩\n"<<"\t0.放弃本次查找\n\t";cout<<"请输入你的指令"<<endl;cin>>control;switch(control){case 1:(headptr+i)->findname();cout<<"查找成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 2:(headptr+i)->findsex();cout<<"查找成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 3:(headptr+i)->findchinese();cout<<"查找成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 4:(headptr+i)->findmath();cout<<"查找成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 5:(headptr+i)->findenglish();cout<<"查找成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 6:(headptr+i)->findphysics();cout<<"查找成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 7:(headptr+i)->findchemistry();cout<<"查找成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;case 0:cout<<"查找成功!一秒后返回主界面"<<endl;Sleep(1000);system("cls");break;default:cout<<"查找错误!请重新输入:"<<endl;Sleep(1000);system("cls");}//switch}//dowhile(control==0);}//if}//forcout<<"操作完成!!"<<endl;Sleep(1000);}void display_Total_score_ranking(Sscience *headptr,int count){int t[N],o[N];int i,j,Temporary,Temporary_Total_score;for(i=0;i<count;i++){t[i]=(headptr+i)->getTotal_score();o[i]=i;}for(j=0;j<count-1;j++){for(i=0;i<count-1;i++){if(t[i]>t[i+1]){Temporary=t[i];Temporary_Total_score=o[i];t[i]=t[i+1];o[i]=o[i+1];t[i+1]=Temporary;o[i+1]=Temporary_Total_score;}}}cout<<"以下是成绩管理系统中各同学的总分排序(从低到高)"<<endl;for(i=0;i<count;i++){cout<<(headptr+o[i])->getnumber()<<" "<<(headptr+o[i])->getname()<<" "<<t[i]<<endl; }cout<< endl;}void display_Average_score(Sscience *headptr,int count)//计算各科平均分并查看低于平均分学生信息{float chinese_Average_score=0;float math_Average_score=0;float english_Average_score=0;float physics_Average_score=0;float chemistry_Average_score=0;for(int i=0;i<count;i++){chinese_Average_score+=(headptr+i)->getchinese();math_Average_score+=(headptr+i)->getmath();english_Average_score+=(headptr+i)->getenglish();physics_Average_score+=(headptr+i)->getphysics();chemistry_Average_score+=(headptr+i)->getchemistry();}cout<<"各位同学语文学科平均分为:"<<chinese_Average_score/count<<endl;cout<<"各位同学数学学科平均分为:"<<math_Average_score/count<<endl;cout<<"各位同学英语学科平均分为:"<<english_Average_score/count<<endl;cout<<"各位同学物理学科平均分为:"<<physics_Average_score/count<<endl;cout<<"各位同学化学学科平均分为:"<<chemistry_Average_score/count<<endl;for(int j=0;j<count;j++){if((headptr+j)->getchinese() < chinese_Average_score/count){cout<<(headptr+j)->getname()<<"同学低于语文平均分,该同学语文分数为:"<<(headptr+j)->getchinese()<<endl;}if((headptr+j)->getmath() < math_Average_score/count){cout<<(headptr+j)->getname()<<"同学低于数学平均分,该同学语文分数为:"<<(headptr+j)->getmath()<<endl;}if((headptr+j)->getenglish() < english_Average_score/count){cout<<(headptr+j)->getname()<<"同学低于英语平均分,该同学语文分数为:"<<(headptr+j)->getenglish()<<endl;}if((headptr+j)->getphysics() < physics_Average_score/count){cout<<(headptr+j)->getname()<<"同学低于物理平均分,该同学语文分数为:"<<(headptr+j)->getphysics()<<endl;}if((headptr+j)->getchemistry() < chemistry_Average_score/count){cout<<(headptr+j)->getname()<<"同学低于化学平均分,该同学语文分数为:"<<(headptr+j)->getchemistry()<<endl;}}}void dispaly_90(Sscience *headptr,int count){for(int j=0;j<count;j++){if((headptr+j)->getchinese() > 90){cout<<(headptr+j)->getname()<<"同学语文高于90分值得鼓励,该同学语文分数为:"<<(headptr+j)->getchinese()<<endl;}if((headptr+j)->getmath() > 90){cout<<(headptr+j)->getname()<<"同学数学高于90分值得鼓励,该同学数学分数为:"<<(headptr+j)->getmath()<<endl;}if((headptr+j)->getenglish() > 90){cout<<(headptr+j)->getname()<<"同学英语高于90分指得鼓励,该同学英语分数为:"<<(headptr+j)->getenglish()<<endl;}if((headptr+j)->getphysics() > 90){cout<<(headptr+j)->getname()<<"同学物理高于90分值得鼓励,该同学物理分数为:"<<(headptr+j)->getphysics()<<endl;}if((headptr+j)->getchemistry() > 90){cout<<(headptr+j)->getname()<<"同学化学高于90分值得鼓励,该同学化学分数为:"<<(headptr+j)->getchemistry()<<endl;}}}void dispaly_60(Sscience *headptr,int count){for(int j=0;j<count;j++){if((headptr+j)->getchinese() < 60){cout<<(headptr+j)->getname()<<"同学语文低于60分需要加油,该同学语文分数为:"<<(headptr+j)->getchinese()<<endl;}if((headptr+j)->getmath() < 60){cout<<(headptr+j)->getname()<<"同学数学低于60分需要加油,该同学数学分数为:"<<(headptr+j)->getmath()<<endl;}if((headptr+j)->getenglish() < 60){cout<<(headptr+j)->getname()<<"同学英语低于60分需要加油,该同学英语分数为:"<<(headptr+j)->getenglish()<<endl;}if((headptr+j)->getphysics() < 60){cout<<(headptr+j)->getname()<<"同学物理低于60分需要加油,该同学物理分数为:"<<(headptr+j)->getphysics()<<endl;}if((headptr+j)->getchemistry() < 60){cout<<(headptr+j)->getname()<<"同学化学低于60分需要加油,该同学化学分数为:"<<(headptr+j)->getchemistry()<<endl;}}}void displayall(Sscience *headptr,int count){for(int i=0;i<count;i++){(headptr+i)->display();}}int count=0,capacity=5;//全局变量int main(){time_t Time;time(&Time);int control;Sscience *headptr = new Sscience[capacity];do{cout << "欢迎进入武汉轻工大学数计学院成绩管理系统"<<endl;cout << "当前系统时间是:" << ctime(&Time) <<endl;cout<<"|========================学生成绩管理系统===========================|"<<endl<<"| *1.学生信息录入|"<<endl<<"| *2.学生信息修改 |"<<endl<<"| *3.删除学生信息 |"<<endl<<"| *4.查找学生信息 |"<<endl<<"| *5.查看总分排名 |"<<endl<<"| *6.计算各科平均分并查看低于平均分学生信息 |"<<endl<<"| *7.显示每门成绩在90分以上学生的信息 |"<<endl<<"| *8.显示每门成绩在60分以下学生的信息 |"<<endl<<"| *9.显示所有学生信息 |"<<endl<<"| *0.退出|"<<endl<<endl;cout<<"请输入你的指令:";cin>>control;switch(control){case 1: system("cls");cout<<"(信息录入函数)"<<endl;informationentry(headptr,count,capacity);break;case 2: system("cls");cout<<"(信息修改函数)"<<endl;informationmodify(headptr,count);break;case 3: system("cls");cout<<"(信息删除函数)"<<endl;informationdelete(headptr,count);break;case 4: system("cls");cout<<"(信息查找函数)"<<endl;informationfind(headptr,count);break;case 5: system("cls");cout<<"(总分排序函数)"<<endl;display_Total_score_ranking(headptr,count);break;case 6: system("cls");cout<<"(各门功课平均分函数)"<<endl;display_Average_score(headptr,count);break;case 7: system("cls");cout<<"(90分以上函数)"<<endl;dispaly_90(headptr,count);break;case 8: system("cls");cout<<"(显示不及格函数)"<<endl;dispaly_60(headptr,count);break;case 9: system("cls");cout<<"(显示所有信息)"<<endl;displayall(headptr,count);cout<<"已将所有信息显示完毕"<<endl;break;case 0: cout<<"感谢使用! 3秒后程序结束"<<endl;Sleep(3000);break;default:cout<<"输入错误!请重新输入:";Sleep(1000);system("cls");}}while(control!=0);delete[] headptr;return 0;}