用c++编写一个能保存学生信息的学生信息管理系统
时间: 2023-09-04 14:15:32 浏览: 105
用C++编写的学生信息管理系统
好的,以下是一个简单的学生信息管理系统的代码实现,包括添加、删除、修改和查询学生信息的功能:
```c++
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
// 学生信息结构体
struct Student {
string name; // 姓名
int age; // 年龄
string gender; // 性别
string id; // 学号
float score; // 分数
};
// 学生信息管理类
class StudentManager {
public:
// 添加学生信息
void addStudent() {
Student student;
cout << "请输入学生信息:" << endl;
cout << "姓名:";
cin >> student.name;
cout << "年龄:";
cin >> student.age;
cout << "性别:";
cin >> student.gender;
cout << "学号:";
cin >> student.id;
cout << "分数:";
cin >> student.score;
students.push_back(student);
saveToFile();
}
// 删除学生信息
void deleteStudent() {
string id;
cout << "请输入要删除的学生学号:";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
students.erase(students.begin() + i);
saveToFile();
cout << "删除成功!" << endl;
return;
}
}
cout << "该学生不存在!" << endl;
}
// 修改学生信息
void modifyStudent() {
string id;
cout << "请输入要修改的学生学号:";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
cout << "请输入修改后的学生信息:" << endl;
cout << "姓名:";
cin >> students[i].name;
cout << "年龄:";
cin >> students[i].age;
cout << "性别:";
cin >> students[i].gender;
cout << "学号:";
cin >> students[i].id;
cout << "分数:";
cin >> students[i].score;
saveToFile();
cout << "修改成功!" << endl;
return;
}
}
cout << "该学生不存在!" << endl;
}
// 查询学生信息
void queryStudent() {
string id;
cout << "请输入要查询的学生学号:";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
cout << "学生信息如下:" << endl;
cout << "姓名:" << students[i].name << endl;
cout << "年龄:" << students[i].age << endl;
cout << "性别:" << students[i].gender << endl;
cout << "学号:" << students[i].id << endl;
cout << "分数:" << students[i].score << endl;
return;
}
}
cout << "该学生不存在!" << endl;
}
private:
vector<Student> students; // 学生信息数组
// 保存学生信息到文件
void saveToFile() {
ofstream fout("students.txt");
for (int i = 0; i < students.size(); i++) {
fout << students[i].name << " ";
fout << students[i].age << " ";
fout << students[i].gender << " ";
fout << students[i].id << " ";
fout << students[i].score << endl;
}
fout.close();
}
// 从文件中读取学生信息
void loadFromFile() {
ifstream fin("students.txt");
while (!fin.eof()) {
Student student;
fin >> student.name;
fin >> student.age;
fin >> student.gender;
fin >> student.id;
fin >> student.score;
students.push_back(student);
}
fin.close();
}
public:
// 构造函数,从文件中读取学生信息
StudentManager() {
loadFromFile();
}
// 析构函数,保存学生信息到文件
~StudentManager() {
saveToFile();
}
};
int main() {
StudentManager manager;
while (true) {
int choice;
cout << "请选择功能:" << endl;
cout << "1.添加学生信息" << endl;
cout << "2.删除学生信息" << endl;
cout << "3.修改学生信息" << endl;
cout << "4.查询学生信息" << endl;
cout << "0.退出系统" << endl;
cin >> choice;
switch (choice) {
case 1:
manager.addStudent();
break;
case 2:
manager.deleteStudent();
break;
case 3:
manager.modifyStudent();
break;
case 4:
manager.queryStudent();
break;
case 0:
return 0;
default:
cout << "输入错误,请重新选择!" << endl;
break;
}
}
return 0;
}
```
该代码实现了一个简单的学生信息管理系统,包括添加、删除、修改和查询学生信息的功能,并将学生信息保存到了文件中。
阅读全文