C++ 管理系统实战

C++ 管理系统实战

1. 目录结构

2. workClass.h


cpp 复制代码
/**
  ******************************************************************************
  * @file           : employee.h
  * @author         : zhong
  * @brief          : None
  * @attention      : None
  * @date           : 2023/8/10
  ******************************************************************************
  */

#include "workManager.h"

/*!
 * 普通员工类
 */
class Employee : public Work{
public:
    Employee(int id, string name, int dept){
        this->m_Id = id;
        this->m_Name = name;
        this->m_DeptId = dept;
    }
    void showWorkerInfo() override {
        cout << "职工编号: " << this->m_Id
             << "\t职工姓名: " << this->m_Name
             << "\t岗位信息: " << this->getDeptName()
             << "\t岗位职责: " << this->workInfo << endl;
    }

    string getDeptName() override {
        return "总裁";
    }
private:
    string workInfo = "管理公司所有事务";
};


/*!
 * 经理类
 */
class Manager : public Work{
public:
    Manager(int id, string name, int dept){
        this->m_Id = id;
        this->m_Name = name;
        this->m_DeptId = dept;
    }
    void showWorkerInfo() override {
        cout << "职工编号: " << this->m_Id
             << "\t职工姓名: " << this->m_Name
             << "\t岗位信息: " << this->getDeptName()
             << "\t岗位职责: " << this->workInfo << endl;
    }

    string getDeptName() override {
        return "经理";
    }
private:
    string workInfo = "管理员工";
};

/*!
 * 老板类
 */
class Boos : public Work{
public:
    Boos(int id, string name, int dept){
        this->m_Id = id;
        this->m_Name = name;
        this->m_DeptId = dept;
    }
    void showWorkerInfo() override {
        cout << "职工编号: " << this->m_Id
             << "\t职工姓名: " << this->m_Name
             << "\t岗位信息: " << this->getDeptName()
             << "\t岗位职责: " << this->workInfo << endl;
    }

    string getDeptName() override {
        return "员工";
    }
private:
    string workInfo = "工作";
};

3. workFunction.h


cpp 复制代码
/**
  ******************************************************************************
  * @file           : workFunction.h
  * @author         : zhong
  * @brief          : None
  * @attention      : None
  * @date           : 2023/8/11
  ******************************************************************************
  */


#include "workManager.h"
#include "workClass.h"
#define FILEPATH "workManager/work.txt"

/*!
 * 添加用户
 */
void WorkManager::addWorkInfo() {
    cout << "请输入添加职工的数量: " << endl;
    int addMuch = 0;
    cin >> addMuch;
    if(addMuch > 0){
        // 添加 计算新空间的大小
        int newSize = this->m_EmpNum + addMuch;
        Work  **newSpace = new Work * [newSize];
        if (this->pWorkArray != NULL){
            for (int i = 0; i < this->m_EmpNum; ++i) {
                newSpace[i] = this->pWorkArray[i];
            }
        }
        // 批量添加新数据
        for (int i = 0; i < addMuch; ++i) {
            int id;         // 职工id
            string name;    // 职工姓名
            int dSelect;    // 职工部门
            cout << "请输入第" << i+1 << "个员工职工的ID: " << endl;
            cin >> id;
            cout << "请输入第" << i+1 << "职工姓名: " << endl;
            cin >> name;
            cout << "请选择岗位职位: " << endl;
            cout << "1、普通员工: " << endl;
            cout << "2、经理: " << endl;
            cout << "3、老板: " << endl;
            cin >> dSelect;

            Work *work = NULL;
            switch (dSelect) {
                case 1:
                    work = new Employee(id, name, 1);
                    break;
                case 2:
                    work = new Manager(id, name, 2);
                    break;
                case 3:
                    work = new Boos(id, name, 3);
                    break;
                default:
                    cout << "输入有误!" << endl;
                    break;
            }

            newSpace[this->m_EmpNum + i] = work;
        }
        // 释放原有堆的空间
        delete [] this->pWorkArray;

        // 更改新空间的指向
        this->pWorkArray = newSpace;

        // 更新员工人数
        this->m_EmpNum = newSize;

        // 更新职工不为空
        this->fileIsHave = false;

        // 保存员工信息
        WorkManager::saveFile();

        // 提示添加成功
        cout << "你已成功添加 " << addMuch << " 名职工!" << endl;
    }else{
        cout << "输入有误!" << endl;
    }

    // 按任意键清屏回到上级
    system("pause");
    system("cls");
}
/*!
 * 读取员工信息
 */
void WorkManager::showWorkInfo() {
    if(this->fileIsHave){
        cout << "文件没有数据!" << endl;
        return;
    }
    // 按照 ID 排序
    for (int i = 0; i < this->m_EmpNum; ++i) {
        for (int j = 0; j < i; ++j) {
            Work *work = NULL;
            // <" 升序 ">" 降序
            if (this->pWorkArray[i]->m_Id < this->pWorkArray[j]->m_Id){
                work = this->pWorkArray[i];
                this->pWorkArray[i] = this->pWorkArray[j];
                this->pWorkArray[j] = work;
            }
        }
    }
    // 输出排序后的内容
    for (int i = 0; i < this->m_EmpNum; ++i) {
        this->pWorkArray[i]->showWorkerInfo();
    }
    system("pause");
    system("cls");
}

/*!
 * 保存工信息
 */
void WorkManager::saveFile() {
    ofstream ofs;
    ofs.open(FILEPATH, ios::out);
    if(!ofs.is_open()){
        cout << "文件不存在或者文件为空!" << endl;
        return ;
    }
    for (int i = 0; i < this->m_EmpNum; ++i) {
        ofs << this->pWorkArray[i]->m_Id << " "
            << this->pWorkArray[i]->m_Name << " "
            << this->pWorkArray[i]->m_DeptId << endl;
    }
    ofs.close();
}
/*!
 * 获取文件里员工个数
 * @return 员工个数
 */
int WorkManager::getFileMenNum() {
    ifstream ifs;
    ifs.open(FILEPATH, ios::in);
    int id;
    string name;
    int dept;
    int count=0;
    while (ifs >> id && ifs >> name && ifs >> dept){
        count++;
    }
    ifs.close();
    return count;
}
/*!
 * 获取文件里的员工信息
 */
void WorkManager::getFileWork() {
    ifstream ifs;
    ifs.open(FILEPATH, ios::in);
    int id;
    string name;
    int dept;
    int count=0;
    while (ifs >> id && ifs >> name && ifs >> dept){
        Work *work = NULL;
        if (dept == 1){
            work = new Employee(id, name, dept);
        }else if (dept == 2){
            work = new Manager(id, name, dept);
        }else{
            work = new Boos(id, name, dept);
        }
        this->pWorkArray[count] = work;
        count++;
    }
    ifs.close();
}
/*!
 * 删除员工信息
 */
void WorkManager::deleteFileWork() {
    cout << "请选择删除方式: \n 1.按编号删除\n 2.按姓名删除" << endl;
    int choose;
    cin >> choose;
    bool status = true;    // 判断 ID 或者 姓名 是否存在
    if (choose == 1){
        cout << "请输入员工编号: " << endl;
        int id;
        cin >> id;
        for (int i = 0; i < this->m_EmpNum; i++) {
            if (id == this->pWorkArray[i]->m_Id){
                status = false;
            }
            if(!status){
                // 数据前移
                this->pWorkArray[i]=this->pWorkArray[i+1];
            }
        }
        if(status) {
            cout << "你输入的 ID 不存在!" << endl;
        }
    }else if (choose == 2){
        cout << "请输入员工姓名: " << endl;
        string name;
        cin >> name;

        for (int i = 0; i < this->m_EmpNum; ++i) {
            if (name == this->pWorkArray[i]->m_Name){
                status = false;
            }
            if(!status){
                // 数据前移
                this->pWorkArray[i]=this->pWorkArray[i+1];
            }
        }
        if(status) {
            cout << "你输入的姓名不存在!" << endl;
            system("cls");
            return;
        }
    }else{
        cout << "你的选择有误,请重新选择!" << endl;
        system("cls");
        return;
    }

    if (!status){
        this->pWorkArray[this->m_EmpNum-1] = NULL;
        this->m_EmpNum--;
        this->saveFile();
        cout << "删除成功!" << endl;
        system("pause");
        system("cls");
    }
}

/*!
 * 更改员工信息
 */
void WorkManager::updateWorkInfo() {

    if(this->fileIsHave) {
        cout << "文件不存在或者为空!" << endl;
        return;
    }

    int id;
    cout << "请输入需要修改信息的 ID: " << endl;
    cin >> id;

    for (int i = 0; i < this->m_EmpNum; ++i) {
        if(this->pWorkArray[i]->m_Id == id){
            cout << "请输入需要修改的内容: \n 1.姓名\n 2.职业" << endl;
            int choose;
            cin >> choose;
            switch (choose) {
                case 1: {
                    cout << "请输入修改后的姓名: " << endl;
                    string name;
                    cin >> name;
                    pWorkArray[i]->m_Name = name;
                    cout << "修改成功!" << endl;
                    system("pause");
                    system("cls");
                    break;
                }
                case 2: {
                    cout << "请输入修改后的部门 ID: " << endl;
                    cout << "1、普通员工: " << endl;
                    cout << "2、经理: " << endl;
                    cout << "3、老板: " << endl;
                    int dept;
                    cin >> dept;
                    pWorkArray[i]->m_DeptId = dept;
                    cout << "修改成功!" << endl;
                    system("pause");
                    system("cls");
                    break;
                }
                default: {
                    cout << "选择错误!" << endl;
                    return;
                }
            }
        }else{
            cout << "未找到相关 ID 的信息" << endl;
            return;
        }
    }
    this->saveFile();
}

/*!
 * 查找员工信息
 */
void WorkManager::findWorkInfo() {
    int id;
    cout << "请输入员工ID查询: " << endl;
    cin >> id;
    bool status = false;

    for (int i = 0; i < this->m_EmpNum; ++i) {
        if(this->pWorkArray[i]->m_Id == id){
            this->pWorkArray[i]->showWorkerInfo();
            status = true;
        }
    }
    if(!status){
        cout << "未查询到该 ID 信息" << endl;
        system("pause");
        system("cls");
    }

}

/*!
 * 按编号排序
 */
void WorkManager::sortWorkInfo() {
    for (int i = 0; i < this->m_EmpNum; ++i) {
        for (int j = 0; j < i; ++j) {
            Work * work = NULL;
            if(this->pWorkArray[i]->m_Id > this->pWorkArray[j]->m_Id){
                work = this->pWorkArray[i];
                this->pWorkArray[i] = this->pWorkArray[j];
                this->pWorkArray[j] = work;
            }
        }
    }
    this->saveFile();
}

/*!
 * 清空所有信息
 */
void WorkManager::deleteAllWorkInfo() {
    int enterDelete = 0;
    cout << "1.确认删除 \t其他任意键撤销"  << endl;
    cin >> enterDelete;
    if(enterDelete == 1){
        this->pWorkArray = NULL;
        this->m_EmpNum = 0;
        this->saveFile();
        cout << "清空所有信息成功!" << endl;
        delete [] this->pWorkArray;
        this->fileIsHave = true;
    }
}

4. workManager.h


cpp 复制代码
/**
  ******************************************************************************
  * @file           : workManager.h
  * @author         : zhong
  * @brief          : None
  * @attention      : None
  * @date           : 2023/8/10
  ******************************************************************************
  */

#include <bits/stdc++.h>

using namespace std;

class Work;

/*!
 * 菜单实现
 */
class WorkManager {
public:
    // 构造函数
    WorkManager();

    int m_EmpNum;       // 记录职工人数

    Work ** pWorkArray;      // 职工数组指针

    bool fileIsHave = false ;        // 文件是否存在


    //添加职工
    void addWorkInfo();

    // 读取文件
    void readFile();

    // 获取文件里员工人数
    int getFileMenNum();

    // 获取文件里的员工信息
    void getFileWork();

    // 删除员工信息
    void deleteFileWork();

    // 修改员工信息
    void updateWorkInfo();

    // 查找员工信息
    void findWorkInfo();

    // 按编号排序
    void sortWorkInfo();

    // 清空所有信息
    void deleteAllWorkInfo();

    // 保存文件
    void saveFile();

    // 显示员工信息
    void showWorkInfo();

    // 菜单
    void showMenu();

    // 退出系统
    void exitSystem();

    // 析构函数
    ~WorkManager();
};

/*!
 * 员工类
 */
class Work {
public:
    int m_Id;           // 员工 ID
    string m_Name;      // 员工姓名
    int m_DeptId;       // 员工编号

    /*!
     * 显示个人信息
     */
    virtual void showWorkerInfo() = 0;

    /*!
     * 获取岗位名称
     * @return 岗位名称
     */
    virtual string getDeptName() = 0;

};

5. workManager.cpp


cpp 复制代码
/**
  ******************************************************************************
  * @file           : workManager.cpp
  * @author         : zhong
  * @brief          : None
  * @attention      : None
  * @date           : 2023/8/10
  ******************************************************************************
  */

#include "workManager.h"
#include <bits/stdc++.h>
#include "workClass.h"
#include "workFunction.h"
#define FILEPATH "workManager/work.txt"

using namespace std;
/*!
 * 构造函数实现
 */
WorkManager::WorkManager() {
    // 初始化属性 文件不存在
    ifstream ifs;
    ifs.open(FILEPATH, ios::in);
    if(!ifs.is_open()){
//        cout << "文件不存在" << endl;
        this->fileIsHave = true;   // 文件初始化为不存在
        this->m_EmpNum = 0;         // 初始化职工细信息
        this->pWorkArray = NULL;    // 初始哈数组指针
        ifs.close();
        return;
    }
   // 初始化属性 文件存在但为空
    char ch;
    ifs >> ch;
    if (ifs.eof()){
//        cout << "文件为空" << endl;
        this->fileIsHave = true;   // 文件初始化为不存在
        this->m_EmpNum = 0;         // 初始化职工细信息
        this->pWorkArray = NULL;    // 初始哈数组指针
        ifs.close();
        return;
    }

    // 初始化属性 文件存在且有值
    cout << this->getFileMenNum() << endl;
    this->m_EmpNum = this->getFileMenNum();
    // 开辟空间
    this->pWorkArray = new Work *[this->m_EmpNum];
    // 初始化所有员工信息
    this->getFileWork();
}

/*!
 * 退出系统
 */
void WorkManager::exitSystem() {
    cout << "欢迎下次使用......" << endl;
    system("pause");
    exit(0);
}

/*!
 * 析构函数实现  释放堆区数据
 */
WorkManager::~WorkManager() {
    if(this->pWorkArray!=NULL){
        delete[] this->pWorkArray;
        this->pWorkArray = NULL;
    }
}



/*!
 * 显示菜单
 */
void WorkManager::showMenu() {
    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;
}

void test() {
    Work *work = NULL;
    work = new Employee(1,"张三", 1);
    work->showWorkerInfo();
    delete work;

    work = new Manager(2, "李四", 2);
    work->showWorkerInfo();
    delete work;

    work = new Boos(3, "王五", 3);
    work->showWorkerInfo();
    delete work;
}

int main() {
    WorkManager wm;
    char choose = '0';

    while (true) {
        wm.showMenu();
        cout << "请输入你的选择: " << endl;
        cin >> choose;
        switch (choose) {
            case '0':
                wm.exitSystem();
                break;
            case '1':
                wm.addWorkInfo();
                break;
            case '2':
                wm.showWorkInfo();
                break;
            case '3':
                wm.deleteFileWork();
                break;
            case '4':
                wm.updateWorkInfo();
                break;
            case '5':
                wm.findWorkInfo();
                break;
            case '6':
                wm.sortWorkInfo();
                break;
            case '7':
                wm.deleteAllWorkInfo();
                break;
            default:
                system("cls");
                break;
        }
    }


    system("pause");
    return 0;
}

感谢观看祝你生活愉快

相关推荐
晨曦_子画几秒前
C#中:Any() 与 Count,选择哪一个??
开发语言·windows·c#
m0_676099585 分钟前
Python高阶函数以及装饰器
开发语言·python
丶Darling.38 分钟前
Day39 | 动态规划 :完全背包应用 零钱兑换&&零钱兑换II
c++·算法·动态规划
Mr_Xuhhh40 分钟前
递归搜索与回溯算法--递归(2)
开发语言·数据结构·c++·算法·链表·深度优先
上趣工作室2 小时前
uniapp中使用全局样式文件引入的三种方式
开发语言·rust·uni-app
提笔惊蚂蚁2 小时前
java-web-苍穹外卖-day1:软件开发步骤简化版+后端环境搭建
java·开发语言·前端·程序人生
Ethan Hunt丶2 小时前
C语言实现IIR型零相位带通滤波器
c语言·开发语言·算法
customer082 小时前
【开源免费】基于SpringBoot+Vue.JS美发门店管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·kafka·开源
梓羽玩Python3 小时前
5.3K Star 烦恼全丢掉!GlazeWM:让Windows下的窗口管理更高效的秘密武器!
开源·github
liang081143 小时前
C# DataTable使用Linq查询详解
开发语言·c#