学校教师工资管理系统

5、教师工资管理系统。 每个教师的信息为:教师号、姓名、性别、单位名称、家庭住址、联系电话、基本工资、津贴、生活补贴、应发工资、电话费、水电费、房租、所得税、卫生费、公积金、合计扣款、实发工资。注:应发工资=基本工资+津贴+生活补贴;合计扣款=电话费+水电费+房租+所得税+卫生费+公积金;实发工资=应发工资-合计扣款。A、教师信息处理 (1)输入教师信息(2)插入(修改)教师信息:(3)删除教师信息:(4)浏览教师信息:提示:具体功能及操作参考题1。B、教师数据处理:(1)按教师号录入教师基本工资、津贴、生活补贴、电话费、水电费、房租、所得税、卫生费、公积金等基本数据。(2)教师实发工资、应发工资、合计扣款计算。提示:计算规则如题目。(3)教师数据管理提示:输入教师号,读出并显示该教师信息,输入新数据,将改后信息写入文件(4)教师数据查询:提示:输入教师号或其他信息,即读出所有数据信息,并显示出来。(5)教师综合信息输出提示:输出教师信息到屏幕。

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

struct Teacher {
    string id;
    string name;
    string gender;
    string institution;
    string address;
    string phone;
    double basicSalary;
    double allowance;
    double livingAllowance;
    double tax;
    double waterElectricityFee;
    double rent;
    double healthFee;
    double accumulationFund;
    double telephoneFee;
    double deduction;
    double shouldPaySalary;
    double actualPaySalary;

    // 构造函数
    Teacher(string id, string name, string gender, string institution, string address, string phone)
        : id(id), name(name), gender(gender), institution(institution), address(address), phone(phone)
    {}

    // 计算应发工资
    void calcShouldPaySalary() {
        shouldPaySalary = basicSalary + allowance + livingAllowance;
    }

    // 计算合计扣款
    void calcDeduction() {
        deduction = telephoneFee + waterElectricityFee + rent + tax + healthFee + accumulationFund;
    }

    // 计算实发工资
    void calcActualPaySalary() {
        actualPaySalary = shouldPaySalary - deduction;
    }

    // 显示教师信息
    void displayInfo() {
        cout << "教师号:" << id << endl;
        cout << "姓名:" << name << endl;
        cout << "性别:" << gender << endl;
        cout << "单位名称:" << institution << endl;
        cout << "家庭住址:" << address << endl;
        cout << "联系电话:" << phone << endl;
        cout << "基本工资:" << basicSalary << endl;
        cout << "津贴:" << allowance << endl;
        cout << "生活补贴:" << livingAllowance << endl;
        cout << "实发工资:" << actualPaySalary << endl;
    }

    // 转换为一行字符串
    string toLine() {
        return id + "," + name + "," + gender + "," + institution + "," + address + ","
                + phone + "," + to_string(basicSalary) + "," + to_string(allowance) + ","
                + to_string(livingAllowance) + "," + to_string(tax) + ","
                + to_string(waterElectricityFee) + "," + to_string(rent) + ","
                + to_string(healthFee) + "," + to_string(accumulationFund) + ","
                + to_string(telephoneFee) + "," + to_string(deduction) + ","
                + to_string(shouldPaySalary) + "," + to_string(actualPaySalary);
    }
};

// 读取教师信息文件
vector<Teacher> readTeachersFromFile(string fileName) {
    vector<Teacher> teachers;
    ifstream file(fileName);
    if (!file.is_open()) {
        cout << "打开文件失败!" << endl;
        return teachers;
    }
    string line, token;
    while (getline(file, line)) {
        int pos, start = 0;
        vector<string> tokens;
        while ((pos = line.find(",", start)) != string::npos) {
            token = line.substr(start, pos - start);
            start = pos + 1;
            tokens.push_back(token);
        }
        token = line.substr(start);
        tokens.push_back(token);

        Teacher teacher(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5]);
        teacher.basicSalary = stod(tokens[6]);
        teacher.allowance = stod(tokens[7]);
        teacher.livingAllowance = stod(tokens[8]);
        teacher.tax = stod(tokens[9]);
        teacher.waterElectricityFee = stod(tokens[10]);
        teacher.rent = stod(tokens[11]);
        teacher.healthFee = stod(tokens[12]);
        teacher.accumulationFund = stod(tokens[13]);
        teacher.telephoneFee = stod(tokens[14]);
        teacher.deduction = stod(tokens[15]);
        teacher.shouldPaySalary = stod(tokens[16]);
        teacher.actualPaySalary = stod(tokens[17]);

        teachers.push_back(teacher);
    }
    file.close();
    return teachers;
}

// 将教师信息保存到文件中
void saveTeachersToFile(vector<Teacher>& teachers, string fileName) {
    ofstream file(fileName);
    if (!file.is_open()) {
        cout << "打开文件失败!" << endl;
        return;
    }
    for (int i = 0; i < teachers.size(); i++) {
        file << teachers[i].toLine() << endl;
    }
    file.close();
}

// 根据教师号查找教师
Teacher* findTeacherById(vector<Teacher>& teachers, string id) {
    for (int i = 0; i < teachers.size(); i++) {
        if (teachers[i].id == id) {
            return &teachers[i];
        }
    }
    return NULL;
}

// 输入教师信息
Teacher inputTeacher(string id) {
    string name, gender, institution, address, phone;
    double basicSalary = 0, allowance = 0, livingAllowance = 0;
    cout << "请输入教师姓名:";
    cin >> name;
    cout << "请输入教师性别:";
    cin >> gender;
    cout << "请输入教师单位名称:";
    cin >> institution;
    cout << "请输入教师家庭住址:";
    cin >> address;
    cout << "请输入教师联系电话:";
    cin >> phone;
    cout << "请输入教师基本工资:";
    cin >> basicSalary;
    cout << "请输入教师津贴:";
    cin >> allowance;
    cout << "请输入教师生活补贴:";
    cin >> livingAllowance;

    Teacher teacher(id, name, gender, institution, address, phone);
    teacher.basicSalary = basicSalary;
    teacher.allowance = allowance;
    teacher.livingAllowance = livingAllowance;

    return teacher;
}

// 录入教师数据
void inputTeacherData(vector<Teacher>& teachers) {
    string id;
    cout << "请输入教师号:";
    cin >> id;
    Teacher* teacher = findTeacherById(teachers, id);
    if (teacher != NULL) {
        double basicSalary, allowance, livingAllowance, tax, waterElectricityFee, rent, healthFee, accumulationFund, telephoneFee;
        cout << "请输入教师基本工资:";
        cin >> basicSalary;
        cout << "请输入教师津贴:";
        cin >> allowance;
        cout << "请输入教师生活补贴:";
        cin >> livingAllowance;
        cout << "请输入教师所得税:";
        cin >> tax;
        cout << "请输入教师水电费:";
        cin >> waterElectricityFee;
        cout << "请输入教师房租:";
        cin >> rent;
        cout << "请输入教师卫生费:";
        cin >> healthFee;
        cout << "请输入教师公积金:";
        cin >> accumulationFund;
        cout << "请输入教师电话费:";
        cin >> telephoneFee;

        teacher->basicSalary = basicSalary;
        teacher->allowance = allowance;
        teacher->livingAllowance = livingAllowance;
        teacher->tax = tax;
        teacher->waterElectricityFee = waterElectricityFee;
        teacher->rent = rent;
        teacher->healthFee = healthFee;
        teacher->accumulationFund = accumulationFund;
        teacher->telephoneFee = telephoneFee;

        teacher->calcShouldPaySalary();
        teacher->calcDeduction();
        teacher->calcActualPaySalary();
        saveTeachersToFile(teachers, "teachers.txt");
    } else {
        cout << "教师不存在!" << endl;
    }
}

// 删除教师信息
void deleteTeacher(vector<Teacher>& teachers) {
    string id;
    cout << "请输入要删除的教师号:";
    cin >> id;
    for (int i = 0; i < teachers.size(); i++) {
        if (teachers[i].id == id) {
            teachers.erase(teachers.begin() + i);
            saveTeachersToFile(teachers, "teachers.txt");
            cout << "删除成功!" << endl;
            return;
        }
    }
    cout << "教师不存在!" << endl;
}

// 浏览教师信息
void browseTeacherInfo(vector<Teacher>& teachers) {
    cout << "教师信息列表:" << endl;
    for (int i = 0; i < teachers.size(); i++) {
        teachers[i].displayInfo();
        cout << endl;
    }
}

// 根据教师号查询教师信息
void queryTeacherById(vector<Teacher>& teachers) {
    string id;
    cout << "请输入要查询的教师号:";
    cin >> id;
    Teacher* teacher = findTeacherById(teachers, id);
    if (teacher != NULL) {
        teacher->displayInfo();
    } else {
        cout << "教师不存在!" << endl;
    }
}

// 输出教师综合信息
void outputTeacherInfo(vector<Teacher>& teachers) {
    ofstream file("teacher_info.txt");
    if (!file.is_open()) {
        cout << "打开文件失败!" << endl;
        return;
    }
    for (int i = 0; i < teachers.size(); i++) {
        file << "教师号:" << teachers[i].id << endl;
        file << "姓名:" << teachers[i].name << endl;
        file << "性别:" << teachers[i].gender << endl;
        file << "单位名称:" << teachers[i].institution << endl;
        file << "家庭住址:" << teachers[i].address << endl;
        file << "联系电话:" << teachers[i].phone << endl;
        file << "基本工资:" << teachers[i].basicSalary << endl;
        file << "津贴:" << teachers[i].allowance << endl;
        file << "生活补贴:" << teachers[i].livingAllowance << endl;
        file << "应发工资:" << teachers[i].shouldPaySalary << endl;
        file << "合计扣款:" << teachers[i].deduction << endl;
        file << "实发工资:" << teachers[i].actualPaySalary << endl;
        file << endl;
    }
    file.close();
    cout << "教师信息已输出到teacher_info.txt文件中!" << endl;
}

int main() {
    vector<Teacher> teachers = readTeachersFromFile("teachers.txt");
    int choice;
    while (true) {
        cout << "请选择功能:" << endl;
        cout << "1. 输入教师信息" << endl;
        cout << "2. 插入或修改教师数据" << endl;
        cout << "3. 删除教师信息" << endl;
        cout << "4. 浏览教师信息" << endl;
        cout << "5. 查询教师信息" << endl;
        cout << "6. 输出教师综合信息到文件" << endl;
        cout << "7. 退出系统" << endl;
        cin >> choice;
        switch (choice) {
            case 1:
                {
                    string id;
                    cout << "请输入教师号:";
                    cin >> id;
                    Teacher teacher = inputTeacher(id);
                    teachers.push_back(teacher);
                    saveTeachersToFile(teachers, "teachers.txt");
                    cout << "教师信息已保存!" << endl;
                    break;
                }
            case 2:
                {
                    inputTeacherData(teachers);
                    break;
                }
            case 3:
                {
                    deleteTeacher(teachers);
                    break;
                }
            case 4:
                {
                    browseTeacherInfo(teachers);
                    break;
                }
            case 5:
                {
                    queryTeacherById(teachers);
                    break;
                }
            case 6:
                {
                    outputTeacherInfo(teachers);
                    break;
                }
            case 7:
                {
                    cout << "谢谢使用!" << endl;
                    return 0;
                }
            default:
                cout << "输入有误,请重新输入!" << endl;
        }
    }
    return 0;
}