帮我用c++写一个程序:学生成绩管理系统,并实现以下要求: 定义学生(student)类,其中包括姓名、性别、学号、班级和语文数学英语物理学科的成绩。 设计菜单实现功能选择; 输入功能:输入学生信息,并以txt格式保存到文件中; 计算每个学生的总分与平均分的功能, 按照学生的每一科的功课成绩排序的功能。 按照学生的总成绩排序的功能。 学号修改学生信息的功能; 根据学号删除学生信息的功能; 查询功能: 能够根据学号查询学生信息; 能够根据姓名、班级查询学生信息; 统计学生成绩,按照班级和科目计算平均分。 所有的增加、修改、删除能同步到txt文件;也从txt文件读取数据到程序。
时间: 2024-01-08 12:02:28 浏览: 295
以下是基于C++的学生成绩管理系统的程序代码,包含了您提供的所有功能要求:
```c++
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 定义学生类
class Student {
public:
string name;
char gender;
string id;
string cls;
int chinese;
int math;
int english;
int physics;
int total_score;
// 构造函数
Student(string name, char gender, string id, string cls, int chinese, int math, int english, int physics) {
this->name = name;
this->gender = gender;
this->id = id;
this->cls = cls;
this->chinese = chinese;
this->math = math;
this->english = english;
this->physics = physics;
this->total_score = chinese + math + english + physics;
}
// 重载小于运算符,用于排序
bool operator < (const Student& st) const {
return total_score < st.total_score;
}
// 输出学生信息
void display() {
cout << setw(10) << name << setw(5) << gender << setw(15) << id << setw(10) << cls;
cout << setw(5) << chinese << setw(5) << math << setw(5) << english << setw(5) << physics;
cout << setw(5) << total_score << endl;
}
};
// 学生信息管理类
class StudentManager {
private:
vector<Student> students;
// 从文件中读取学生信息
void readFromFile() {
ifstream in("students.txt");
if (!in) return;
string name, id, cls;
char gender;
int chinese, math, english, physics;
while (in >> name >> gender >> id >> cls >> chinese >> math >> english >> physics) {
students.push_back(Student(name, gender, id, cls, chinese, math, english, physics));
}
in.close();
}
// 将学生信息写入文件
void writeToFile() {
ofstream out("students.txt");
for (auto& st : students) {
out << st.name << " " << st.gender << " " << st.id << " " << st.cls << " ";
out << st.chinese << " " << st.math << " " << st.english << " " << st.physics << endl;
}
out.close();
}
// 根据学号查找学生
Student* findById(string id) {
for (auto& st : students) {
if (st.id == id) {
return &st;
}
}
return nullptr;
}
public:
// 构造函数
StudentManager() {
readFromFile();
}
// 添加学生
void addStudent() {
string name, id, cls;
char gender;
int chinese, math, english, physics;
cout << "请输入学生姓名:";
cin >> name;
cout << "请输入学生性别(男/女):";
cin >> gender;
cout << "请输入学生学号:";
cin >> id;
cout << "请输入学生班级:";
cin >> cls;
cout << "请输入学生语文成绩:";
cin >> chinese;
cout << "请输入学生数学成绩:";
cin >> math;
cout << "请输入学生英语成绩:";
cin >> english;
cout << "请输入学生物理成绩:";
cin >> physics;
students.push_back(Student(name, gender, id, cls, chinese, math, english, physics));
cout << "学生信息添加成功!" << endl;
writeToFile();
}
// 修改学生信息
void modifyStudent() {
string id;
cout << "请输入要修改的学生学号:";
cin >> id;
Student* st = findById(id);
if (st == nullptr) {
cout << "未找到对应的学生信息!" << endl;
} else {
cout << "请输入新的学生姓名:";
cin >> st->name;
cout << "请输入新的学生性别(男/女):";
cin >> st->gender;
cout << "请输入新的学生班级:";
cin >> st->cls;
cout << "请输入新的学生语文成绩:";
cin >> st->chinese;
cout << "请输入新的学生数学成绩:";
cin >> st->math;
cout << "请输入新的学生英语成绩:";
cin >> st->english;
cout << "请输入新的学生物理成绩:";
cin >> st->physics;
st->total_score = st->chinese + st->math + st->english + st->physics;
cout << "学生信息修改成功!" << endl;
writeToFile();
}
}
// 删除学生
void deleteStudent() {
string id;
cout << "请输入要删除的学生学号:";
cin >> id;
auto it = students.begin();
while (it != students.end()) {
if (it->id == id) {
it = students.erase(it);
cout << "学生信息删除成功!" << endl;
writeToFile();
return;
} else {
++it;
}
}
cout << "未找到对应的学生信息!" << endl;
}
// 按照每个科目成绩排序
void sortBySubject() {
int choice;
cout << "请选择排序方式(1.语文 2.数学 3.英语 4.物理):";
cin >> choice;
switch (choice) {
case 1:
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.chinese > b.chinese;
});
break;
case 2:
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.math > b.math;
});
break;
case 3:
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.english > b.english;
});
break;
case 4:
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.physics > b.physics;
});
break;
default:
cout << "无效的选择!" << endl;
return;
}
cout << "按照科目成绩排序成功!" << endl;
}
// 按照总成绩排序
void sortByTotalScore() {
sort(students.begin(), students.end());
cout << "按照总成绩排序成功!" << endl;
}
// 统计平均分
void calculateAverageScore() {
int choice;
cout << "请选择统计方式(1.班级 2.科目):";
cin >> choice;
if (choice == 1) {
string cls;
cout << "请输入要统计的班级:";
cin >> cls;
int sum_chinese = 0, sum_math = 0, sum_english = 0, sum_physics = 0;
int count = 0;
for (auto& st : students) {
if (st.cls == cls) {
sum_chinese += st.chinese;
sum_math += st.math;
sum_english += st.english;
sum_physics += st.physics;
count++;
}
}
if (count > 0) {
cout << "班级:" << cls << endl;
cout << "语文平均分:" << sum_chinese / count << endl;
cout << "数学平均分:" << sum_math / count << endl;
cout << "英语平均分:" << sum_english / count << endl;
cout << "物理平均分:" << sum_physics / count << endl;
} else {
cout << "未找到对应的班级信息!" << endl;
}
} else if (choice == 2) {
int sum_chinese = 0, sum_math = 0, sum_english = 0, sum_physics = 0;
int count = students.size();
for (auto& st : students) {
sum_chinese += st.chinese;
sum_math += st.math;
sum_english += st.english;
sum_physics += st.physics;
}
cout << "科目平均分:" << endl;
cout << "语文:" << sum_chinese / count << endl;
cout << "数学:" << sum_math / count << endl;
cout << "英语:" << sum_english / count << endl;
cout << "物理:" << sum_physics / count << endl;
} else {
cout << "无效的选择!" << endl;
return;
}
}
// 根据学号查询学生信息
void queryById() {
string id;
cout << "请输入要查询的学生学号:";
cin >> id;
Student* st = findById(id);
if (st == nullptr) {
cout << "未找到对应的学生信息!" << endl;
} else {
cout << setw(10) << "姓名" << setw(5) << "性别" << setw(15) << "学号" << setw(10) << "班级";
cout << setw(5) << "语文" << setw(5) << "数学" << setw(5) << "英语" << setw(5) << "物理";
cout << setw(5) << "总分" << endl;
st->display();
}
}
// 根据姓名和班级查询学生信息
void queryByNameAndCls() {
string name, cls;
cout << "请输入要查询的学生姓名:";
cin >> name;
cout << "请输入要查询的学生班级:";
cin >> cls;
bool found = false;
cout << setw(10) << "姓名" << setw(5) << "性别" << setw(15) << "学号" << setw(10) << "班级";
cout << setw(5) << "语文" << setw(5) << "数学" << setw(5) << "英语" << setw(5) << "物理";
cout << setw(5) << "总分" << endl;
for (auto& st : students) {
if (st.name == name && st.cls == cls) {
st.display();
found = true;
}
}
if (!found) {
cout << "未找到对应的学生信息!" << endl;
}
}
// 显示菜单
void showMenu() {
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;
}
// 运行程序
void run() {
int choice;
while (true) {
showMenu();
cout << "请输入要执行的操作序号:";
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
modifyStudent();
break;
case 3:
deleteStudent();
break;
case 4:
sortBySubject();
break;
case 5:
sortByTotalScore();
break;
case 6:
calculateAverageScore();
break;
case 7:
queryById();
break;
case 8:
queryByNameAndCls();
break;
case 0:
cout << "感谢使用本系统,再见!" << endl;
return;
default:
cout << "无效的选择,请重新输入!" << endl;
break;
}
}
}
};
int main() {
StudentManager manager;
manager.run();
return 0;
}
```
程序中使用了文件存储学生信息,数据会被保存在名为“students.txt”的文件中。每次进行学生信息的添加、修改、删除等操作后,文件都会被更新。当程序启动时,会从文件中读取学生信息并存储到内存中,以便进行后续的查询和统计。
阅读全文