devc++学生信息管理系统
时间: 2023-11-28 13:45:02 浏览: 176
Dev-C++是一个免费的C++ IDE,可以用于开发各种类型的C++应用程序,包括学生信息管理系统。下面是一个简单的学生信息管理系统的示例:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
// 定义学生结构体
struct Student {
string name;
int age;
string gender;
string id;
double score;
};
// 定义学生信息管理类
class StudentManager {
public:
// 添加学生信息
void addStudent(Student student) {
students.push_back(student);
}
// 根据学号删除学生信息
void deleteStudent(string id) {
for (vector<Student>::iterator it = students.begin(); it != students.end(); it++) {
if (it->id == id) {
students.erase(it);
break;
}
}
}
// 修改学生信息
void modifyStudent(string id, Student student) {
for (vector<Student>::iterator it = students.begin(); it != students.end(); it++) {
if (it->id == id) {
*it = student;
break;
}
}
}
// 根据学号查询学生信息
Student queryStudent(string id) {
for (vector<Student>::iterator it = students.begin(); it != students.end(); it++) {
if (it->id == id) {
return *it;
}
}
Student student;
return student;
}
// 显示所有学生信息
void showAllStudents() {
for (vector<Student>::iterator it = students.begin(); it != students.end(); it++) {
cout << "姓名:" << it->name << ",年龄:" << it->age << ",性别:" << it->gender << ",学号:" << it->id << ",成绩:" << it->score << endl;
}
}
// 保存学生信息到文件
void saveToFile(string filename) {
ofstream outfile(filename);
for (vector<Student>::iterator it = students.begin(); it != students.end(); it++) {
outfile << it->name << "," << it->age << "," << it->gender << "," << it->id << "," << it->score << endl;
}
outfile.close();
}
// 从文件中读取学生信息
void loadFromFile(string filename) {
ifstream infile(filename);
string line;
while (getline(infile, line)) {
Student student;
int pos = line.find(",");
student.name = line.substr(0, pos);
line = line.substr(pos + 1);
pos = line.find(",");
student.age = stoi(line.substr(0, pos));
line = line.substr(pos + 1);
pos = line.find(",");
student.gender = line.substr(0, pos);
line = line.substr(pos + 1);
pos = line.find(",");
student.id = line.substr(0, pos);
line = line.substr(pos + 1);
student.score = stod(line);
students.push_back(student);
}
infile.close();
}
private:
vector<Student> students;
};
int main() {
StudentManager manager;
manager.loadFromFile("students.txt");
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;
int choice;
cin >> choice;
if (choice == 1) {
Student student;
cout << "请输入学生姓名:";
cin >> student.name;
cout << "请输入学生年龄:";
cin >> student.age;
cout << "请输入学生性别:";
cin >> student.gender;
cout << "请输入学生学号:";
cin >> student.id;
cout << "请输入学生成绩:";
cin >> student.score;
manager.addStudent(student);
cout << "添加成功!" << endl;
} else if (choice == 2) {
string id;
cout << "请输入要删除的学生学号:";
cin >> id;
manager.deleteStudent(id);
cout << "删除成功!" << endl;
} else if (choice == 3) {
string id;
cout << "请输入要修改的学生学号:";
cin >> id;
Student student;
cout << "请输入学生姓名:";
cin >> student.name;
cout << "请输入学生年龄:";
cin >> student.age;
cout << "请输入学生性别:";
cin >> student.gender;
cout << "请输入学生成绩:";
cin >> student.score;
manager.modifyStudent(id, student);
cout << "修改成功!" << endl;
} else if (choice == 4) {
string id;
cout << "请输入要查询的学生学号:";
cin >> id;
Student student = manager.queryStudent(id);
if (student.id == "") {
cout << "未找到该学生!" << endl;
} else {
cout << "姓名:" << student.name << ",年龄:" << student.age << ",性别:" << student.gender << ",学号:" << student.id << ",成绩:" << student.score << endl;
}
} else if (choice == 5) {
manager.showAllStudents();
} else if (choice == 6) {
manager.saveToFile("students.txt");
cout << "保存成功!" << endl;
} else if (choice == 7) {
break;
} else {
cout << "输入有误,请重新输入!" << endl;
}
}
return 0;
}
```
阅读全文