C++ 简单的成绩管理
#include<iostream> #include<iomanip> using namespace std; struct Student{ int studentNunber;//学号 string studentName;//姓名 double Chinese;//语文成绩 double English;//英语成绩 double Computer;//计算机成绩 double Math;//数学成绩 double Total;//总分 int rank;//排名 }; /***************** 成绩排序 m=1,按照名次排序 m=2,按照学号排序 m=3,按照语文成绩排序 m=4,按照英语成绩排序 m=5,按照计算机成绩排序 m=6,按照数学成绩排序 *****************/ void sort(int m,struct Student *stu,int n) { if(m==1) { for (int k =0; k<n-1;k++) { for(int h=0; h<n-1-k;h++) { if(stu[h].Total<stu[h+1].Total) { struct Student tempStu; tempStu=stu[h]; stu[h]=stu[h+1]; stu[h+1]=tempStu; } } stu[n-k-1].rank=n-k; } } else if(m==2) { for (int k =0; k<n-1;k++) { for(int h=0; h<n-1-k;h++) { if(stu[h].studentNunber>stu[h+1].studentNunber) { struct Student tempStu; tempStu=stu[h+1]; stu[h+1]=stu[h]; stu[h]=tempStu; } } } } else if(m==3) { for (int k =0; k<n-1;k++) { for(int h=0; h<n-1-k;h++) { if(stu[h].Chinese<stu[h+1].Chinese) { struct Student tempStu; tempStu=stu[h]; stu[h]=stu[h+1]; stu[h+1]=tempStu; } } } } else if(m==4) { for (int k =0; k<n-1;k++) { for(int h=0; h<n-1-k;h++) { if(stu[h].English<stu[h+1].English) { struct Student tempStu; tempStu=stu[h]; stu[h]=stu[h+1]; stu[h+1]=tempStu; } } } } else if(m==5) { for (int k =0; k<n-1;k++) { for(int h=0; h<n-1-k;h++) { if(stu[h].Computer<stu[h+1].Computer) { struct Student tempStu; tempStu=stu[h]; stu[h]=stu[h+1]; stu[h+1]=tempStu; } } } } else if(m==6) { for (int k =0; k<n-1;k++) { for(int h=0; h<n-1-k;h++) { if(stu[h].Math<stu[h+1].Math) { struct Student tempStu; tempStu=stu[h]; stu[h]=stu[h+1]; stu[h+1]=tempStu; } } } } } /***************** 成绩录入 *****************/ void input(struct Student *stu,int n) { for(int i=0;i<n;i++) { cout<<"请输入学号为"<<i+1<<"的学生的姓名以及语文,英语,计算机,数学无门课程的成绩,每一项以回车键结束"<<endl; stu[i].studentNunber=i+1; cin>>stu[i].studentName; cin>>stu[i].Chinese; cin>>stu[i].English; cin>>stu[i].Computer; cin>>stu[i].Math; stu[i].Total= stu[i].Chinese+ stu[i].English+stu[i].Computer+stu[i].Math; stu[i].rank=1; } //排序,计算名次 sort(1,stu,n); } void update(struct Student *stu,int n) { int num; int sub; double score; cout<<"请输入要更改学生的学号:"<<endl; cin>>num; while(num>n||num<1) {cout<<"该学号不存在,请重新输入:"<<endl; cin>>num; } cout<<"请输入要更改的课程代号1:语文;2:英语;3:计算机;4:数学:"<<endl; cin>>sub; while(sub>4||sub<1) {cout<<"请输入正确的课程代号:"<<endl; cin>>sub; } cout<<"请输入新成绩(1 -100)"<<endl; cin>>score; while(score>100||sub<0) {cout<<"请输入正确范围的成绩:"<<endl; cin>>score; } //按照学号重新排序 sort(2,stu,n); switch(sub) { case 1: stu[num-1].Chinese=score; break; case 2: stu[num-1].English=score; break; case 3: stu[num-1].Computer=score; break; case 4: stu[num-1].Math=score; break; default: break; } //重新排序,计算名次 sort(1,stu,n); cout<<"成绩更改成功!" ; } /***************** 成绩输出 *****************/ void display(struct Student *stu,int n) { cout<<"学号:"<<setw(10)<<"姓名:"<<setw(10)<<"语文"<<setw(10)<< "英语"<<setw(10)<<"计算机"<<setw(10)<<"数学"<<setw(10)<<"总分"<<setw(10)<<"名次"<<setw(10)<<""<<endl; for(int j=0;j<n;j++) { cout<<setw(4)<< stu[j].studentNunber<<setw(10)<<stu[j].studentName<<setw(10)<< stu[j].Chinese<<setw(10)<<stu[j].English<<setw(10)<<stu[j].Computer<< setw(10)<<stu[j].Math<<setw(10)<< stu[j].Total<<setw(10)<<stu[j].rank<<endl; } } /***************** 格式化输出选项 *****************/ void printChoice() { cout<<"请选择要操作的类型"<<endl; cout<<"*************************************"<<endl; cout<<"*查看按名次排序的名单,请输入1 *"<<endl; cout<<"*查看按学号排序的名单,请输入2 *"<<endl; cout<<"*查看按语文成绩排序的名单,请输入3 *"<<endl; cout<<"*查看按数学成绩排序的名单,请输入4 *"<<endl; cout<<"*查看按计算机成绩排序的名单,请输入5*"<<endl; cout<<"*查看按数学成绩排序的名单,请输入6 *"<<endl; cout<<"*修改学生成绩,请输入7 *"<<endl; cout<<"*退出系统,请输入1 — 7以外的任意键 *"<<endl; cout<<"*************************************"<<endl; } int main() { int n=0;//班级人数 cout<<"欢迎张老师进入成绩输入系统,请输入班级人数"<<endl; cout<<"-------(请保证班级人数小于等于50)-------"<<endl; while(!(cin>>n)) { cout<<"请输入数字!"<<endl; cin.clear(); cin.sync(); } while(n<0||n>50) { cout<<"请输入于等于50的班级人数"<<endl; cin>>n; } struct Student *stu =new struct Student [n]; //录入成绩 input(stu,n); cout<<"成绩录入成功!"<<endl; printChoice(); int m ; cin>>m; while(m>0&&m<8) { if(m==7) { update(stu,n); } else { sort(m,stu,n); display(stu,n); } printChoice(); cin>>m; } cout<<"退出系统成功!"; int a; cin>>a; return 0; }