用C++语言面向对象的编程思维写学生成绩管理系统,功能包括学生信息的添加、查找、删除、导出、显示、保存等,用简单易懂的代码编写
时间: 2023-07-10 10:13:29 浏览: 73
好的,下面是一个简单易懂的 C++ 学生成绩管理系统的代码:
```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
// 学生类
class Student {
public:
Student(string name, string id, double score) {
this->name = name;
this->id = id;
this->score = score;
}
string getName() const { return name; }
string getId() const { return id; }
double getScore() const { return score; }
private:
string name;
string id;
double score;
};
// 学生管理类
class StudentManager {
public:
// 添加学生
void addStudent(Student student) {
students.push_back(student);
}
// 查找学生
vector<Student> findStudent(string id) {
vector<Student> foundStudents;
for (auto student : students) {
if (student.getId() == id) {
foundStudents.push_back(student);
}
}
return foundStudents;
}
// 删除学生
void deleteStudent(string id) {
for (auto it = students.begin(); it != students.end(); ) {
if (it->getId() == id) {
it = students.erase(it);
} else {
++it;
}
}
}
// 导出学生信息
void exportStudentInfo(string filename) {
ofstream fout(filename);
for (auto student : students) {
fout << student.getName() << " " << student.getId() << " " << student.getScore() << endl;
}
fout.close();
}
// 显示学生信息
void showStudentInfo() {
for (auto student : students) {
cout << student.getName() << " " << student.getId() << " " << student.getScore() << endl;
}
}
// 保存学生信息
void saveStudentInfo(string filename) {
ofstream fout(filename);
for (auto student : students) {
fout << student.getName() << " " << student.getId() << " " << student.getScore() << endl;
}
fout.close();
}
private:
vector<Student> students;
};
// 主函数
int main() {
StudentManager manager;
// 添加学生
manager.addStudent(Student("张三", "20190101", 90.5));
manager.addStudent(Student("李四", "20190102", 85.0));
manager.addStudent(Student("王五", "20190103", 92.0));
// 查找学生
vector<Student> foundStudents = manager.findStudent("20190102");
if (!foundStudents.empty()) {
cout << "查找到的学生信息:" << endl;
for (auto student : foundStudents) {
cout << student.getName() << " " << student.getId() << " " << student.getScore() << endl;
}
} else {
cout << "未找到该学生信息!" << endl;
}
// 删除学生
manager.deleteStudent("20190103");
// 导出学生信息
manager.exportStudentInfo("students.txt");
// 显示学生信息
manager.showStudentInfo();
// 保存学生信息
manager.saveStudentInfo("students.txt");
return 0;
}
```
以上代码实现了学生成绩管理系统的基本功能,可以添加、查找、删除、导出、显示、保存学生信息。注意,上述代码中并没有进行输入数据的处理,你需要自己添加相应的代码实现。
阅读全文