一、方案概述
1.教师信息数据结构
教师信息被定义为一个包含以下字段的结构体:
- 职工号
- 姓名
- 职称
- 性别
- 三门课程
- 教学效果综合评分
2.功能模块
以下是教学信息管理系统的主要功能模块:
3.输入教师信息
编写函数用于输入每位教师的信息,并将其写入文件中。
4.显示教师信息
编写函数用于读取文件中的教师记录,并在屏幕上显示每位教师的信息。
5.排序
编写函数用于按照职工号或教学效果综合评分对教师记录进行排序,并在屏幕上显示排序结果。
6.查找
编写函数用于按照姓名或课程查找相关教师的记录,并在屏幕上显示查找结果。
二、 实施步骤
- 总体设计:根据功能需求,逐步完成总体设计,搭建程序框架,并确定人机对话界面,确定所需的函数个数。
- 文件操作:建立一个文件,将每位教师的教学信息写入文件,并能从文件中读取并显示于屏幕上。
- 界面设计:确保系统界面友好,与用户进行良好的人机交互。可以使用适当的用户提示和菜单选择来提高用户体验。
- 程序注释:在代码中添加必要的注释,使其易于理解和维护。
三、部分代码
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
using namespace std;
struct Teacher {
int work_id;
string name;
string title;
string gender;
string course[3];
double score;
};
void input_teacher_info() {
Teacher new_teacher;
cout << "请输入职工号:";
cin >> new_teacher.work_id;
cout << "请输入姓名:";
cin >> new_teacher.name;
cout << "请输入职称:";
cin >> new_teacher.title;
cout << "请输入性别:";
cin >> new_teacher.gender;
cout << "请输入三门课程:";
for (int i = 0; i < 3; i++) {
cin >> new_teacher.course[i];
}
cout << "请输入教学效果综合评分:";
cin >> new_teacher.score;
teacher_list[teacher_num++] = new_teacher;
ofstream out_file("teacher_info.txt", ios::app);
out_file << new_teacher.work_id << " " << new_teacher.name << " " << new_teacher.title << " " << new_teacher.gender << " ";
for (int i = 0; i < 3; i++) {
out_file << new_teacher.course[i] << " ";
}
out_file << new_teacher.score << endl;
out_file.close();
}
bool cmp_by_work_id(Teacher a, Teacher b) {
return a.work_id < b.work_id;
}
bool cmp_by_score(Teacher a, Teacher b) {
return a.score > b.score;
}
void sort_teacher_info() {
int option;
cout << "请选择排序方式(1.按职工号排序,2.按教学效果综合评分排序):";
cin >> option;
if (option == 1) {
sort(teacher_list, teacher_list + teacher_num, cmp_by_work_id);
}
else if (option == 2) {
sort(teacher_list, teacher_list + teacher_num, cmp_by_score);
}
else {
cout << "无效的选项,请重试。" << endl;
return;
}
cout << "排序结果:" << endl;
for (int i = 0; i < teacher_num; i++) {
cout << "职工号:" << teacher_list[i].work_id << ",姓名:" << teacher_list[i].name << endl;
}
}
void display_teacher_info() {
ifstream in_file("teacher_info.txt");
if (!in_file) {
cout << "文件打开失败。" << endl;
return;
}
int work_id;
double score;
while (in_file >> work_id >> name >> title >> gender) {
for (int i = 0; i < 3; i++) {
in_file >> course[i];
}
in_file >> score;
cout << "职工号:" << work_id << ",姓名:" << name << ",教学效果综合评分:" << score << endl;
}
in_file.close();
}
void search_teacher_info() {
int option;
cout << "请选择查找方式(1.按姓名查找,2.按课程查找):";
cin >> option;
if (option == 1) {
string name;
cout << "请输入要查找的姓名:";
cin >> name;
bool found = false;
for (int i = 0; i < teacher_num; i++) {
if (teacher_list[i].name == name) {
found = true;
cout << "职工号:" << teacher_list[i].work_id << endl;
}
}
if (!found) {
cout << "未找到相关记录。" << endl;
}
}
else if (option == 2) {
string course;
cout << "请输入要查找的课程:";
cin >> course;
bool found = false;
for (int i = 0; i < teacher_num; i++) {
for (int j = 0; j < 3; j++) {
if (teacher_list[i].course[j] == course) {
found = true;
cout << "职工号:" << teacher_list[i].work_id << ",姓名:" << teacher_list[i].name << endl;
break;
}
}
}
}
}
else {
cout << "无效的选项,请重试。" << endl;
return;
}
}
int main() {
while (true) {
cout << "请选择操作:1.输入教师信息,2.显示教师信息,3.排序,4.查找,0.退出程序。" << endl;
int option;
cin >> option;
switch (option) {
case 1:
input_teacher_info();
break;
case 2:
display_teacher_info();
break;
case 3:
sort_teacher_info();
break;
case 4:
search_teacher_info();
break;
case 0:
return 0;
default:
cout << "无效的选项,请重试。" << endl;
break;
}
}
return 0;
}
四、运行截图