特级茶叶 2023-02-23 13:09 采纳率: 78.6%
浏览 113
已结题

用C++/C编写相关程序

请编写程序,上机调试,题目如下图,请按题目要求编写,并给出程序

img

  • 写回答

8条回答 默认 最新

  • 社区专家-Monster-XH 2023-02-23 13:34
    关注

    基于Monster 组和GPT的调写:

    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    // 定义教师表结构体
    struct Teacher {
        string Tno;
        string Tname;
        int Tage;
        char Tsex;
    };
    
    // 定义系表结构体
    struct Department {
        string Dno;
        string Dname;
        string Tno;
    };
    
    // 定义工作表结构体
    struct Work {
        string Tno;
        string Dno;
        int Year;
        int Salary;
    };
    
    int main() {
        // 创建教师表
        vector<Teacher> teacher_table = {
            {"T1", "张三", 35, 'M'},
            {"T2", "李四", 28, 'F'},
            {"T3", "王五", 45, 'M'},
            {"T4", "赵六", 32, 'F'},
            {"T5", "钱七", 50, 'M'}
        };
    
        // 创建系表
        vector<Department> department_table = {
            {"D1", "计算机系", "T1"},
            {"D2", "数学系", "T3"},
            {"D3", "物理系", "T4"}
        };
    
        // 创建工作表
        vector<Work> work_table = {
            {"T1", "D1", 2010, 8000},
            {"T1", "D2", 2015, 6000},
            {"T2", "D1", 2013, 4000},
            {"T3", "D2", 2011, 7500},
            {"T4", "D3", 2014, 5000},
            {"T5", "D3", 2012, 9000}
        };
    
        // 1. 列出工资超过5000的教师的不同年龄
        vector<int> ages;
        for (auto work : work_table) {
            if (work.Salary > 5000) {
                for (auto teacher : teacher_table) {
                    if (teacher.Tno == work.Tno) {
                        ages.push_back(teacher.Tage);
                    }
                }
            }
        }
        // 去重
        sort(ages.begin(), ages.end());
        auto last = unique(ages.begin(), ages.end());
        ages.erase(last, ages.end());
        // 输出结果
        cout << "工资超过5000的教师的不同年龄:";
        for (auto age : ages) {
            cout << age << " ";
        }
        cout << endl;
    
        // 2. 查找不在计算机系工作的教师代码
        vector<string> tnos;
        for (auto work : work_table) {
            if (work.Dno != "D1") {
                tnos.push_back(work.Tno);
            }
        }
        // 去重
        sort(tnos.begin(), tnos.end());
        last = unique(tnos.begin(), tnos.end());
        tnos.erase(last, tnos.end());
        // 输出结果
        cout << "不在计算机系工作的教师代码:";
        for (auto tno : tnos) {
            cout << tno << " ";
        }
        cout << endl;
    
        // 3. 系主任T1管辖范围内的所有教师姓名
        string director_tno;
        for (auto department : department_table) {
            if (department.Dno == "D1") {
                director_tno = department.Tno;
            }
        }
        vector<string> teacher_names;
        for (auto work : work_table) {
            if (work.Dno == "D1") {
                for (auto teacher : teacher_table) {
                    if (teacher.Tno == work.Tno) {
                        teacher_names.push_back(teacher.Tname);
                    }
                }
            }
        }
        // 输出结果
        cout << "系主任T1管辖范围内的所有教师姓名:";
        for (auto teacher_name : teacher_names) {
            cout << teacher_name << " ";
        }
        cout << endl;
    
        // 4. 列出比D1系的所有教师工资都高的教师代码
        int d1_salary_sum = 0;
        int d1_teacher_count = 0;
        for (auto work : work_table) {
            if (work.Dno == "D1") {
                for (auto teacher : teacher_table) {
                    if (teacher.Tno == work.Tno) {
                        d1_salary_sum += work.Salary;
                        d1_teacher_count++;
                    }
                }
            }
        }
        double d1_salary_average = d1_salary_sum / d1_teacher_count;
        vector<string> high_salary_tnos;
        for (auto work : work_table) {
            for (auto teacher : teacher_table) {
                if (teacher.Tno == work.Tno) {
                    if (work.Salary > d1_salary_average) {
                        int count = 0;
                        for (auto w : work_table) {
                            if (w.Tno == teacher.Tno && w.Dno != "D1") {
                                count++;
                            }
                        }
                        if (count == 0) {
                            high_salary_tnos.push_back(teacher.Tno);
                        }
                    }
                }
            }
        }
        // 去重
        sort(high_salary_tnos.begin(), high_salary_tnos.end());
        last = unique(high_salary_tnos.begin(), high_salary_tnos.end());
        high_salary_tnos.erase(last, high_salary_tnos.end());
        // 输出结果
        cout << "比D1系的所有教师工资都高的教师代码:";
        for (auto tno : high_salary_tnos) {
            cout << tno << " ";
        }
        cout << endl;
    
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(7条)

报告相同问题?

问题事件

  • 系统已结题 3月7日
  • 已采纳回答 2月27日
  • 创建了问题 2月23日

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加