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


上述代码中,人员信息被保存在名为employees.txt的文本文件中,每行表示一个人员信息,各个字段之间使用空格或制表符

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

using namespace std;

struct Person {  
    string department;  // 所在部门  
    int id;            // 工号  
    string name;      // 姓名  
    int age;          // 年龄  
    string join_time;  // 入职时间  
};

void import_data(string filename, vector<Person>& people) {  
    ifstream fin(filename);  
    if (!fin.is_open()) {  
        cout << "错误读取文件" << filename << endl;  
        return;  
    }

    vector<Person> temp_people;  
    while (fin >> temp_people.size() >> temp_people[0].id >> temp_people[0].name >> temp_people[0].age >> temp_people[0].join_time) {  
        temp_people.push_back(temp_people[0]);  
    }  
    fin.close();

    people = temp_people;  
}

void export_data(string filename, vector<Person>& people) {  
    ofstream fout(filename);  
    if (!fout.is_open()) {  
        cout << "错误读取文件 " << filename << endl;  
        return;  
    }

    for (const auto& person : people) {  
        fout << person.department << endl;  
        fout << person.id << endl;  
        fout << person.name << endl;  
        fout << person.age << endl;  
        fout << person.join_time << endl;  
    }  
    fout.close();  
}

int main() {  
    string filename;  
    cout << "文件名: ";  
    cin >> filename;

    vector<Person> people;  
    import_data(filename, people);

    cout << "导入成功" << endl;

    export_data(filename, people);

    cout << "导出成功!" << endl;

    return 0;  
}


这个不难,按部就班就好

你的问题代码如下:

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

using namespace std;

// 定义人员信息结构体
struct Employee {
    string department;  // 所在部门
    int id;             // 工号
    string name;        // 姓名
    int age;            // 年龄
    string hiredate;    // 入职时间
};

int main() {
    vector<Employee> employees;    // 存放所有员工信息

    // 示例:手动添加两个员工信息
    Employee employee1 = {"财务处", 1001, "张三", 30, "2020-01-01"};
    Employee employee2 = {"人事处", 1002, "李四", 25, "2021-03-15"};
    employees.push_back(employee1);
    employees.push_back(employee2);

    // 输出表头
    cout << "部门\t工号\t姓名\t年龄\t入职时间" << endl;

    // 循环输出每个员工信息
    for (Employee employee : employees) {
        cout << employee.department << "\t"
             << employee.id << "\t"
             << employee.name << "\t"
             << employee.age << "\t"
             << employee.hiredate << endl;
    }

    return 0;
}

这段代码运行之后会输出如下的表格:

部门    工号    姓名    年龄    入职时间
财务处  1001   张三     30     2020-01-01
人事处  1002   李四     25     2021-03-15
  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/7577804
  • 这篇博客你也可以参考下:【C++探索之旅】第一部分第十二课:指针一出,谁与争锋
  • 除此之外, 这篇博客: C++>继承,继承方式及其比较,子类和父类对象指针,派生类的默认成员函数,虚继承,继承与友元,继承与静态成员中的 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 虚继承是在声明继承关系时,在继承的类前面加上virtual关键字,这时被继承的类称为虚基类。

    class TerrestrialAnimal:virtual public Animal	//TerrestrialAnimal虚继承Animal
    

    1.普通虚拟继承比普通单继承多4个字节,用于存放虚基表指针,指向虚基表,虚基表中存放这偏移量,通过偏移量可以找到Animal的成员。
    2.虚继承中子类部分在上,基类部分在下。存放顺序为TerrestrialAnimal虚基表指针>TerrestrialAnimal成员>AquaticAnimal虚基表指针>AquaticAnimal>Frog成员>Animal成员。其中AquaticAnimal虚基表中的指针偏移量要比TerrestrialAnimal虚基表中的指针偏移量大sizeof(TerrestrialAnimal虚基表指针+TerrestrialAnimal成员)。
    在这里插入图片描述

  • 您还可以看一下 朱有鹏老师的朱老师C++第2部分-2.3.C++继承和多态特性课程中的 2.3.15.多继承及其二义性问题2小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    我可以提供一个实现部门信息管理功能的代码框架,但具体实现将根据需求而变化。需要注意的是,需要使用文件读写操作来实现导入导出的功能。 首先,建立一个结构体Department,包含成员变量departmentName和一个vector,存储该部门下的所有员工信息。员工信息可以通过定义一个结构体Employee来实现,包含成员变量jobId、name、age和entryTime。 然后,使用一个map来存储四个部门及其对应的Department对象。其中,部门名称作为map的key,Department对象作为value。 接下来,就可以构建相应的管理函数了。包括向特定部门添加员工、删除特定部门员工、查找特定部门员工、导入导出部门信息等。其中,涉及到文件读写的操作可以使用fstream库实现。 以下是代码框架,仅供参考:

    include

    include

    include

    include

    include

    using namespace std;

    struct Employee{ int jobId; string name; int age; string entryTime; };

    struct Department{ string departmentName; vector employeeList; };

    map departmentMap;

    void addEmployeeToDepartment(string departmentName, Employee employee){ departmentMap[departmentName].employeeList.push_back(employee); }

    bool deleteEmployeeFromDepartment(string departmentName, int jobId){ vector employeeList = departmentMap[departmentName].employeeList; for(int i=0; i<employeeList.size(); i++){ if(employeeList[i].jobId == jobId){ employeeList.erase(employeeList.begin()+i); departmentMap[departmentName].employeeList = employeeList; return true; } } return false; }

    Employee findEmployeeInDepartment(string departmentName, int jobId){ vector employeeList = departmentMap[departmentName].employeeList; for(int i=0; i<employeeList.size(); i++){ if(employeeList[i].jobId == jobId){ return employeeList[i]; } } Employee nullEmployee = {0,"",0,""}; return nullEmployee; }

    void importDepartmentInfo(){ // 读取文件,将部门信息添加到departmentMap中 }

    void exportDepartmentInfo(){ // 将departmentMap中的部门信息导出到文件 }

    int main(){ // 主函数中调用各种管理函数

    return 0;
    

    }