梨柚127 2021-12-27 17:04 采纳率: 50%
浏览 271
已结题

用C++编写一个实训提目分配与管理系统

包括实训名称,教师和学生三个类
1、将实训提姆写入文件以及从文件读出
2、增加提目
3、进行提目分配,每三个学生选择一个,进行登记
4、可以读出信息进行输出
5、自己扩展功能(可有可无)
尽量多标注。
实训题木(目)有(实验室管理系统
,简单加减乘除计算器
,实训分配与管理系统
,学生信息管理系统
,学校教师信息管理
,动物关系系统,
计算图形面积和体积计算器

  • 写回答

7条回答 默认 最新

  • togolife 2021-12-28 00:02
    关注
    
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <map>
    using namespace std;
    
    // 题目结构
    struct TiMu {
        int id;          // 题目id
        string title;    // 题目标题
    };
    
    // 教师
    struct Teacher {
        int id;         // 教师编号
        string name;    // 教师姓名
    };
    
    // 学生
    struct Student {
        int id;        // 学生学号
        string name;   // 学生姓名
        int tid;       // 分配实训题目
    };
    
    class Manager {
    public:
        explicit Manager(const string &file); // 构造函数
        bool LoadTiMu(); // 从文件中加载题目
        bool SaveTiMu(); // 保存题目到文件
        void AddTiMu(const string &title); // 添加题目
        void AddStudent(int id, const string &name); // 添加学生信息
        void AddTeacher(int id, const string &name); // 添加教师信息
        void DistributeOrder(); // 顺序分配题目
        void OutputTiMu(); // 输出题目信息
        void OutputDistribute(); // 输出分配信息
    
    private:
        int getNextTiMu(int current); // 获取下一个分配的题目
    
    private:
        string mTitleFile; // 实训题目保存文件路径
        map<int, TiMu> mTiMuMap; // 实训题目
        map<int, Teacher> mTeacher; // 教师信息
        map<int, Student> mStudent; // 学生信息
    };
    
    Manager::Manager(const string &file)
    {
        mTitleFile = file;
    }
    
    bool Manager::LoadTiMu()
    {
        ifstream fp;
        fp.open(mTitleFile);
        if (!fp.is_open()) {
            return false;
        }
        // 读取实训题目文件,文件格式为:
        // 0 实验室管理系统
        // 1 简单加减乘除计算器
        // ....
        string line;
        while (getline(fp, line)) { // 读取文件每一行
            istringstream is(line);
            TiMu tmp;
            is >> tmp.id >> tmp.title;
            mTiMuMap.insert(make_pair(tmp.id, tmp));
        }
        fp.close();
        return true;
    }
    
    bool Manager::SaveTiMu()
    {
        ofstream fp;
        fp.open(mTitleFile);
        if (!fp.is_open()) {
            return false;
        }
        for (map<int, TiMu>::iterator it = mTiMuMap.begin(); it != mTiMuMap.end(); ++it) {
            TiMu &tmp = it->second;
            fp << tmp.id << " " << tmp.title << endl; // 按上述加载文件的格式: id 题目名称 保存实训题目信息
        }
        fp.close();
        return true;
    }
    
    void Manager::AddTiMu(const string &title)
    {
        int nextid = 0;
        for (map<int, TiMu>::iterator it = mTiMuMap.begin(); it != mTiMuMap.end(); ++it) {
            TiMu &tmp = it->second;
            if (tmp.title == title) { // 如果待添加实训题目已经存在,则返回
                return;
            }
            nextid = tmp.id + 1;
        }
        // 新增实训题目,id为内部递增序号
        TiMu n;
        n.id = nextid;
        n.title = title;
        mTiMuMap.insert(make_pair(n.id, n));
        return;
    }
    
    void Manager::AddStudent(int id, const string &name)
    {
        if (mStudent.find(id) != mStudent.end()) { // 已存在学生信息时,直接返回
            return;
        }
        Student st;
        st.id = id;
        st.name = name;
        st.tid = -1;
        mStudent.insert(make_pair(id, st));
        return;
    }
    
    void Manager::AddTeacher(int id, const string &name)
    {
        if (mTeacher.find(id) != mTeacher.end()) { // 已存在教师信息时,直接返回
            return;
        }
        Teacher th;
        th.id = id;
        th.name = name;
        mTeacher.insert(make_pair(id, th));
        return;
    }
    
    // 获取下一个分配的题目ID
    int Manager::getNextTiMu(int current)
    {
        if (mTiMuMap.empty()) { // 没有实训题目,返回-1
            return -1;
        }
        if (current == -1 || current == mTiMuMap.size() - 1) { // 当为-1或者最后一个题目时,下一个题目从头开始获取
            return mTiMuMap.begin()->first;
        }
        return current + 1; // 内部题目id为递增序列,直接+1取下一个
    }
    
    void Manager::DistributeOrder()
    {
        int i = 0; // 计数, 每3个学生分配一个实训题目
        int tid = getNextTiMu(-1);
        for (map<int, Student>::iterator it = mStudent.begin(); it != mStudent.end(); ++it) {
            it->second.tid = tid;
            ++i;
            if (i % 3 == 0) { // 每3个学生为一组,获取下一组待分配题目ID
                tid = getNextTiMu(tid);
            }
        }
    }
    
    void Manager::OutputTiMu()
    {
        cout << "已有实训题目如下:" << endl;
        for (map<int, TiMu>::iterator it = mTiMuMap.begin(); it != mTiMuMap.end(); ++it) {
            cout << it->second.title << endl;
        }
    }
    
    void Manager::OutputDistribute()
    {
        cout << "学生分配实训题目信息如下:" << endl;
        for (map<int, Student>::iterator it = mStudent.begin(); it != mStudent.end(); ++it) {
            map<int, TiMu>::iterator it2 = mTiMuMap.find(it->second.tid);
            if (it2 == mTiMuMap.end()) {
                cout << "学生: 学号 " << it->second.id << ", 姓名 " << it->second.name << " 未分配实训题目" << endl;
            } else {
                cout << "学生: 学号 " << it->second.id << ", 姓名 " << it->second.name << " 分配实训题目 " << it2->second.title << endl;
            }
        }
    }
    
    void Menu(void)
    {
        cout << "1 添加题目" << endl;
        cout << "2 添加学生信息" << endl;
        cout << "3 添加教师信息" << endl;
        cout << "4 输出题目信息" << endl;
        cout << "5 分配实训题目" << endl;
        cout << "6 输出分配信息" << endl;
        cout << "7 退出" << endl;
    }
    
    int main(void)
    {
        string file = "timu.txt";
        Manager ma(file);
        if (!ma.LoadTiMu()) {
            cout << "加载题目文件失败" << endl;
        }
        bool flag = true;
        while (flag) {
            Menu();
            int n;
            cin >> n;
            // 根据Menu中设置,输入相关信息并处理
            switch(n) {
            case 1: {
                string title;
                cout << "输入实训题目" << endl;
                cin >> title;
                ma.AddTiMu(title);
                break;
            }
            case 2: {
                int id;
                string name;
                cout << "输入学生学号" << endl;
                cin >> id;
                cout << "输入学生姓名" << endl;
                cin >> name;
                ma.AddStudent(id, name);
                break;
            }
            case 3: {
                int id;
                string name;
                cout << "输入教师编号" << endl;
                cin >> id;
                cout << "输入教师姓名" << endl;
                cin >> name;
                ma.AddTeacher(id, name);
                break;
            }
            case 4: {
                ma.OutputTiMu();
                break;
            }
            case 5: {
                ma.DistributeOrder();
                cout << "分配结束" << endl;
                break;
            }
            case 6: {
                ma.OutputDistribute();
                break;
            }
            case 7: {
                flag = false;
                break;
            }
            }
        }
        ma.SaveTiMu();
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月30日
  • 已采纳回答 12月30日
  • 创建了问题 12月27日

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值