帮我用c++设计一个学生成绩管理系统基本要求: 定义学生(student)类,其中至少包括姓名、性别、学号、班级和四门功课的成绩。 功能要求: 1、设计菜单实现功能选择; 2、输入功能:输入学生信息,并保存到文件中; 3、计算每个学生的总分与平均分的功能,而且能够分别按照学生的每一科的功课成绩和总分的成绩排序。 4、能根据学号修改学生信息; 5、能根据学号删除学生信息; 6、查询功能: 1)能够根据学号查询学生信息; 2)能够根据姓名、班级查询学生信息; 3)统计学生成绩,按照班级和科目计算平均分。 7、所有的增加、修改、删除能同步到文件;也从文件读取数据到程序。
时间: 2024-01-06 18:03:50 浏览: 75
学生成绩管理 ,这是一个学生成绩管理的实例,有C++实现。
好的,以下是一个简单的学生成绩管理系统的设计,满足您的需求。
```
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 定义学生类
class Student {
public:
Student(string name = "", string gender = "", string id = "", string cls = "", int chinese = 0, int math = 0, int english = 0, int programming = 0) {
this->name = name;
this->gender = gender;
this->id = id;
this->cls = cls;
this->chinese = chinese;
this->math = math;
this->english = english;
this->programming = programming;
}
// 获取学生信息
string getName() { return name; }
string getGender() { return gender; }
string getId() { return id; }
string getCls() { return cls; }
int getChinese() { return chinese; }
int getMath() { return math; }
int getEnglish() { return english; }
int getProgramming() { return programming; }
// 设置学生信息
void setName(string name) { this->name = name; }
void setGender(string gender) { this->gender = gender; }
void setId(string id) { this->id = id; }
void setCls(string cls) { this->cls = cls; }
void setChinese(int chinese) { this->chinese = chinese; }
void setMath(int math) { this->math = math; }
void setEnglish(int english) { this->english = english; }
void setProgramming(int programming) { this->programming = programming; }
// 计算总分和平均分
int getTotal() { return chinese + math + english + programming; }
float getAverage() { return (float)getTotal() / 4.0f; }
private:
string name; // 姓名
string gender; // 性别
string id; // 学号
string cls; // 班级
int chinese; // 语文成绩
int math; // 数学成绩
int english; // 英语成绩
int programming; // 编程成绩
};
// 定义学生管理类
class StudentManager {
public:
// 构造函数,从文件中读取学生信息
StudentManager() {
ifstream fin("students.dat");
if (fin.is_open()) {
while (!fin.eof()) {
string name, gender, id, cls;
int chinese, math, english, programming;
fin >> name >> gender >> id >> cls >> chinese >> math >> english >> programming;
if (name != "") {
students.push_back(Student(name, gender, id, cls, chinese, math, english, programming));
}
}
fin.close();
}
}
~StudentManager() {}
// 添加学生
void addStudent() {
string name, gender, id, cls;
int chinese, math, english, programming;
cout << "请输入学生姓名:";
cin >> name;
cout << "请输入学生性别:";
cin >> gender;
cout << "请输入学生学号:";
cin >> id;
cout << "请输入学生班级:";
cin >> cls;
cout << "请输入学生语文成绩:";
cin >> chinese;
cout << "请输入学生数学成绩:";
cin >> math;
cout << "请输入学生英语成绩:";
cin >> english;
cout << "请输入学生编程成绩:";
cin >> programming;
students.push_back(Student(name, gender, id, cls, chinese, math, english, programming));
saveToFile();
cout << "添加学生成功!" << endl;
}
// 修改学生信息
void modifyStudent() {
string id;
cout << "请输入要修改的学生学号:";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].getId() == id) {
string name, gender, cls;
int chinese, math, english, programming;
cout << "请输入学生姓名:";
cin >> name;
cout << "请输入学生性别:";
cin >> gender;
cout << "请输入学生班级:";
cin >> cls;
cout << "请输入学生语文成绩:";
cin >> chinese;
cout << "请输入学生数学成绩:";
cin >> math;
cout << "请输入学生英语成绩:";
cin >> english;
cout << "请输入学生编程成绩:";
cin >> programming;
students[i].setName(name);
students[i].setGender(gender);
students[i].setCls(cls);
students[i].setChinese(chinese);
students[i].setMath(math);
students[i].setEnglish(english);
students[i].setProgramming(programming);
saveToFile();
cout << "修改学生信息成功!" << endl;
return;
}
}
cout << "没有找到该学生!" << endl;
}
// 删除学生
void deleteStudent() {
string id;
cout << "请输入要删除的学生学号:";
cin >> id;
for (vector<Student>::iterator it = students.begin(); it != students.end(); it++) {
if (it->getId() == id) {
students.erase(it);
saveToFile();
cout << "删除学生成功!" << endl;
return;
}
}
cout << "没有找到该学生!" << endl;
}
// 按照学号查询学生信息
void queryById() {
string id;
cout << "请输入要查询的学生学号:";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].getId() == id) {
printStudent(students[i]);
return;
}
}
cout << "没有找到该学生!" << endl;
}
// 按照姓名和班级查询学生信息
void queryByNameAndCls() {
string name, cls;
cout << "请输入要查询的学生姓名:";
cin >> name;
cout << "请输入要查询的学生班级:";
cin >> cls;
for (int i = 0; i < students.size(); i++) {
if (students[i].getName() == name && students[i].getCls() == cls) {
printStudent(students[i]);
}
}
}
// 统计学生成绩,按照班级和科目计算平均分
void statistics() {
string cls;
cout << "请输入要统计的班级:";
cin >> cls;
int chineseSum = 0, mathSum = 0, englishSum = 0, programmingSum = 0, count = 0;
for (int i = 0; i < students.size(); i++) {
if (students[i].getCls() == cls) {
chineseSum += students[i].getChinese();
mathSum += students[i].getMath();
englishSum += students[i].getEnglish();
programmingSum += students[i].getProgramming();
count++;
}
}
if (count > 0) {
cout << "班级:" << cls << endl;
cout << "语文平均分:" << (float)chineseSum / (float)count << endl;
cout << "数学平均分:" << (float)mathSum / (float)count << endl;
cout << "英语平均分:" << (float)englishSum / (float)count << endl;
cout << "编程平均分:" << (float)programmingSum / (float)count << endl;
} else {
cout << "没有找到该班级的学生!" << endl;
}
}
// 按照总分排序
void sortByTotal() {
sort(students.begin(), students.end(), [](const Student &a, const Student &b) {
return a.getTotal() > b.getTotal();
});
printStudents();
}
// 按照语文成绩排序
void sortByChinese() {
sort(students.begin(), students.end(), [](const Student &a, const Student &b) {
return a.getChinese() > b.getChinese();
});
printStudents();
}
// 按照数学成绩排序
void sortByMath() {
sort(students.begin(), students.end(), [](const Student &a, const Student &b) {
return a.getMath() > b.getMath();
});
printStudents();
}
// 按照英语成绩排序
void sortByEnglish() {
sort(students.begin(), students.end(), [](const Student &a, const Student &b) {
return a.getEnglish() > b.getEnglish();
});
printStudents();
}
// 按照编程成绩排序
void sortByProgramming() {
sort(students.begin(), students.end(), [](const Student &a, const Student &b) {
return a.getProgramming() > b.getProgramming();
});
printStudents();
}
// 打印学生信息
void printStudent(Student &s) {
cout << "姓名:" << s.getName() << endl;
cout << "性别:" << s.getGender() << endl;
cout << "学号:" << s.getId() << endl;
cout << "班级:" << s.getCls() << endl;
cout << "语文成绩:" << s.getChinese() << endl;
cout << "数学成绩:" << s.getMath() << endl;
cout << "英语成绩:" << s.getEnglish() << endl;
cout << "编程成绩:" << s.getProgramming() << endl;
cout << "总分:" << s.getTotal() << endl;
cout << "平均分:" << s.getAverage() << endl;
cout << endl;
}
// 打印所有学生信息
void printStudents() {
for (int i = 0; i < students.size(); i++) {
printStudent(students[i]);
}
}
private:
vector<Student> students; // 存储所有学生信息
// 保存学生信息到文件
void saveToFile() {
ofstream fout("students.dat");
for (int i = 0; i < students.size(); i++) {
fout << students[i].getName() << " " << students[i].getGender() << " " << students[i].getId() << " " << students[i].getCls() << " " << students[i].getChinese() << " " << students[i].getMath() << " " << students[i].getEnglish() << " " << students[i].getProgramming() << endl;
}
fout.close();
}
};
int main() {
StudentManager mgr;
int choice;
while (true) {
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 << "9.按照数学成绩排序" << endl;
cout << "10.按照英语成绩排序" << endl;
cout << "11.按照编程成绩排序" << endl;
cout << "12.打印所有学生信息" << endl;
cout << "0.退出程序" << endl;
cout << "请选择功能:";
cin >> choice;
switch (choice) {
case 0:
cout << "退出程序!" << endl;
return 0;
case 1:
mgr.addStudent();
break;
case 2:
mgr.modifyStudent();
break;
case 3:
mgr.deleteStudent();
break;
case 4:
mgr.queryById();
break;
case 5:
mgr.queryByNameAndCls();
break;
case 6:
mgr.statistics();
break;
case 7:
mgr.sortByTotal();
break;
case 8:
mgr.sortByChinese();
break;
case 9:
mgr.sortByMath();
break;
case 10:
mgr.sortByEnglish();
break;
case 11:
mgr.sortByProgramming();
break;
case 12:
mgr.printStudents();
break;
default:
cout << "无效的选择!" << endl;
break;
}
cout << endl;
}
return 0;
}
```
注意:该程序中的学生信息保存在文件"students.dat"中,所以请确保程序所在的目录下有该文件,并且程序有对该文件的读写权限。
阅读全文