1. Program overview
1. Teacher information data structure
Teacher information is defined as a structure with the following fields:
- employee number
- Name
- job title
- gender
- three courses
- Comprehensive score of teaching effect
2. Functional modules
The following are the main functional modules of the teaching information management system:
3. Enter teacher information
Write a function that takes each teacher's information and writes it to a file.
4. Display teacher information
Write a function that reads the teacher records from a file and displays each teacher's information on the screen.
5. Sort
Write a function to sort teacher records according to employee number or comprehensive score of teaching effect, and display the sorting results on the screen.
6. Find
Write a function to find records for relevant teachers by name or course and display the results on the screen.
2. Implementation steps
- Overall design: According to the functional requirements, gradually complete the overall design, build the program framework, determine the man-machine dialogue interface, and determine the number of functions required.
- File operation: create a file, write the teaching information of each teacher into the file, read it from the file and display it on the screen.
- Interface design: ensure that the system interface is friendly and has good human-computer interaction with users. Appropriate user prompts and menu selections can be used to enhance the user experience.
- Program Comments: Add necessary comments to the code to make it easy to understand and maintain.
3. Part of the code
#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;
}
4. Run the screenshot