《软件开发与创新》第一次作业——软件二次开发
一、项目来源和源代码
这个项目来自CSDN上一个项目《学生成绩信息管理系统设计》。该系统已有较为完善的功能和可观的页面,因此本人二次开发主要针对代码本身冗余复杂、无体系结构等问题,在功能上仅做细微调整,而主要调整代码结构,重写项目。
源代码
点击查看代码
#include<iostream>
#include<string>
#define MAX 100
using namespace std;
int i=0;
class student
{
public:
string Name[MAX],Sex[MAX],ID[MAX];
float Cpp[MAX],Math[MAX],English[MAX];
int Age[MAX],Sum[MAX],Avg[MAX],ranking[MAX];
void Luru();//批量添加
void Input(); //增
void Delete();//删
void Amend(); //改
void Show(); //显
void Find(); //查
void Ranking();//排
void Analyse();//分析
void bukao();//补考
};
class Page:public student
{
//各类表格
public:
void menu();
void menu1();
void menu2();
void menu3();
void menu4();
void Login();
void Login1();
};
int main()
{
Page p1;
student s1;
p1.Login();
p1.Login1();
int zhiling;
s1.Luru();
while(1)
{
p1.menu();
cin>>zhiling;
while(cin.fail())
{ cin.clear();
cout<<endl<<"请输入0-7内的整数:";
cin.ignore(1024,'\n');
cin>>zhiling;
}
switch(zhiling)
{
case 1: s1.Input();system("pause");break;
case 2: s1.Show();system("pause");break;
case 3: s1.Amend();system("pause");break;
case 4: s1.Delete();system("pause");break;
case 5: s1.Find();system("pause");break;
case 6: s1.Ranking();system("pause");break;
case 7: s1.Analyse();system("pause");break;
case 8: s1.bukao();system("pause");break;
case 0: return 0;
default:cout<<"输入错误!请重新输入:"<<endl;
}
}
return 0;
}
void student::Input()
{
string name,sex,id;
float cpp,math,english;
int age;
cout<<"==================================="<<endl;
cout<<"请输入第 "<<i+1<<" 个同学的学号:";cin>>id;
while(1)
{
bool flag=true;
for(int j=0;j<i;j++)
{
if(ID[j]==id)
{
cout<<"该学号已存在!请重新输入:";cin>>id;
flag=false;
}
}
if(flag==true){
break;
}
}
cout<<" 姓名:";cin>>name;
cout<<" 性别:";cin>>sex;
while(1)
{
if(sex=="男"||sex=="女")
{
break;
}
else cout<<"-->输入错误!请重新输入:";
cin>>sex;
}
cout<<" 年龄:";cin>>age;
while(cin.fail())
{ cin.clear();
cout<<"请输入1~200内的有效数字:";
cin.ignore(1024,'\n');
cin>>age;
}
while(1)
{
if(age>0 && age<=200)
{
break;
}
else cout<<"请输入1~200内的有效数字:";
cin>>age;
}
cout<<" C++成绩:";cin>>cpp;
while(cin.fail())
{ cin.clear();
cout<<"请输入1~100内的有效数字:";
cin.ignore(1024,'\n');
cin>>cpp;
}
while(1)
{
if(cpp>=0 && cpp<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩:";
cin>>cpp;
}
cout<<" 数学成绩:";cin>>math;
while(cin.fail())
{ cin.clear();
cout<<"请输入1~100内的有效数字:";
cin.ignore(1024,'\n');
cin>>math;
}
while(1)
{
if(math>=0 && math<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩:";
cin>>math;
}
cout<<" 英语成绩:";cin>>english;
while(cin.fail())
{ cin.clear();
cout<<"请输入1~100内的有效数字:";
cin.ignore(1024,'\n');
cin>>english;
}
while(1)
{
if(english>=0 && english<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩:";
cin>>english;
}
ID[i]=id;
Name[i]=name;
Sex[i]=sex;
Age[i]=age;
Cpp[i]=cpp;
Math[i]=math;
English[i]=english;
Sum[i]=Cpp[i]+Math[i]+English[i];
Avg[i]=Sum[i]/3;
i++;
cout<<"==================================="<<endl;
cout<<" 添加成功!!!"<<endl;
cout<<"==================================="<<endl;
if(i>MAX)
{
cout<<"学生总数超过最大值(100),不可再添加!"<<endl;exit(0);
}
}
void student::Show()
{
float CppSum=0,MathSum=0,EnglishSum=0,CppAvg=0,MathAvg=0,EnglishAvg=0;
cout<<"\t"<<"______________________________________________________________________"<<endl;
cout<<"\t"<<"学号\t姓名\t性别\t年龄\tC++\t数学\t英语\t总分\t平均分"<<endl;
for(int j=0;j<i;j++)
{
Sum[j]=Cpp[j]+Math[j]+English[j]; Avg[j]=Sum[j]/3;
cout<<"\t"<<ID[j]<<"\t"<<Name[j]<<"\t"<<Sex[j]<<"\t"<<Age[j]<<"\t"
<<Cpp[j]<<"\t"<<Math[j]<<"\t"<<English[j]<<"\t"<<Sum[j]<<"\t"<<Avg[j]<<endl;
}
cout<<"\t"<<"______________________________________________________________________"<<endl;
for(int r=0;r<i;r++)
{
CppSum+=Cpp[r];
MathSum+=Math[r];
EnglishSum+=English[r];
}
CppAvg=CppSum/i;MathAvg=MathSum/i;EnglishAvg=EnglishSum/i;
cout<<"\t"<<"卍卍卍卍卍卍卍卍卍卍卍卍< 班级总分与平均分 >卍卍卍卍卍卍卍卍卍卍卍卍"<<endl;
cout<<"\t"<<"----------------------------------------------------------------------"<<endl;
cout<<"\t"<<"C++:总 分:"<<CppSum<<"\t"<<" 数学:总 分:"<<MathSum<<"\t"<<" 英语:总 分:"<<EnglishSum<<endl
<<"\t"<<" 平均分:"<<CppAvg<<"\t"<<" 平均分:"<<MathAvg<<"\t"<<" 平均分:"<<EnglishAvg<<endl;;
cout<<"\t"<<"______________________________________________________________________"<<endl<<endl;
}
void student::Delete()
{
int n;
string id,name;
char yes;
bool flag=false;
Page p2;
p2.menu2();
cout<<"请选择删除方式:"; cin>>n;
while(1){
if(n==1)
{
cout<<"请输入学号:";cin>>id;
for(int j=0;j<i;j++)
{
if(ID[j]==id)
{
cout<<"您确认要删除该学生信息?"<<endl;
cout<<"请选择(删除 Y,放弃 N )"<<endl;
while(1){
cin>>yes;
if(yes=='y'||yes=='Y')
{
ID[j]=ID[j+1];
Name[j]=Name[j+1];
Sex[j]=Sex[j+1];
Age[j]=Age[j+1];
Cpp[j]=Cpp[j+1];
Math[j]=Math[j+1];
English[j]=English[j+1];
cout<<"删除成功!"<<endl;
flag=true;break;
}
else if(yes=='n'||yes=='N')
{
cout<<"放弃选择!"<<endl;
flag=true;break;
}
else
{
cout<<"输入错误!请重新输入:";
}
}
}
}i--;
if(flag==false)
{
cout<<"查无此学号!"<<endl;break;
}
break;
}
else if(n==2)
{
cout<<"请输入姓名:"; cin>>name;
for(int j=0;j<i;j++)
{
if(Name[j]==name)
{
cout<<"您确认要删除该学生信息?"<<endl;
cout<<"请选择(删除 Y,放弃 N )"<<endl;
while(1){
cin>>yes;
if(yes=='y'||yes=='Y')
{
ID[j]=ID[j+1];
Name[j]=Name[j+1];
Sex[j]=Sex[j+1];
Age[j]=Age[j+1];
Cpp[j]=Cpp[j+1];
Math[j]=Math[j+1];
English[j]=English[j+1];
cout<<"删除成功!"<<endl;
flag=true;break;
}
else if(yes=='n'||yes=='N')
{
cout<<"放弃选择!"<<endl;
flag=true;break;
}
else
{
cout<<"输入错误!请重新输入:";
}
}
}
}i--;
if(flag==false)
{
cout<<"查无此姓名!"<<endl;break;
}
break;
}
else if(n==0){break;}
else{
cout<<"没有该选项,请重新选择:";
cin>>n;
}
}
}
void student::Amend()
{
Page p2;
string id,name,sex;
string Id,NAME,SeX;
float cpp,math,english;
float CPP,MATH,ENGLISH;
bool flag=false;
int age,n,a;
p2.menu4();
cin>>a;
while(1)
{
if(a==1)
{
p2.menu3();
cin>>n;
while(1)
{
if(n==1)
{
cout<<"请输入学生学号:";cin>>Id;
for(int j=0;j<i;j++)
{
if(ID[j]==Id)
{
cout<<"请输入要修改的学生的学号:"; cin>>id;
while(1)
{
bool flag=true;
for(int j=0;j<i;j++)
{
if(ID[j]==id)
{
cout<<"该学号已存在!请重新输入:";cin>>id;
flag=false;
}
}
if(flag==true){
ID[j]=id;
break;
}
}
cout<<"修改成功!"<<endl;
flag=true;
break;
}
}
if(flag==false)
{
cout<<"查无此学号!"<<endl;
}
break;
}
else if(n==2)
{
cout<<"请输入学生姓名:";
cin>>NAME;
for(int j=0;j<i;j++)
{
if(Name[j]==NAME)
{
cout<<"请输入要修改的学生的学号:"; cin>>id;
while(1)
{
bool flag=true;
for(int j=0;j<i;j++)
{
if(ID[j]==id)
{
cout<<"该学号已存在!请重新输入:";cin>>id;
flag=false;
}
}
if(flag==true){
ID[j]=id;
break;
}
}
cout<<"修改成功!"<<endl;
flag=true;break;
}
}
if(flag==false)
{
cout<<"查无此姓名!"<<endl;
}
break;
}
else if(n==3)
{
int m;
cout<<"============================="<<endl;
cout<<"<<输入1:按C++成绩查找! >>"<<endl
<<"<<输入2:按数学成绩查找! >>"<<endl
<<"<<输入3:按数英语成绩查找!>>"<<endl
<<"<<输入0:返回! >>"<<endl;
cout<<"============================="<<endl;
cout<<"*请输入查找方式:";
cin>>m;
if(m==1)
{
cout<<"请输入C++成绩:";cin>>CPP;
for(int j=0;j<i;j++)
{
if(Cpp[j]==CPP)
{
cout<<"请输入要修改的学生的学号:"; cin>>id;
while(1)
{
bool flag=true;
for(int j=0;j<i;j++)
{
if(ID[j]==id)
{
cout<<"该学号已存在!请重新输入:";cin>>id;
flag=false;
}
}
if(flag==true){
ID[j]=id;
break;
}
}
cout<<"修改成功!"<<endl;
flag=true;
}
}
if(flag==false)
{
cout<<"查无此成绩!"<<endl;
}
break;
}
else if(m==2)
{
cout<<"请输入数学成绩:";cin>>MATH;
for(int j=0;j<i;j++)
{
if(Math[j]==MATH)
{
cout<<"请输入要修改的学生的学号:"; cin>>id;
while(1)
{
bool flag=true;
for(int j=0;j<i;j++)
{
if(ID[j]==id)
{
cout<<"该学号已存在!请重新输入:";cin>>id;
flag=false;
}
}
if(flag==true){
ID[j]=id;
break;
}
}
cout<<"修改成功!"<<endl;
flag=true;
}
}
if(flag==false)
{
cout<<"查无此成绩!"<<endl;
}
break;
}
else if(m==3)
{
cout<<"请输入英语成绩:";cin>>ENGLISH;
for(int j=0;j<i;j++)
{
if(English[j]==ENGLISH)
{
cout<<"请输入要修改的学生的学号:"; cin>>id;
while(1)
{
bool flag=true;
for(int j=0;j<i;j++)
{
if(ID[j]==id)
{
cout<<"该学号已存在!请重新输入:";cin>>id;
flag=false;
}
}
if(flag==true){
ID[j]=id;
break;
}
}
cout<<"修改成功!"<<endl;
flag=true;
}
}
if(flag==false)
{
cout<<"查无此成绩!"<<endl;
}
break;
}
else if(m==0)
{
cout<<"退出!"<<endl;break;
}
else {
cout<<"输入错误!请重新输入:"<<endl;
}
}
else if(n==0){break;}
else
{
cout<<"输入错误!请重新输入:";
cin>>n;
}
}
break;
}
else if(a==2)
{
p2.menu3();
cin>>n;
while(1)
{
if(n==1)
{
cout<<"请输入学生学号:";cin>>Id;
for(int j=0;j<i;j++)
{
if(ID[j]==Id)
{
cout<<"请输入要修改的学生的姓名:"; cin>>name; Name[j]=name;
cout<<"修改成功!"<<endl;
flag=true;
break;
}
}
if(flag==false)
{
cout<<"查无此学号!"<<endl;
}
break;
}
else if(n==2)
{
cout<<"请输入学生姓名:";
cin>>NAME;
for(int j=0;j<i;j++)
{
if(Name[j]==NAME)
{
cout<<"请输入要修改的学生的姓名:"; cin>>name; Name[j]=name;
cout<<"修改成功!"<<endl;
flag=true;break;
}
}
if(flag==false)
{
cout<<"查无此姓名!"<<endl;
}
break;
}
else if(n==3)
{
int m;
cout<<"============================="<<endl;
cout<<"<<输入1:按C++成绩查找! >>"<<endl
<<"<<输入2:按数学成绩查找! >>"<<endl
<<"<<输入3:按数英语成绩查找!>>"<<endl
<<"<<输入0:返回! >>"<<endl;
cout<<"============================="<<endl;
cout<<"*请输入查找方式:";
cin>>m;
if(m==1)
{
cout<<"请输入C++成绩:";cin>>CPP;
for(int j=0;j<i;j++)
{
if(Cpp[j]==CPP)
{
cout<<"请输入要修改的学生的姓名:"; cin>>name; Name[j]=name;
cout<<"修改成功!"<<endl;
flag=true;
}
}
if(flag==false)
{
cout<<"查无此成绩!"<<endl;
}
break;
}
else if(m==2)
{
cout<<"请输入数学成绩:";cin>>MATH;
for(int j=0;j<i;j++)
{
if(Math[j]==MATH)
{
cout<<"请输入要修改的学生的姓名:"; cin>>name; Name[j]=name;
cout<<"修改成功!"<<endl;
flag=true;
}
}
if(flag==false)
{
cout<<"查无此成绩!"<<endl;
}
break;
}
else if(m==3)
{
cout<<"请输入英语成绩:";cin>>ENGLISH;
for(int j=0;j<i;j++)
{
if(English[j]==ENGLISH)
{
cout<<"请输入要修改的学生的姓名:"; cin>>name; Name[j]=name;
cout<<"修改成功!"<<endl;
flag=true;
}
}
if(flag==false)
{
cout<<"查无此成绩!"<<endl;
}
break;
}
else if(m==0)
{
cout<<"退出!"<<endl;break;
}
else {
cout<<"输入错误!请重新输入:"<<endl;
}
}
else if(n==0){break;}
else
{
cout<<"输入错误!请重新输入:";
cin>>n;
}
}
break;
}
else if(a==3)
{
p2.menu3();
cin>>n;
while(1)
{
if(n==1)
{
cout<<"请输入学生学号:";cin>>Id;
for(int j=0;j<i;j++)
{
if(ID[j]==Id)
{
cout<<"请输入要修改的学生的C++成绩:"; cin>>cpp;
while(1)
{
if(cpp>=0 && cpp<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>cpp;
}
Cpp[j]=cpp;
cout<<"请输入要修改的学生的数学成绩:"; cin>>math;
while(1)
{
if(math>=0 && math<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>math;
}
Math[j]=math;
cout<<"请输入要修改的学生的英语成绩:"; cin>>english;
while(1)
{
if(english>=0 && english<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>english;
}
English[j]=english;
Sum[j]=Cpp[j]+Math[j]+English[j];
Avg[j]=Sum[j]/3;
cout<<"修改成功!"<<endl;
flag=true;
break;
}
}
if(flag==false)
{
cout<<"查无此学号!"<<endl;
}
break;
}
else if(n==2)
{
cout<<"请输入学生姓名:";
cin>>NAME;
for(int j=0;j<i;j++)
{
if(Name[j]==NAME)
{
cout<<"请输入要修改的学生的C++成绩:"; cin>>cpp;
while(1)
{
if(cpp>=0 && cpp<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>cpp;
}
Cpp[j]=cpp;
cout<<"请输入要修改的学生的数学成绩:"; cin>>math;
while(1)
{
if(math>=0 && math<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>math;
}
Math[j]=math;
cout<<"请输入要修改的学生的英语成绩:"; cin>>english;
while(1)
{
if(english>=0 && english<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>english;
}
English[j]=english;
Sum[j]=Cpp[j]+Math[j]+English[j];
Avg[j]=Sum[j]/3;
cout<<"修改成功!"<<endl;
flag=true;break;
}
}
if(flag==false)
{
cout<<"查无此姓名!"<<endl;
}
break;
}
else if(n==3)
{
int m;
cout<<"============================="<<endl;
cout<<"<<输入1:按C++成绩查找! >>"<<endl
<<"<<输入2:按数学成绩查找! >>"<<endl
<<"<<输入3:按数英语成绩查找!>>"<<endl
<<"<<输入0:返回! >>"<<endl;
cout<<"============================="<<endl;
cout<<"*请输入查找方式:";
cin>>m;
if(m==1)
{
cout<<"请输入C++成绩:";cin>>CPP;
for(int j=0;j<i;j++)
{
if(Cpp[j]==CPP)
{
cout<<"请输入要修改的学生的C++成绩:"; cin>>cpp;
while(1)
{
if(cpp>=0 && cpp<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>cpp;
}
Cpp[j]=cpp;
cout<<"请输入要修改的学生的数学成绩:"; cin>>math;
while(1)
{
if(math>=0 && math<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>math;
}
Math[j]=math;
cout<<"请输入要修改的学生的英语成绩:"; cin>>english;
while(1)
{
if(english>=0 && english<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>english;
}
English[j]=english;
Sum[j]=Cpp[j]+Math[j]+English[j];
Avg[j]=Sum[j]/3;
cout<<"修改成功!"<<endl;
flag=true;
}
}
if(flag==false)
{
cout<<"查无此成绩!"<<endl;
}
break;
}
else if(m==2)
{
cout<<"请输入数学成绩:";cin>>MATH;
for(int j=0;j<i;j++)
{
if(Math[j]==MATH)
{
cout<<"请输入要修改的学生的C++成绩:"; cin>>cpp;
while(1)
{
if(cpp>=0 && cpp<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>cpp;
}
Cpp[j]=cpp;
cout<<"请输入要修改的学生的数学成绩:"; cin>>math;
while(1)
{
if(math>=0 && math<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>math;
}
Math[j]=math;
cout<<"请输入要修改的学生的英语成绩:"; cin>>english;
while(1)
{
if(english>=0 && english<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>english;
}
English[j]=english;
Sum[j]=Cpp[j]+Math[j]+English[j];
Avg[j]=Sum[j]/3;
cout<<"修改成功!"<<endl;
flag=true;
}
}
if(flag==false)
{
cout<<"查无此成绩!"<<endl;
}
break;
}
else if(m==3)
{
cout<<"请输入英语成绩:";cin>>ENGLISH;
for(int j=0;j<i;j++)
{
if(English[j]==ENGLISH)
{
cout<<"请输入要修改的学生的C++成绩:"; cin>>cpp;
while(1)
{
if(cpp>=0 && cpp<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>cpp;
}
Cpp[j]=cpp;
cout<<"请输入要修改的学生的数学成绩:"; cin>>math;
while(1)
{
if(math>=0 && math<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>math;
}
Math[j]=math;
cout<<"请输入要修改的学生的英语成绩:"; cin>>english;
while(1)
{
if(english>=0 && english<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>english;
}
English[j]=english;
Sum[j]=Cpp[j]+Math[j]+English[j];
Avg[j]=Sum[j]/3;
cout<<"修改成功!"<<endl;
flag=true;
}
}
if(flag==false)
{
cout<<"查无此成绩!"<<endl;
}
break;
}
else if(m==0)
{
cout<<"退出!"<<endl;break;
}
else {
cout<<"输入错误!请重新输入:"<<endl;
}
}
else if(n==0){break;}
else
{
cout<<"输入错误!请重新输入:";
cin>>n;
}
}
break;
}
else if(a==4)
{
p2.menu3();
cin>>n;
while(1)
{
if(n==1)
{
cout<<"请输入学生学号:";cin>>Id;
for(int j=0;j<i;j++)
{
if(ID[j]==Id)
{
cout<<"请输入要修改的学生的学号:"; cin>>id;
while(1)
{
bool flag=true;
for(int j=0;j<i;j++)
{
if(ID[j]==id)
{
cout<<"该学号已存在!请重新输入:";cin>>id;
flag=false;
}
}
if(flag==true){
ID[j]=id;
break;
}
}
cout<<"请输入要修改的学生的姓名:"; cin>>name; Name[j]=name;
cout<<"请输入要修改的学生的性别:"; cin>>sex;
while(1)
{
if(sex=="男"||sex=="女")
{
break;
}
else cout<<"输入错误,请重新输入!!"<<endl;
cin>>sex;
}
Sex[j]=sex;
cout<<"请输入要修改的学生的年龄:"; cin>>age;
while(1)
{
if(age>0 && age<=200)
{
break;
}
else cout<<"请输入1~200内的有效数字!!!"<<endl;
cin>>age;
}
Age[j]=age;
cout<<"请输入要修改的学生的C++成绩:"; cin>>cpp;
while(1)
{
if(cpp>=0 && cpp<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>cpp;
}
Cpp[j]=cpp;
cout<<"请输入要修改的学生的数学成绩:"; cin>>math;
while(1)
{
if(math>=0 && math<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>math;
}
Math[j]=math;
cout<<"请输入要修改的学生的英语成绩:"; cin>>english;
while(1)
{
if(english>=0 && english<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>english;
}
English[j]=english;
Sum[j]=Cpp[j]+Math[j]+English[j];
Avg[j]=Sum[j]/3;
cout<<"修改成功!"<<endl;
flag=true;
break;
}
}
if(flag==false)
{
cout<<"查无此学号!"<<endl;
}
break;
}
else if(n==2)
{
cout<<"请输入学生姓名:";
cin>>NAME;
for(int j=0;j<i;j++)
{
if(Name[j]==NAME)
{
cout<<"请输入要修改的学生的学号:"; cin>>id;
while(1)
{
bool flag=true;
for(int j=0;j<i;j++)
{
if(ID[j]==id)
{
cout<<"该学号已存在!请重新输入:";cin>>id;
flag=false;
}
}
if(flag==true){
ID[j]=id;
break;
}
}
cout<<"请输入要修改的学生的姓名:"; cin>>name; Name[j]=name;
cout<<"请输入要修改的学生的性别:"; cin>>sex;
while(1)
{
if(sex=="男"||sex=="女")
{
break;
}
else cout<<"输入错误,请重新输入!!"<<endl;
cin>>sex;
}
Sex[j]=sex;
cout<<"请输入要修改的学生的年龄:"; cin>>age;
while(1)
{
if(age>0 && age<=200)
{
break;
}
else cout<<"请输入1~200内的有效数字!!!"<<endl;
cin>>age;
}
Age[j]=age;
cout<<"请输入要修改的学生的C++成绩:"; cin>>cpp;
while(1)
{
if(cpp>=0 && cpp<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>cpp;
}
Cpp[j]=cpp;
cout<<"请输入要修改的学生的数学成绩:"; cin>>math;
while(1)
{
if(math>=0 && math<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>math;
}
Math[j]=math;
cout<<"请输入要修改的学生的英语成绩:"; cin>>english;
while(1)
{
if(english>=0 && english<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>english;
}
English[j]=english;
Sum[j]=Cpp[j]+Math[j]+English[j];
Avg[j]=Sum[j]/3;
cout<<"修改成功!"<<endl;
flag=true;break;
}
}
if(flag==false)
{
cout<<"查无此姓名!"<<endl;
}
break;
}
else if(n==3)
{
int m;
cout<<"============================="<<endl;
cout<<"<<输入1:按C++成绩查找! >>"<<endl
<<"<<输入2:按数学成绩查找! >>"<<endl
<<"<<输入3:按数英语成绩查找!>>"<<endl
<<"<<输入0:返回! >>"<<endl;
cout<<"============================="<<endl;
cout<<"*请输入查找方式:";
cin>>m;
if(m==1)
{
cout<<"请输入C++成绩:";cin>>CPP;
for(int j=0;j<i;j++)
{
if(Cpp[j]==CPP)
{
cout<<"请输入要修改的学生的学号:"; cin>>id;
while(1)
{
bool flag=true;
for(int j=0;j<i;j++)
{
if(ID[j]==id)
{
cout<<"该学号已存在!请重新输入:";cin>>id;
flag=false;
}
}
if(flag==true){
ID[j]=id;
break;
}
}
cout<<"请输入要修改的学生的姓名:"; cin>>name; Name[j]=name;
cout<<"请输入要修改的学生的性别:"; cin>>sex;
while(1)
{
if(sex=="男"||sex=="女")
{
break;
}
else cout<<"输入错误,请重新输入!!"<<endl;
cin>>sex;
}
Sex[j]=sex;
cout<<"请输入要修改的学生的年龄:"; cin>>age;
while(1)
{
if(age>0 && age<=200)
{
break;
}
else cout<<"请输入1~200内的有效数字!!!"<<endl;
cin>>age;
}
Age[j]=age;
cout<<"请输入要修改的学生的C++成绩:"; cin>>cpp;
while(1)
{
if(cpp>=0 && cpp<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>cpp;
}
Cpp[j]=cpp;
cout<<"请输入要修改的学生的数学成绩:"; cin>>math;
while(1)
{
if(math>=0 && math<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>math;
}
Math[j]=math;
cout<<"请输入要修改的学生的英语成绩:"; cin>>english;
while(1)
{
if(english>=0 && english<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>english;
}
English[j]=english;
Sum[j]=Cpp[j]+Math[j]+English[j];
Avg[j]=Sum[j]/3;
cout<<"修改成功!"<<endl;
flag=true;
}
}
if(flag==false)
{
cout<<"查无此成绩!"<<endl;
}
break;
}
else if(m==2)
{
cout<<"请输入数学成绩:";cin>>MATH;
for(int j=0;j<i;j++)
{
if(Math[j]==MATH)
{
cout<<"请输入要修改的学生的学号:"; cin>>id;
while(1)
{
bool flag=true;
for(int j=0;j<i;j++)
{
if(ID[j]==id)
{
cout<<"该学号已存在!请重新输入:";cin>>id;
flag=false;
}
}
if(flag==true){
ID[j]=id;
break;
}
}
cout<<"请输入要修改的学生的姓名:"; cin>>name; Name[j]=name;
cout<<"请输入要修改的学生的性别:"; cin>>sex;
while(1)
{
if(sex=="男"||sex=="女")
{
break;
}
else cout<<"输入错误,请重新输入!!"<<endl;
cin>>sex;
}
Sex[j]=sex;
cout<<"请输入要修改的学生的年龄:"; cin>>age;
while(1)
{
if(age>0 && age<=200)
{
break;
}
else cout<<"请输入1~200内的有效数字!!!"<<endl;
cin>>age;
}
Age[j]=age;
cout<<"请输入要修改的学生的C++成绩:"; cin>>cpp;
while(1)
{
if(cpp>=0 && cpp<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>cpp;
}
Cpp[j]=cpp;
cout<<"请输入要修改的学生的数学成绩:"; cin>>math;
while(1)
{
if(math>=0 && math<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>math;
}
Math[j]=math;
cout<<"请输入要修改的学生的英语成绩:"; cin>>english;
while(1)
{
if(english>=0 && english<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>english;
}
English[j]=english;
Sum[j]=Cpp[j]+Math[j]+English[j];
Avg[j]=Sum[j]/3;
cout<<"修改成功!"<<endl;
flag=true;
}
}
if(flag==false)
{
cout<<"查无此成绩!"<<endl;
}
break;
}
else if(m==3)
{
cout<<"请输入英语成绩:";cin>>ENGLISH;
for(int j=0;j<i;j++)
{
if(English[j]==ENGLISH)
{
cout<<"请输入要修改的学生的学号:"; cin>>id;
while(1)
{
bool flag=true;
for(int j=0;j<i;j++)
{
if(ID[j]==id)
{
cout<<"该学号已存在!请重新输入:";cin>>id;
flag=false;
}
}
if(flag==true){
ID[j]=id;
break;
}
}
cout<<"请输入要修改的学生的姓名:"; cin>>name; Name[j]=name;
cout<<"请输入要修改的学生的性别:"; cin>>sex;
while(1)
{
if(sex=="男"||sex=="女")
{
break;
}
else cout<<"输入错误,请重新输入!!"<<endl;
cin>>sex;
}
Sex[j]=sex;
cout<<"请输入要修改的学生的年龄:"; cin>>age;
while(1)
{
if(age>0 && age<=200)
{
break;
}
else cout<<"请输入1~200内的有效数字!!!"<<endl;
cin>>age;
}
Age[j]=age;
cout<<"请输入要修改的学生的C++成绩:"; cin>>cpp;
while(1)
{
if(cpp>=0 && cpp<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>cpp;
}
Cpp[j]=cpp;
cout<<"请输入要修改的学生的数学成绩:"; cin>>math;
while(1)
{
if(math>=0 && math<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>math;
}
Math[j]=math;
cout<<"请输入要修改的学生的英语成绩:"; cin>>english;
while(1)
{
if(english>=0 && english<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>english;
}
English[j]=english;
Sum[j]=Cpp[j]+Math[j]+English[j];
Avg[j]=Sum[j]/3;
cout<<"修改成功!"<<endl;
flag=true;
}
}
if(flag==false)
{
cout<<"查无此成绩!"<<endl;
}
break;
}
else if(m==0)
{
cout<<"退出!"<<endl;break;
}
else {
cout<<"输入错误!请重新输入:"<<endl;
}
}
else if(n==0){break;}
else
{
cout<<"输入错误!请重新输入:";
cin>>n;
}
}
break;
}
else if(a==0)
{
cout<<"退出!!"<<endl;break;
}
else
{
cout<<"输入错误!请重新输入:";
cin>>a;
}
}
}
void student::Find()
{
int n;
bool flag=false;
Page p2;
p2.menu1();
string id,name;
cout<<"请输入查找方式:"; cin>>n;
while(1)
{
if(n==1)
{
cout<<"请输入学号:"; cin>>id;
for(int j=0;j<i;j++)
{
if(ID[j]==id)
{
cout<<"\t"<<"____________________________________________________"<<endl;
cout<<"\t"<<"学号\t姓名\t性别\t年龄\tC++\t数学\t英语"<<endl;
cout<<"\t"<<ID[j]<<"\t"<<Name[j]<<"\t"<<Sex[j]<<"\t"<<Age[j]<<"\t"<<Cpp[j]<<"\t"<<Math[j]<<"\t"<<English[j]<<endl;
cout<<"\t"<<"____________________________________________________"<<endl;
flag=true;break;
}
}
if(flag==false)
{
cout<<"查不到此学号!!!"<<endl; break;
}
break;
}
else if(n==2)
{
cout<<"请输入姓名:"; cin>>name;
for(int j=0;j<i;j++)
{
if(Name[j]==name)
{
cout<<"\t"<<"____________________________________________________"<<endl;
cout<<"\t"<<"学号\t姓名\t性别\t年龄\tC++\t数学\t英语"<<endl;
cout<<"\t"<<ID[j]<<"\t"<<Name[j]<<"\t"<<Sex[j]<<"\t"<<Age[j]<<"\t"<<Cpp[j]<<"\t"<<Math[j]<<"\t"<<English[j]<<endl;
cout<<"\t"<<"____________________________________________________"<<endl;
flag=true;
}
}
if(flag==false)
{
cout<<"查不到此姓名!!!"<<endl; break;
}
break;
}
else if(n==0){ cout<<"退出!!"<<endl;break;}
else
{
cout<<"输入错误!请重新输入!!";
cin>>n;
}
}
}
void Page::menu()
{
cout<<"____________________________________"<<endl;
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<<"|$$$$$ 8.查看补考名单 $$$$$|"<<endl;
cout<<"|$$$$$ 0.离开 $$$$$|"<<endl;
cout<<"|**********************************|"<<endl;
cout<<"|__________________________________|"<<endl;
cout<<"请输入要选的功能:";
}
void Page::menu1()
{
cout<<"__________________________________"<<endl;
cout<<"| |"<<endl;
cout<<"| *请选择查找方式* |"<<endl;
cout<<"| |"<<endl;
cout<<"| 1.按学号查找 2.按姓名查找 |"<<endl;
cout<<"| |"<<endl;
cout<<"| 0.退出查找 |"<<endl;
cout<<"|________________________________|"<<endl;
}
void Page::menu2()
{
cout<<"__________________________________"<<endl;
cout<<"| |"<<endl;
cout<<"| *请选择删除方式* |"<<endl;
cout<<"| |"<<endl;
cout<<"| 1.按学号删除 2.按姓名删除 |"<<endl;
cout<<"| |"<<endl;
cout<<"| 0.退出删除 |"<<endl;
cout<<"|________________________________|"<<endl;
}
void Page::menu3()
{
cout<<"__________________________________"<<endl;
cout<<"| |"<<endl;
cout<<"| *请选择收索方式* |"<<endl;
cout<<"| |"<<endl;
cout<<"| 1.按学号搜索 2.按姓名搜索 |"<<endl;
cout<<"| |"<<endl;
cout<<"| 3.按成绩搜索 0.返回菜单 |"<<endl;
cout<<"| |"<<endl;
cout<<"|________________________________|"<<endl;
}
void Page::Login()
{
cout<<"\t\t§§§§§§§§§§§§§§§§§§§§§§§§§§"<<endl;
for(int i=0;i<3;i++)
cout<<"\t\t◎\t\t\t\t\t\t ◎"<<endl;
cout<<"\t\t◎★★★★【 欢迎进入学生成绩管理系统 】★★★★◎"<<endl;
for(int i=0;i<3;i++)
cout<<"\t\t◎\t\t\t\t\t\t ◎"<<endl;
cout<<"\t\t§§§§§§§§§§§§§§§§§§§§§§§§§§\n"<<endl;
cout<<"\t\t\t按Y继续 \t\t 按N退出"<<endl;
char ling; // 定义一个指令
cin>>ling; //输入指令
if(ling=='y'|| ling =='n') ling-=32;
cout<<endl;
switch(ling)
{
case 'N':
cout<<"\t"<<"即将退出,请稍等......"<<endl; exit(1);
case 'Y':
cout<<"\t"<<"即将进入系统......"<<endl;system("pause");system("cls");break;
default:
cout<<"\t"<<"输入错误,请重新输入!!!"<<endl;system("cls");
Page::Login();
}
}
void student::Luru()
{
cout<<"==================================="<<endl<<endl;
cout<<"请先录入学生信息,才能执行其他操作!"<<endl<<endl;
cout<<"==================================="<<endl<<endl;
cout<<"请输入学生人数:";
cin>>i;
cout<<endl<<"==================================="<<endl;
while(cin.fail())
{ cin.clear();
cout<<"请输入1~100内的有效数字:";
cin.ignore(1024,'\n');
cin>>i;
}
while(1){
if(i<=0||i>100)
{
cout<<"学生人数至少为 1 个,请输入1-100内的有效数字!"<<endl;
cin>>i;
}
else break;
}
for(int j=0;j<i;j++)
{
cout<<"请输入第 "<<j+1<<" 个同学的学号:";cin>>ID[j];
while(1)
{
bool flag=true;
for(int r=0;r<j;r++)
{
if(ID[r]==ID[j])
{
cout<<"该学号已存在!请重新输入:";cin>>ID[j];
flag=false;
}
}
if(flag==true){
break;
}
}
cout<<" 姓名:";cin>>Name[j];
cout<<" 性别:";cin>>Sex[j];
while(1)
{
if(Sex[j]=="男"||Sex[j]=="女")
{
break;
}
else cout<<"-->输入错误!请重新输入:";
cin>>Sex[j];
}
cout<<" 年龄:";cin>>Age[j];
while(cin.fail())
{ cin.clear();
cout<<"请输入1~200内的有效数字:";
cin.ignore(1024,'\n');
cin>>Age[j];
}
while(1)
{
if(Age[j]>0 && Age[j]<=200)
{
break;
}
else cout<<"请输入1~200内的有效数字:";
cin>>Age[j];
}
cout<<" C++:";cin>>Cpp[j];
while(cin.fail())
{ cin.clear();
cout<<"请输入1~100内的有效数字:";
cin.ignore(1024,'\n');
cin>>Cpp[j];
}
while(1)
{
if(Cpp[j]>=0 && Cpp[j]<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩:";
cin>>Cpp[j];
}
cout<<" 数学:";cin>>Math[j];
while(cin.fail())
{ cin.clear();
cout<<"请输入1~100内的有效数字:";
cin.ignore(1024,'\n');
cin>>Math[j];
}
while(1)
{
if(Math[j]>=0 && Math[j]<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩:";
cin>>Math[j];
}
cout<<" 英语:";cin>>English[j];
while(cin.fail())
{ cin.clear();
cout<<"请输入1~100内的有效数字:";
cin.ignore(1024,'\n');
cin>>English[j];
}
while(1)
{
if(English[j]>=0 && English[j]<=100)
{
break;
}
else cout<<"请输入0~100内的有效成绩!!!"<<endl;
cin>>English[j];
}
cout<<" 添加成功!!!"<<endl;
cout<<"==================================="<<endl;
}
system("pause");system("cls");
}
void student::Ranking()
{
int z[100],R_age[100];
float t[100],R_cpp[100],R_math[100],R_english[100],R_sum[100],R_avg[100];
string y[100],R_name[100],R_id[100],R_sex[100];
cout<<endl;
cout<<"\t"<<"--------------------------------------------------------------------------------"<<endl;
cout<<"\t"<<"*学号 姓名 性别 年龄 C++. 数学 英语 总分 平均分 排名*"<<endl;
for(int j=0;j<i;j++)
{
R_id[j]=ID[j];R_name[j]=Name[j];R_sex[j]=Sex[j];R_age[j]=Age[j];
R_cpp[j]=Cpp[j];R_math[j]=Math[j];R_english[j]=English[j];
R_sum[j]=R_cpp[j]+R_math[j]+R_english[j];
R_avg[j]=R_sum[j]/3;
}
for(int r=0;r<i;r++)
{
for(int j=0;j<i-r-1;j++)
{
if(R_sum[j+1]>R_sum[j])
{
y[j]=R_id[j];R_id[j]=R_id[j+1];R_id[j+1]=y[j];
y[j]=R_name[j];R_name[j]=R_name[j+1];R_name[j+1]=y[j];
y[j]=R_sex[j];R_sex[j]=R_sex[j+1];R_sex[j+1]=y[j];
z[j]=R_age[j];R_age[j]=R_age[j+1];R_age[j+1]=z[j];
t[j]=R_cpp[j];R_cpp[j]=R_cpp[j+1];R_cpp[j+1]=t[j];
t[j]=R_math[j];R_math[j]=R_math[j+1];R_math[j+1]=t[j];
t[j]=R_english[j];R_english[j]=R_english[j+1];R_english[j+1]=t[j];
t[j]=R_sum[j];R_sum[j]=R_sum[j+1];R_sum[j+1]=t[j];
t[j]=R_avg[j];R_avg[j]=R_avg[j+1];R_avg[j+1]=t[j];
}
else if(R_sum[j+1]==R_sum[j])
{
if(R_cpp[j+1]>R_cpp[j])
{
y[j]=R_id[j];R_id[j]=R_id[j+1];R_id[j+1]=y[j];
y[j]=R_name[j];R_name[j]=R_name[j+1];R_name[j+1]=y[j];
y[j]=R_sex[j];R_sex[j]=R_sex[j+1];R_sex[j+1]=y[j];
z[j]=R_age[j];R_age[j]=R_age[j+1];R_age[j+1]=z[j];
t[j]=R_cpp[j];R_cpp[j]=R_cpp[j+1];R_cpp[j+1]=t[j];
t[j]=R_math[j];R_math[j]=R_math[j+1];R_math[j+1]=t[j];
t[j]=R_english[j];R_english[j]=R_english[j+1];R_english[j+1]=t[j];
t[j]=R_sum[j];R_sum[j]=R_sum[j+1];R_sum[j+1]=t[j];
t[j]=R_avg[j];R_avg[j]=R_avg[j+1];R_avg[j+1]=t[j];
}
else if(R_cpp[j+1]==R_cpp[j])
{
if(R_math[j+1]>R_math[j])
{
y[j]=R_id[j];R_id[j]=R_id[j+1];R_id[j+1]=y[j];
y[j]=R_name[j];R_name[j]=R_name[j+1];R_name[j+1]=y[j];
y[j]=R_sex[j];R_sex[j]=R_sex[j+1];R_sex[j+1]=y[j];
z[j]=R_age[j];R_age[j]=R_age[j+1];R_age[j+1]=z[j];
t[j]=R_cpp[j];R_cpp[j]=R_cpp[j+1];R_cpp[j+1]=t[j];
t[j]=R_math[j];R_math[j]=R_math[j+1];R_math[j+1]=t[j];
t[j]=R_english[j];R_english[j]=R_english[j+1];R_english[j+1]=t[j];
t[j]=R_sum[j];R_sum[j]=R_sum[j+1];R_sum[j+1]=t[j];
t[j]=R_avg[j];R_avg[j]=R_avg[j+1];R_avg[j+1]=t[j];
}
else if(R_english[j+1]>R_english[j])
{
y[j]=R_id[j];R_id[j]=R_id[j+1];R_id[j+1]=y[j];
y[j]=R_name[j];R_name[j]=R_name[j+1];R_name[j+1]=y[j];
y[j]=R_sex[j];R_sex[j]=R_sex[j+1];R_sex[j+1]=y[j];
z[j]=R_age[j];R_age[j]=R_age[j+1];R_age[j+1]=z[j];
t[j]=R_cpp[j];R_cpp[j]=R_cpp[j+1];R_cpp[j+1]=t[j];
t[j]=R_math[j];R_math[j]=R_math[j+1];R_math[j+1]=t[j];
t[j]=R_english[j];R_english[j]=R_english[j+1];R_english[j+1]=t[j];
t[j]=R_sum[j];R_sum[j]=R_sum[j+1];R_sum[j+1]=t[j];
t[j]=R_avg[j];R_avg[j]=R_avg[j+1];R_avg[j+1]=t[j];
}
}
}
}
}
for(int k=0;k<i;k++)
{
ranking[k]=k+1;
cout<<"\t"<<" "<<R_id[k]<<"\t"<<" "<<R_name[k]<<"\t"<<" "<<R_sex[k]<<"\t"<<" "<<R_age[k]<<"\t"<<" "
<<R_cpp[k]<<"\t"<<" "<<R_math[k]<<"\t"<<" "<<R_english[k]<<"\t"<<" "<<R_sum[k]<<"\t"
<<" "<<R_avg[k]<<"\t"<<" "<<ranking[k]<<endl;
}
cout<<"\t"<<"--------------------------------------------------------------------------------"<<endl;
}
void student::Analyse()
{
int exceed_cpp=0,exceed_math=0,exceed_english=0,
pass_cpp=0,pass_math=0,pass_english=0,
excellent_cpp=0,excellent_math=0,excellent_english=0;
float avg_cpp,max_cpp=0,min_cpp=100,sum_cpp=0,
avg_math,max_math=0,min_math=100,sum_math=0,
avg_english,max_english=0,min_english=100,sum_english=0;
{
for(int j=0;j<i;j++)
{
sum_cpp+=Cpp[j];
if(Cpp[j]>max_cpp) max_cpp=Cpp[j];
if(Cpp[j]<min_cpp) min_cpp=Cpp[j];
}
avg_cpp=sum_cpp/i;
for(int j=0;j<i;j++)
{
if(Cpp[j]>=60)
pass_cpp++;
}
for(int j=0;j<i;j++)
{
if(Cpp[j]>=80)
excellent_cpp++;
}
for(int j=0;j<i;j++)
{
if(Cpp[j]>=avg_cpp)
exceed_cpp++;
}
cout<<endl;
cout<<"\t"<<"__________________________________________________________________"<<endl;
cout<<"\t"<<"< 班级C++.成绩情况如下: >"<<endl;
cout<<"\t"<<"=================================================================="<<endl;
cout<<"\t"<<"最高分 最低分 平均分 优良人数 及格人数 超过平均分的人数"<<endl;
cout<<"\t"<<max_cpp<<"\t"<<min_cpp<<"\t"<<avg_cpp<<"\t"<<"\t"<<excellent_cpp<<"\t"<<pass_cpp<<"\t"<<"\t"<<exceed_cpp<<endl;
cout<<"\t"<<"------------------------------------------------------------------"<<endl;
cout<<endl;
}
{
for(int j=0;j<i;j++)
{
sum_math+=Math[j];
if(Math[j]>max_math) max_math=Math[j];
if(Math[j]<min_math) min_math=Math[j];
}
avg_math=sum_math/i;
for(int j=0;j<i;j++)
{
if(Math[j]>=60)
pass_math++;
}
for(int j=0;j<i;j++)
{
if(Math[j]>=80)
excellent_math++;
}
for(int j=0;j<i;j++)
{
if(Math[j]>=avg_math)
exceed_math++;
}
cout<<endl;
cout<<"\t"<<"__________________________________________________________________"<<endl;
cout<<"\t"<<"< 班级数学成绩情况如下: >"<<endl;
cout<<"\t"<<"=================================================================="<<endl;
cout<<"\t"<<"最高分 最低分 平均分 优良人数 及格人数 超过平均分的人数"<<endl;
cout<<"\t"<<max_math<<"\t"<<min_math<<"\t"<<avg_math<<"\t"<<"\t"<<excellent_math<<"\t"<<pass_math<<"\t"<<"\t"<<exceed_math<<endl;
cout<<"\t"<<"------------------------------------------------------------------"<<endl;
cout<<endl;
}
{
for(int j=0;j<i;j++)
{
sum_english+=English[j];
if(English[j]>max_english) max_english=English[j];
if(English[j]<min_english) min_english=English[j];
}
avg_english=sum_english/i;
for(int j=0;j<i;j++)
{
if(English[j]>=60)
pass_english++;
}
for(int j=0;j<i;j++)
{
if(English[j]>=80)
excellent_english++;
}
for(int j=0;j<i;j++)
{
if(English[j]>=avg_english)
exceed_english++;
}
cout<<endl;
cout<<"\t"<<"__________________________________________________________________"<<endl;
cout<<"\t"<<"< 班级英语成绩情况如下: >"<<endl;
cout<<"\t"<<"=================================================================="<<endl;
cout<<"\t"<<"最高分 最低分 平均分 优良人数 及格人数 超过平均分的人数"<<endl;
cout<<"\t"<<max_english<<"\t"<<min_english<<"\t"<<avg_english<<"\t"<<"\t"<<excellent_english<<"\t"<<pass_english<<"\t"<<"\t"<<exceed_english<<endl;
cout<<"\t"<<"------------------------------------------------------------------"<<endl;
cout<<endl;
}
}
void Page::menu4()
{
cout<<"〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓"<<endl;
cout<<"▓ ▓"<<endl;
cout<<"▓ ★请输入要修改的信息★ ▓"<<endl;
cout<<"▓ ▓"<<endl;
cout<<"▓ 1.仅修改学号 2.仅修改姓名 ▓"<<endl;
cout<<"▓ ▓"<<endl;
cout<<"▓ ▓"<<endl;
cout<<"▓ 3.仅修改成绩 4.修改全部信息 ▓"<<endl;
cout<<"▓ ▓"<<endl;
cout<<"▓ 0.退出 ▓"<<endl;
cout<<"▓ ▓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓"<<endl;
cout<<endl<<"请输入要修改的信息:";
}
void student::bukao()
{
cout<<"==============================================="<<endl;
cout<<" < 补考学生信息:> "<<endl;
cout<<"-----------------------------------------------"<<endl;
cout<<" 学号 姓名 性别 年龄 科目"<<endl;
cout<<"==============================================="<<endl;
for(int b=0;b<i;b++)
{
if(Cpp[b]<60&&Math[b]<60&&English[b]<60)
{
cout<<" "<<ID[b]<<"\t"<<Name[b]<<"\t"<<Sex[b]<<"\t"<<Age[b]<<"\t"<<"C++、数学、英语"<<endl;
//cout<<" 数学"<<endl;
}
else if(Cpp[b]<60&&Math[b]<60)
{
cout<<" "<<ID[b]<<"\t"<<Name[b]<<"\t"<<Sex[b]<<"\t"<<Age[b]<<"\t"<<"C++、数学"<<endl;
}
else if(Cpp[b]<60&&English[b]<60)
{
cout<<" "<<ID[b]<<"\t"<<Name[b]<<"\t"<<Sex[b]<<"\t"<<Age[b]<<"\t"<<"C++、英语"<<endl;
}
else if(Math[b]<60&&English[b]<60)
{
cout<<" "<<ID[b]<<"\t"<<Name[b]<<"\t"<<Sex[b]<<"\t"<<Age[b]<<"\t"<<"数学、英语"<<endl;
}
else if(Cpp[b]<60)
{
cout<<" "<<ID[b]<<"\t"<<Name[b]<<"\t"<<Sex[b]<<"\t"<<Age[b]<<"\t"<<"C++"<<endl;
}
else if(Math[b]<60)
{
cout<<" "<<ID[b]<<"\t"<<Name[b]<<"\t"<<Sex[b]<<"\t"<<Age[b]<<"\t"<<"数学"<<endl;
}
else if(English[b]<60)
{
cout<<" "<<ID[b]<<"\t"<<Name[b]<<"\t"<<Sex[b]<<"\t"<<Age[b]<<"\t"<<"英语"<<endl;
}
}
cout<<"_______________________________________________"<<endl;
}
void Page::Login1()
{
int n=1;
string password;
cout<<"============================"<<endl;
cout<<"->请输入密码:";
cin>>password;
while(1){
if(password=="030421")
{
cout<<"============================"<<endl;
cout<<"密码正确!"<<endl;
cout<<"============================"<<endl;system("pause");system("cls");break;
}
else
{
cout<<"============================" <<endl;
cout<<"密码错误!请重新输入:";
n++;
cin>>password;
if(n==5)
{
cout<<"============================"<<endl;
cout<<"*密码错误5次,你无权访问!!!*"<<endl;
cout<<"============================"<<endl;
exit(0);
}
}
}
}
二、运行环境和运行结果
运行环境
Visual Studio 2022
运行结果
系统首页:

输入密码错误:

输入正确密码:

录入学生信息:

进入系统,查看完整成绩表:

修改学生信息(以修改学号为例):

删除学生信息:

查看修改和删除信息结果:

查看学生成绩和排名:


各科成绩分析:

查看补考名单:

三、主要问题及改善思路
1、耦合度极高:student类既存储数据(学号、成绩),又包含业务逻辑(排序、统计),还混杂界面交互(输入输出),修改一个功能可能影响其他功能。
改善思路:将原student类拆分为3个独立模块StudentData(数据类)、StudentManager(业务类)、UIManager(界面类),降低耦合。
2、无数据持久化:所有数据仅存于内存,程序退出即丢失。
改善思路:用JSON格式文件存储替代内存存储,支持数据保存和加载。
3、修改功能存在问题:修改逻辑有问题,且仅支持按学号、姓名、成绩定位,修改维度单一,无 “取消修改” 等容错机制。
改善思路:支持按学号定位并选择修改字段,而非全量修改;增加 “确认修改”、“取消修改” 选项,增加容错机制。
4、查找功能弱:仅支持精确匹配,无模糊查询(如按姓名关键词、成绩区间查找)。
改善思路:增加模糊查询、多条件筛选功能。
5、代码冗余:重复的输入校验、菜单打印逻辑分散在各个函数中。
改善思路:将重复的输入校验逻辑封装为工具函数ToolUtils,避免冗余。
6、算法效率低:排名用冒泡排序,数据量稍大时性能差。
改善思路:用C++标准库的sort替代冒泡排序。
7、缺乏异常处理:仅靠简单的条件判断校验输入,无try-catch或边界检查(如数组越界),极端输入可能导致程序崩溃。
改善思路:增加异常处理和边界检查。
8、硬编码泛滥:密码(030421)、最大学生数(100)、菜单提示语等直接写死在代码里,修改需改动源码,灵活性差。
改善思路:创建config.json文件,存储密码、最大学生数、文件路径等配置,避免硬编码。
四、修改后的新代码
修改后的代码结构如下:

下面按文件附上新代码:
StudentData.h(学生数据类)
点击查看代码
#ifndef STUDENT_DATA_H
#define STUDENT_DATA_H
#include <string>
using namespace std;
class StudentData {
private:
string id;
string name;
string gender;
int age;
float cpp;
float math;
float english;
public:
StudentData();
StudentData(string id, string name, string gender, int age,
float cpp, float math, float english);
string getId() const;
void setId(const string& newId);
string getName() const;
void setName(const string& newName);
string getGender() const;
void setGender(const string& newGender);
int getAge() const;
void setAge(int newAge);
float getCpp() const;
void setCpp(float score);
float getMath() const;
void setMath(float score);
float getEnglish() const;
void setEnglish(float score);
float getTotal() const;
float getAvg() const;
};
#endif
StudentManager.h(业务类)
点击查看代码
#ifndef STUDENT_DATA_H
#define STUDENT_DATA_H
#include <string>
using namespace std;
class StudentData {
private:
string id;
string name;
string gender;
int age;
float cpp;
float math;
float english;
public:
StudentData();
StudentData(string id, string name, string gender, int age,
float cpp, float math, float english);
string getId() const;
void setId(const string& newId);
string getName() const;
void setName(const string& newName);
string getGender() const;
void setGender(const string& newGender);
int getAge() const;
void setAge(int newAge);
float getCpp() const;
void setCpp(float score);
float getMath() const;
void setMath(float score);
float getEnglish() const;
void setEnglish(float score);
float getTotal() const;
float getAvg() const;
};
#endif
UIManager.h(界面类)
点击查看代码
#ifndef STUDENT_DATA_H
#define STUDENT_DATA_H
#include <string>
using namespace std;
class StudentData {
private:
string id;
string name;
string gender;
int age;
float cpp;
float math;
float english;
public:
StudentData();
StudentData(string id, string name, string gender, int age,
float cpp, float math, float english);
string getId() const;
void setId(const string& newId);
string getName() const;
void setName(const string& newName);
string getGender() const;
void setGender(const string& newGender);
int getAge() const;
void setAge(int newAge);
float getCpp() const;
void setCpp(float score);
float getMath() const;
void setMath(float score);
float getEnglish() const;
void setEnglish(float score);
float getTotal() const;
float getAvg() const;
};
#endif
ToolUtils.h(工具类)
点击查看代码
#ifndef TOOL_UTILS_H
#define TOOL_UTILS_H
#include <string>
using namespace std;
class ToolUtils {
public:
static bool isValidScore(float score);
static bool isValidAge(int age);
static bool isValidGender(const string& gender);
static int getIntInput(const string& prompt);
static float getFloatInput(const string& prompt);
static string getStringInput(const string& prompt);
};
#endif
ConfigManager.h(配置管理类)
点击查看代码
#ifndef TOOL_UTILS_H
#define TOOL_UTILS_H
#include <string>
using namespace std;
class ToolUtils {
public:
static bool isValidScore(float score);
static bool isValidAge(int age);
static bool isValidGender(const string& gender);
static int getIntInput(const string& prompt);
static float getFloatInput(const string& prompt);
static string getStringInput(const string& prompt);
};
#endif
StudentData.cpp(学生数据类实现)
点击查看代码
#include "StudentData.h"
StudentData::StudentData()
: id(""), name(""), gender("未知"), age(0), cpp(0), math(0), english(0) {
}
StudentData::StudentData(string id, string name, string gender, int age,
float cpp, float math, float english)
: id(id), name(name), gender(gender), age(age), cpp(cpp), math(math), english(english) {
}
string StudentData::getId() const { return id; }
void StudentData::setId(const string& newId) { id = newId; }
string StudentData::getName() const { return name; }
void StudentData::setName(const string& newName) { name = newName; }
string StudentData::getGender() const { return gender; }
void StudentData::setGender(const string& newGender) { gender = newGender; }
int StudentData::getAge() const { return age; }
void StudentData::setAge(int newAge) { age = newAge; }
float StudentData::getCpp() const { return cpp; }
void StudentData::setCpp(float score) { cpp = score; }
float StudentData::getMath() const { return math; }
void StudentData::setMath(float score) { math = score; }
float StudentData::getEnglish() const { return english; }
void StudentData::setEnglish(float score) { english = score; }
float StudentData::getTotal() const { return cpp + math + english; }
float StudentData::getAvg() const { return getTotal() / 3.0f; }
StudentManager.cpp (业务类实现)
点击查看代码
#include "StudentManager.h"
#include "ToolUtils.h"
#include <algorithm>
#include <fstream>
#include "nlohmann/json.hpp"
using namespace std;
using json = nlohmann::json;
StudentManager::StudentManager(ConfigManager* cfg) : config(cfg) {}
bool StudentManager::addStudent(const StudentData& stu) {
if (idIndex.count(stu.getId())) {
return false;
}
if (students.size() >= config->getMaxStudents()) {
return false;
}
students.push_back(stu);
idIndex[stu.getId()] = stu;
return true;
}
bool StudentManager::deleteStudent(const string& id) {
if (!idIndex.count(id)) return false;
auto it = find_if(students.begin(), students.end(),
[&](const StudentData& s) { return s.getId() == id; });
if (it != students.end()) {
students.erase(it);
idIndex.erase(id);
return true;
}
return false;
}
bool StudentManager::updateStudent(const string& id, const StudentData& newStu) {
if (!idIndex.count(id)) return false;
if (id != newStu.getId() && idIndex.count(newStu.getId())) {
return false;
}
auto it = find_if(students.begin(), students.end(),
[&](const StudentData& s) { return s.getId() == id; });
if (it != students.end()) {
*it = newStu;
idIndex.erase(id);
idIndex[newStu.getId()] = newStu;
return true;
}
return false;
}
vector<StudentData> StudentManager::findStudent(const string& key, bool isFuzzy) {
vector<StudentData> result;
if (!isFuzzy) {
if (idIndex.count(key)) {
result.push_back(idIndex[key]);
return result;
}
for (const auto& s : students) {
if (s.getName() == key) {
result.push_back(s);
}
}
return result;
}
for (const auto& s : students) {
if (s.getId().find(key) != string::npos || s.getName().find(key) != string::npos) {
result.push_back(s);
}
}
return result;
}
StudentData StudentManager::findById(const string& id) {
if (idIndex.count(id)) return idIndex[id];
return StudentData();
}
void StudentManager::sortByTotal() {
sort(students.begin(), students.end(),
[](const StudentData& a, const StudentData& b) {
if (a.getTotal() != b.getTotal()) return a.getTotal() > b.getTotal();
if (a.getCpp() != b.getCpp()) return a.getCpp() > b.getCpp();
if (a.getMath() != b.getMath()) return a.getMath() > b.getMath();
return a.getEnglish() > b.getEnglish();
});
}
vector<StudentData> StudentManager::getRanking() {
sortByTotal();
return students;
}
vector<StudentData> StudentManager::getMakeupStudents() const {
vector<StudentData> res;
for (const auto& s : students) {
if (s.getCpp() < 60 || s.getMath() < 60 || s.getEnglish() < 60) {
res.push_back(s);
}
}
return res;
}
bool StudentManager::saveToFile(const string& filename) {
ofstream file(filename);
if (!file.is_open()) return false;
json j;
for (const auto& s : students) {
json sj;
sj["id"] = s.getId();
sj["name"] = s.getName();
sj["gender"] = s.getGender();
sj["age"] = s.getAge();
sj["cpp"] = s.getCpp();
sj["math"] = s.getMath();
sj["english"] = s.getEnglish();
j.push_back(sj);
}
file << j.dump(4);
file.close();
return true;
}
bool StudentManager::loadFromFile(const string& filename) {
ifstream file(filename);
if (!file.is_open()) return false;
json j;
file >> j;
students.clear();
idIndex.clear();
for (const auto& sj : j) {
StudentData s(
sj["id"], sj["name"], sj["gender"], sj["age"],
sj["cpp"], sj["math"], sj["english"]
);
students.push_back(s);
idIndex[s.getId()] = s;
}
file.close();
return true;
}
int StudentManager::getStudentCount() const { return students.size(); }
vector<StudentData>& StudentManager::getAllStudents() { return students; }
UIManager.cpp(界面类实现)
点击查看代码
#include "UIManager.h"
#include "ToolUtils.h"
#include <iostream>
#include <iomanip>
using namespace std;
UIManager::UIManager(StudentManager* mgr, ConfigManager* cfg)
: stuMgr(mgr), config(cfg) {
}
bool UIManager::login() {
int attempts = 0;
int maxAttempts = config->getMaxLoginAttempts();
string correctPwd = config->getPassword();
while (attempts < maxAttempts) {
string pwd = ToolUtils::getStringInput("请输入密码:");
if (pwd == correctPwd) {
cout << "登录成功!" << endl;
return true;
}
attempts++;
cout << "密码错误,剩余尝试次数:" << (maxAttempts - attempts) << endl;
}
cout << "登录失败次数过多,程序退出。" << endl;
return false;
}
void UIManager::showStudentInfo(const StudentData& stu) {
cout << "学号:" << stu.getId()
<< " 姓名:" << stu.getName()
<< " 性别:" << stu.getGender()
<< " 年龄:" << stu.getAge() << endl;
cout << "C++:" << stu.getCpp()
<< " 数学:" << stu.getMath()
<< " 英语:" << stu.getEnglish()
<< " 总分:" << stu.getTotal()
<< " 平均分:" << fixed << setprecision(2) << stu.getAvg() << endl;
}
void UIManager::showStudentList(const vector<StudentData>& list) {
if (list.empty()) {
cout << "无匹配数据。" << endl;
return;
}
cout << left << setw(10) << "学号"
<< left << setw(8) << "姓名"
<< left << setw(6) << "性别"
<< left << setw(6) << "年龄"
<< left << setw(6) << "C++"
<< left << setw(6) << "数学"
<< left << setw(6) << "英语"
<< left << setw(8) << "总分"
<< left << setw(8) << "平均分" << endl;
for (const auto& s : list) {
cout << left << setw(10) << s.getId()
<< left << setw(8) << s.getName()
<< left << setw(6) << s.getGender()
<< left << setw(6) << s.getAge()
<< left << setw(6) << s.getCpp()
<< left << setw(6) << s.getMath()
<< left << setw(6) << s.getEnglish()
<< left << setw(8) << s.getTotal()
<< left << setw(8) << fixed << setprecision(2) << s.getAvg() << endl;
}
}
void UIManager::handleAdd() {
cout << "=== 添加学生 ===" << endl;
string id = ToolUtils::getStringInput("输入学号:");
if (stuMgr->findById(id).getId() == id) {
cout << "学号已存在!" << endl;
return;
}
string name = ToolUtils::getStringInput("输入姓名:");
string gender;
do {
gender = ToolUtils::getStringInput("输入性别(男/女):");
} while (!ToolUtils::isValidGender(gender));
int age;
do {
age = ToolUtils::getIntInput("输入年龄:");
} while (!ToolUtils::isValidAge(age));
float cpp, math, english;
do { cpp = ToolUtils::getFloatInput("输入C++成绩:"); } while (!ToolUtils::isValidScore(cpp));
do { math = ToolUtils::getFloatInput("输入数学成绩:"); } while (!ToolUtils::isValidScore(math));
do { english = ToolUtils::getFloatInput("输入英语成绩:"); } while (!ToolUtils::isValidScore(english));
StudentData stu(id, name, gender, age, cpp, math, english);
if (stuMgr->addStudent(stu)) {
cout << "添加成功!" << endl;
}
else {
cout << "添加失败(可能超出最大人数)!" << endl;
}
}
void UIManager::handleShowAll() {
cout << "=== 全部学生信息 ===" << endl;
showStudentList(stuMgr->getAllStudents());
}
void UIManager::handleModify() {
cout << "=== 修改学生信息 ===" << endl;
string id = ToolUtils::getStringInput("输入要修改的学生学号:");
StudentData old = stuMgr->findById(id);
if (old.getId().empty()) {
cout << "未找到该学生!" << endl;
return;
}
showStudentInfo(old);
string newId = ToolUtils::getStringInput("输入新学号(回车保持不变):");
if (newId.empty()) newId = old.getId();
string newName = ToolUtils::getStringInput("输入新姓名(回车保持不变):");
if (newName.empty()) newName = old.getName();
string newGender = ToolUtils::getStringInput("输入新性别(男/女,回车保持不变):");
if (newGender.empty()) newGender = old.getGender();
int newAge = ToolUtils::getIntInput("输入新年龄(输入-1保持不变):");
if (newAge == -1) newAge = old.getAge();
float newCpp = ToolUtils::getFloatInput("输入新C++成绩(输入-1保持不变):");
if (newCpp < 0) newCpp = old.getCpp();
float newMath = ToolUtils::getFloatInput("输入新数学成绩(输入-1保持不变):");
if (newMath < 0) newMath = old.getMath();
float newEnglish = ToolUtils::getFloatInput("输入新英语成绩(输入-1保持不变):");
if (newEnglish < 0) newEnglish = old.getEnglish();
StudentData newStu(newId, newName, newGender, newAge, newCpp, newMath, newEnglish);
if (stuMgr->updateStudent(id, newStu)) {
cout << "修改成功!" << endl;
}
else {
cout << "修改失败!" << endl;
}
}
void UIManager::handleDelete() {
cout << "=== 删除学生 ===" << endl;
string id = ToolUtils::getStringInput("输入要删除的学生学号:");
if (stuMgr->deleteStudent(id)) {
cout << "删除成功!" << endl;
}
else {
cout << "未找到该学生!" << endl;
}
}
void UIManager::handleQuery() {
cout << "=== 查询学生 ===" << endl;
int mode = ToolUtils::getIntInput("1-精确查询 2-模糊查询:");
string key = ToolUtils::getStringInput("输入查询关键字:");
vector<StudentData> res = stuMgr->findStudent(key, mode == 2);
showStudentList(res);
}
void UIManager::handleRanking() {
cout << "=== 学生排名(按总分降序)===" << endl;
showStudentList(stuMgr->getRanking());
}
void UIManager::handleStats() {
cout << "=== 各科统计 ===" << endl;
vector<StudentData>& all = stuMgr->getAllStudents();
if (all.empty()) { cout << "暂无数据" << endl; return; }
float cppMax = 0, cppMin = 100, cppSum = 0;
float mathMax = 0, mathMin = 100, mathSum = 0;
float engMax = 0, engMin = 100, engSum = 0;
int n = all.size();
for (const auto& s : all) {
float c = s.getCpp(), m = s.getMath(), e = s.getEnglish();
cppMax = max(cppMax, c); cppMin = min(cppMin, c); cppSum += c;
mathMax = max(mathMax, m); mathMin = min(mathMin, m); mathSum += m;
engMax = max(engMax, e); engMin = min(engMin, e); engSum += e;
}
cout << "C++:最高分=" << cppMax << " 最低分=" << cppMin << " 平均分=" << cppSum / n << endl;
cout << "数学:最高分=" << mathMax << " 最低分=" << mathMin << " 平均分=" << mathSum / n << endl;
cout << "英语:最高分=" << engMax << " 最低分=" << engMin << " 平均分=" << engSum / n << endl;
}
void UIManager::handleMakeup() {
cout << "=== 补考名单 ===" << endl;
showStudentList(stuMgr->getMakeupStudents());
}
void UIManager::showMainMenu() {
cout << "\n===== 学生成绩管理系统 =====" << endl;
cout << "1. 添加学生信息" << endl;
cout << "2. 显示全部学生" << endl;
cout << "3. 修改学生信息" << endl;
cout << "4. 删除学生信息" << endl;
cout << "5. 查询学生信息" << endl;
cout << "6. 查看学生排名" << endl;
cout << "7. 各科情况分析" << endl;
cout << "8. 查看补考名单" << endl;
cout << "0. 保存并退出" << endl;
}
void UIManager::run() {
if (!login()) return;
stuMgr->loadFromFile(config->getDataFile());
while (true) {
showMainMenu();
int choice = ToolUtils::getIntInput("请输入功能编号:");
switch (choice) {
case 1: handleAdd(); break;
case 2: handleShowAll(); break;
case 3: handleModify(); break;
case 4: handleDelete(); break;
case 5: handleQuery(); break;
case 6: handleRanking(); break;
case 7: handleStats(); break;
case 8: handleMakeup(); break;
case 0:
stuMgr->saveToFile(config->getDataFile());
cout << "数据已保存,程序退出。" << endl;
return;
default: cout << "无效选项,请重新输入!" << endl;
}
cout << "\n按任意键继续...";
cin.get();
}
}
ToolUtils.cpp(工具类实现)
点击查看代码
#include "ToolUtils.h"
#include <iostream>
#include <limits>
using namespace std;
bool ToolUtils::isValidScore(float score) {
return score >= 0.0f && score <= 100.0f;
}
bool ToolUtils::isValidAge(int age) {
return age >= 10 && age <= 100;
}
bool ToolUtils::isValidGender(const string& gender) {
return gender == "男" || gender == "女";
}
int ToolUtils::getIntInput(const string& prompt) {
int num;
while (true) {
cout << prompt;
if (cin >> num) {
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return num;
}
else {
cout << "输入错误,请输入整数!" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
}
float ToolUtils::getFloatInput(const string& prompt) {
float num;
while (true) {
cout << prompt;
if (cin >> num) {
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return num;
}
else {
cout << "输入错误,请输入数字!" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
}
string ToolUtils::getStringInput(const string& prompt) {
string s;
cout << prompt;
getline(cin, s);
return s;
}
ConfigManager.cpp(配置管理类实现)
点击查看代码
#include "ConfigManager.h"
#include <fstream>
using namespace std;
ConfigManager::ConfigManager(const string& configFile) {
ifstream file(configFile);
if (file.is_open()) {
file >> config;
}
}
string ConfigManager::getPassword() const {
return config.value("password", "030421");
}
int ConfigManager::getMaxStudents() const {
return config.value("max_students", 100);
}
string ConfigManager::getDataFile() const {
return config.value("data_file", "students.json");
}
int ConfigManager::getMaxLoginAttempts() const {
return config.value("max_login_attempts", 5);
}
main.cpp
点击查看代码
#include "ConfigManager.h"
#include "StudentManager.h"
#include "UIManager.h"
#include <iostream>
using namespace std;
int main() {
setlocale(LC_ALL, "");
try {
ConfigManager config("config.json");
StudentManager stuMgr(&config);
UIManager ui(&stuMgr, &config);
ui.run();
}
catch (const exception& e) {
cerr << "错误:" << e.what() << endl;
return 1;
}
return 0;
}
config.json
点击查看代码
{
"password": "030421",
"max_students": 100,
"data_file": "students.json",
"max_login_attempts": 5
}
五、重构的软件测试图(仅展示修改的部分)
修改功能-支持修改部分信息:

查询功能-增加模糊查询:

简化系统进入流程:

六、总结思考
在这次二次开发过程中,耗时最久的就是第三方库(nlohmann/json)的路径与集成问题。明明已经下载了json.hpp并且添加到项目目录中,并且正确使用了#include <nlohmann/json.hpp>,但是一直报错无法打开文件。

在这个问题上卡了一两个小时,在CSDN上查询尝试多次也没有解决,最后发现是这个文件没有放到文件目录里,只在项目目录里创建了。后来在项目目录里手动创建,再把json.hpp文件拖过去,别忘记修改附加包含目录,这样就不会再报错了。



参考CSDN博客《VScode引入json.hpp文件》
《主要是使用#includenlohmannjson.hpp时显示找不到文件》
遇到的难点主要是通过逆向工程梳理 ConfigManager/StudentManager/UIManager 的调用关系,需要明确数据流向:main.cpp → 加载配置 → 登录 → 加载学生数据 → 业务操作 → 持久化;确保二次开发不破坏原有功能,仅在薄弱点(异常处理、文件读写)做增强。
关于逆向软件工程的一些思考
从这个二次开发中体会到的逆向工程的核心就是先读懂,再动手。本次开发中,先通过 main.cpp 追踪到配置加载、学生管理的核心流程,再针对性优化异常处理,避免乱改一通;逆向修改时,不要一次性大改,要采用一小步一小步增量修改,模块化地改;优先保证兼容性,不改变原有接口、数据格式,只在内部增强健壮性,避免因重构导致原有功能失效,增加测试成本。

浙公网安备 33010602011771号