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

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

img

基于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;
}


该回答使用gpt 辅助回答
1、列出工资超过5000的教师的不同年龄

#include <stdio.h>

int main() {
    int salary = 5000;
    FILE* fptr = fopen("Work.txt", "r");
    int ino, dno, year;
    float s;
    while (fscanf(fptr, "%d%d%d%f", &ino, &dno, &year, &s) != EOF) {
        if (s > salary) {
            FILE* fptr2 = fopen("Teacher.txt", "r");
            int tno, age;
            char name[20], sex[5];
            while (fscanf(fptr2, "%d%s%d%s", &tno, name, &age, sex) != EOF) {
                if (tno == ino) {
                    printf("%d\n", age);
                    break;
                }
            }
            fclose(fptr2);
        }
    }
    fclose(fptr);
    return 0;
}


2、查找不在计算机系工作的教师代码

#include <stdio.h>

int main() {
    FILE* fptr = fopen("Teacher.txt", "r");
    int tno, age;
    char name[20], sex[5];
    while (fscanf(fptr, "%d%s%d%s", &tno, name, &age, sex) != EOF) {
        FILE* fptr2 = fopen("Department.txt", "r");
        int dno, tno2;
        char dname[20];
        int found = 0;
        while (fscanf(fptr2, "%d%s%d", &dno, dname, &tno2) != EOF) {
            if (tno == tno2 && strcmp(dname, "计算机系") != 0) {
                found = 1;
                break;
            }
        }
        fclose(fptr2);
        if (!found) {
            printf("%d\n", tno);
        }
    }
    fclose(fptr);
    return 0;
}


3、 系主任T1管辖范围内的所有教师姓名

#include <stdio.h>

int main() {
    int tno;
    char tname[20];
    FILE* fptr = fopen("Department.txt", "r");
    int dno, tno2;
    char dname[20];
    while (fscanf(fptr, "%d%s%d", &dno, dname, &tno2) != EOF) {
        if (tno2 == 1) {
            FILE* fptr2 = fopen("Teacher.txt", "r");
            while (fscanf(fptr2, "%d%s%*d%*s", &tno, tname) != EOF) {
                FILE* fptr3 = fopen("Work.txt", "r");
                int ino, dno2;
                float salary;
                while (fscanf(fptr3, "%d%d%*d%f", &ino, &dno2, &salary) != EOF) {
                    if (tno == ino && dno == dno2) {
                        printf("%s\n", tname);
                        break;
                    }
                }
                fclose(fptr3);
            }
            fclose(fptr2);
            break;
        }
    }
    fclose(fptr);
    return 0;
}


该回答引用GPTᴼᴾᴱᴺᴬᴵ
由于题目要求使用关系代数表示查询,下面给出对应的关系代数表达式和对应的 SQL 语句。

  1. 列出工资超过5000的教师的不同年龄
    关系代数表达式:πTage(σSalary > 5000 (Teacher ⋈ Work))
    SQL语句:SELECT DISTINCT Teacher.Tage FROM Teacher JOIN Work ON Teacher.Ino = Work.Ino WHERE Work.Salary > 5000;
  2. 查找不在计算机系工作的教师代码
    关系代数表达式:πTno(Teacher - πTno(Teacher ⋈ σDname = '计算机系'(Department)))
    SQL语句:SELECT Teacher.Tno FROM Teacher WHERE Teacher.Tno NOT IN (SELECT Teacher.Tno FROM Teacher JOIN Department ON Teacher.Ino = Department.Tno WHERE Department.Dname = '计算机系');
  3. 系主任T1管辖范围内的所有教师姓名
    关系代数表达式:πTname(σTno = T1 (Teacher ⋈ πDno(σTno = T1(Department))))
    SQL语句:SELECT DISTINCT Teacher.Tname FROM Teacher JOIN Department ON Teacher.Ino = Department.Tno WHERE Department.Tno = 'T1';
  4. 列出比D1系的所有教师工资都高的教师代码
    关系代数表达式:πTno((πIno(σDno = 'D1'(Work))) - πTno(σSalary <= (πSalary(σDno = 'D1'(Work)))))
    SQL语句:SELECT DISTINCT Ino FROM Work WHERE Dno <> 'D1' AND Salary > (SELECT MAX(Salary) FROM Work WHERE Dno = 'D1');

以下是使用SQL语言来表示每个查询的示例:

  1. 列出工资超过5000的教师的不同年龄:
SELECT DISTINCT Tage
FROM Teacher, Work
WHERE Teacher.Ino = Work.Ino AND Salary > 5000;
  1. 查找不在计算机系工作的教师代码:
SELECT Teacher.Ino
FROM Teacher
WHERE Teacher.Ino NOT IN (
    SELECT Work.Ino
    FROM Work, Department
    WHERE Work.Dno = Department.Dno AND Department.Dname = '计算机系'
);
  1. 系主任T1管辖范围内的所有教师姓名:
SELECT Teacher.Tname
FROM Teacher, Department
WHERE Teacher.Tno = Department.Tno AND Department.Tno = 'T1';
  1. 列出比D1系的所有教师工资都高的教师代码:
SELECT DISTINCT Work.Ino
FROM Work AS w1, Work AS w2
WHERE w1.Dno = 'D1' AND w1.Salary < w2.Salary AND w1.Ino = w2.Ino;

注意:以上查询语句仅为示例,具体实现可能需要根据具体情况进行修改。

是要做成执行程序吗?还是直接数据库操作?如果做成软件可以用ado连接数据库,我之前的博客有比较详细的介绍用vc++操作的教程

参考GPT和自己的思路,代码如下:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct Teacher {
    string Tno;
    string Tname;
    int Tage;
    string Tsex;
};

struct Department {
    string Dno;
    string Dname;
    string Tno;
};

struct Work {
    string Tno;
    string Dno;
    int Year;
    int Salary;
};

vector<Teacher> teacherList {
    {"T001", "Tom", 28, "M"},
    {"T002", "Jerry", 32, "M"},
    {"T003", "Lucy", 26, "F"},
    {"T004", "Lily", 35, "F"},
    {"T005", "Bob", 29, "M"},
};

vector<Department> departmentList {
    {"D001", "Computer Science", "T001"},
    {"D002", "Mathematics", "T002"},
    {"D003", "Physics", "T003"},
    {"D004", "Chemistry", "T004"},
    {"D005", "Biology", "T005"},
};

vector<Work> workList {
    {"T001", "D001", 2020, 6000},
    {"T002", "D002", 2021, 4500},
    {"T003", "D003", 2022, 5500},
    {"T004", "D004", 2020, 4000},
    {"T005", "D005", 2021, 4800},
};

// 列出工资超过5000的教师的不同年龄
void query1() {
    cout << "Teachers' age with salary > 5000:\n";
    for (auto t : teacherList) {
        for (auto w : workList) {
            if (t.Tno == w.Tno && w.Salary > 5000) {
                cout << t.Tage << endl;
                break;
            }
        }
    }
}

// 查找不在计算机系工作的教师代码
void query2() {
    cout << "Teachers' code not working in Computer Science:\n";
    for (auto t : teacherList) {
        bool found = false;
        for (auto w : workList) {
            if (t.Tno == w.Tno && w.Dno == "D001") {
                found = true;
                break;
            }
        }
        if (!found) {
            cout << t.Tno << endl;
        }
    }
}

// 系主任T1管辖范围内的所有教师姓名
void query3() {
    cout << "Teachers' name in the department led by T1:\n";
    string T1 = "";
    for (auto d : departmentList) {
        if (d.Tno == "T1") {
            T1 = d.Dno;
            break;
        }
    }
    for (auto w : workList) {
        if (w.Dno == T1) {
            for (auto t : teacherList) {
                if (w.Tno == t.Tno) {
                    cout << t.Tname << endl;
                    break;
                }
            }
        }
    }
}

// 比D1系的所有教师工资都高的教师代码
void query4() {
    cout << "Teachers' code with higher salary than the department D1:\n";
    int maxSalary = 0;
    for (auto w : workList) {
        if (w.Dno == "D001" && w.Salary > maxSalary) {
        maxSalary = w.Salary;
    }
}
for (auto w : workList) {
    if (w.Salary > maxSalary) {
        cout << w.Tno << endl;
    }
}
}

int main() {
query1();
query2();
query3();
query4();
return 0;
}

注意:由于原始数据中没有提供"系主任"的编号,所以在query3中使用"T1"代替了实际的系主任编号。实际情况中,需要根据实际数据进行修改。另外,在query4中,如果有多个部门的最高工资相同,那么会漏掉一些教师的编号,需要进行进一步处理。
如果对您有帮助,请给与采纳,谢谢。

以下答案基于ChatGPT与GISer Liu编写:
由于题目中并没有给出具体的数据库内容,我们在程序中使用一些模拟数据进行测试。

以下是C++代码,使用了结构体和vector存储数据库信息,使用关系代数表达式进行查询:

#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> teachers = {
        {"1001", "张三", 35, 'M'},
        {"1002", "李四", 30, 'M'},
        {"1003", "王五", 40, 'F'},
        {"1004", "赵六", 28, 'F'},
        {"1005", "孙七", 45, 'M'}
    };
    vector<Department> departments = {
        {"001", "计算机系", "1001"},
        {"002", "电子系", "1003"},
        {"003", "数学系", "1005"}
    };
    vector<Work> works = {
        {"1001", "001", 2020, 8000},
        {"1002", "002", 2021, 4000},
        {"1003", "001", 2020, 9000},
        {"1004", "002", 2022, 5000},
        {"1005", "003", 2021, 6000},
        {"1002", "003", 2020, 7000}
    };

    // 查询1:列出工资超过5000的教师的不同年龄
    cout << "查询1:列出工资超过5000的教师的不同年龄" << endl;
    vector<int> ages;
    for (auto t : teachers) {
        for (auto w : works) {
            if (t.Tno == w.Tno && w.Salary > 5000) {
                if (find(ages.begin(), ages.end(), t.Tage) == ages.end()) {
                    ages.push_back(t.Tage);
                    cout << t.Tage << endl;
                }
            }
        }
    }

    // 查询2:查找不在计算机系工作的教师代码
    cout << "查询2:查找不在计算机系工作的教师代码" << endl;
    for (auto t : teachers) {
        bool flag = true;
        for (auto w : works) {
            if (t.Tno == w.Tno && w.Dno == "001") {
                flag = false;
                break;
            }
        }
        if (flag) {
            cout << t.Tno << endl;
        }
    }
    // 查询3:列出系主任T1管辖范围内的所有教师姓名
    cout << "查询3:列出系主任T1管辖范围内的所有教师姓名" << endl;
    string T1;
    for (auto d : departments) {
        if (d.Dno == "001") {  // 假设T1是计算机系系主任
            T1 = d.Tno;
            break;
        }
    }
    for (auto t : teachers) {
        for (auto d : departments) {
            for (auto w : works) {
                if (t.Tno == w.Tno && w.Dno == d.Dno && d.Tno == T1) {
                    cout << t.Tname << endl;
                }
            }
        }
    }
    // 查询4:列出比D1系的所有教师工资都高的教师代码
    cout << "查询4:列出比D1系的所有教师工资都高的教师代码" << endl;
    vector<string> tnos;
    for (auto w : works) {
        if (find(tnos.begin(), tnos.end(), w.Tno) == tnos.end()) {
            tnos.push_back(w.Tno);
        }
    }
    vector<string> d1_tnos;
    for (auto w : works) {
        if (w.Dno == "001") {
            d1_tnos.push_back(w.Tno);
        }
    }
    for (auto t : tnos) {
        if (find(d1_tnos.begin(), d1_tnos.end(), t) == d1_tnos.end()) {
            bool flag = true;
            int salary;
            for (auto w : works) {
                if (w.Tno == t) {
                    if (flag) {
                        salary = w.Salary;
                        flag = false;
                    } else if (w.Salary <= salary) {
                        flag = true;
                        break;
                    }
                }
            }
            if (!flag) {
                cout << t << endl;
            }
        }
    }

如有帮助望采纳


#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;
}