学院教学信息管理系统(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;
    }
}
相关推荐
花海少爷1 分钟前
第十章 JavaScript的应用课后习题
开发语言·javascript·ecmascript
手握风云-1 分钟前
数据结构(Java版)第二期:包装类和泛型
java·开发语言·数据结构
喵叔哟21 分钟前
重构代码中引入外部方法和引入本地扩展的区别
java·开发语言·重构
尘浮生27 分钟前
Java项目实战II基于微信小程序的电影院买票选座系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
hopetomorrow41 分钟前
学习路之PHP--使用GROUP BY 发生错误 SELECT list is not in GROUP BY clause .......... 解决
开发语言·学习·php
小牛itbull1 小时前
ReactPress vs VuePress vs WordPress
开发语言·javascript·reactpress
怀澈1221 小时前
高性能服务器模型之Reactor(单线程版本)
linux·服务器·网络·c++
请叫我欧皇i1 小时前
html本地离线引入vant和vue2(详细步骤)
开发语言·前端·javascript
闲暇部落1 小时前
‌Kotlin中的?.和!!主要区别
android·开发语言·kotlin
GIS瞧葩菜1 小时前
局部修改3dtiles子模型的位置。
开发语言·javascript·ecmascript