学院教学信息管理系统(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;
    }
}
相关推荐
小屁孩大帅-杨一凡30 分钟前
java后端请求想接收多个对象入参的数据
java·开发语言
m0_6569747435 分钟前
C#中的集合类及其使用
开发语言·c#
java1234_小锋37 分钟前
使用 RabbitMQ 有什么好处?
java·开发语言
wjs20241 小时前
R 数据框
开发语言
幺零九零零1 小时前
【C++】socket套接字编程
linux·服务器·网络·c++
肘击鸣的百k路1 小时前
Java 代理模式详解
java·开发语言·代理模式
捕鲸叉1 小时前
MVC(Model-View-Controller)模式概述
开发语言·c++·设计模式
wrx繁星点点1 小时前
享元模式:高效管理共享对象的设计模式
java·开发语言·spring·设计模式·maven·intellij-idea·享元模式
真的想不出名儿1 小时前
Java基础——反射
java·开发语言
努力编程的阿伟2 小时前
【Java SE语法】抽象类(abstract class)和接口(interface)有什么异同?
java·开发语言