13重学C++之【综合案例:职工管理系统】

work.h

#pragma once
#include<iostream>
#include<string>
using namespace std;


// 职工抽象类
class Worker{
public:
    int id;
    string name;
    int dept_id;

    virtual void show_info() = 0;
    virtual string get_dept() = 0;
};

employee.h

#pragma once
#include<iostream>
#include<string>
using namespace std;
#include "worker.h"



// 普通员工类
class Emplyee : public Worker{
public:
    Emplyee(int id, string name, int dept_id);

    virtual void show_info();
    virtual string get_dept();

};

employee.cpp

#include "employee.h"



Emplyee::Emplyee(int id, string name, int dept_id){
    this->id = id;
    this->name = name;
    this->dept_id = dept_id;
}


void Emplyee::show_info(){
    cout << "职工编号:" << this->id
         << "\t职工姓名:" << this->name
         << "\t岗位:" << this->get_dept()
         << "\t职责:完成经理交给的任务" << endl;
}


string Emplyee::get_dept(){
    return string("员工");
}

manager.h

#pragma once
#include<iostream>
#include<string>
using namespace std;
#include "worker.h"



// 经理类
class Manager : public Worker{
public:
    Manager(int id, string name, int dept_id);

    virtual void show_info();
    virtual string get_dept();

};

manager.cpp

#include "manager.h"




Manager::Manager(int id, string name, int dept_id){
    this->id = id;
    this->name = name;
    this->dept_id = dept_id;
}



void Manager::show_info(){
    cout << "职工编号:" << this->id
         << "\t职工姓名:" << this->name
         << "\t岗位:" << this->get_dept()
         << "\t职责:完成老板交给的任务,并下发子任务给员工" << endl;
}




string Manager::get_dept(){
    return string("经理");
}

boss.h

#pragma once
#include<iostream>
#include<string>
using namespace std;
#include "worker.h"



// 老板类
class Boss : public Worker{
public:
    Boss(int id, string name, int dept_id);

    virtual void show_info();
    virtual string get_dept();

};

boss.cpp

#include "boss.h"


Boss::Boss(int id, string name, int dept_id){
    this->id = id;
    this->name = name;
    this->dept_id = dept_id;
}



void Boss::show_info(){
    cout << "职工编号:" << this->id
         << "\t职工姓名:" << this->name
         << "\t岗位:" << this->get_dept()
         << "\t职责:管理公司所有事务" << endl;
}



string Boss::get_dept(){
    return string("老板");
}

worker_manager.h

#pragma once // 防止头文件重复包含
#include<iostream>
#include<windows.h> // exit(0)需要
#include<fstream>

using namespace std;

#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"

#define FILENAME "emp_file.txt"



class WorkerManager{
public:

    WorkerManager();

    void show_menu();

    void exit_system();

    void add_emp();

    void save();

    int get_emp_num();

    void init_emp();

    void show_emp();

    int is_exit(int id); // 判断编号为id的职工是否存在,存在返回其在数组中的下标位置,不存在返回-1

    void del_emp();

    void update_emp();

    void find_emp();

    void sort_emp();

    void clean_file();

    ~WorkerManager();

public:
    int emp_num;
    Worker ** emp_arr;
    bool file_is_empty;
};

worker_manager.cpp

#include "worker_manager.h"



WorkerManager::WorkerManager(){
    fstream fs;
    fs.open(FILENAME, ios::in);

    if(!fs.is_open()){
        cout << "文件不存在" << endl;
        // 初始化属性
        this->emp_num = 0;
        this->emp_arr = NULL;
        this->file_is_empty = true;
        fs.close();
        return;
    }

    char ch;
    fs >> ch;
    if(fs.eof()){
        cout << "文件存在,但无数据,是空文件" << endl;
        // 初始化属性
        this->emp_num = 0;
        this->emp_arr = NULL;
        this->file_is_empty = true;
        fs.close();
        return;
    }

    int num = this->get_emp_num();
    cout << "文件存在且数据,当前职员人数:" << num << endl;
    this->file_is_empty = false;
    this->emp_num = num;
    this->emp_arr = new Worker*[this->emp_num]; // 开辟空间
    this->init_emp(); // 将文件中的数据存到数组中
    /*
    for(int i=0; i<this->emp_num; i++){
        cout << this->emp_arr[i]->id << endl;
        cout << this->emp_arr[i]->name << endl;
        cout << this->emp_arr[i]->dept_id << endl;
    } // 测试
    */
}


void WorkerManager::show_menu(){
    cout << "********************************************" << endl;
    cout << "********* 欢迎使用职工管理系统! **********" << endl;
    cout << "************* 0.退出管理程序 *************" << endl;
    cout << "************* 1.增加职工信息 *************" << endl;
    cout << "************* 2.显示职工信息 *************" << endl;
    cout << "************* 3.删除离职职工 *************" << endl;
    cout << "************* 4.修改职工信息 *************" << endl;
    cout << "************* 5.查找职工信息 *************" << endl;
    cout << "************* 6.按照编号排序 *************" << endl;
    cout << "************* 7.清空所有文档 *************" << endl;
    cout << "********************************************" << endl;
    cout << endl;
}


void WorkerManager::exit_system(){
    cout << "欢迎下次使用" << endl;
    system("pause");
    exit(0);
}


void WorkerManager::add_emp(){
    cout << "请输入添加职工数量:" << endl;
    int add_num = 0;
    cin >> add_num;

    if(add_num > 0){
        //计算添加新空间大小
        int new_size = this->emp_num + add_num; // 新空间大小=原有记录人数+新增人数

        //开辟新空间
        Worker* * new_space = new Worker*[new_size]; // Worker*为该数组的数据类型
        /*
            // worker ** 可以理解为* worker *,表示指向worker*数组的指针
            // **是二级指针,表示指向指针的指针
            // 指向指针的指针数组
        */

        //将原来空间下的数据拷贝到新空间
        if(this->emp_arr != NULL){
            for(int i=0; i<this->emp_num; i++){
                new_space[i] = this->emp_arr[i];
            }
        }

        //批量添加新数据
        for(int i=0; i<add_num; i++){
            int id;
            string name;
            int dept_id;

            //cout << "请输入第" << i+1 << "个新职员的编号:" << endl;
            //cin >> id;
            // 职员编号必须唯一
            while(true){
                cout << "请输入第" << i+1 << "个新职员的编号:" << endl;
                cin >> id;
                if(this->is_exit(id) != -1){
                    cout << "编号已存在,请重新输入" << endl;
                }else{
                    break;
                }
            }

            cout << "请输入第" << i+1 << "个新职员的姓名:" << endl;
            cin >> name;

            cout << "请选择该职员的岗位(1-普通员工;2-经理;3-老板):" << endl;
            cin >> dept_id;
            Worker * worker = NULL;
            switch(dept_id){
            case 1:
                worker = new Emplyee(id, name, dept_id);
                break;
            case 2:
                worker = new Manager(id, name, dept_id);
                break;
            case 3:
                worker = new Boss(id, name, dept_id);
                break;
            default:
                break;
            }

            //将新创建职员的指针保存到数组中
            new_space[this->emp_num + i] = worker;
        }

        //释放原有空间
        delete[] this->emp_arr;
        //更改新空间指向
        this->emp_arr = new_space;
        // 更新当前职员人数
        this->emp_num = new_size;

        this->file_is_empty = false;
        this->save();
        cout << "成功添加" << add_num << "名新职员" << endl;

    }else{
        cout << "输入数量有误" << endl;
    }

    system("pause");
    system("cls");
}


void WorkerManager::save(){
    fstream fs;
    fs.open(FILENAME, ios::out);

    for(int i=0; i<this->emp_num; i++){
        fs << this->emp_arr[i]->id << " "
           << this->emp_arr[i]->name << " "
           << this->emp_arr[i]->dept_id << endl;
    }

    fs.close();
}


int WorkerManager::get_emp_num(){
    ifstream ifs;
    ifs.open(FILENAME, ios::in);

    int id;
    string name;
    int dept_id;

    int num = 0;
    while(ifs>>id && ifs>>name && ifs>>dept_id){
        num++;
    }
    return num;
}


void WorkerManager::init_emp(){
    ifstream ifs;
    ifs.open(FILENAME, ios::in);

    int id;
    string name;
    int dept_id;

    int index = 0;
    while(ifs>>id && ifs>>name && ifs>>dept_id){
        Worker * worker = NULL;
        if(dept_id == 1){
            worker = new Emplyee(id, name, dept_id);
        }else if(dept_id == 2){
            worker = new Manager(id, name, dept_id);
        }else{
            worker = new Boss(id, name, dept_id);
        }

        this->emp_arr[index] = worker;
        index++;
    }

    ifs.close();
}


void WorkerManager::show_emp(){
    if(this->file_is_empty){
        cout << "文件不存在或者记录为空" << endl;
    }else{
        for(int i=0; i<this->emp_num; i++){
            this->emp_arr[i]->show_info();
        }
    }

    system("pause");
    system("cls");
}


int WorkerManager::is_exit(int id){
    int index = -1;
    for(int i=0; i<this->emp_num; i++){
        if(this->emp_arr[i]->id == id){
            index = i;
            break;
        }
    }
    return index;
}


void WorkerManager::del_emp(){
    if(this->file_is_empty){
        cout << "文件不存在或者记录为空" << endl;
    }else{
        cout << "请输入要删除的职员编号:" << endl;
        int id = 0;
        cin >> id;

        int index = this->is_exit(id);
        if(index != -1){
            //cout << "职员存在" << endl;
            for(int i=index; i<this->emp_num-1; i++){
                this->emp_arr[i] = this->emp_arr[i+1]; //数组数据前移实现逻辑删除
            }
            this->emp_num--;
            this->save();
            cout << "删除成功" << endl;

        }else{
            cout << "删除失败,职员不存在" << endl;
        }
    }

    system("pause");
    system("cls");
}


void WorkerManager::update_emp(){
    if(this->file_is_empty){
        cout << "文件不存在或者记录为空" << endl;
    }else{
        cout << "请输入要修改的职员编号:" << endl;
        int id = 0;
        cin >> id;

        int index = this->is_exit(id);
        if(index != -1){
            delete this->emp_arr[index];
            int new_id = 0;
            string new_name = "";
            int new_dept_id = 0;

            // 职员编号必须唯一
            while(true){
                cout << "请输入职员新编号:" << endl;
                cin >> new_id;
                if(this->is_exit(new_id) != -1){
                    cout << "编号已存在,请重新输入" << endl;
                }else{
                    break;
                }
            }

            cout << "请输入职员新姓名:" << endl;
            cin >> new_name;
            cout << "请输入职员新岗位(1-普通员工;2-经理;3-老板):" << endl;
            cin >>new_dept_id;

            Worker * worker = NULL;
            switch(new_dept_id){
            case 1:
                worker = new Emplyee(new_id, new_name, new_dept_id);
                break;
            case 2:
                worker = new Manager(new_id, new_name, new_dept_id);
                break;
            case 3:
                worker = new Boss(new_id, new_name, new_dept_id);
                break;
            default:
                break;
            }

            this->emp_arr[index] = worker;
            this->save();
            cout << "修改成功" << endl;
        }else{
            cout << "修改失败,职员不存在" << endl;
        }
    }

    system("pause");
    system("cls");
}


void WorkerManager::find_emp(){
    if(this->file_is_empty){
        cout << "文件不存在或者记录为空" << endl;
    }else{
        cout << "请输入查找方式(1-按编号;2-按姓名):" << endl;
        int select = 0;
        cin >> select;

        if(select == 1){
            cout << "请输入要查找职员的编号:" << endl;
            int id;
            cin >> id;
            int result = this->is_exit(id);
            if(result != -1){
                this->emp_arr[result]->show_info();
            }else{
                cout << "查无此人" << endl;
            }
        }else if(select == 2){
            cout << "请输入要查找职员的姓名:" << endl;
            string name;
            cin >> name;
            bool flag = false; // 判断是否查到的标志
            for(int i=0; i<this->emp_num; i++){
                if(this->emp_arr[i]->name == name){
                    flag = true;
                    this->emp_arr[i]->show_info();
                }
            }
            if(flag == false){
                cout << "查无此人" << endl;
            }

        }else{
            cout << "输入选项有误" << endl;
        }
    }

    system("pause");
    system("cls");
}


void WorkerManager::sort_emp(){
    if(this->file_is_empty){
        cout << "文件不存在或者记录为空" << endl;
        system("pause");
        system("cls");
    }else{
        cout << "请输入排序方式(1-升序;2-降序):" << endl;
        int select = 0;
        cin >> select;

        for(int i=0; i<this->emp_num; i++){

            int min_or_max = i; //声明当前最小ID或最大ID的下标

            for(int j=i+1; j<this->emp_num; j++){
                if(select == 1){
                    if(this->emp_arr[min_or_max]->id > this->emp_arr[j]->id){
                        min_or_max = j;
                    }
                }else if(select == 2){
                    if(this->emp_arr[min_or_max]->id < this->emp_arr[j]->id){
                        min_or_max = j;
                    }
                }else{
                    cout << "输入选项有误" << endl;
                }
            }

            if(i != min_or_max){
                Worker * temp = this->emp_arr[i];
                this->emp_arr[i] = this->emp_arr[min_or_max];
                this->emp_arr[min_or_max] = temp;
            }
        }

        cout << "排序成功,排序结果:" << endl;
        this->save();
        this->show_emp();
    }

    //system("pause");
    //system("cls");
}


void WorkerManager::clean_file(){
    cout << "确定清空吗?不可恢复喔!(1-清空;2-返回)" << endl;
    int select = 0;
    cin >> select;

    if(select == 1){
        ofstream ofs(FILENAME, ios::trunc); // 删除文件后重新创建新的空文件
        ofs.close();

        if(this->emp_arr != NULL){
            for(int i=0; i<this->emp_num; i++){
                if(this->emp_arr[i] != NULL){
                    delete this->emp_arr[i]; // 删除堆区的每个职员对象
                }
            }
            delete[] this->emp_arr; // 删除堆区数组指针
            this->emp_arr = NULL;
        }

        this->emp_num = 0;
        this->file_is_empty = true;
        cout << "清空成功" << endl;
    }else if(select == 2){
        system("pause");
        system("cls");
        return;
    }else{
        cout << "输入选项有误" << endl;
    }

    system("pause");
    system("cls");
}


WorkerManager::~WorkerManager(){
    if(this->emp_arr != NULL){
        for(int i=0; i<this->emp_num; i++){
            if(this->emp_arr[i] != NULL){
                delete this->emp_arr[i];
            }
        }
        delete[] this->emp_arr;
        this->emp_arr = NULL;
    }
}

职工管理系统.cpp

 

#include<stdlib.h>
#include<iostream>
#include<string>

using namespace std;

#include "worker_manager.h"
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"

// ================================================================================================

void run(){
    WorkerManager wm;

    int choice = 0;
    while(true){
        wm.show_menu();
        cout << "请输入您的选择:" << endl;
        cin >> choice;

        switch(choice){
        case 0: // 退出
            wm.exit_system();
            break;
        case 1: // 增加
            wm.add_emp();
            break;
        case 2: // 显示
            wm.show_emp();
            break;
        case 3: // 删除
            /*
            //测试
            {
                 int re = wm.is_exit(5);
                 if(re != -1){
                    cout << "职员存在" << endl;
                 }else{
                    cout << "职员不存在" << endl;
                 }
            }
            */
            wm.del_emp();
            break;
        case 4: // 修改
            wm.update_emp();
            break;
        case 5: // 查找
            wm.find_emp();
            break;
        case 6: // 排序
            wm.sort_emp();
            break;
        case 7: // 清空
            wm.clean_file();
            break;
        default:
            system("cls");
            break;
        }
    }
}

// ================================================================================================

void test(){
    Worker * worker = NULL;
    worker = new Emplyee(1, "张三", 1);
    worker->show_info();
    delete worker;

    worker = new Manager(2, "李四", 2);
    worker->show_info(); // 多态表现:不同的对象调用统一函数,有不同的输出
    delete worker;

    worker = new Boss(3, "王五", 3);
    worker->show_info();
    delete worker;
}

// ================================================================================================

int main(){
    run();
    //test();

    system("pause");
    return 0;
}

 

posted @   yub4by  阅读(153)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示