0
点赞
收藏
分享

微信扫一扫

C++实现一个简单部门信息管理功能


 用C++实现部门人员信息导入导出功能,
部门分为财务处,人事处,教务处,学生处
人员信息:所处部门,工号,姓名,年龄,入职时间
最终输出表格

下面是一个简单的C++程序,实现了部门人员信息的导入和导出功能,并输出为表格形式。程序中使用了结构体来表示人员信息,使用了vector容器来存储多个人员信息。请注意,这只是一个简化的示例,您可以根据自己的需求进行修改和扩展。:

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

using namespace std;

// 部门枚举
enum class Department {
    Finance,
    HR,
    Academic,
    Student
};

// 人员信息结构体
struct Employee {
    Department department;
    string employeeID;
    string name;
    int age;
    string hireDate;
};

// 函数声明
void importEmployees(vector<Employee>& employees);
void exportEmployees(const vector<Employee>& employees);

int main() {
    vector<Employee> employees;

    // 导入人员信息
    importEmployees(employees);

    // 导出人员信息
    exportEmployees(employees);

    return 0;
}

void importEmployees(vector<Employee>& employees) {
    ifstream inputFile("employees.txt"); // 假设人员信息保存在名为employees.txt的文本文件中

    if (!inputFile) {
        cout << "无法打开文件!" << endl;
        return;
    }

    string department;
    string employeeID;
    string name;
    int age;
    string hireDate;

    while (inputFile >> department >> employeeID >> name >> age >> hireDate) {
        Employee employee;
        if (department == "财务处")
            employee.department = Department::Finance;
        else if (department == "人事处")
            employee.department = Department::HR;
        else if (department == "教务处")
            employee.department = Department::Academic;
        else if (department == "学生处")
            employee.department = Department::Student;

        employee.employeeID = employeeID;
        employee.name = name;
        employee.age = age;
        employee.hireDate = hireDate;

        employees.push_back(employee);
    }

    inputFile.close();
}

void exportEmployees(const vector<Employee>& employees) {
    ofstream outputFile("employee_table.txt"); // 将导出的表格保存在名为employee_table.txt的文本文件中

    if (!outputFile) {
        cout << "无法创建文件!" << endl;
        return;
    }

    outputFile << "部门\t工号\t姓名\t年龄\t入职时间" << endl;

    for (const auto& employee : employees) {
        string department;
        switch (employee.department) {
            case Department::Finance:
                department = "财务处";
                break;
            case Department::HR:
                department = "人事处";
                break;
            case Department::Academic:
                department = "教务处";
                break;
            case Department::Student:
                department = "学生处";
                break;
        }

        outputFile << department << "\t"
                   << employee.employeeID << "\t"
                   << employee.name << "\t"
                   << employee.age << "\t"
                   << employee.hireDate << endl;
    }

    outputFile.close();

    cout << "人员信息导出成功!" << endl;
}



举报

相关推荐

0 条评论