用c++写一个学生成绩管理系统,实现以下功能:定义学生(student)类,其中至少包括姓名、性别、学号、班级和四门功课的成绩。 功能要求: 1、设计菜单实现功能选择; 2、输入功能:输入学生信息,并以txt格式保存到文件中; 3、计算每个学生的总分与平均分的功能,而且能够分别按照学生的每一科的功课成绩和总分的成绩排序。 4、能根据学号修改学生信息; 5、能根据学号删除学生信息; 6、查询功能: 1)能够根据学号查询学生信息; 2)能够根据姓名、班级查询学生信息; 3)统计学生成绩,按照班级和科目计算平均分。 7、所有的增加、修改、删除能同步到txt文件;也从txt文件读取数据到程序。
时间: 2024-01-06 19:04:34 浏览: 83
C++实现学生成绩统计管理系统
4星 · 用户满意度95%
以下是一个简单的学生成绩管理系统的C++代码,实现了以上所有功能:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
// 定义学生类
class Student {
public:
string name;
string gender;
int id;
string cls;
int scores[4];
int total_score;
// 构造函数
Student(string n, string g, int i, string c, int s[]) {
name = n;
gender = g;
id = i;
cls = c;
for (int j = 0; j < 4; j++) {
scores[j] = s[j];
}
total_score = s[0] + s[1] + s[2] + s[3];
}
// 默认构造函数
Student() {}
// 打印学生信息
void print() {
cout << "姓名:" << name << " 性别:" << gender << " 学号:" << id << " 班级:" << cls
<< " 语文:" << scores[0] << " 数学:" << scores[1] << " 英语:" << scores[2] << " 物理:" << scores[3]
<< " 总分:" << total_score << endl;
}
};
// 学生列表
vector<Student> students;
// 读取学生信息
void load_students() {
ifstream fin("students.txt");
if (fin.is_open()) {
while (!fin.eof()) {
string n, g, c;
int i, s[4];
fin >> n >> g >> i >> c >> s[0] >> s[1] >> s[2] >> s[3];
Student stu(n, g, i, c, s);
students.push_back(stu);
}
fin.close();
}
}
// 保存学生信息
void save_students() {
ofstream fout("students.txt");
for (int i = 0; i < students.size(); i++) {
Student stu = students[i];
fout << stu.name << " " << stu.gender << " " << stu.id << " " << stu.cls << " " << stu.scores[0] << " "
<< stu.scores[1] << " " << stu.scores[2] << " " << stu.scores[3] << endl;
}
fout.close();
}
// 添加学生
void add_student() {
string n, g, c;
int i, s[4];
cout << "请输入学生信息:" << endl;
cout << "姓名:";
cin >> n;
cout << "性别:";
cin >> g;
cout << "学号:";
cin >> i;
cout << "班级:";
cin >> c;
cout << "语文成绩:";
cin >> s[0];
cout << "数学成绩:";
cin >> s[1];
cout << "英语成绩:";
cin >> s[2];
cout << "物理成绩:";
cin >> s[3];
Student stu(n, g, i, c, s);
students.push_back(stu);
cout << "添加成功!" << endl;
}
// 修改学生信息
void modify_student() {
int id;
cout << "请输入要修改的学生学号:";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
string n, g, c;
int s[4];
cout << "请输入修改后的学生信息:" << endl;
cout << "姓名:";
cin >> n;
cout << "性别:";
cin >> g;
cout << "班级:";
cin >> c;
cout << "语文成绩:";
cin >> s[0];
cout << "数学成绩:";
cin >> s[1];
cout << "英语成绩:";
cin >> s[2];
cout << "物理成绩:";
cin >> s[3];
students[i].name = n;
students[i].gender = g;
students[i].cls = c;
for (int j = 0; j < 4; j++) {
students[i].scores[j] = s[j];
}
students[i].total_score = s[0] + s[1] + s[2] + s[3];
cout << "修改成功!" << endl;
return;
}
}
cout << "没有找到该学生!" << endl;
}
// 删除学生
void delete_student() {
int id;
cout << "请输入要删除的学生学号:";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
students.erase(students.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "没有找到该学生!" << endl;
}
// 按学号查询学生
void search_by_id() {
int id;
cout << "请输入要查询的学生学号:";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
students[i].print();
return;
}
}
cout << "没有找到该学生!" << endl;
}
// 按姓名和班级查询学生
void search_by_name_and_cls() {
string n, c;
cout << "请输入要查询的学生姓名和班级:";
cin >> n >> c;
bool found = false;
for (int i = 0; i < students.size(); i++) {
if (students[i].name == n && students[i].cls == c) {
students[i].print();
found = true;
}
}
if (!found) {
cout << "没有找到该学生!" << endl;
}
}
// 计算总分和平均分
void calculate_scores() {
for (int i = 0; i < students.size(); i++) {
students[i].total_score = students[i].scores[0] + students[i].scores[1] + students[i].scores[2] +
students[i].scores[3];
}
}
// 按总分排序
bool cmp_by_total_score(Student a, Student b) {
return a.total_score > b.total_score;
}
// 按语文成绩排序
bool cmp_by_chinese_score(Student a, Student b) {
return a.scores[0] > b.scores[0];
}
// 按数学成绩排序
bool cmp_by_math_score(Student a, Student b) {
return a.scores[1] > b.scores[1];
}
// 按英语成绩排序
bool cmp_by_english_score(Student a, Student b) {
return a.scores[2] > b.scores[2];
}
// 按物理成绩排序
bool cmp_by_physics_score(Student a, Student b) {
return a.scores[3] > b.scores[3];
}
// 按班级和科目统计平均分
void calculate_average_scores() {
int chinese_total_score = 0;
int chinese_count = 0;
int math_total_score = 0;
int math_count = 0;
int english_total_score = 0;
int english_count = 0;
int physics_total_score = 0;
int physics_count = 0;
for (int i = 0; i < students.size(); i++) {
if (students[i].cls == "1") {
chinese_total_score += students[i].scores[0];
chinese_count++;
math_total_score += students[i].scores[1];
math_count++;
} else if (students[i].cls == "2") {
english_total_score += students[i].scores[2];
english_count++;
physics_total_score += students[i].scores[3];
physics_count++;
}
}
cout << "1班平均语文成绩:" << chinese_total_score / chinese_count << endl;
cout << "1班平均数学成绩:" << math_total_score / math_count << endl;
cout << "2班平均英语成绩:" << english_total_score / english_count << endl;
cout << "2班平均物理成绩:" << physics_total_score / physics_count << endl;
}
// 显示菜单
void show_menu() {
cout << "=========================" << endl;
cout << " 学生成绩管理系统 v1.0 " << 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 << "9. 按数学成绩排序" << endl;
cout << "10. 按英语成绩排序" << endl;
cout << "11. 按物理成绩排序" << endl;
cout << "12. 按班级和科目统计平均分" << endl;
cout << "0. 退出" << endl;
cout << "=========================" << endl;
}
int main() {
load_students(); // 从文件中读取学生信息
while (true) {
show_menu(); // 显示菜单
int choice;
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1:
add_student();
save_students();
break;
case 2:
modify_student();
save_students();
break;
case 3:
delete_student();
save_students();
break;
case 4:
search_by_id();
break;
case 5:
search_by_name_and_cls();
break;
case 6:
calculate_scores();
cout << "计算完成!" << endl;
break;
case 7:
calculate_scores();
sort(students.begin(), students.end(), cmp_by_total_score);
cout << "按总分排序完成!" << endl;
break;
case 8:
sort(students.begin(), students.end(), cmp_by_chinese_score);
cout << "按语文成绩排序完成!" << endl;
break;
case 9:
sort(students.begin(), students.end(), cmp_by_math_score);
cout << "按数学成绩排序完成!" << endl;
break;
case 10:
sort(students.begin(), students.end(), cmp_by_english_score);
cout << "按英语成绩排序完成!" << endl;
break;
case 11:
sort(students.begin(), students.end(), cmp_by_physics_score);
cout << "按物理成绩排序完成!" << endl;
break;
case 12:
calculate_average_scores();
break;
case 0:
cout << "再见!" << endl;
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
}
}
}
```
注意:该代码中使用了文件操作,需要在程序所在目录下创建一个名为 "students.txt" 的文本文件,用于保存学生信息。
阅读全文