用c++写一个高校师生信息管理系统,功能包括添加,查询,显示,编辑,删除,统计,保存,读取
时间: 2023-10-01 17:09:52 浏览: 166
好的,以下是一个简单的高校师生信息管理系统的代码示例:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Student {
string name;
int age;
string major;
int id;
};
struct Teacher {
string name;
int age;
string department;
int id;
};
// 添加学生信息
void add_student(vector<Student>& students) {
Student s;
cout << "请输入学生姓名:";
cin >> s.name;
cout << "请输入学生年龄:";
cin >> s.age;
cout << "请输入学生专业:";
cin >> s.major;
cout << "请输入学生学号:";
cin >> s.id;
students.push_back(s);
cout << "添加成功!" << endl;
}
// 添加教师信息
void add_teacher(vector<Teacher>& teachers) {
Teacher t;
cout << "请输入教师姓名:";
cin >> t.name;
cout << "请输入教师年龄:";
cin >> t.age;
cout << "请输入教师所在院系:";
cin >> t.department;
cout << "请输入教师工号:";
cin >> t.id;
teachers.push_back(t);
cout << "添加成功!" << endl;
}
// 查询学生信息
void search_student(const vector<Student>& students) {
int id;
cout << "请输入要查询的学生学号:";
cin >> id;
for (const auto& s : students) {
if (s.id == id) {
cout << "姓名:" << s.name << endl;
cout << "年龄:" << s.age << endl;
cout << "专业:" << s.major << endl;
cout << "学号:" << s.id << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
// 查询教师信息
void search_teacher(const vector<Teacher>& teachers) {
int id;
cout << "请输入要查询的教师工号:";
cin >> id;
for (const auto& t : teachers) {
if (t.id == id) {
cout << "姓名:" << t.name << endl;
cout << "年龄:" << t.age << endl;
cout << "所在院系:" << t.department << endl;
cout << "工号:" << t.id << endl;
return;
}
}
cout << "未找到该教师!" << endl;
}
// 显示所有学生信息
void show_students(const vector<Student>& students) {
for (const auto& s : students) {
cout << "姓名:" << s.name << endl;
cout << "年龄:" << s.age << endl;
cout << "专业:" << s.major << endl;
cout << "学号:" << s.id << endl;
cout << endl;
}
}
// 显示所有教师信息
void show_teachers(const vector<Teacher>& teachers) {
for (const auto& t : teachers) {
cout << "姓名:" << t.name << endl;
cout << "年龄:" << t.age << endl;
cout << "所在院系:" << t.department << endl;
cout << "工号:" << t.id << endl;
cout << endl;
}
}
// 编辑学生信息
void edit_student(vector<Student>& students) {
int id;
cout << "请输入要编辑的学生学号:";
cin >> id;
for (auto& s : students) {
if (s.id == id) {
cout << "请输入学生姓名:";
cin >> s.name;
cout << "请输入学生年龄:";
cin >> s.age;
cout << "请输入学生专业:";
cin >> s.major;
cout << "编辑成功!" << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
// 编辑教师信息
void edit_teacher(vector<Teacher>& teachers) {
int id;
cout << "请输入要编辑的教师工号:";
cin >> id;
for (auto& t : teachers) {
if (t.id == id) {
cout << "请输入教师姓名:";
cin >> t.name;
cout << "请输入教师年龄:";
cin >> t.age;
cout << "请输入教师所在院系:";
cin >> t.department;
cout << "编辑成功!" << endl;
return;
}
}
cout << "未找到该教师!" << endl;
}
// 删除学生信息
void delete_student(vector<Student>& students) {
int id;
cout << "请输入要删除的学生学号:";
cin >> id;
for (auto it = students.begin(); it != students.end(); ++it) {
if (it->id == id) {
students.erase(it);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
// 删除教师信息
void delete_teacher(vector<Teacher>& teachers) {
int id;
cout << "请输入要删除的教师工号:";
cin >> id;
for (auto it = teachers.begin(); it != teachers.end(); ++it) {
if (it->id == id) {
teachers.erase(it);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该教师!" << endl;
}
// 统计学生人数和教师人数
void count(const vector<Student>& students, const vector<Teacher>& teachers) {
cout << "学生人数:" << students.size() << endl;
cout << "教师人数:" << teachers.size() << endl;
}
// 保存信息到文件
void save(const vector<Student>& students, const vector<Teacher>& teachers) {
ofstream fout("info.txt");
if (!fout) {
cout << "文件打开失败!" << endl;
return;
}
for (const auto& s : students) {
fout << "S " << s.name << " " << s.age << " " << s.major << " " << s.id << endl;
}
for (const auto& t : teachers) {
fout << "T " << t.name << " " << t.age << " " << t.department << " " << t.id << endl;
}
fout.close();
cout << "保存成功!" << endl;
}
// 从文件读取信息
void load(vector<Student>& students, vector<Teacher>& teachers) {
ifstream fin("info.txt");
if (!fin) {
cout << "文件打开失败!" << endl;
return;
}
string type, name, major, department;
int age, id;
while (fin >> type >> name >> age >> major >> id) {
if (type == "S") {
Student s = {name, age, major, id};
students.push_back(s);
} else if (type == "T") {
Teacher t = {name, age, major, id};
teachers.push_back(t);
}
}
fin.close();
cout << "读取成功!" << endl;
}
int main() {
vector<Student> students;
vector<Teacher> teachers;
int choice;
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 << "10. 删除教师信息" << endl;
cout << "11. 统计学生人数和教师人数" << endl;
cout << "12. 保存信息到文件" << endl;
cout << "13. 从文件读取信息" << endl;
cout << "0. 退出程序" << endl;
cout << "=====================================" << endl;
cout << "请输入您的选择:";
cin >> choice;
switch (choice) {
case 1:
add_student(students);
break;
case 2:
add_teacher(teachers);
break;
case 3:
search_student(students);
break;
case 4:
search_teacher(teachers);
break;
case 5:
show_students(students);
break;
case 6:
show_teachers(teachers);
break;
case 7:
edit_student(students);
break;
case 8:
edit_teacher(teachers);
break;
case 9:
delete_student(students);
break;
case 10:
delete_teacher(teachers);
break;
case 11:
count(students, teachers);
break;
case 12:
save(students, teachers);
break;
case 13:
load(students, teachers);
break;
case 0:
return 0;
default:
cout << "输入有误,请重新输入!" << endl;
}
}
}
```
这个代码示例实现了一个简单的高校师生信息管理系统,包括添加、查询、显示、编辑、删除、统计、保存和读取等功能。用户可以通过命令行菜单进行操作,所有信息都保存在内存中,并可以保存到文件中。注意,这个示例代码还有很多可以改进的地方,如增加输入检查、优化文件读写等。
阅读全文