帮我用c++写一个学生成绩管理系统的课程设计,并实现以下要求: 定义学生(student)类,其中至少包括姓名、性别、学号、班级和四门功课的成绩。 设计菜单实现功能选择; 输入功能:输入学生信息,并以txt格式保存到文件中; 计算每个学生的总分与平均分的功能,而且能够分别按照学生的每一科的功课成绩和总分的成绩排序。 能根据学号修改学生信息; 能根据学号删除学生信息; 查询功能: 能够根据学号查询学生信息; 能够根据姓名、班级查询学生信息; 统计学生成绩,按照班级和科目计算平均分。 所有的增加、修改、删除能同步到txt文件;也从txt文件读取数据到程序。
时间: 2023-12-30 14:03:05 浏览: 71
好的,以下是一个简单的学生成绩管理系统的代码实现,希望对您有所帮助。在代码中,我使用了 STL 中的 vector 和 map 容器来存储学生信息和进行查询操作。
```
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
// 定义学生类
class Student {
public:
string name; // 姓名
string gender; // 性别
string id; // 学号
string cls; // 班级
int score[4]; // 四门功课的成绩
Student(string n, string g, string i, string c, int s[]) {
name = n;
gender = g;
id = i;
cls = c;
for (int j = 0; j < 4; j++) {
score[j] = s[j];
}
}
};
// 定义全局变量
vector<Student> students; // 学生信息
map<string, int> id2index; // 学号到下标的映射
// 菜单函数
void menu() {
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 << "**** 9. 统计学生成绩,计算平均分 ****" << endl;
cout << "**** 0. 退出程序 ****" << endl;
cout << "********************************************" << endl;
}
// 输入学生信息并保存到文件中
void input() {
string name, gender, id, cls;
int score[4];
cout << "请输入学生姓名:";
cin >> name;
cout << "请输入学生性别:";
cin >> gender;
cout << "请输入学生学号:";
cin >> id;
cout << "请输入学生班级:";
cin >> cls;
cout << "请输入四门功课的成绩(语文,数学,英语,计算机):";
for (int i = 0; i < 4; i++) {
cin >> score[i];
}
Student s(name, gender, id, cls, score);
students.push_back(s);
id2index[id] = students.size() - 1;
ofstream fout("students.txt", ios::app);
fout << name << " " << gender << " " << id << " " << cls << " ";
for (int i = 0; i < 4; i++) {
fout << score[i] << " ";
}
fout << endl;
fout.close();
cout << "学生信息已保存到文件中" << endl;
}
// 计算每个学生的总分与平均分
void calculate() {
cout << "学生总分与平均分如下:" << endl;
for (int i = 0; i < students.size(); i++) {
int sum = 0;
for (int j = 0; j < 4; j++) {
sum += students[i].score[j];
}
double avg = (double)sum / 4;
cout << students[i].name << " " << students[i].id << " 总分:" << sum << " 平均分:" << avg << endl;
}
}
// 按照学生的每一科的功课成绩排序
void sort_by_score() {
int choice;
cout << "请选择排序依据(1. 语文,2. 数学,3. 英语,4. 计算机):";
cin >> choice;
sort(students.begin(), students.end(), [choice](const Student& a, const Student& b) {
return a.score[choice - 1] > b.score[choice - 1];
});
cout << "按照学生每一科的成绩排序如下:" << endl;
for (int i = 0; i < students.size(); i++) {
cout << students[i].name << " " << students[i].id << " " << students[i].score[choice - 1] << endl;
}
}
// 按照学生总分的成绩排序
void sort_by_total_score() {
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
int sum1 = 0, sum2 = 0;
for (int i = 0; i < 4; i++) {
sum1 += a.score[i];
sum2 += b.score[i];
}
return sum1 > sum2;
});
cout << "按照学生总分的成绩排序如下:" << endl;
for (int i = 0; i < students.size(); i++) {
int sum = 0;
for (int j = 0; j < 4; j++) {
sum += students[i].score[j];
}
cout << students[i].name << " " << students[i].id << " 总分:" << sum << endl;
}
}
// 根据学号修改学生信息
void modify() {
string id;
cout << "请输入需要修改信息的学生学号:";
cin >> id;
if (id2index.find(id) == id2index.end()) {
cout << "学号不存在" << endl;
return;
}
Student& s = students[id2index[id]];
string name, gender, cls;
int score[4];
cout << "请输入学生姓名(不修改请输-1):";
cin >> name;
if (name != "-1") {
s.name = name;
}
cout << "请输入学生性别(不修改请输-1):";
cin >> gender;
if (gender != "-1") {
s.gender = gender;
}
cout << "请输入学生班级(不修改请输-1):";
cin >> cls;
if (cls != "-1") {
s.cls = cls;
}
cout << "请输入四门功课的成绩(语文,数学,英语,计算机,不修改请输-1):";
for (int i = 0; i < 4; i++) {
cin >> score[i];
if (score[i] != -1) {
s.score[i] = score[i];
}
}
ofstream fout("students.txt");
for (int i = 0; i < students.size(); i++) {
fout << students[i].name << " " << students[i].gender << " " << students[i].id << " " << students[i].cls << " ";
for (int j = 0; j < 4; j++) {
fout << students[i].score[j] << " ";
}
fout << endl;
}
fout.close();
cout << "学生信息已修改" << endl;
}
// 根据学号删除学生信息
void remove() {
string id;
cout << "请输入需要删除信息的学生学号:";
cin >> id;
if (id2index.find(id) == id2index.end()) {
cout << "学号不存在" << endl;
return;
}
students.erase(students.begin() + id2index[id]);
id2index.erase(id);
ofstream fout("students.txt");
for (int i = 0; i < students.size(); i++) {
fout << students[i].name << " " << students[i].gender << " " << students[i].id << " " << students[i].cls << " ";
for (int j = 0; j < 4; j++) {
fout << students[i].score[j] << " ";
}
fout << endl;
}
fout.close();
cout << "学生信息已删除" << endl;
}
// 根据学号查询学生信息
void search_by_id() {
string id;
cout << "请输入学生学号:";
cin >> id;
if (id2index.find(id) == id2index.end()) {
cout << "学号不存在" << endl;
return;
}
Student s = students[id2index[id]];
cout << "学生信息如下:" << endl;
cout << "姓名:" << s.name << " 性别:" << s.gender << " 学号:" << s.id << " 班级:" << s.cls << " ";
for (int i = 0; i < 4; i++) {
cout << "第" << i + 1 << "门成绩:" << s.score[i] << " ";
}
cout << endl;
}
// 根据姓名、班级查询学生信息
void search_by_name_cls() {
string name, cls;
cout << "请输入学生姓名:";
cin >> name;
cout << "请输入学生班级:";
cin >> cls;
vector<Student> res;
for (int i = 0; i < students.size(); i++) {
if (students[i].name == name && students[i].cls == cls) {
res.push_back(students[i]);
}
}
if (res.empty()) {
cout << "没有找到符合条件的学生" << endl;
return;
}
cout << "学生信息如下:" << endl;
for (int i = 0; i < res.size(); i++) {
cout << "姓名:" << res[i].name << " 性别:" << res[i].gender << " 学号:" << res[i].id << " 班级:" << res[i].cls << " ";
for (int j = 0; j < 4; j++) {
cout << "第" << j + 1 << "门成绩:" << res[i].score[j] << " ";
}
cout << endl;
}
}
// 统计学生成绩,计算平均分
void statistics() {
map<string, vector<Student>> cls2students;
for (int i = 0; i < students.size(); i++) {
cls2students[students[i].cls].push_back(students[i]);
}
cout << "按照班级统计学生成绩,计算平均分如下:" << endl;
for (auto& it : cls2students) {
double sum[4] = {0, 0, 0, 0};
for (int i = 0; i < it.second.size(); i++) {
for (int j = 0; j < 4; j++) {
sum[j] += it.second[i].score[j];
}
}
cout << "班级:" << it.first << " 语文平均分:" << sum[0] / it.second.size() << " 数学平均分:" << sum[1] / it.second.size()
<< " 英语平均分:" << sum[2] / it.second.size() << " 计算机平均分:" << sum[3] / it.second.size() << endl;
}
}
int main() {
ifstream fin("students.txt");
string name, gender, id, cls;
int score[4];
while (fin >> name >> gender >> id >> cls >> score[0] >> score[1] >> score[2] >> score[3]) {
Student s(name, gender, id, cls, score);
students.push_back(s);
id2index[id] = students.size() - 1;
}
fin.close();
while (true) {
menu();
int choice;
cout << "请输入你的选择:";
cin >> choice;
switch (choice) {
case 1:
input();
break;
case 2:
calculate();
break;
case 3:
sort_by_score();
break;
case 4:
sort_by_total_score();
break;
case 5:
modify();
break;
case 6:
remove();
break;
case 7:
search_by_id();
break;
case 8:
search_by_name_cls();
break;
case 9:
statistics();
break;
case 0:
cout << "退出程序" << endl;
return 0;
default:
cout << "输入有误,请重新输入" << endl;
break;
}
}
return 0;
}
```
阅读全文