帮我用c++写一个程序:学生成绩管理系统,并实现以下要求: 定义学生(student)类,其中包括姓名、性别、学号、班级和语文数学英语物理学科的成绩。 设计菜单实现功能选择; 输入功能:输入学生信息,并以txt格式保存到文件中; 计算每个学生的总分与平均分的功能,而且能够分别按照学生的每一科的功课成绩和总分的成绩排序。 能根据学号修改学生信息; 能根据学号删除学生信息; 查询功能: 能够根据学号查询学生信息; 能够根据姓名、班级查询学生信息; 统计学生成绩,按照班级和科目计算平均分。 所有的增加、修改、删除能同步到txt文件;也从txt文件读取数据到程序。
时间: 2023-12-28 21:03:55 浏览: 217
基于C++实现学生成绩管理系统【100012905】
好的,下面是我给您实现的代码,希望能满足您的需求:
```
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
class Student {
public:
string name, gender, id, cls;
int chinese, math, english, physics;
int totalScore;
double avgScore;
Student(string _name, string _gender, string _id, string _cls, int _chinese, int _math, int _english, int _physics) {
name = _name;
gender = _gender;
id = _id;
cls = _cls;
chinese = _chinese;
math = _math;
english = _english;
physics = _physics;
totalScore = chinese + math + english + physics;
avgScore = totalScore / 4.0;
}
};
vector<Student> students;
void addStudent() {
string name, gender, id, cls;
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));
ofstream outfile;
outfile.open("students.txt", ios::app);
outfile << name << " " << gender << " " << id << " " << cls << " " << chinese << " " << math << " " << english << " " << physics << endl;
outfile.close();
}
void readStudentData() {
ifstream infile;
infile.open("students.txt");
if (!infile) {
cout << "文件不存在" << endl;
return;
}
while (!infile.eof()) {
string name, gender, id, cls;
int chinese, math, english, physics;
infile >> name >> gender >> id >> cls >> chinese >> math >> english >> physics;
students.push_back(Student(name, gender, id, cls, chinese, math, english, physics));
}
infile.close();
}
void saveStudentData() {
ofstream outfile;
outfile.open("students.txt", ios::trunc);
for (int i = 0; i < students.size(); ++i) {
outfile << students[i].name << " " << students[i].gender << " " << students[i].id << " " << students[i].cls << " " << students[i].chinese << " " << students[i].math << " " << students[i].english << " " << students[i].physics << endl;
}
outfile.close();
}
void printStudents() {
cout << "姓名\t性别\t学号\t班级\t语文\t数学\t英语\t物理\t总分\t平均分" << endl;
for (int i = 0; i < students.size(); ++i) {
cout << students[i].name << "\t" << students[i].gender << "\t" << students[i].id << "\t" << students[i].cls << "\t" << students[i].chinese << "\t" << students[i].math << "\t" << students[i].english << "\t" << students[i].physics << "\t" << students[i].totalScore << "\t" << students[i].avgScore << endl;
}
}
bool compareChinese(Student a, Student b) {
return a.chinese > b.chinese;
}
bool compareMath(Student a, Student b) {
return a.math > b.math;
}
bool compareEnglish(Student a, Student b) {
return a.english > b.english;
}
bool comparePhysics(Student a, Student b) {
return a.physics > b.physics;
}
bool compareTotalScore(Student a, Student b) {
return a.totalScore > b.totalScore;
}
void sortByName() {
sort(students.begin(), students.end(), [](Student a, Student b) { return a.name < b.name; });
printStudents();
}
void sortByChinese() {
sort(students.begin(), students.end(), compareChinese);
printStudents();
}
void sortByMath() {
sort(students.begin(), students.end(), compareMath);
printStudents();
}
void sortByEnglish() {
sort(students.begin(), students.end(), compareEnglish);
printStudents();
}
void sortByPhysics() {
sort(students.begin(), students.end(), comparePhysics);
printStudents();
}
void sortByTotalScore() {
sort(students.begin(), students.end(), compareTotalScore);
printStudents();
}
void modifyStudent() {
string id;
cout << "请输入要修改信息的学生的学号:";
cin >> id;
bool found = false;
for (int i = 0; i < students.size(); ++i) {
if (students[i].id == id) {
cout << "请输入学生姓名:";
cin >> students[i].name;
cout << "请输入学生性别:";
cin >> students[i].gender;
cout << "请输入学生学号:";
cin >> students[i].id;
cout << "请输入学生班级:";
cin >> students[i].cls;
cout << "请输入学生语文成绩:";
cin >> students[i].chinese;
cout << "请输入学生数学成绩:";
cin >> students[i].math;
cout << "请输入学生英语成绩:";
cin >> students[i].english;
cout << "请输入学生物理成绩:";
cin >> students[i].physics;
students[i].totalScore = students[i].chinese + students[i].math + students[i].english + students[i].physics;
students[i].avgScore = students[i].totalScore / 4.0;
found = true;
break;
}
}
if (!found) {
cout << "未找到该学生" << endl;
return;
}
saveStudentData();
cout << "修改成功" << endl;
}
void deleteStudent() {
string id;
cout << "请输入要删除信息的学生的学号:";
cin >> id;
bool found = false;
for (int i = 0; i < students.size(); ++i) {
if (students[i].id == id) {
students.erase(students.begin() + i);
found = true;
break;
}
}
if (!found) {
cout << "未找到该学生" << endl;
return;
}
saveStudentData();
cout << "删除成功" << endl;
}
void searchById() {
string id;
cout << "请输入要查询信息的学生的学号:";
cin >> id;
bool found = false;
for (int i = 0; i < students.size(); ++i) {
if (students[i].id == id) {
cout << "姓名\t性别\t学号\t班级\t语文\t数学\t英语\t物理\t总分\t平均分" << endl;
cout << students[i].name << "\t" << students[i].gender << "\t" << students[i].id << "\t" << students[i].cls << "\t" << students[i].chinese << "\t" << students[i].math << "\t" << students[i].english << "\t" << students[i].physics << "\t" << students[i].totalScore << "\t" << students[i].avgScore << endl;
found = true;
break;
}
}
if (!found) {
cout << "未找到该学生" << endl;
return;
}
}
void searchByNameOrCls() {
string nameOrCls;
cout << "请输入要查询信息的学生的姓名或班级:";
cin >> nameOrCls;
bool found = false;
for (int i = 0; i < students.size(); ++i) {
if (students[i].name == nameOrCls || students[i].cls == nameOrCls) {
cout << "姓名\t性别\t学号\t班级\t语文\t数学\t英语\t物理\t总分\t平均分" << endl;
cout << students[i].name << "\t" << students[i].gender << "\t" << students[i].id << "\t" << students[i].cls << "\t" << students[i].chinese << "\t" << students[i].math << "\t" << students[i].english << "\t" << students[i].physics << "\t" << students[i].totalScore << "\t" << students[i].avgScore << endl;
found = true;
}
}
if (!found) {
cout << "未找到该学生" << endl;
return;
}
}
void calculateAvgScoreByClsAndSubject() {
string cls;
int chineseSum = 0, mathSum = 0, englishSum = 0, physicsSum = 0;
double chineseAvg = 0.0, mathAvg = 0.0, englishAvg = 0.0, physicsAvg = 0.0;
int chineseCount = 0, mathCount = 0, englishCount = 0, physicsCount = 0;
cout << "请输入要统计的班级:";
cin >> cls;
for (int i = 0; i < students.size(); ++i) {
if (students[i].cls == cls) {
chineseSum += students[i].chinese;
mathSum += students[i].math;
englishSum += students[i].english;
physicsSum += students[i].physics;
chineseCount++;
mathCount++;
englishCount++;
physicsCount++;
}
}
chineseAvg = chineseSum / (double)chineseCount;
mathAvg = mathSum / (double)mathCount;
englishAvg = englishSum / (double)englishCount;
physicsAvg = physicsSum / (double)physicsCount;
cout << "班级:" << cls << endl;
cout << "语文平均分:" << chineseAvg << endl;
cout << "数学平均分:" << mathAvg << endl;
cout << "英语平均分:" << englishAvg << endl;
cout << "物理平均分:" << physicsAvg << endl;
}
int main() {
readStudentData();
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 << "------------------------" << endl;
int choice;
cout << "请输入选项:";
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
printStudents();
break;
case 3:
int sortChoice;
cout << "1.按照语文成绩排序" << endl;
cout << "2.按照数学成绩排序" << endl;
cout << "3.按照英语成绩排序" << endl;
cout << "4.按照物理成绩排序" << endl;
cout << "5.按照总分排序" << endl;
cout << "请输入选项:";
cin >> sortChoice;
switch (sortChoice) {
case 1:
sortByChinese();
break;
case 2:
sortByMath();
break;
case 3:
sortByEnglish();
break;
case 4:
sortByPhysics();
break;
case 5:
sortByTotalScore();
break;
default:
cout << "无效选项" << endl;
break;
}
break;
case 4:
modifyStudent();
break;
case 5:
deleteStudent();
break;
case 6:
searchById();
break;
case 7:
searchByNameOrCls();
break;
case 8:
calculateAvgScoreByClsAndSubject();
break;
case 9:
return 0;
default:
cout << "无效选项" << endl;
break;
}
}
return 0;
}
```
程序中使用了一个 vector 来存储学生信息,通过读写文件实现了数据的持久化。在实现各种功能时,可以看到程序使用了 C++11 中引入的 lambda 表达式来简化排序操作,提高了代码的可读性和简洁性。
阅读全文