用c++写一个学校师生信息管理系统,包含添加,查询,显示,编辑,删除,统计,保存,读取功能
时间: 2023-07-31 16:06:15 浏览: 131
学生信息管理程序课程设计
好的,我可以为你提供一个基本的学校师生信息管理系统的实现框架。具体的实现需要根据你的具体需求进行调整和完善。
```cpp
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int MAX_SIZE = 100; // 最大记录数
// 记录结构体
struct Record {
char name[20]; // 姓名
char id[10]; // 学号或工号
char gender[5]; // 性别
char department[20]; // 部门或系别
char phone[20]; // 电话号码
};
// 全局变量
Record records[MAX_SIZE]; // 记录数组
int size = 0; // 记录数
// 函数声明
void addRecord(); // 添加记录
void queryRecord(); // 查询记录
void showRecords(); // 显示所有记录
void editRecord(); // 编辑记录
void deleteRecord(); // 删除记录
void saveToFile(); // 保存记录到文件
void readFromFile(); // 从文件中读取记录
int main() {
int choice;
readFromFile(); // 读取之前保存的记录
do {
cout << "\n==== 学校师生信息管理系统 ====\n";
cout << "1. 添加记录\n";
cout << "2. 查询记录\n";
cout << "3. 显示所有记录\n";
cout << "4. 编辑记录\n";
cout << "5. 删除记录\n";
cout << "6. 统计记录\n";
cout << "7. 保存记录到文件\n";
cout << "8. 从文件中读取记录\n";
cout << "0. 退出\n";
cout << "请选择功能:";
cin >> choice;
switch (choice) {
case 1:
addRecord();
break;
case 2:
queryRecord();
break;
case 3:
showRecords();
break;
case 4:
editRecord();
break;
case 5:
deleteRecord();
break;
case 6:
// TODO: 统计记录
break;
case 7:
saveToFile();
break;
case 8:
readFromFile();
break;
case 0:
break;
default:
cout << "输入错误,请重新输入!\n";
break;
}
} while (choice != 0);
return 0;
}
// 添加记录
void addRecord() {
if (size >= MAX_SIZE) {
cout << "记录已满,无法添加!\n";
return;
}
Record record;
cout << "请输入姓名:";
cin >> record.name;
cout << "请输入学号或工号:";
cin >> record.id;
cout << "请输入性别:";
cin >> record.gender;
cout << "请输入部门或系别:";
cin >> record.department;
cout << "请输入电话号码:";
cin >> record.phone;
records[size++] = record;
cout << "添加成功!\n";
}
// 查询记录
void queryRecord() {
char keyword[20];
cout << "请输入查询关键字:";
cin >> keyword;
bool found = false;
for (int i = 0; i < size; i++) {
if (strstr(records[i].name, keyword) != NULL ||
strstr(records[i].id, keyword) != NULL ||
strstr(records[i].gender, keyword) != NULL ||
strstr(records[i].department, keyword) != NULL ||
strstr(records[i].phone, keyword) != NULL) {
cout << "姓名:" << records[i].name << endl;
cout << "学号或工号:" << records[i].id << endl;
cout << "性别:" << records[i].gender << endl;
cout << "部门或系别:" << records[i].department << endl;
cout << "电话号码:" << records[i].phone << endl;
found = true;
}
}
if (!found) {
cout << "没有找到匹配的记录!\n";
}
}
// 显示所有记录
void showRecords() {
if (size == 0) {
cout << "没有记录!\n";
return;
}
for (int i = 0; i < size; i++) {
cout << "姓名:" << records[i].name << endl;
cout << "学号或工号:" << records[i].id << endl;
cout << "性别:" << records[i].gender << endl;
cout << "部门或系别:" << records[i].department << endl;
cout << "电话号码:" << records[i].phone << endl;
}
}
// 编辑记录
void editRecord() {
char id[10];
cout << "请输入要编辑的学号或工号:";
cin >> id;
int index = -1;
for (int i = 0; i < size; i++) {
if (strcmp(records[i].id, id) == 0) {
index = i;
break;
}
}
if (index == -1) {
cout << "没有找到匹配的记录!\n";
return;
}
cout << "请输入新的姓名(原值为" << records[index].name << "):";
cin >> records[index].name;
cout << "请输入新的学号或工号(原值为" << records[index].id << "):";
cin >> records[index].id;
cout << "请输入新的性别(原值为" << records[index].gender << "):";
cin >> records[index].gender;
cout << "请输入新的部门或系别(原值为" << records[index].department << "):";
cin >> records[index].department;
cout << "请输入新的电话号码(原值为" << records[index].phone << "):";
cin >> records[index].phone;
cout << "编辑成功!\n";
}
// 删除记录
void deleteRecord() {
char id[10];
cout << "请输入要删除的学号或工号:";
cin >> id;
int index = -1;
for (int i = 0; i < size; i++) {
if (strcmp(records[i].id, id) == 0) {
index = i;
break;
}
}
if (index == -1) {
cout << "没有找到匹配的记录!\n";
return;
}
for (int i = index; i < size - 1; i++) {
records[i] = records[i + 1];
}
size--;
cout << "删除成功!\n";
}
// 保存记录到文件
void saveToFile() {
ofstream fout("records.txt");
if (!fout) {
cout << "打开文件失败!\n";
return;
}
for (int i = 0; i < size; i++) {
fout << records[i].name << "," << records[i].id << ","
<< records[i].gender << "," << records[i].department << ","
<< records[i].phone << endl;
}
fout.close();
cout << "保存成功!\n";
}
// 从文件中读取记录
void readFromFile() {
ifstream fin("records.txt");
if (!fin) {
cout << "打开文件失败!\n";
return;
}
size = 0;
while (fin && size < MAX_SIZE) {
fin.getline(records[size].name, 20, ',');
fin.getline(records[size].id, 10, ',');
fin.getline(records[size].gender, 5, ',');
fin.getline(records[size].department, 20, ',');
fin.getline(records[size].phone, 20);
size++;
}
fin.close();
cout << "读取成功!\n";
}
```
这个程序包含了添加、查询、显示、编辑、删除、统计、保存和读取记录等功能。你可以根据需要进行修改和完善,比如增加统计功能、改进查询算法等等。同时,你也需要自己编写一个菜单来调用这些功能。
阅读全文