用c++语言编写一个学生成绩管理系统,实现学生基本信息(包括学号、姓名、数学成绩、英语成绩、计算机课成绩)的录入并用类进行封装、计算学生的总成绩和平均成绩、学生基本信息的删除、学生基本信息的查询、学生基本信息的修改,并且实现对学生基本信息的存盘(保存为文本文件)和读盘
时间: 2023-08-27 09:04:02 浏览: 371
好的,以下是一个简单的学生成绩管理系统的C++代码,实现了您要求的功能:
```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
// 学生类
class Student {
public:
int id;
string name;
float mathScore;
float englishScore;
float computerScore;
// 构造函数
Student(int id, string name, float mathScore, float englishScore, float computerScore) {
this->id = id;
this->name = name;
this->mathScore = mathScore;
this->englishScore = englishScore;
this->computerScore = computerScore;
}
// 计算总成绩
float getTotalScore() {
return mathScore + englishScore + computerScore;
}
// 计算平均成绩
float getAverageScore() {
return getTotalScore() / 3;
}
// 输出学生信息
void printInfo() {
cout << "学号:" << id << ",姓名:" << name << ",数学成绩:" << mathScore
<< ",英语成绩:" << englishScore << ",计算机课成绩:" << computerScore
<< ",总成绩:" << getTotalScore() << ",平均成绩:" << getAverageScore() << endl;
}
// 将学生信息写入文件
void writeToFile(ofstream& out) {
out << id << " " << name << " " << mathScore << " " << englishScore << " " << computerScore << endl;
}
};
// 学生管理类
class StudentManager {
public:
vector<Student> students;
// 添加学生信息
void addStudent(int id, string name, float mathScore, float englishScore, float computerScore) {
Student student(id, name, mathScore, englishScore, computerScore);
students.push_back(student);
}
// 删除学生信息
void deleteStudent(int id) {
for (vector<Student>::iterator it = students.begin(); it != students.end(); ++it) {
if (it->id == id) {
students.erase(it);
break;
}
}
}
// 查询学生信息
void queryStudent(int id) {
for (vector<Student>::iterator it = students.begin(); it != students.end(); ++it) {
if (it->id == id) {
it->printInfo();
break;
}
}
}
// 修改学生信息
void modifyStudent(int id, string name, float mathScore, float englishScore, float computerScore) {
for (vector<Student>::iterator it = students.begin(); it != students.end(); ++it) {
if (it->id == id) {
it->name = name;
it->mathScore = mathScore;
it->englishScore = englishScore;
it->computerScore = computerScore;
break;
}
}
}
// 从文件中读取学生信息
void readFromFile(string filename) {
ifstream in(filename);
if (!in) {
cout << "打开文件失败!" << endl;
return;
}
int id;
string name;
float mathScore, englishScore, computerScore;
while (in >> id >> name >> mathScore >> englishScore >> computerScore) {
addStudent(id, name, mathScore, englishScore, computerScore);
}
in.close();
}
// 将学生信息写入文件
void writeToFile(string filename) {
ofstream out(filename);
if (!out) {
cout << "打开文件失败!" << endl;
return;
}
for (vector<Student>::iterator it = students.begin(); it != students.end(); ++it) {
it->writeToFile(out);
}
out.close();
}
};
int main() {
StudentManager manager;
manager.readFromFile("students.txt");
while (true) {
cout << "请选择操作:1.添加学生信息 2.删除学生信息 3.查询学生信息 4.修改学生信息 5.保存学生信息 6.退出" << endl;
int option;
cin >> option;
if (option == 1) {
int id;
string name;
float mathScore, englishScore, computerScore;
cout << "请输入学号、姓名、数学成绩、英语成绩、计算机课成绩:" << endl;
cin >> id >> name >> mathScore >> englishScore >> computerScore;
manager.addStudent(id, name, mathScore, englishScore, computerScore);
cout << "添加成功!" << endl;
} else if (option == 2) {
int id;
cout << "请输入要删除的学生学号:" << endl;
cin >> id;
manager.deleteStudent(id);
cout << "删除成功!" << endl;
} else if (option == 3) {
int id;
cout << "请输入要查询的学生学号:" << endl;
cin >> id;
manager.queryStudent(id);
} else if (option == 4) {
int id;
string name;
float mathScore, englishScore, computerScore;
cout << "请输入要修改的学生学号、姓名、数学成绩、英语成绩、计算机课成绩:" << endl;
cin >> id >> name >> mathScore >> englishScore >> computerScore;
manager.modifyStudent(id, name, mathScore, englishScore, computerScore);
cout << "修改成功!" << endl;
} else if (option == 5) {
manager.writeToFile("students.txt");
cout << "保存成功!" << endl;
} else if (option == 6) {
break;
} else {
cout << "输入有误,请重新输入!" << endl;
}
}
return 0;
}
```
该程序中,Student类表示学生,包含学号、姓名、数学成绩、英语成绩、计算机课成绩等信息,并封装了计算总成绩、平均成绩、输出学生信息、将学生信息写入文件等方法;StudentManager类表示学生管理器,包含了添加、删除、查询、修改、从文件中读取、将学生信息写入文件等方法。主函数中通过循环实现了不断选择操作的功能。
阅读全文