目录
1.题目要求
学生成绩管理系统
(1)增加记录:要求可以连续增加多条记录。
(2)查找:可以根据姓名(或学号)查找某个学生的课程成绩,查找某门课程成绩处于指定分数段内的学生名单等等。可以实现模糊查询,即输入名字的一部
分,可以列出满足条件的所有记录。再从这个记录中进行二次选择。
(3)删除一个学生的记录:要求可以先查找,再删除。删除前,要求用户确认。
(4)成绩修改:若输入错误可进行修改;要求可以先查找,再修改。
(5)统计分析:对某个班级学生的单科成绩进行统计,求出平均成绩;求平均成绩要求实现函数的重载,既能求单科的平均成绩,又能求三科总分的平均成绩。
求出一门课程标准差和合格率;
(6)排序功能:要求按总分进行排序(从高到低),若总分相同,则按数学排序;
若总分和数学相同,则按物理排序;若总分和各科成绩都相同,则按学号排
序;
(7)文件操作:可以打开文件,显示班级的所有学生信息;可以将增加或修改后的成绩重新写入文件;可以将排序好的信息写入新的文件;
2.需求分析
基本思路
设计一个学生的类。类的设计测试数据的记录和将要实现的功能。学生成绩
管理系统有若干学生成绩记录组成,学生一条记录包括:学号、姓名、数学
成绩、物理成绩和英语成绩。主函数显示功能菜单,供用户选择操作。每步操
作之前,都要显示菜单。在主函数中调用类的方法
编译环境
在dev-c++上运行通过
3.整体设计
4.详细设计
主函数设计
int main()
{
studentlist stulist;
stulist.init(); //读入文件数据初始化
stulist.menu(); //打开主菜单
return 0;
}
效果如下
学科科目类
class subject
{
public:
string proname; //学科名
double proscore; //分数
void showcpinfo()
{
cout << left << setw(45) << proname;
cout << left << setw(6) << proscore << endl;
}
//输出属性名,只显示总积分
static void showheader()
{
cout << left << setw(45) << "科目";
cout << left << setw(6) << "分数" <<endl;
}
};
效果如下
主菜单
void menu()
{
string sel = "0";
system("cls");
cout << "\t\t\t**********欢迎来到学生成绩管理系统**********" << endl;
cout << "\t\t\t你可以进行以下操作:" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 1 添加学生信息 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 2 删除学生信息 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 3 修改学生信息 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 4 查询学生信息 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 5 显示信息列表 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 6 统计学生数据 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 7 清空系统数据 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 0 退出 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t请选择【0-7】:";
cin >> sel;
while(sel != "0" && sel != "1" && sel != "2" && sel != "3" && sel != "4" && sel != "5" && sel != "6" && sel != "7")
{
cout << "\t\t\t输入不合法,请重新选择【0-7】:";
cin >> sel;
}
if(sel == "1")
{
this->insertlist();
this->menu();
}
else if(sel == "2")
{
this->deletelist();
this->menu();
}
else if(sel == "3")
{
this->updatelist();
this->menu();
}
else if(sel == "4")
{
this->selectlist();
this->menu();
}
else if(sel == "5")
{
this->displaylist();
this->menu();
}
else if(sel == "6")
{
this->statisticlist();
this->menu();
}
else if(sel == "7")
{
this->clearlist();
this->menu();
}
else if(sel == "0")
{
exit(0);
}
}
效果如下
读取文件与写入文件
void readfile()
{
ifstream ifs;
ifs.open("stulist.txt", ios::in);
int n = 0; //用来接收学生数量的值
ifs >> n;
for(int i = 0; i < n; i++)
{
student s;
ifs >> s.stunum >> s.stuname >> s.gender >> s.gradenum >> s.department >> s.classnum >> s.cpnum >> s.score;
for(int j = 0; j < s.cpnum; j++)
{
subject cp;
ifs >> cp.proname >> cp.proscore;
s.cps.push_back(cp);
}
stulist.push_back(s);
}
ifs.close();
}
//写入文件
void writefile()
{
ofstream ofs;
ofs.open("stulist.txt", ios::out);
ofs << stulist.size() << endl; //先写入学生数量
for (int i = 0; i < stulist.size(); i++)
{
ofs << stulist[i].stunum << " " << stulist[i].stuname << " " << stulist[i].gender
<< " " << stulist[i].gradenum << " " << stulist[i].department << " " << stulist[i].classnum
<< " " << stulist[i].cpnum << " " << stulist[i].score << endl;
for(int j = 0; j < stulist[i].cps.size(); j++)
{
ofs << stulist[i].cps[j].proname << " " << stulist[i].cps[j].proscore << endl;
}
}
ofs.close();
}
效果如下
添加学生信息
void insertlist()
{
while(true)
{
system("cls");
cout << "\t\t**********************欢迎来到添加学生信息功能*************************" << endl;
cout<< "\t\t学生基本信息一览表:"<<endl;
displaystu();
cout<<endl;
cout << "\t\t------------------" << endl;
cout << "\t\t1 添加学生信息" << endl;
cout << "\t\t2 返回主菜单" << endl;
cout << "\t\t------------------" << endl;
cout << "\t\t请选择【1-2】:";
string sel;
cin >> sel;
while(sel != "1" && sel != "2")
{
cout << "\t\t输入不合法,请重新输入【1-2】:";
cin >> sel;
}
if(sel == "1")
{
string flag = "1";
while (flag == "1")
{
cout << "\t\t输入学生信息:"<<endl;
cout << "\t\t学号:";
student s;
bool check = false;
do
{
check = false;
cin >> s.stunum;
for(int i = 0; i < stulist.size(); ++i)
{
if(s.stunum == stulist[i].stunum)
{
cout<<"\t\t该学号已存在,请重新输入:";
check = true;
break;
}
}
}
while(check);
cout << "\t\t姓名:";
cin >> s.stuname;
cout << "\t\t性别:";
cin >> s.gender;
cout << "\t\t年级:";
cin >> s.gradenum;
cout<<"\t\t专业:";
cin >> s.department;
cout<<"\t\t班级:";
cin >> s.classnum;
s.score = 0;
string option = "1";
cout<<"\t\t是否添加成绩信息?(1 是 0 否)"<<endl;
cout << "\t\t请进行选择【0-1】:";
cin >> option;
while(option != "0" && option != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> option;
}
if(option == "1")
{
int cnt = 0;
while(option == "1")
{
cnt++;
subject cp;
cout<<"\t\t第"<<cnt<<"科科目名称:";
cin>>cp.proname;
cout<<"\t\t第"<<cnt<<"科科目成绩:";
cin>>cp.proscore;
s.cps.push_back(cp);
cout << "\t\t该科成绩信息添加成功!是否继续添加?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
cin >> option;
while(option != "0" && option != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> option;
}
}
}
for(int i = 0; i < s.cps.size(); i++)
{
s.score += s.cps[i].proscore;
}
if(s.cps.size() > 0) s.score /= s.cps.size(); //计算平均分
s.cpnum = s.cps.size();
stulist.push_back(s);
writefile();
cout << "\n\t\t该名学生信息添加成功!是否继续添加?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
cin >> flag;
while(flag != "0" && flag != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> flag;
}
}
cout << "\t\t";
system("pause");
}
else
{
break;
}
}
}
效果如下
删除学生信息
void deletelist()
{
while (true)
{
system("cls");
cout << "\t\t***********************欢迎来到删除学生信息功能***********************" << endl;
string sel = "0";
cout << "\t\t-----------------" << endl;
cout << "\t\t1 按学号删除" << endl;
cout << "\t\t2 按姓名删除" << endl;
cout << "\t\t3 返回主菜单" << endl;
cout << "\t\t-----------------" << endl;
cout << "\t\t请进行选择【1-3】:";
cin >> sel;
while(sel != "1" && sel != "2" && sel != "3")
{
cout << "\t\t输入不合法,请重新选择【1-3】:";
cin >> sel;
}
if (sel == "1")
{
string keynum;
bool flag = false;
cout << "\t\t请输入待删除学生的学号:";
cin >> keynum;
for (vector<student>::iterator it = stulist.begin(); it != stulist.end(); ++it)
{
if (it->stunum == keynum)
{
flag = true;
cout << "\t\t待删除学生的信息如下:" << endl;
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
student::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
it->showstuinfo();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout << "\t\t确认删除?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while(ch != "0" && ch != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> ch;
}
if (ch == "0") break;
else
{
stulist.erase(it);
writefile();
cout << "\t\t删除成功!" << endl;
break;
}
}
}
if (!flag) cout << "\t\t查无此人,无法删除!\n" << endl;
cout << "\t\t";
system("pause");
}
else if (sel == "2")
{
string keyname;
bool flag = false;
cout << "\t\t请输入待删除学生的姓名:";
cin >> keyname;
for (vector<student>::iterator it = stulist.begin(); it != stulist.end(); ++it)
{
if (it->stuname == keyname)
{
flag = true;
cout << "\t\t待删除学生的信息如下:" << endl;
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t\t";
student::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
it->showstuinfo();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout << "\t\t确认删除?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while(ch != "0" && ch != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> ch;
}
if (ch == "0") break;
else
{
stulist.erase(it);
writefile();
cout << "\t\t删除成功!" << endl;
break;
}
}
}
if (!flag) cout << "\t\t查无此人,无法删除!\n" << endl;
cout << "\t\t";
system("pause");
}
else
{
break;
}
}
}
效果如下
修改学生信息
void updatelist()
{
while(true)
{
system("cls");
cout << "\t\t***********************欢迎来到修改学生信息功能***********************" << endl;
string sel = "0";
cout << "\t\t-----------------" << endl;
cout << "\t\t1 修改学生基本信息" << endl;
cout << "\t\t2 修改学生成绩信息" << endl;
cout << "\t\t3 返回主菜单" << endl;
cout << "\t\t-----------------" << endl;
cout << "\t\t请进行选择【1-3】:";
cin >> sel;
while(sel != "1" && sel != "2" && sel != "3")
{
cout << "\t\t输入不合法,请重新选择【1-3】:";
cin >> sel;
}
if(sel == "1")
{
bool flag = false;
string keynum;
cout << "\t\t请输入待修改学生的学号:";
cin >> keynum;
for (int i = 0; i < stulist.size(); i++)
{
if (stulist[i].stunum == keynum)
{
flag = true;
cout << "\t\t待修改学生基本信息如下:" << endl;
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
student::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
stulist[i].showstuinfo();
cout << "\t\t-----------------------------------------------------------------------" << endl;
student s = stulist[i];
cout << "\t\t请输入修改后的学号:";
bool check = false;
do
{
check = false;
cin >> s.stunum;
for(int j = 0; j < stulist.size(); ++j)
{
if(s.stunum == stulist[j].stunum && i != j)
{
cout<<"\t\t该学号已被录入,请重新输入学号:";
check = true;
break;
}
}
}
while(check);
cout << "\t\t请输入修改后的姓名:";
cin >> s.stuname;
cout << "\t\t请输入修改后的性别:";
cin >> s.gender;
cout << "\t\t请输入修改后的年级:";
cin >> s.gradenum;
cout<<"\t\t请输入修改后的专业:";
cin >> s.department;
cout<<"\t\t请输入修改后的班级:";
cin >> s.classnum;
cout << "\t\t是否确认修改?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while(ch != "0" && ch != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> ch;
}
if (ch == "0") break;
else
{
stulist[i] = s;
cout << "\t\t修改成功!" << endl;
writefile();
break;
}
}
}
if (!flag) cout << "\t\t查无此人,无法修改!\n" << endl;
}
else if(sel == "2")
{
bool flag = false;
string keynum;
cout << "\t\t请输入待修改学生的学号:";
cin >> keynum;
for (int i = 0; i < stulist.size(); i++)
{
if (stulist[i].stunum == keynum)
{
flag = true;
cout << "\t\t待修改学生成绩信息如下:" << endl;
cout << "\t\t----------------------------------------------------------" << endl;
cout<<"\t\t";
cout << left << setw(8) << "编号";
subject::showheader();
cout << "\t\t----------------------------------------------------------" << endl;
for(int j = 0; j < stulist[i].cps.size(); j++)
{
cout<<"\t\t";
cout << left << setw(8) << j + 1;
stulist[i].cps[j].showcpinfo();
}
cout << "\t\t----------------------------------------------------------" << endl;
student s = stulist[i];
cout << "\t\t请选择修改方式:"<<endl;
string option = "1";
cout << "\t\t-------------------------------" << endl;
cout << "\t\t1 基于该学生原有成绩信息进行修改" << endl;
cout << "\t\t2 清空该学生所有科目及成绩信息" << endl;
cout << "\t\t-------------------------------" << endl;
cout << "\t\t请进行选择【1-2】:";
cin >> option;
while(option != "1" && option != "2")
{
cout << "\t\t输入不合法,请重新选择【1-2】:";
cin >> option;
}
subject cp;
if(option == "1")
{
s.cps.clear();
s.score = 0;
s.cpnum = 0;
cout<<"\t\t输入修改后的学科成绩信息:"<<endl;
int cnt = 0;
while(option == "1")
{
cnt++;
cout<<"\t\t第"<<cnt<<"科科目名称:";
cin>>cp.proname;
cout<<"\t\t第"<<cnt<<"科科目成绩:";
cin>>cp.proscore;
s.cps.push_back(cp);
cout << "\n\t\t添加成功!是否继续添加?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
cin >> option;
while(option != "0" && option != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> option;
}
}
}
else if(option == "2")
{
s.cps.clear();
s.score = 0;
s.cpnum = 0;
}
//计算平均分
for(int i = 0; i < s.cps.size(); i++)
{
s.score += s.cps[i].proscore;
}
if(s.cps.size() > 0) s.cpnum = s.cps.size();
cout << "\t\t是否确认修改?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while(ch != "0" && ch != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> ch;
}
if (ch == "0") break;
else
{
stulist[i] = s;
cout << "\t\t修改成功!" << endl;
writefile();
break;
}
}
}
if (!flag) cout << "\t\t查无此人,无法修改!\n" << endl;
}
else
{
break;
}
cout << "\t\t";
system("pause");
}
}
效果如下
显示信息列表
void selectlist()
{
while (true)
{
system("cls");
cout << "\t\t***********************欢迎来到查询学生信息功能************************" << endl;
cout << "\t\t-----------------" << endl;
cout << "\t\t1 按学号查询" << endl;
cout << "\t\t2 按姓名查询" << endl;
cout << "\t\t3 返回主菜单" << endl;
cout << "\t\t-----------------" << endl;
cout << "\t\t请进行选择【1-3】:";
string sel = "0";
cin >> sel;
while(sel != "1" && sel != "2" && sel != "3")
{
cout << "\t\t输入不合法,请重新选择【1-3】:";
cin >> sel;
}
if (sel == "1")
{
string keynum;
bool flag = false;
cout << "\t\t请输入待查询学生的学号:";
cin >> keynum;
cout << "\t\t查询结果如下:" << endl;
cout<<"\t\t基本信息:"<<endl;
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
student::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
for (int i = 0; i < stulist.size(); i++)
{
if (stulist[i].stunum == keynum)
{
flag = true;
cout<<"\t\t";
stulist[i].showstuinfo();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<endl;
cout<<"\t\t"<<stulist[i].stuname<<"成绩信息:"<<endl;
cout << "\t\t----------------------------------------------------------" << endl;
cout<<"\t\t";
cout << left << setw(8) << "编号";
subject::showheader();
cout << "\t\t----------------------------------------------------------" << endl;
for(int j = 0; j < stulist[i].cps.size(); j++)
{
cout<<"\t\t";
cout << left << setw(8) << j + 1;
stulist[i].cps[j].showcpinfo();
}
cout << "\t\t----------------------------------------------------------" << endl;
break;
}
}
if (!flag) cout << "\t\t查无此人!\n" << endl;
cout << "\t\t";
system("pause");
}
else if (sel == "2")
{
string keyname;
bool flag = false;
cout << "\t\t请输入待查询联系人的姓名:";
cin >> keyname;
cout << "\t\t查询结果如下:" << endl;
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
student::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
for (int i = 0; i < stulist.size(); i++)
{
if (stulist[i].stuname == keyname)
{
flag = true;
cout<<"\t\t";
stulist[i].showstuinfo();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<endl;
cout<<"\t\t"<<stulist[i].stuname<<"成绩信息:"<<endl;
cout << "\t\t----------------------------------------------------------" << endl;
cout<<"\t\t";
cout << left << setw(8) << "编号";
subject::showheader();
cout << "\t\t----------------------------------------------------------" << endl;
for(int j = 0; j < stulist[i].cps.size(); j++)
{
cout<<"\t\t";
cout << left << setw(8) << j + 1;
stulist[i].cps[j].showcpinfo();
}
cout << "\t\t----------------------------------------------------------" << endl;
break;
}
}
if (!flag) cout << "\t\t查无此人!\n" << endl;
cout << "\t\t";
system("pause");
}
else
{
break;
}
}
}
//遍历学生列表
void displaystu()
{
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
student::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
for (int i = 0; i < stulist.size(); i++)
{
cout << "\t\t";
stulist[i].showstuinfo();
}
cout << "\t\t-----------------------------------------------------------------------" << endl;
}
//查询获奖信息
void displaycp()
{
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
cout << left << setw(12) << "学号";
cout << left << setw(10) << "姓名";
subject::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
for (int i = 0; i < stulist.size(); i++)
{
for(int j = 0; j < stulist[i].cps.size(); j++)
{
cout << "\t\t";
cout << left << setw(12) << stulist[i].stunum;
cout << left << setw(10) << stulist[i].stuname;
stulist[i].cps[j].showcpinfo();
}
}
cout << "\t\t-----------------------------------------------------------------------" << endl;
}
显示学生列表
void displaylist()
{
system("cls");
cout << "\t\t***********************欢迎来到显示信息列表功能************************" << endl;
cout << "\t\t表1:学生基本信息一览表"<<endl;
this->displaystu();
cout << "\n\t\t表2:学生成绩信息一览表"<<endl;
this->displaycp();
cout << "\t\t";
system("pause");
}
效果如下
将学生列表按学号升序排列
static bool cmpnum(const student& s1,const student& s2)
{
return s1.stunum < s2.stunum;
}
将学生列表按平均分降序排列,平均分相同的再按学号升序排列
static bool cmpscore(const student& s1,const student& s2)
{
if(s1.stunum != s2.stunum) return s1.score > s2.score;
else return s1.stunum < s2.stunum;
}
统计学生数据
void statisticlist()
{
while(true)
{
system("cls");
cout << "\t\t***********************欢迎来到统计学生数据功能************************" << endl;
string sel = "0";
cout << "\t\t-----------------" << endl;
cout << "\t\t1 按学号排序" << endl;
cout << "\t\t2 按平均分排序" << endl;
cout << "\t\t3 返回主菜单" << endl;
cout << "\t\t-----------------" << endl;
cout << "\t\t请进行选择【1-3】:";
cin >> sel;
while(sel != "1" && sel != "2" && sel != "3")
{
cout << "\t\t输入不合法,请重新选择【1-3】:";
cin >> sel;
}
if(sel == "1")
{
sort(stulist.begin(), stulist.end(), cmpnum);
cout<<"\t\t按学号升序排列如下:"<<endl;
this->displaystu();
int nummale = 0, numfemale = 0;
for(int i = 0; i < stulist.size(); ++i)
{
if(stulist[i].gender == "男") nummale++;
else if(stulist[i].gender == "女") numfemale++;
}
cout<<"\t\t一共 " << stulist.size() << " 人,其中男生 " << nummale << " 人,女生 " << numfemale << " 人。"<<endl;
cout << "\t\t";
system("pause");
}
else if(sel == "2")
{
sort(stulist.begin(), stulist.end(), cmpscore);
cout<<"\t\t按平均分降序排列如下:"<<endl;
this->displaystu();
vector<int> z(10);
double totalscore = 0;
for(int i = 0; i < stulist.size(); ++i)
{
totalscore += stulist[i].score;
if(0 <= stulist[i].score && stulist[i].score < 60) z[0]++;
else if(60 <= stulist[i].score && stulist[i].score < 70) z[1]++;
else if(70 <= stulist[i].score && stulist[i].score < 80) z[2]++;
else if(80 <= stulist[i].score && stulist[i].score < 90) z[3]++;
else z[4]++;
}
cout<<"\t\tscore < 60 -------------- " << z[0] << " 人" << endl;
cout<<"\t\t60 <= score < 70 --------- " << z[1] << " 人" << endl;
cout<<"\t\t70 <= score < 80 --------- " << z[2] << " 人" << endl;
cout<<"\t\t80 <= score < 90 --------- " << z[3] << " 人" << endl;
cout<<"\t\tscore >= 90 ------------- " << z[4] << " 人" << endl;
cout<<"\t\t所有学生平均分为:"<<totalscore / stulist.size()<<endl;
cout<<"\t\t"<<stulist.back().gradenum<<"级"<<stulist.back().department<<stulist.back().classnum<<"班---";
cout<<stulist.back().stuname<<"平均分最低,为:"<<stulist.back().score<<"分"<<endl;
cout<<"\t\t"<<stulist.front().gradenum<<"级"<<stulist.front().department<<stulist.front().classnum<<"班---";
cout<<stulist.front().stuname<<"平均分最高,为:"<<stulist.front().score<<"分"<<endl;
cout << "\t\t";
system("pause");
}
else
{
break;
}
}
}
效果如下
清空系统数据
void clearlist()
{
while (true)
{
string sel = "0";
system("cls");
cout << "\t\t**************欢迎来到清空系统数据功能***************" << endl;
cout << "\t\t------------------" << endl;
cout << "\t\t1 确认清空系统数据" << endl;
cout << "\t\t2 返回主菜单" << endl;
cout << "\t\t------------------" << endl;
cout << "\t\t请慎重选择【1-2】:";
cin >> sel;
while(sel != "1" && sel != "2")
{
cout << "\t\t输入不合法,请重新输入【1-2】:";
cin >> sel;
}
if (sel == "1")
{
stulist.clear();
cout << "\t\t清空成功!" << endl;
cout << "\t\t";
system("pause");
writefile();
}
else
{
break;
}
}
}
};
效果如下
5.总结
实现亮点:
界面简洁、每次操作完一个功能可进行一次清屏操作,仅保留功能提示文字;
排序功能实现了多种排序方式:单科排序以及总成绩排序
统计功能实现了平均分、及格人数、标准差等数据的统计
输出新文件时可以新建文件名,保存路径,输出文件同时统计数据:包括总分平均分、及格人数、标准差等;
读入文件允许输入文件路径进行读取;
不足之处:
代码实现中有些变量显得多余,部分变量命名与其功能不太匹配;
如需完整代码请给我一个一键三连,并评论:学生管理系统!我会根据大家情况发送完整代码
算了,还是发给大家吧,记得给个三连哈
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <iomanip>
using namespace std;
//学科科目类
class subject
{
public:
string proname; //学科名
double proscore; //分数
void showcpinfo()
{
cout << left << setw(45) << proname;
cout << left << setw(6) << proscore << endl;
}
//输出属性名,只显示总积分
static void showheader()
{
cout << left << setw(45) << "科目";
cout << left << setw(6) << "分数" <<endl;
}
};
//学生类
class student
{
public:
string stunum; //学号
string stuname; //姓名
string gender; //性别
int gradenum; //年级
string department; //专业
int classnum; //班级
vector<subject> cps; //学生的各个科目
int cpnum; //科目门数
double score; //平均分
student()
{
}
student(string stunum, string stuname, string gender, int gradenum, string department, int classnum,vector<subject> cps, int cpnum, double score)
{
this->stunum = stunum;
this->stuname = stuname;
this->gender = gender;
this->gradenum = gradenum;
this->department = department;
this->classnum = classnum;
this->cps = cps;
this->cpnum = cpnum;
this->score = score;
}
~student()
{
}
//输出属性值 score是平均分
void showstuinfo()
{
cout << left << setw(12) << stunum;
cout << left << setw(10) << stuname;
cout << left << setw(6) << gender;
cout << left << setw(8) << gradenum;
cout << left << setw(23) << department;
cout << left << setw(6) << classnum;
cout << left << setw(6) << score << endl;
}
//输出属性名
static void showheader()
{
cout << left << setw(12) << "学号";
cout << left << setw(10) << "姓名";
cout << left << setw(6) << "性别";
cout << left << setw(8) << "年级";
cout << left << setw(23) << "专业";
cout << left << setw(6) << "班级";
cout << left << setw(6) << "平均分" << endl;
}
};
//学生列表类
class studentlist
{
private:
vector<student> stulist;
public:
studentlist()
{
}
studentlist(vector<student> stulist)
{
this->stulist = stulist;
}
~studentlist()
{
}
//初始化
void init()
{
readfile(); //读取文件
}
//主菜单
void menu()
{
string sel = "0";
system("cls");
cout << "\t\t\t**********欢迎来到学生成绩管理系统**********" << endl;
cout << "\t\t\t你可以进行以下操作:" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 1 添加学生信息 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 2 删除学生信息 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 3 修改学生信息 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 4 查询学生信息 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 5 显示信息列表 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 6 统计学生数据 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 7 清空系统数据 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t| 0 退出 |" << endl;
cout << "\t\t\t|------------------------------------------|" << endl;
cout << "\t\t\t请选择【0-7】:";
cin >> sel;
while(sel != "0" && sel != "1" && sel != "2" && sel != "3" && sel != "4" && sel != "5" && sel != "6" && sel != "7")
{
cout << "\t\t\t输入不合法,请重新选择【0-7】:";
cin >> sel;
}
if(sel == "1")
{
this->insertlist();
this->menu();
}
else if(sel == "2")
{
this->deletelist();
this->menu();
}
else if(sel == "3")
{
this->updatelist();
this->menu();
}
else if(sel == "4")
{
this->selectlist();
this->menu();
}
else if(sel == "5")
{
this->displaylist();
this->menu();
}
else if(sel == "6")
{
this->statisticlist();
this->menu();
}
else if(sel == "7")
{
this->clearlist();
this->menu();
}
else if(sel == "0")
{
exit(0);
}
}
//读取文件
void readfile()
{
ifstream ifs;
ifs.open("stulist.txt", ios::in);
int n = 0; //用来接收学生数量的值
ifs >> n;
for(int i = 0; i < n; i++)
{
student s;
ifs >> s.stunum >> s.stuname >> s.gender >> s.gradenum >> s.department >> s.classnum >> s.cpnum >> s.score;
for(int j = 0; j < s.cpnum; j++)
{
subject cp;
ifs >> cp.proname >> cp.proscore;
s.cps.push_back(cp);
}
stulist.push_back(s);
}
ifs.close();
}
//写入文件
void writefile()
{
ofstream ofs;
ofs.open("stulist.txt", ios::out);
ofs << stulist.size() << endl; //先写入学生数量
for (int i = 0; i < stulist.size(); i++)
{
ofs << stulist[i].stunum << " " << stulist[i].stuname << " " << stulist[i].gender
<< " " << stulist[i].gradenum << " " << stulist[i].department << " " << stulist[i].classnum
<< " " << stulist[i].cpnum << " " << stulist[i].score << endl;
for(int j = 0; j < stulist[i].cps.size(); j++)
{
ofs << stulist[i].cps[j].proname << " " << stulist[i].cps[j].proscore << endl;
}
}
ofs.close();
}
//添加学生信息
void insertlist()
{
while(true)
{
system("cls");
cout << "\t\t**********************欢迎来到添加学生信息功能*************************" << endl;
cout<< "\t\t学生基本信息一览表:"<<endl;
displaystu();
cout<<endl;
cout << "\t\t------------------" << endl;
cout << "\t\t1 添加学生信息" << endl;
cout << "\t\t2 返回主菜单" << endl;
cout << "\t\t------------------" << endl;
cout << "\t\t请选择【1-2】:";
string sel;
cin >> sel;
while(sel != "1" && sel != "2")
{
cout << "\t\t输入不合法,请重新输入【1-2】:";
cin >> sel;
}
if(sel == "1")
{
string flag = "1";
while (flag == "1")
{
cout << "\t\t输入学生信息:"<<endl;
cout << "\t\t学号:";
student s;
bool check = false;
do
{
check = false;
cin >> s.stunum;
for(int i = 0; i < stulist.size(); ++i)
{
if(s.stunum == stulist[i].stunum)
{
cout<<"\t\t该学号已存在,请重新输入:";
check = true;
break;
}
}
}
while(check);
cout << "\t\t姓名:";
cin >> s.stuname;
cout << "\t\t性别:";
cin >> s.gender;
cout << "\t\t年级:";
cin >> s.gradenum;
cout<<"\t\t专业:";
cin >> s.department;
cout<<"\t\t班级:";
cin >> s.classnum;
s.score = 0;
string option = "1";
cout<<"\t\t是否添加成绩信息?(1 是 0 否)"<<endl;
cout << "\t\t请进行选择【0-1】:";
cin >> option;
while(option != "0" && option != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> option;
}
if(option == "1")
{
int cnt = 0;
while(option == "1")
{
cnt++;
subject cp;
cout<<"\t\t第"<<cnt<<"科科目名称:";
cin>>cp.proname;
cout<<"\t\t第"<<cnt<<"科科目成绩:";
cin>>cp.proscore;
s.cps.push_back(cp);
cout << "\t\t该科成绩信息添加成功!是否继续添加?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
cin >> option;
while(option != "0" && option != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> option;
}
}
}
for(int i = 0; i < s.cps.size(); i++)
{
s.score += s.cps[i].proscore;
}
if(s.cps.size() > 0) s.score /= s.cps.size(); //计算平均分
s.cpnum = s.cps.size();
stulist.push_back(s);
writefile();
cout << "\n\t\t该名学生信息添加成功!是否继续添加?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
cin >> flag;
while(flag != "0" && flag != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> flag;
}
}
cout << "\t\t";
system("pause");
}
else
{
break;
}
}
}
//删除学生信息
void deletelist()
{
while (true)
{
system("cls");
cout << "\t\t***********************欢迎来到删除学生信息功能***********************" << endl;
string sel = "0";
cout << "\t\t-----------------" << endl;
cout << "\t\t1 按学号删除" << endl;
cout << "\t\t2 按姓名删除" << endl;
cout << "\t\t3 返回主菜单" << endl;
cout << "\t\t-----------------" << endl;
cout << "\t\t请进行选择【1-3】:";
cin >> sel;
while(sel != "1" && sel != "2" && sel != "3")
{
cout << "\t\t输入不合法,请重新选择【1-3】:";
cin >> sel;
}
if (sel == "1")
{
string keynum;
bool flag = false;
cout << "\t\t请输入待删除学生的学号:";
cin >> keynum;
for (vector<student>::iterator it = stulist.begin(); it != stulist.end(); ++it)
{
if (it->stunum == keynum)
{
flag = true;
cout << "\t\t待删除学生的信息如下:" << endl;
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
student::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
it->showstuinfo();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout << "\t\t确认删除?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while(ch != "0" && ch != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> ch;
}
if (ch == "0") break;
else
{
stulist.erase(it);
writefile();
cout << "\t\t删除成功!" << endl;
break;
}
}
}
if (!flag) cout << "\t\t查无此人,无法删除!\n" << endl;
cout << "\t\t";
system("pause");
}
else if (sel == "2")
{
string keyname;
bool flag = false;
cout << "\t\t请输入待删除学生的姓名:";
cin >> keyname;
for (vector<student>::iterator it = stulist.begin(); it != stulist.end(); ++it)
{
if (it->stuname == keyname)
{
flag = true;
cout << "\t\t待删除学生的信息如下:" << endl;
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t\t";
student::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
it->showstuinfo();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout << "\t\t确认删除?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while(ch != "0" && ch != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> ch;
}
if (ch == "0") break;
else
{
stulist.erase(it);
writefile();
cout << "\t\t删除成功!" << endl;
break;
}
}
}
if (!flag) cout << "\t\t查无此人,无法删除!\n" << endl;
cout << "\t\t";
system("pause");
}
else
{
break;
}
}
}
//修改学生信息
void updatelist()
{
while(true)
{
system("cls");
cout << "\t\t***********************欢迎来到修改学生信息功能***********************" << endl;
string sel = "0";
cout << "\t\t-----------------" << endl;
cout << "\t\t1 修改学生基本信息" << endl;
cout << "\t\t2 修改学生成绩信息" << endl;
cout << "\t\t3 返回主菜单" << endl;
cout << "\t\t-----------------" << endl;
cout << "\t\t请进行选择【1-3】:";
cin >> sel;
while(sel != "1" && sel != "2" && sel != "3")
{
cout << "\t\t输入不合法,请重新选择【1-3】:";
cin >> sel;
}
if(sel == "1")
{
bool flag = false;
string keynum;
cout << "\t\t请输入待修改学生的学号:";
cin >> keynum;
for (int i = 0; i < stulist.size(); i++)
{
if (stulist[i].stunum == keynum)
{
flag = true;
cout << "\t\t待修改学生基本信息如下:" << endl;
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
student::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
stulist[i].showstuinfo();
cout << "\t\t-----------------------------------------------------------------------" << endl;
student s = stulist[i];
cout << "\t\t请输入修改后的学号:";
bool check = false;
do
{
check = false;
cin >> s.stunum;
for(int j = 0; j < stulist.size(); ++j)
{
if(s.stunum == stulist[j].stunum && i != j)
{
cout<<"\t\t该学号已被录入,请重新输入学号:";
check = true;
break;
}
}
}
while(check);
cout << "\t\t请输入修改后的姓名:";
cin >> s.stuname;
cout << "\t\t请输入修改后的性别:";
cin >> s.gender;
cout << "\t\t请输入修改后的年级:";
cin >> s.gradenum;
cout<<"\t\t请输入修改后的专业:";
cin >> s.department;
cout<<"\t\t请输入修改后的班级:";
cin >> s.classnum;
cout << "\t\t是否确认修改?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while(ch != "0" && ch != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> ch;
}
if (ch == "0") break;
else
{
stulist[i] = s;
cout << "\t\t修改成功!" << endl;
writefile();
break;
}
}
}
if (!flag) cout << "\t\t查无此人,无法修改!\n" << endl;
}
else if(sel == "2")
{
bool flag = false;
string keynum;
cout << "\t\t请输入待修改学生的学号:";
cin >> keynum;
for (int i = 0; i < stulist.size(); i++)
{
if (stulist[i].stunum == keynum)
{
flag = true;
cout << "\t\t待修改学生成绩信息如下:" << endl;
cout << "\t\t----------------------------------------------------------" << endl;
cout<<"\t\t";
cout << left << setw(8) << "编号";
subject::showheader();
cout << "\t\t----------------------------------------------------------" << endl;
for(int j = 0; j < stulist[i].cps.size(); j++)
{
cout<<"\t\t";
cout << left << setw(8) << j + 1;
stulist[i].cps[j].showcpinfo();
}
cout << "\t\t----------------------------------------------------------" << endl;
student s = stulist[i];
cout << "\t\t请选择修改方式:"<<endl;
string option = "1";
cout << "\t\t-------------------------------" << endl;
cout << "\t\t1 基于该学生原有成绩信息进行修改" << endl;
cout << "\t\t2 清空该学生所有科目及成绩信息" << endl;
cout << "\t\t-------------------------------" << endl;
cout << "\t\t请进行选择【1-2】:";
cin >> option;
while(option != "1" && option != "2")
{
cout << "\t\t输入不合法,请重新选择【1-2】:";
cin >> option;
}
subject cp;
if(option == "1")
{
s.cps.clear();
s.score = 0;
s.cpnum = 0;
cout<<"\t\t输入修改后的学科成绩信息:"<<endl;
int cnt = 0;
while(option == "1")
{
cnt++;
cout<<"\t\t第"<<cnt<<"科科目名称:";
cin>>cp.proname;
cout<<"\t\t第"<<cnt<<"科科目成绩:";
cin>>cp.proscore;
s.cps.push_back(cp);
cout << "\n\t\t添加成功!是否继续添加?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
cin >> option;
while(option != "0" && option != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> option;
}
}
}
else if(option == "2")
{
s.cps.clear();
s.score = 0;
s.cpnum = 0;
}
//计算平均分
for(int i = 0; i < s.cps.size(); i++)
{
s.score += s.cps[i].proscore;
}
if(s.cps.size() > 0) s.cpnum = s.cps.size();
cout << "\t\t是否确认修改?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while(ch != "0" && ch != "1")
{
cout << "\t\t输入不合法,请重新选择【0-1】:";
cin >> ch;
}
if (ch == "0") break;
else
{
stulist[i] = s;
cout << "\t\t修改成功!" << endl;
writefile();
break;
}
}
}
if (!flag) cout << "\t\t查无此人,无法修改!\n" << endl;
}
else
{
break;
}
cout << "\t\t";
system("pause");
}
}
//查询学生信息
void selectlist()
{
while (true)
{
system("cls");
cout << "\t\t***********************欢迎来到查询学生信息功能************************" << endl;
cout << "\t\t-----------------" << endl;
cout << "\t\t1 按学号查询" << endl;
cout << "\t\t2 按姓名查询" << endl;
cout << "\t\t3 返回主菜单" << endl;
cout << "\t\t-----------------" << endl;
cout << "\t\t请进行选择【1-3】:";
string sel = "0";
cin >> sel;
while(sel != "1" && sel != "2" && sel != "3")
{
cout << "\t\t输入不合法,请重新选择【1-3】:";
cin >> sel;
}
if (sel == "1")
{
string keynum;
bool flag = false;
cout << "\t\t请输入待查询学生的学号:";
cin >> keynum;
cout << "\t\t查询结果如下:" << endl;
cout<<"\t\t基本信息:"<<endl;
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
student::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
for (int i = 0; i < stulist.size(); i++)
{
if (stulist[i].stunum == keynum)
{
flag = true;
cout<<"\t\t";
stulist[i].showstuinfo();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<endl;
cout<<"\t\t"<<stulist[i].stuname<<"成绩信息:"<<endl;
cout << "\t\t----------------------------------------------------------" << endl;
cout<<"\t\t";
cout << left << setw(8) << "编号";
subject::showheader();
cout << "\t\t----------------------------------------------------------" << endl;
for(int j = 0; j < stulist[i].cps.size(); j++)
{
cout<<"\t\t";
cout << left << setw(8) << j + 1;
stulist[i].cps[j].showcpinfo();
}
cout << "\t\t----------------------------------------------------------" << endl;
break;
}
}
if (!flag) cout << "\t\t查无此人!\n" << endl;
cout << "\t\t";
system("pause");
}
else if (sel == "2")
{
string keyname;
bool flag = false;
cout << "\t\t请输入待查询联系人的姓名:";
cin >> keyname;
cout << "\t\t查询结果如下:" << endl;
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
student::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
for (int i = 0; i < stulist.size(); i++)
{
if (stulist[i].stuname == keyname)
{
flag = true;
cout<<"\t\t";
stulist[i].showstuinfo();
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<endl;
cout<<"\t\t"<<stulist[i].stuname<<"成绩信息:"<<endl;
cout << "\t\t----------------------------------------------------------" << endl;
cout<<"\t\t";
cout << left << setw(8) << "编号";
subject::showheader();
cout << "\t\t----------------------------------------------------------" << endl;
for(int j = 0; j < stulist[i].cps.size(); j++)
{
cout<<"\t\t";
cout << left << setw(8) << j + 1;
stulist[i].cps[j].showcpinfo();
}
cout << "\t\t----------------------------------------------------------" << endl;
break;
}
}
if (!flag) cout << "\t\t查无此人!\n" << endl;
cout << "\t\t";
system("pause");
}
else
{
break;
}
}
}
//遍历学生列表
void displaystu()
{
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
student::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
for (int i = 0; i < stulist.size(); i++)
{
cout << "\t\t";
stulist[i].showstuinfo();
}
cout << "\t\t-----------------------------------------------------------------------" << endl;
}
//查询获奖信息
void displaycp()
{
cout << "\t\t-----------------------------------------------------------------------" << endl;
cout<<"\t\t";
cout << left << setw(12) << "学号";
cout << left << setw(10) << "姓名";
subject::showheader();
cout << "\t\t-----------------------------------------------------------------------" << endl;
for (int i = 0; i < stulist.size(); i++)
{
for(int j = 0; j < stulist[i].cps.size(); j++)
{
cout << "\t\t";
cout << left << setw(12) << stulist[i].stunum;
cout << left << setw(10) << stulist[i].stuname;
stulist[i].cps[j].showcpinfo();
}
}
cout << "\t\t-----------------------------------------------------------------------" << endl;
}
//显示信息列表
void displaylist()
{
system("cls");
cout << "\t\t***********************欢迎来到显示信息列表功能************************" << endl;
cout << "\t\t表1:学生基本信息一览表"<<endl;
this->displaystu();
cout << "\n\t\t表2:学生成绩信息一览表"<<endl;
this->displaycp();
cout << "\t\t";
system("pause");
}
//将学生列表按学号升序排列
static bool cmpnum(const student& s1,const student& s2)
{
return s1.stunum < s2.stunum;
}
//将学生列表按平均分降序排列,平均分相同的再按学号升序排列
static bool cmpscore(const student& s1,const student& s2)
{
if(s1.stunum != s2.stunum) return s1.score > s2.score;
else return s1.stunum < s2.stunum;
}
//统计学生数据
void statisticlist()
{
while(true)
{
system("cls");
cout << "\t\t***********************欢迎来到统计学生数据功能************************" << endl;
string sel = "0";
cout << "\t\t-----------------" << endl;
cout << "\t\t1 按学号排序" << endl;
cout << "\t\t2 按平均分排序" << endl;
cout << "\t\t3 返回主菜单" << endl;
cout << "\t\t-----------------" << endl;
cout << "\t\t请进行选择【1-3】:";
cin >> sel;
while(sel != "1" && sel != "2" && sel != "3")
{
cout << "\t\t输入不合法,请重新选择【1-3】:";
cin >> sel;
}
if(sel == "1")
{
sort(stulist.begin(), stulist.end(), cmpnum);
cout<<"\t\t按学号升序排列如下:"<<endl;
this->displaystu();
int nummale = 0, numfemale = 0;
for(int i = 0; i < stulist.size(); ++i)
{
if(stulist[i].gender == "男") nummale++;
else if(stulist[i].gender == "女") numfemale++;
}
cout<<"\t\t一共 " << stulist.size() << " 人,其中男生 " << nummale << " 人,女生 " << numfemale << " 人。"<<endl;
cout << "\t\t";
system("pause");
}
else if(sel == "2")
{
sort(stulist.begin(), stulist.end(), cmpscore);
cout<<"\t\t按平均分降序排列如下:"<<endl;
this->displaystu();
vector<int> z(10);
double totalscore = 0;
for(int i = 0; i < stulist.size(); ++i)
{
totalscore += stulist[i].score;
if(0 <= stulist[i].score && stulist[i].score < 60) z[0]++;
else if(60 <= stulist[i].score && stulist[i].score < 70) z[1]++;
else if(70 <= stulist[i].score && stulist[i].score < 80) z[2]++;
else if(80 <= stulist[i].score && stulist[i].score < 90) z[3]++;
else z[4]++;
}
cout<<"\t\tscore < 60 -------------- " << z[0] << " 人" << endl;
cout<<"\t\t60 <= score < 70 --------- " << z[1] << " 人" << endl;
cout<<"\t\t70 <= score < 80 --------- " << z[2] << " 人" << endl;
cout<<"\t\t80 <= score < 90 --------- " << z[3] << " 人" << endl;
cout<<"\t\tscore >= 90 ------------- " << z[4] << " 人" << endl;
cout<<"\t\t所有学生平均分为:"<<totalscore / stulist.size()<<endl;
cout<<"\t\t"<<stulist.back().gradenum<<"级"<<stulist.back().department<<stulist.back().classnum<<"班---";
cout<<stulist.back().stuname<<"平均分最低,为:"<<stulist.back().score<<"分"<<endl;
cout<<"\t\t"<<stulist.front().gradenum<<"级"<<stulist.front().department<<stulist.front().classnum<<"班---";
cout<<stulist.front().stuname<<"平均分最高,为:"<<stulist.front().score<<"分"<<endl;
cout << "\t\t";
system("pause");
}
else
{
break;
}
}
}
//清空系统数据
void clearlist()
{
while (true)
{
string sel = "0";
system("cls");
cout << "\t\t**************欢迎来到清空系统数据功能***************" << endl;
cout << "\t\t------------------" << endl;
cout << "\t\t1 确认清空系统数据" << endl;
cout << "\t\t2 返回主菜单" << endl;
cout << "\t\t------------------" << endl;
cout << "\t\t请慎重选择【1-2】:";
cin >> sel;
while(sel != "1" && sel != "2")
{
cout << "\t\t输入不合法,请重新输入【1-2】:";
cin >> sel;
}
if (sel == "1")
{
stulist.clear();
cout << "\t\t清空成功!" << endl;
cout << "\t\t";
system("pause");
writefile();
}
else
{
break;
}
}
}
};
int main()
{
studentlist stulist;
stulist.init(); //读入文件数据初始化
stulist.menu(); //打开主菜单
return 0;
}
发表评论