帮我用c++写一个学生成绩管理程序:并实现以下要求: 定义学生(student)类,其中包括姓名、性别、学号、班级和语文数学英语物理学科的成绩。 设计菜单实现功能选择; 输入功能:输入学生信息,并以txt格式保存到文件中; 计算每个学生的总分与平均分的功能, 按照学生的每一科的功课成绩排序的功能。 按照学生的总成绩排序的功能。 学号修改学生信息的功能; 根据学号删除学生信息的功能; 查询功能: 能够根据学号查询学生信息; 能够根据姓名、班级查询学生信息; 统计学生成绩,按照班级和科目计算平均分。 所有的增加、修改、删除能同步到txt文件;也从txt文件读取数据到程序。
时间: 2024-01-08 16:02:28 浏览: 192
学生成绩管理 ,这是一个学生成绩管理的实例,有C++实现。
以下是一个基本的学生成绩管理程序,实现了上述要求:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 学生类
class Student {
public:
string name; // 姓名
string gender; // 性别
string id; // 学号
string cls; // 班级
int Chinese; // 语文成绩
int Maths; // 数学成绩
int English; // 英语成绩
int Physics; // 物理成绩
// 构造函数
Student(string name, string gender, string id, string cls,
int Chinese, int Maths, int English, int Physics) {
this->name = name;
this->gender = gender;
this->id = id;
this->cls = cls;
this->Chinese = Chinese;
this->Maths = Maths;
this->English = English;
this->Physics = Physics;
}
// 计算总分
int get_total() const {
return Chinese + Maths + English + Physics;
}
// 计算平均分
double get_average() const {
return get_total() / 4.0;
}
// 重载小于号,用于排序
bool operator < (const Student& other) const {
return id < other.id;
}
};
// 学生成绩管理类
class StudentManager {
public:
// 构造函数,从文件中读取学生信息
StudentManager() {
ifstream fin("students.txt");
string name, gender, id, cls;
int Chinese, Maths, English, Physics;
while (fin >> name >> gender >> id >> cls >> Chinese >> Maths >> English >> Physics) {
students.push_back(Student(name, gender, id, cls, Chinese, Maths, English, Physics));
}
fin.close();
}
// 菜单
void menu() {
while (true) {
cout << "\n-------------------\n";
cout << "1. 输入学生信息\n";
cout << "2. 计算每个学生的总分与平均分\n";
cout << "3. 按照学生的每一科的功课成绩排序\n";
cout << "4. 按照学生的总成绩排序\n";
cout << "5. 学号修改学生信息\n";
cout << "6. 根据学号删除学生信息\n";
cout << "7. 根据学号查询学生信息\n";
cout << "8. 根据姓名、班级查询学生信息\n";
cout << "9. 统计学生成绩,按照班级和科目计算平均分\n";
cout << "0. 退出程序\n";
cout << "请选择功能:";
int choice;
cin >> choice;
switch (choice) {
case 1:
input_student();
break;
case 2:
calculate_scores();
break;
case 3:
sort_by_subject();
break;
case 4:
sort_by_total();
break;
case 5:
modify_student();
break;
case 6:
delete_student();
break;
case 7:
search_by_id();
break;
case 8:
search_by_name_or_class();
break;
case 9:
calculate_average_scores();
break;
case 0:
save_to_file();
return;
default:
cout << "无效的选项,请重新选择!\n";
break;
}
}
}
private:
vector<Student> students; // 学生信息列表
// 输入学生信息
void input_student() {
cout << "请输入学生信息(姓名 性别 学号 班级 语文成绩 数学成绩 英语成绩 物理成绩):\n";
string name, gender, id, cls;
int Chinese, Maths, English, Physics;
cin >> name >> gender >> id >> cls >> Chinese >> Maths >> English >> Physics;
students.push_back(Student(name, gender, id, cls, Chinese, Maths, English, Physics));
cout << "学生信息已添加!\n";
}
// 计算每个学生的总分与平均分
void calculate_scores() {
for (auto& student : students) {
cout << student.id << " " << student.name << " 总分:" << student.get_total() << " 平均分:" << student.get_average() << "\n";
}
}
// 按照学生的每一科的功课成绩排序
void sort_by_subject() {
cout << "请选择排序方式(1.语文 2.数学 3.英语 4.物理):";
int choice;
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.Maths > b.Maths;
});
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 << "无效的选项,请重新选择!\n";
return;
}
cout << "排序结果:\n";
for (auto& student : students) {
cout << student.id << " " << student.name << " " << choice_to_subject(choice) << ":" << get_score(student, choice) << "\n";
}
}
// 按照学生的总成绩排序
void sort_by_total() {
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.get_total() > b.get_total();
});
cout << "排序结果:\n";
for (auto& student : students) {
cout << student.id << " " << student.name << " 总分:" << student.get_total() << "\n";
}
}
// 修改学生信息
void modify_student() {
cout << "请输入要修改的学生的学号:";
string id;
cin >> id;
for (auto& student : students) {
if (student.id == id) {
cout << "请输入修改后的学生信息(姓名 性别 学号 班级 语文成绩 数学成绩 英语成绩 物理成绩):\n";
string name, gender, cls;
int Chinese, Maths, English, Physics;
cin >> name >> gender >> id >> cls >> Chinese >> Maths >> English >> Physics;
student.name = name;
student.gender = gender;
student.cls = cls;
student.Chinese = Chinese;
student.Maths = Maths;
student.English = English;
student.Physics = Physics;
cout << "学生信息已修改!\n";
return;
}
}
cout << "找不到该学生!\n";
}
// 根据学号删除学生信息
void delete_student() {
cout << "请输入要删除的学生的学号:";
string id;
cin >> id;
for (auto it = students.begin(); it != students.end(); ++it) {
if (it->id == id) {
students.erase(it);
cout << "学生信息已删除!\n";
return;
}
}
cout << "找不到该学生!\n";
}
// 根据学号查询学生信息
void search_by_id() {
cout << "请输入要查询的学生的学号:";
string id;
cin >> id;
for (auto& student : students) {
if (student.id == id) {
print_student_info(student);
return;
}
}
cout << "找不到该学生!\n";
}
// 根据姓名、班级查询学生信息
void search_by_name_or_class() {
cout << "请选择查询方式(1.姓名 2.班级):";
int choice;
cin >> choice;
string query;
cout << "请输入查询关键字:";
cin >> query;
bool found = false;
for (auto& student : students) {
if ((choice == 1 && student.name == query) || (choice == 2 && student.cls == query)) {
print_student_info(student);
found = true;
}
}
if (!found) {
cout << "找不到符合条件的学生!\n";
}
}
// 统计学生成绩,按照班级和科目计算平均分
void calculate_average_scores() {
string cls;
cout << "请输入要统计的班级:";
cin >> cls;
int Chinese_total = 0, Maths_total = 0, English_total = 0, Physics_total = 0;
int num = 0;
for (auto& student : students) {
if (student.cls == cls) {
Chinese_total += student.Chinese;
Maths_total += student.Maths;
English_total += student.English;
Physics_total += student.Physics;
num++;
}
}
if (num == 0) {
cout << "找不到该班级的学生!\n";
return;
}
cout << "班级:" << cls << "\n";
cout << "语文平均分:" << Chinese_total / (double)num << "\n";
cout << "数学平均分:" << Maths_total / (double)num << "\n";
cout << "英语平均分:" << English_total / (double)num << "\n";
cout << "物理平均分:" << Physics_total / (double)num << "\n";
}
// 将学生信息保存到文件中
void save_to_file() {
ofstream fout("students.txt");
for (auto& student : students) {
fout << student.name << " " << student.gender << " " << student.id << " " << student.cls << " " << student.Chinese << " " << student.Maths << " " << student.English << " " << student.Physics << "\n";
}
fout.close();
cout << "学生信息已保存到文件!\n";
}
// 打印学生信息
void print_student_info(const Student& student) {
cout << "姓名:" << student.name << " 性别:" << student.gender << " 学号:" << student.id << " 班级:" << student.cls << " 语文成绩:" << student.Chinese << " 数学成绩:" << student.Maths << " 英语成绩:" << student.English << " 物理成绩:" << student.Physics << "\n";
}
// 将数字转换为科目名称
string choice_to_subject(int choice) {
switch (choice) {
case 1:
return "语文";
case 2:
return "数学";
case 3:
return "英语";
case 4:
return "物理";
default:
return "";
}
}
// 获取学生指定科目的成绩
int get_score(const Student& student, int choice) {
switch (choice) {
case 1:
return student.Chinese;
case 2:
return student.Maths;
case 3:
return student.English;
case 4:
return student.Physics;
default:
return 0;
}
}
};
int main() {
StudentManager manager;
manager.menu();
return 0;
}
```
注意,这只是一个简单的实现,对于更复杂的应用场景可能需要更多的功能和更完善的错误处理。同时,由于时间和篇幅的限制,此处也未对代码进行详细的解释,请结合注释和自行查阅文档理解。
阅读全文