学院教学信息管理系统(c++)

每一条记录包括一位教师的职工号、姓名、职称、性别、3门课程,教学效果综合评分。

系统实现了以下功能:

1、输入:输入每一位教师记录,将其信息写入文件中。

2、显示:显示每位教师记录。

3、排序:按职工号或教学效果综合评分进行排序,并显示。

4、查找:完成按姓名或课程查找教师的相关记录,并显示。

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

// 定义教师结构体
struct Teacher {
    int id; //职工号
    string name; //姓名
    string title; //职称
    char gender; //性别
    string courses[3]; //课程
    double teachingEffectiveness; //教学效果综合评分

    // 显示教师信息,声明为 const 成员函数
    void display() const {
        cout << "教师信息:" << endl;
        cout << "职工号: " << id << endl;
        cout << "姓名: " << name << endl;
        cout << "职称: " << title << endl;
        cout << "性别: " << gender << endl;
        cout << "课程: ";
        for (int i = 0; i < 3; ++i) {
            cout << courses[i] << " ";
        }
        cout << endl;
        cout << "教学效果综合评分: " << teachingEffectiveness << endl;
        cout << "-----------------------------" << endl;
    }
};


// 比较函数,用于排序
bool compareById(const Teacher& a, const Teacher& b) {
    return a.id < b.id;
}

bool compareByEffectiveness(const Teacher& a, const Teacher& b) {
    return a.teachingEffectiveness > b.teachingEffectiveness;
}

// 函数声明
void writeToFile(const vector<Teacher>& teachers, const string& filename);
void readFromFile(vector<Teacher>& teachers, const string& filename);
void displayTeachers(const vector<Teacher>& teachers);
void sortByOption(vector<Teacher>& teachers, int option);
void searchTeachers(const vector<Teacher>& teachers, const string& key);

int main() {
    vector<Teacher> teachers;
    string filename = "teachers.txt"; //文件 teachers.txt 将保存在程序的当前工作目录中
    int choice;

    // 读取文件中的数据
    readFromFile(teachers, filename);

    while (true) {
        cout << "欢迎使用学院教学信息管理系统:" << endl;
        cout << "1. 输入教师记录" << endl;
        cout << "2. 显示所有教师记录" << endl;
        cout << "3. 按职工号排序并显示" << endl;
        cout << "4. 按教学效果评分排序并显示" << endl;
        cout << "5. 按姓名或课程查找教师记录" << endl;
        cout << "0. 退出系统" << endl;
        cout << "请选择操作:";
        cin >> choice;

        switch (choice) {
            case 1: {
                Teacher newTeacher;
                cout << "请输入教师信息:" << endl;
                cout << "职工号: ";
                cin >> newTeacher.id;
                cout << "姓名: ";
                cin.ignore(); // 忽略之前的回车符
                getline(cin, newTeacher.name);
                cout << "职称: ";
                getline(cin, newTeacher.title);
                cout << "性别 (M/F): ";
                cin >> newTeacher.gender;
                cout << "输入3门课程名称:" << endl;
                for (int i = 0; i < 3; ++i) {
                    cout << "课程 " << i + 1 << ": ";
                    cin >> newTeacher.courses[i];
                }
                cout << "教学效果综合评分: ";
                cin >> newTeacher.teachingEffectiveness;
                teachers.push_back(newTeacher);

                // 写入文件
                writeToFile(teachers, filename);
                break;
            }
            case 2:
                displayTeachers(teachers);
                break;
            case 3:
                sortByOption(teachers, 1); // 1 表示按职工号排序
                displayTeachers(teachers);
                break;
            case 4:
                sortByOption(teachers, 2); // 2 表示按教学效果评分排序
                displayTeachers(teachers);
                break;
            case 5: {
                string key;
                cout << "请输入要查找的姓名或课程名称:";
                cin.ignore(); // 忽略之前的回车符
                getline(cin, key);
                searchTeachers(teachers, key);
                break;
            }
            case 0:
                cout << "感谢使用!再见!" << endl;
                return 0;
            default:
                cout << "无效的选项,请重新选择。" << endl;
                break;
        }
    }

    return 0;
}

// 将教师信息写入文件
void writeToFile(const vector<Teacher>& teachers, const string& filename) {
    ofstream outFile(filename);
    if (outFile.is_open()) {
        for (const auto& teacher : teachers) {
            outFile << teacher.id << "," << teacher.name << "," << teacher.title << ","
                    << teacher.gender << "," << teacher.courses[0] << "," << teacher.courses[1] << ","
                    << teacher.courses[2] << "," << teacher.teachingEffectiveness << endl;
        }
        outFile.close();
        cout << "数据已成功写入文件 " << filename << endl;
    } else {
        cout << "无法打开文件 " << filename << " 进行写入。" << endl;
    }
}

// 从文件中读取教师信息
void readFromFile(vector<Teacher>& teachers, const string& filename) {
    ifstream inFile(filename);
    if (inFile.is_open()) {
        teachers.clear();
        Teacher teacher;
        char comma;
        while (inFile >> teacher.id >> comma >> teacher.name >> comma >> teacher.title >> comma
                      >> teacher.gender >> comma >> teacher.courses[0] >> comma >> teacher.courses[1] >> comma
                      >> teacher.courses[2] >> comma >> teacher.teachingEffectiveness) {
            teachers.push_back(teacher);
        }
        inFile.close();
        cout << "成功从文件 " << filename << " 中读取数据。" << endl;
    } else {
        cout << "无法打开文件 " << filename << " 进行读取。" << endl;
    }
}

// 显示所有教师记录
void displayTeachers(const vector<Teacher>& teachers) {
    if (teachers.empty()) {
        cout << "暂无教师记录。" << endl;
    } else {
        for (const auto& teacher : teachers) {
            teacher.display();
        }
    }
}

// 按选项排序教师信息
void sortByOption(vector<Teacher>& teachers, int option) {
    switch (option) {
        case 1:
            sort(teachers.begin(), teachers.end(), compareById);
            cout << "按职工号排序结果:" << endl;
            break;
        case 2:
            sort(teachers.begin(), teachers.end(), compareByEffectiveness);
            cout << "按教学效果评分排序结果:" << endl;
            break;
        default:
            cout << "无效的排序选项。" << endl;
            return;
    }
}

// 按姓名或课程查找教师记录
void searchTeachers(const vector<Teacher>& teachers, const string& key) {
    bool found = false;
    for (const auto& teacher : teachers) {
        if (teacher.name == key) {
            teacher.display();
            found = true;
        } else {
            for (int i = 0; i < 3; ++i) {
                if (teacher.courses[i] == key) {
                    teacher.display();
                    found = true;
                    break;
                }
            }
        }
    }
    if (!found) {
        cout << "未找到与 " << key << " 相关的教师记录。" << endl;
    }
}
相关推荐
bryant_meng15 分钟前
【python】OpenCV—Image Moments
开发语言·python·opencv·moments·图片矩
若亦_Royi39 分钟前
C++ 的大括号的用法合集
开发语言·c++
资源补给站2 小时前
大恒相机开发(2)—Python软触发调用采集图像
开发语言·python·数码相机
m0_748247552 小时前
Web 应用项目开发全流程解析与实战经验分享
开发语言·前端·php
6.942 小时前
Scala学习记录 递归调用 练习
开发语言·学习·scala
Aileen_0v02 小时前
【AI驱动的数据结构:包装类的艺术与科学】
linux·数据结构·人工智能·笔记·网络协议·tcp/ip·whisper
是小胡嘛2 小时前
数据结构之旅:红黑树如何驱动 Set 和 Map
数据结构·算法
FF在路上3 小时前
Knife4j调试实体类传参扁平化模式修改:default-flat-param-object: true
java·开发语言
众拾达人3 小时前
Android自动化测试实战 Java篇 主流工具 框架 脚本
android·java·开发语言
皓木.3 小时前
Mybatis-Plus
java·开发语言