学生成绩管理系统
大一下册课程设计,瞎勾八画的流程图
学习参考文章:https://blog.csdn.net/LYK13469998283/article/details/114712238使用容器存储一步到位
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<Windows.h>
using namespace std;
class Student //学生类
{
public:
Student( string name, int id, string sex)
{
this->name = name;
this->id = id;
this->sex = sex;
}
string name;
int id;
string sex;
};
class Grade //课程类
{
public:
string English;
string Math;
string C_plus;
string Analog_circuit;
string Sports;
string Matlab;
string Data_structure;
string Physical;
};
class Score : public Grade,virtual public Student //成绩类
{
public:
Score(string name, int id, string sex, float English_score, float Math_score, float C_plus_score, float Analog_circuit_score, float Sports_score, float Matlab_score, float Data_structure_score, float Physical_score) :Student(name, id, sex)
{
this->English_score = English_score;
this->Math_score = Math_score;
this->C_plus_score = C_plus_score;
this->Analog_circuit_score = Analog_circuit_score;
this->Sports_score = Sports_score;
this->Matlab_score = Matlab_score;
this->Data_structure_score = Data_structure_score;
this->Physical_score = Physical_score;
GPA = (English_score + Math_score + C_plus_score + Analog_circuit_score + Sports_score + Matlab_score + Data_structure_score + Physical_score) / 8.0;
}
float English_score;
float Math_score;
float C_plus_score;
float Analog_circuit_score;
float Sports_score;
float Matlab_score;
float Data_structure_score;
float Physical_score;
float GPA;
};
class stusystem
{
public:
stusystem() //打开文件
{
ifstream ifs("学生成绩.txt", ios::in);
if (ifs.is_open() != NULL)
{
string name, sex;
int id;
float English_score;
float Math_score;
float C_plus_score;
float Analog_circuit_score;
float Sports_score;
float Matlab_score;
float Data_structure_score;
float Physical_score;
float GPA; //说实话你设的这变量名真的长
while (ifs >> name >> id >> sex >> English_score >> Math_score >> C_plus_score >> Analog_circuit_score >> Sports_score >> Matlab_score >> Data_structure_score >> Physical_score)
{
Score s(name , id , sex , English_score , Math_score , C_plus_score , Analog_circuit_score , Sports_score , Matlab_score , Data_structure_score , Physical_score);
a.push_back(s);
}
}
else
{
cout << "不存在该文件" << endl;
}
}
vector<Score>a;
void Save(); //保存
void Add(); //添加
void AddAgain(); //再度添加
void Del(); //删除
void Find(); //查找菜单
void Findname(); //查找名字
void Findid(); //查找学号
void Sort(); //排序
void Remake(); //重新编辑
void Print(); //打印
void mainmenu() //主菜单
{
system("cls");
cout << "____________________________________________" << endl;
cout << "| |" << endl;
cout << "| 添加学生信息 1 |" << endl;
cout << "| 查找学生信息 2 |" << endl;
cout << "| GPA排名 3 |" << endl;
cout << "| 修改学生信息 4 |" << endl;
cout << "| 删除学生信息 5 |" << endl;
cout << "| 打印学生信息 6 |" << endl;
cout << "| 退出系统 0 |" << endl;
cout << "| |" << endl;
cout << "--------------------------------------------" << endl;
cout << "请输入你的选择:";
int x;
cin >> x;
switch (x)
{
case 1:Add(); break;
case 2:Find(); break;
case 3:Sort(); break;
case 4:Remake(); break;
case 5:Del(); break;
case 6:Print(); break;
case 0:Save(); break;
default:cout << "输入错误,请重新输入\n"; Sleep(1000); mainmenu(); break;
}
}
};
void stusystem::Save() //保存文件
{
ofstream ofs("学生成绩.txt", ios::out);
for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
{
ofs << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t" << (*i).English_score << "\t" <<
(*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
<< "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t" << (*i).GPA;
}
ofs.close();
}
void stusystem::Add() //添加
{
system("cls");
int flag;
cout << "请输入学生信息:" << endl;
string name, sex;
int id;
float eng, math, cplus, mndl, ty, matlab, sjjg, dw;
while (1)
{
cout << "姓名:";
cin >> name;
cout << "学号:";
cin >> id;
for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
{
if ((*i).id == id)
{
cout << "此同学信息已经存在,请重新输入";
Sleep(700);
Add();
}
}
cout << "性别:";
cin >> sex;
do {
flag = 0;
cout << "英语学分:";
cin >> eng;
if (eng > 5.0 || eng < 0) {
cout << " 对不起,请输入0-5.0之间的数字!!\n";
}
else {
flag = 1;
}
} while (flag == 0);
do {
flag = 0;
cout << "高数学分:";
cin >> math;
if (math > 5.00 || math < 0) {
cout << " 对不起,请输入0-5.00之间的数字!!\n";
}
else {
flag = 1;
}
} while (flag == 0);
do {
flag = 0;
cout << "C++学分:";
cin >> cplus;
if (cplus > 5.00 || cplus < 0) {
cout << " 对不起,请输入0-5.00之间的数字!!\n";
}
else {
flag = 1;
}
} while (flag == 0);
do {
flag = 0;
cout << "模拟电路学分:";
cin >> mndl;
if (mndl > 5.00 || mndl < 0) {
cout << " 对不起,请输入0-5.00之间的数字!!\n";
}
else {
flag = 1;
}
} while (flag == 0);
do {
flag = 0;
cout << "体育学分:";
cin >> ty;
if (ty > 5.00 || ty < 0) {
cout << " 对不起,请输入0-5.00之间的数字!!\n";
}
else {
flag = 1;
}
} while (flag == 0);
do {
flag = 0;
cout << "Matlab学分:";
cin >> matlab;
if (matlab > 5.00 || matlab < 0) {
cout << " 对不起,请输入0-5.00之间的数字!!\n";
}
else {
flag = 1;
}
} while (flag == 0);
do {
flag = 0;
cout << "数据结构学分:";
cin >> sjjg;
if (sjjg > 5.00 || sjjg < 0) {
cout << " 对不起,请输入0-5.00之间的数字!!\n";
}
else {
flag = 1;
}
} while (flag == 0);
do {
flag = 0;
cout << "大学物理学分:";
cin >> dw;
if (dw > 5.00 || dw < 0) {
cout << " 对不起,请输入0-5.00之间的数字!!\n";
}
else {
flag = 1;
}
} while (flag == 0);
Score s(name, id, sex, eng, math, cplus, mndl, ty, matlab, sjjg, dw);
a.push_back(s); //vector头文件中的push_back操作符,意思是插入到表尾
Save();
cout << "添加成功" << endl;
AddAgain();
}
}
void stusystem::AddAgain()
{
cout << "是否继续添加学生成绩信息?(y/n)" << endl;
char sign = '0';
cin >> sign;
switch (sign) {
case'y':
case'Y':
Add(); break;
case'n':
case'N':
mainmenu(); break;
default:cout << "输入错误,请重新输入\n"; Sleep(1000); AddAgain(); break;
}
}
void stusystem::Find()
{
system("cls");
cout << "查找学生成绩:" << endl;
cout << "请选择查询方式:" << endl;
cout << "1-姓名查询" << endl << "2-学号查询" << endl;
int x = 0;
cin >> x;
switch (x)
{
case 1:
Findname(); break;
case 2:
Findid(); break;
}
}
void stusystem::Findname()
{
string s;
bool flag = false; //异常解决方案
cout << "请输入需查找的学生姓名:";
cin >> s;
for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
{
if ((*i).name == s)
{
flag = true;
cout << "学号" << "\t" << "姓名" << "\t" << "性别" << " " << "各科学分" << " " << "英语" << " " << "高等数学"
<< " " << "C++" << " " << "模拟电路" << " " << "体育" << " " << "Matlab"
<< " " << "数据结构" << " " << "大学物理" << " " << "GPA" << endl;
cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t \t" << (*i).English_score << "\t" <<
(*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
<< "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t " << (*i).GPA << endl;
cout << "\n按任意键返回主菜单"; char ch;
cin >> ch; mainmenu();
}
if (flag == false)
{
cout << "查无此人!" << endl;
Findname();
}
}
}
void stusystem::Findid()
{
int y=0;
bool flag = false; //异常解决方案
cout << "请输入需查找的学生学号:";
cin >> y;
for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
{
if ((*i).id == y)
{
flag = true;
cout << "学号" << "\t" << "姓名" << "\t" << "性别" << " " << "各科学分" << " " << "英语" << " " << "高等数学"
<< " " << "C++" << " " << "模拟电路" << " " << "体育" << " " << "Matlab"
<< " " << "数据结构" << " " << "大学物理" << " " << "GPA" << endl;
cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t \t" << (*i).English_score << "\t" <<
(*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
<< "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t " << (*i).GPA << endl;
cout << "\n按任意键返回主菜单"; char ch;
cin >> ch; mainmenu();
}
if (flag == false)
{
cout << "查无此人!" << endl;
Findid();
}
}
}
void stusystem::Del() //删除
{
cout << "请输入学生的学号" << endl;
int id;
cin >> id;
for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
{
if ((*i).id == id)
{
int select;
cout << "学号" << "\t" << "姓名" << "\t" << "性别" << " " << "各科学分" << " " << "英语" << " " << "高等数学"
<< " " << "C++" << " " << "模拟电路" << " " << "体育" << " " << "Matlab"
<< " " << "数据结构" << " " << "大学物理" << " " << "GPA" << endl;
cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t \t" << (*i).English_score << "\t" <<
(*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
<< "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t " << (*i).GPA << endl;
cout << "是否删除?" << endl;
cout << "1.是" << " " << "2.否" << endl;
cin >> select;
switch (select)
{
case 1:
a.erase(i); //以string.h为头文件的删除函数
cout << "删除成功" << endl; Sleep(100); mainmenu(); break;
case 2:
cout << "您未删除" << endl; Sleep(100); mainmenu(); break;
default:cout << "输入错误,将返回主菜单\n"; Sleep(1000); mainmenu(); break;
}
if ((*i).id != id)
{
cout << "查无此人" << endl;
}
}
}
Save();
cout << "\n按任意键返回主菜单"; char ch;
cin >> ch; Sleep(500); return;
}
void stusystem::Remake()
{
system("cls");
cout << "修改学生成绩:" << endl;
cout << "请输入要修改的学生学号:";
int id;
cin >> id;
for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
{
if ((*i).id == id)
{
cout << "已找到该学生,请重新输入该学生的信息" << endl;
int ID;
string name, sex;
float eng, math, cplus, mndl, ty, matlab, sjjg, dw;
cout << "姓名:"; cin >> name;
cout << "学号:"; cin >> id;
cout << "性别:"; cin >> sex;
cout << "英语:"; cin >>eng;
cout << "高数:"; cin >> math;
cout << "C++:"; cin >>cplus;
cout << "模拟电路:"; cin >> mndl;
cout << "体育:"; cin >> ty;
cout << "Matlab:"; cin >> matlab;
cout << "数据结构:"; cin >> sjjg;
cout << "大学物理:"; cin >> dw;
char c;
cout << "是否保存修改?(y/n)";
cin >> c;
switch (c) {
case'y':
case'Y':
(*i).name = name; (*i).id = id; (*i).sex = sex; (*i).English_score = eng; (*i).Math_score = math;
(*i).C_plus_score = cplus; (*i).Analog_circuit_score = mndl; (*i).Sports_score = ty; (*i).Physical_score = dw; break;
case'n':
case'N':
break;
default:cout << "输入错误,将返回主菜单\n"; Sleep(1000); mainmenu(); break;
}
}
}
Save();
cout << "\n按任意键返回主菜单"; char ch;
cin >> ch; Sleep(500); return;
}
bool mysort(Score a,Score b)//自定义排序规则
{
if (a.GPA == b.GPA) {
return a.id > b.id; //若gpa相同则按学号从小到大排列
}
else {
return a.GPA > b.GPA; //gpa从大到小排
}
}
void stusystem::Sort()
{
system("cls");
if (a.size() == 0)
cout << "文件不存在" << endl;
else {
sort(a.begin(), a.end(), mysort); //排序函数从向量表头至表尾使用自定义mysort规则
cout << "学号" << "\t" << "姓名" << "\t" << "性别" << " " << "各科学分" << " " << "英语" << " " << "高等数学"
<< " " << "C++" << " " << "模拟电路" << " " << "体育" << " " << "Matlab"
<< " " << "数据结构" << " " << "大学物理" << " " << "GPA" << endl;
for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
{
cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t \t" << (*i).English_score << "\t" <<
(*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
<< "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t " << (*i).GPA << endl;
}
cout << "\n按任意键返回主菜单"; char ch;
cin >> ch; Sleep(500); return;
}
}
void stusystem::Print()
{
system("cls");
if (a.size() == 0)
cout << "文件不存在" << endl;
else {
cout << "学号" << "\t" << "姓名" << "\t" << "性别" << " " << "各科学分" << " " << "英语" << " " << "高等数学"
<< " " << "C++" << " " << "模拟电路" << " " << "体育" << " " << "Matlab"
<< " " << "数据结构" << " " << "大学物理" << " " << "GPA" << endl;
for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
{
cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t \t" << (*i).English_score << "\t" <<
(*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
<< "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t " << (*i).GPA << endl;
}
}
cout << "\n按任意键返回主菜单"; char ch;
cin >> ch; Sleep(500); return;
}
int main()
{
stusystem stu;
while (1)
{
stu.mainmenu();
}
return 0;
}