参考文档:
C++实现:学生管理系统(详细解析)
关于switch中的default里面的break是否可以省略这件事
需求要求:
(1)增加记录
(2)查找记录
(3)删除记录
(4)成绩修改
(5)统计分析:单科统计、课程标准差和合格率;
(6)排序功能:总分排序(从高到低),如果相同,则按学号排
序;
(7)文件操作:可以打开文件,显示班级的所有学生信息;可以将增加或修改后的成绩重新写入文件;可以将排序好的信息写入新的文件;
需求思路:
抽象处理:
- 学科类:
class Subject
- 学生类:
class Student
- 学生列表类:
class StudentList
完整代码:
StudentAdmin.h
#ifndef STUDENTADMIN_H_
#define STUDENTADMIN_H_
#include <vector>
#include <string>
using namespace std;
// 学科类
class Subject
{
public:
string proName; //学科名
double proScore; //分数
void showCpInfo(); //显示分数
static void showHeader(); // 显示表头
};
//学生类
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;
};
void showStuInfo(); //输出属性值
static void showHeader(); //输出表头
};
//学生列表类
class StudentList
{
public:
StudentList(){}
StudentList(vector<Student> stuList)
{
this->stuList = stuList;
}
void init(); //初始化
void menu(); //主菜单
void readFile(); //读取数据
void writeFile(); //写入数据
void insertList(); //添加学生信息
void deleteList(); //删除学生信息
void updateList(); //修改学生信息
void selectList(); //查询学生信息
void displayStu(); //遍历学生信息
void displayCp(); //查询获奖信息
void diaplayList(); //显示信息列表
void statisticList();//统计学生数据
void clearList(); //清空系统数据
// 二级函数
void doInsert();
void doDeleteByNum(); //按学号删除
void doDeleteByName(); //按姓名删除
void doUpdateInfo(); //更新学生信息
void doUpdateScore(); //更新学生成绩
void doSelectByNum(); //按学号查询
void doSelectByName(); //按姓名查询
void doSortByNum(); //按学号排序
void doSortByScore(); //按平均分排序
void doClear(); //清理数据
// 三级函数
void doUpDataScoreByOne(Student &s); //基于原有成绩信息修改
void doUpDataScoreByAll(Student &s); //清空所有科目及成绩信息
//工具类函数
static bool cmpNum(const Student& s1, const Student& s2); //将学生列表按学号升序排列
static bool cmpScroe(const Student& s1, const Student& s2); //将学生列表按平均分降序排列,平均分相同的再按学号升序排列
private:
vector<Student> stuList;
}
;
#endif
StudentAdmin.cpp
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#include "StudentAdmin.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm> // 算法库,提供大量迭代器成员函数, sort()
#include <iomanip> // 格式输入输出
#include <conio.h> // 使用getch()函数
#include <windows.h> //使用Sleep()函数
using namespace std;
void Subject::showCpInfo()
{
cout << left << setw(45) << proName; // 设置字段宽度为45位
cout << left << setw(6) << proScore << endl;
}
void Subject::showHeader()
{
cout << left << setw(45) << "科目";
cout << left << setw(6) << "分数" << endl;
}
void Student::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;
}
void Student::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;
}
void StudentList::init()
{
readFile();
}
void StudentList::menu()
{
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;
bool sign = true;
while (sign)
{
switch (getch())
{
case '1':
insertList();
sign = false;
break;
case '2':
deleteList();
sign = false;
break;
case '3':
updateList();
sign = false;
break;
case '4':
selectList();
sign = false;
break;
case '5':
diaplayList();
sign = false;
break;
case '6':
statisticList();
sign = false;
break;
case '7':
clearList();
sign = false;
break;
case '0':
exit(0);
break;
default:
cout << "\n\t\t\t输入不合法,请重新选择【0-7】:";
}
}
}
void StudentList::readFile()
{
ifstream ifs("stuList.txt", ios::in);
if (ifs)
{
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 StudentList::writeFile()
{
ofstream ofs("stuList.txt", ios::out);
if (!ofs)
{
cout << "\t\t存档失败!程序即将自动退出 . . ." << endl;
Sleep(3000);//睡眠3000毫秒,即停留3秒钟
exit(0);
}
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 StudentList::insertList()
{
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】:";
bool sign = true;
while (sign)
{
switch (getch())
{
case '1':
doInsert();
sign = false;
break;
case '2':
menu();
sign = false;
break;
default:
cout << "\n\t\t输入不合法,请重新选择【1-2】:";
}
}
}
void StudentList::deleteList()
{
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】:";
bool sign = true;
while (sign)
{
switch (getch())
{
case '1':
doDeleteByNum();
sign = false;
break;
case '2':
doDeleteByName();
sign = false;
break;
case '3':
menu();
sign = false;
break;
default:
cout << "\n\t\t输入不合法,请重新选择【1-3】:";
}
}
}
void StudentList::updateList()
{
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】:";
bool sign = true;
while (sign)
{
switch (getch())
{
case '1':
doUpdateInfo();
sign = false;
break;
case '2':
doUpdateScore();
sign = false;
break;
case '3':
menu();
sign = false;
break;
default:
cout << "\n\t\t输入不合法,请重新选择【1-3】:";
}
}
}
void StudentList::selectList()
{
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】:";
bool sign = true;
while (sign)
{
switch (getch())
{
case '1':
doSelectByNum();
sign = false;
break;
case '2':
doSelectByName();
sign = false;
break;
case '3':
menu();
sign = false;
break;
default:
cout << "\n\t\t输入不合法,请重新选择【1-3】:";
}
}
}
void StudentList::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 StudentList::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 StudentList::diaplayList()
{
system("cls");
cout << "\t\t***********************欢迎来到显示信息列表功能************************" << endl;
cout << "\t\t表1:学生基本信息一览表" << endl;
this->displayStu();
cout << "\n\t\t表2:学生成绩信息一览表" << endl;
this->displayCp();
cout << "\n\t\t按任意键返回菜单" << endl;
while (getch())
{
menu();
}
}
void StudentList::statisticList()
{
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】:";
bool sign = true;
while (sign)
{
switch (getch())
{
case '1':
doSortByNum();
sign = false;
break;
case '2':
doSortByScore();
sign = false;
break;
case '3':
menu();
sign = false;
break;
default:
cout << "\n\t\t输入不合法,请重新选择【1-3】:";
}
}
}
void StudentList::clearList()
{
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】:";
bool sign = true;
while (sign)
{
switch (getch())
{
case '1':
doClear();
sign = false;
break;
case '2':
menu();
sign = false;
break;
default:
cout << "\n\t\t输入不合法,请重新选择【1-3】:";
}
}
}
void StudentList::doInsert()
{
string flag = "1";
while (flag == "1")
{
cout << "\n\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 << "\n\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 << "\n\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 << "\n\t\t输入不合法,请重新选择【0-1】:";
cin >> flag;
}
}
insertList(); //返回上一级
}
void StudentList::doDeleteByNum()
{
string keyNum;
bool flag = false; //成功找到该学生
cout << "\n\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 << "\n\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 << "\n\t\t按任意键返回上一级" << endl;
while (getch())
{
deleteList(); // 返回上一级
}
}
void StudentList::doDeleteByName()
{
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 << "\n\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 << "\n\t\t按任意键返回上一级" << endl;
while (getch())
{
deleteList(); // 返回上一级
}
}
void StudentList::doUpdateInfo()
{
system("cls");
string keyNum;
bool flag = false; //成功找到该学生
cout << "\n\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 << "\n\t\t是否确认修改?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while (ch != "0" && ch != "1")
{
cout << "\n\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;
cout << "\n\t\t按任意键返回上一级" << endl;
while (getch())
{
updateList(); // 返回上一级
}
}
void StudentList::doUpdateScore()
{
system("cls");
string keyNum;
bool flag = false; //成功找到该学生
cout << "\n\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 << "\n\t\t请选择修改方式:" << endl;
cout << "\t\t-------------------------------" << endl;
cout << "\t\t1 基于该学生原有成绩信息进行修改" << endl;
cout << "\t\t2 清空该学生所有科目及成绩信息" << endl;
cout << "\t\t-------------------------------" << endl;
cout << "\t\t请进行选择【1-2】:";
bool sign = true;
while (sign)
{
switch (getch())
{
case '1':
doUpDataScoreByOne(s);
sign = false;
break;
case '2':
doUpDataScoreByAll(s);
sign = false;
break;
default:
cout << "\n\t\t\t输入不合法,请重新选择【1-2】:";
}
}
// 计算平均分
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();
cout << "\n\t\t是否确认修改?(1 是 0 否)" << endl;
cout << "\t\t请进行选择【0-1】:";
string ch = "0";
cin >> ch;
while (ch != "0" && ch != "1")
{
cout << "\n\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;
cout << "\n\t\t按任意键返回上一级" << endl;
while (getch())
{
updateList(); // 返回上一级
}
}
void StudentList::doUpDataScoreByOne(Student &s)
{
Subject cp;
s.cps.clear();
s.score = 0;
s.cpNum = 0;
cout << "\n\t\t输入修改后的学科成绩信息:" << endl;
int cnt = 0;
bool sign = true;
while (sign)
{
cnt++;
cout << "\n\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】:";
string ch = "0";
cin >> ch;
while (ch != "0" && ch != "1")
{
cout << "\n\t\t输入不合法,请重新选择【0-1】:";
cin >> ch;
}
if (ch == "0")
{
sign = false;
}
}
}
void StudentList::doUpDataScoreByAll(Student &s)
{
s.cps.clear();
s.score = 0;
s.cpNum = 0;
}
void StudentList::doSelectByNum()
{
system("cls");
string keyNum;
bool flag = false;
cout << "\n\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 << "\n\t\t按任意键返回上一级" << endl;
while (getch())
{
selectList(); // 返回上一级
}
}
void StudentList::doSelectByName()
{
system("cls");
string keyName;
bool flag = false;
cout << "\n\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 << "\n\t\t按任意键返回上一级" << endl;
while (getch())
{
selectList(); // 返回上一级
}
}
void StudentList::doSortByNum()
{
system("cls");
sort(stuList.begin(), stuList.end(), cmpNum);
cout << "\n\t\t按学号升序排列如下:" << endl;
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 << "\n\t\t按任意键返回上一级" << endl;
while (getch())
{
statisticList(); // 返回上一级
}
}
void StudentList::doSortByScore()
{
system("cls");
sort(stuList.begin(), stuList.end(), cmpScroe);
cout << "\n\t\t按平均分降序排列如下:" << endl;
displayStu();
vector<int> scoreRange(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) scoreRange[0]++;
else if (60 <= stuList[i].score && stuList[i].score < 70) scoreRange[1]++;
else if (70 <= stuList[i].score && stuList[i].score < 80) scoreRange[2]++;
else if (80 <= stuList[i].score && stuList[i].score < 90) scoreRange[3]++;
else scoreRange[4]++;
}
cout << "\n\t\t60分以上 ------------ " << scoreRange[0] << " 人" << endl;
cout << "\t\t60分 ~ 70分 --------- " << scoreRange[1] << " 人" << endl;
cout << "\t\t70分 ~ 80分 --------- " << scoreRange[2] << " 人" << endl;
cout << "\t\t80分 ~ 90分 --------- " << scoreRange[3] << " 人" << endl;
cout << "\t\t90分以上 ------------ " << scoreRange[4] << " 人" << endl;
cout << "\n\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 << "\n\t\t按任意键返回上一级" << endl;
while (getch())
{
statisticList(); // 返回上一级
}
}
void StudentList::doClear()
{
stuList.clear();
cout << "\n\t\t清空成功!" << endl;
writeFile();
cout << "\n\t\t按任意键返回菜单" << endl;
while (getch())
{
menu();
}
}
bool StudentList::cmpNum(const Student& s1, const Student& s2)
{
return s1.stuNum < s2.stuNum;
}
bool StudentList::cmpScroe(const Student& s1, const Student& s2)
{
if (s1.stuNum != s2.stuNum) return s1.score > s2.score;
else return s1.stuNum < s2.stuNum;
}
main.cpp
/*********************************************************************
程序名:
说明:
*********************************************************************/
#include <iostream>
#include <conio.h> // 使用getch()函数
#include "StudentAdmin.h"
using namespace std;
int main() {
StudentList stuList;
stuList.init(); //读入文件数据初始化
stuList.menu(); //打开主菜单
system("PAUSE");
return 0;
}
运行示例:
(1)主界面
(2)增加记录
(3)查找记录
(4)删除记录
(5)成绩修改
(5)统计分析:单科统计、课程标准差和合格率;