吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1998|回复: 19
收起左侧

[C&C++ 原创] 职工管理系统

[复制链接]
hzh193536 发表于 2024-8-29 13:48
CMakeList.txt
[C++] 纯文本查看 复制代码
cmake_minimum_required(VERSION 3.19)
project(main)

set(CMAKE_CXX_STANDARD 14)

include_directories(.)

add_executable(main
        main.cpp
        worker.h
        workerManager.cpp
        workerManager.h employee.cpp employee.h manager.cpp manager.h boss.cpp boss.h)


main.cpp
[C++] 纯文本查看 复制代码
#include "workerManager.h"

int main() {
    WorkerManager wm;
    int choice = 0;

    while (true) {
        wm.showMenu();
        cout << "请输入您的选择:";
        cin >> choice;

        switch (choice) {
        case 0:
            wm.exitSystem();
            break;
        case 1:
            wm.addEmp();
            break;
        case 2:
            wm.showEmpInfo();
            break;
        case 3:
            wm.delEmp();
            break;
        case 4:
            wm.modEmp();
            break;
        case 5:
            wm.findEmp();
            break;
        case 6:
            wm.sortEmp();
            break;
        case 7:
            wm.cleanEmp();
            break;
        default:
            system("clear");
            break;
        }
    }

    return 0;
}


workerManager.h
[C++] 纯文本查看 复制代码
#pragma once
#include "worker.h"
#include <iostream>
#include <string>
#include <fstream>

#define FILENAME "empFile.txt"

using namespace std;

class WorkerManager {
public:
    WorkerManager();

    void showMenu();

    void exitSystem();

    void addEmp();

    void saveFile();

    int getEmpNumFromFile();

    void initEmpArray();

    void showEmpInfo();

    int isExist(int id); // 根据职工编号判断是否存在,返回数组下标,-1表示不存在

    void delEmp();

    void modEmp();

    void findEmp();

    void sortEmp();

    void cleanEmp();

    ~WorkerManager();
private:
    int empNum;
    Worker **empArr; // Worker *的指针数组,多态保存每种职工
};


workerManager.cpp
[C++] 纯文本查看 复制代码
#include "workerManager.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"

WorkerManager::WorkerManager() {
    int num = this->getEmpNumFromFile();
    // 文件不存在或者内容为空
    if (num == 0) {
        this->empNum = 0;
        this->empArr = NULL;
        return;
    }

    this->empNum = num;
    this->empArr = new Worker*[num]; // 申请数组空间
    this->initEmpArray(); // 初始化职工信息
}

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

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

Worker *workerFactory(int id, string name, int deptId) {
    Worker *worker = NULL;

    switch (deptId) {
        case 1: // 普通职工
            worker = new Employee(id, name, 1);
            break;
        case 2: // 经理
            worker = new Manager(id, name, 2);
            break;
        case 3: // 老板
            worker = new Boss(id, name, 3);
            break;
        default:
            break;
    }

    return worker;
}

void WorkerManager::addEmp() {
    cout << "请输入增加职工数量:" << endl;

    int addNum = 0;
    cin >> addNum;

    if (addNum <= 0) {
        cout << "输入有误" << endl;
        return;
    }

    // 计算新空间大小
    int newSize = this->empNum + addNum;
    // 开辟新空间
    Worker **newSpace = new Worker *[newSize];
    // 将原空间里内容拷贝到新空间下
    if (this->empArr != NULL) {
        for (int i = 0; i < this->empNum; ++i) {
            newSpace[i] = this->empArr[i];
        }
    }
    // 输入新数据
    for (int i = 0; i < addNum; ++i) {
        int id;
        string name;
        int deptId;

        cout << "请输入第 " << i + 1 << " 个新职工编号:" << endl;
        cin >> id;

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

        cout << "请选择该职工的岗位:" << endl;
        cout << "1、普通职工" << endl;
        cout << "2、经理" << endl;
        cout << "3、老板" << endl;
        cin >> deptId;

        Worker *worker = workerFactory(id, name, deptId);
        newSpace[this->empNum + i] = worker;
    }

    // 释放原有空间
    delete[] this->empArr;
    // 更改新空间指向
    this->empArr = newSpace;
    // 更新员工个数
    this->empNum = newSize;
    // 保存数据到文件
    this->saveFile();

    cout << "成功添加" << addNum << "名新职工" << endl;
}

void WorkerManager::saveFile() {
    ofstream ofs(FILENAME, ios::out);

    for (int i = 0; i < this->empNum; ++i) {
        ofs << this->empArr[i]->getId() << " "
            << this->empArr[i]->getName() << " "
            << this->empArr[i]->getDeptId() << endl;
    }

    ofs.close();
}

int WorkerManager::getEmpNumFromFile() {
    int empNumFromFile = 0;
    ifstream ifs(FILENAME, ios::in);

    // 文件不存在,返回职工数0
    if (!ifs.is_open()) {
        ifs.close();
        return empNumFromFile;
    }

    // 读一个字符,看文件是否到eof,判断文件是否为空
//    char ch;
//    ifs >> ch;
//    if (ifs.eof())

    int id;
    string name;
    int deptId;

    while (ifs >> id && ifs >> name && ifs >> deptId) {
        empNumFromFile++;
    }

    ifs.close();
    return empNumFromFile;
}

void WorkerManager::initEmpArray() {
    ifstream ifs(FILENAME, ios::in);

    int id;
    string name;
    int deptId;
    int index = 0;

    while (ifs >> id && ifs >> name && ifs >> deptId) {
        Worker *worker = workerFactory(id, name, deptId);
        this->empArr[index++] = worker;
    }

    ifs.close();
}

void WorkerManager::showEmpInfo() {
    if (this->empNum == 0) {
        cout << "文件不存在或记录为空!" << endl;
        return;
    }

    for (int i = 0; i < this->empNum; ++i) {
        // 利用多态调用接口
        this->empArr[i]->showInfo();
    }
}

int WorkerManager::isExist(int id) {
    int index = -1;

    for (int i = 0; i < this->empNum; ++i) {
        if (this->empArr[i]->getId() == id) {
            index = i;
            break;
        }
    }

    return index;
}

void WorkerManager::delEmp() {
    if (this->empNum == 0) {
        cout << "职工记录为空" << endl;
        return;
    }

    cout << "请输入想要删除的职工编号:" << endl;
    int id = 0;
    cin >> id;

    int index = this->isExist(id);
    if (index == -1) {
        cout << "删除失败,未找到该职工" << endl;
        return;
    }

    // 删除 index 位置的数据
    for (int i = index; i < this->empNum - 1; i++) {
        this->empArr[i] = this->empArr[i + 1];
    }
    this->empNum--;
    // 删除后数据同步到文件中
    this->saveFile();
    cout << "删除成功" << endl;
}

void WorkerManager::modEmp() {
    if (this->empNum == 0) {
        cout << "职工记录为空" << endl;
        return;
    }

    cout << "请输入修改职工的编号:" << endl;
    int id;
    cin >> id;

    int ret = this->isExist(id);
    if (ret == -1) {
        cout << "修改失败,查无此人" << endl;
        return;
    }

    // 找到编号的职工,先释放空间
    delete this->empArr[ret];

    int newId;
    string name;
    int deptId;

    cout << "查到:" << id << "号职工,请输入新职工号:" << endl;
    cin >> newId;
    
    cout << "请输入新职工姓名:" << endl;
    cin >> name;

    cout << "请选择该职工的岗位:" << endl;
    cout << "1、普通职工" << endl;
    cout << "2、经理" << endl;
    cout << "3、老板" << endl;
    cin >> deptId;

    Worker *worker = workerFactory(newId, name, deptId);
    this->empArr[ret] = worker;

    this->saveFile();
    cout << "修改成功" << endl;
}

void WorkerManager::findEmp() {
    if (this->empNum == 0) {
        cout << "职工记录为空" << endl;
        return;
    }

    cout << "请输入查找的方式:" << endl;
    cout << "1、按职工编号查找" << endl;
    cout << "2、按姓名查找" << endl;

    int select = 0;
    cin >> select;

    if (select == 1) {
        cout << "请输入查找的职工编号:" << endl;
        int id;
        cin >> id;

        int ret = this->isExist(id);
        if (ret != -1) {
            this->empArr[ret]->showInfo();
        } else {
            cout << "查无此人" << endl;
        }
    } else if (select == 2) {
        cout << "请输入查找的姓名:" << endl;
        string name;
        cin >> name;

        bool found = false;
        for (int i = 0; i < this->empNum; i++) {
            if (this->empArr[i]->getName() == name) {
                found = true;
                this->empArr[i]->showInfo();
            }
        }

        if (!found) {
            cout << "查无此人" << endl;
        }
    } else {
        cout << "输入选项有误" << endl;
    }
}

void WorkerManager::sortEmp() {
    if (this->empNum == 0) {
        cout << "职工记录为空" << endl;
        return;
    }

    cout << "请选择排序方式:" << endl;
    cout << "1、按职工号进行升序" << endl;
    cout << "2、按职工号进行降序" << endl;

    int select = 0;
    cin >> select;

    // 选择排序
    for (int i = 0; i < this->empNum - 1; i++) {
        int minOrMax = i;
        for (int j = i + 1; j < this->empNum; j++) {
            if (select == 1) { // 升序
                if (this->empArr[minOrMax]->getId() > this->empArr[j]->getId()) {
                    minOrMax = j;
                }
            } else { // 降序
                if (this->empArr[minOrMax]->getId() < this->empArr[j]->getId()) {
                    minOrMax = j;
                }
            }
        }

        // 交换位置
        if (minOrMax != i) {
            Worker *tmp = this->empArr[i];
            this->empArr[i] = this->empArr[minOrMax];
            this->empArr[minOrMax] = tmp;
        }
    }

    cout << "排序成功,排序后结果为:" << endl;
    this->saveFile();
    this->showEmpInfo();
}

void WorkerManager::cleanEmp() {
    cout << "确认清空?" << endl;
    cout << "1、确认" << endl;
    cout << "2、返回" << endl;

    int select = 0;
    cin >> select;
    if (select != 1) {
        return;
    }

    // 打开模式 trunc 如果存在删除并重新创建文件
    ofstream ofs(FILENAME, ios::trunc);
    ofs.close();

    if (this->empArr != NULL) {
        for (int i = 0; i < this->empNum; i++) {
            if (this->empArr[i] != NULL) {
                delete this->empArr[i];
            }
        }
        this->empNum = 0;
        delete[] this->empArr;
        this->empArr = NULL;
    }

    cout << "清空成功!" << endl;
}

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


worker.h
[C++] 纯文本查看 复制代码
#pragma once
#include <iostream>
#include <string>

using namespace std;

class Worker {
public:
    // 显示个人信息
    virtual void showInfo() = 0;

    int getId() {
        return id;
    }

    string getName() {
        return name;
    }

    int getDeptId() {
        return deptId;
    }

    // 获取岗位名称
    string getDeptName() {
        string deptName;
        switch (deptId) {
        case 1:
            deptName = "普通职工";
            break;
        case 2:
            deptName = "经理";
            break;
        case 3:
            deptName = "老板";
            break;
        default:
            break;
        }
        return deptName;
    }

protected:
    int id;      // 职工编号
    string name; // 职工姓名
    int deptId;  // 职工所在部门名称编号
};


boss.h
[C++] 纯文本查看 复制代码
#ifndef MAIN_BOSS_H
#define MAIN_BOSS_H

#include "worker.h"

class Boss : public Worker {
public:
    Boss(int id, string name, int deptId);

    void showInfo() override;
};

#endif // MAIN_BOSS_H


boss.cpp
[C++] 纯文本查看 复制代码
#include "boss.h"

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

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


manager.h
[C++] 纯文本查看 复制代码
#ifndef MAIN_MANAGER_H
#define MAIN_MANAGER_H

#include "worker.h"

class Manager : public Worker {
public:
    Manager(int id, string name, int deptId);

    void showInfo() override;
};

#endif //MAIN_MANAGER_H


manager.cpp
[C++] 纯文本查看 复制代码
#include "manager.h"

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

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


employee.h
[C++] 纯文本查看 复制代码
#ifndef MAIN_EMPLOYEE_H
#define MAIN_EMPLOYEE_H

#include "worker.h"

class Employee: public Worker{
public:
    Employee(int id, string name, int deptId);

    void showInfo() override;
};

#endif //MAIN_EMPLOYEE_H


employee.cpp
[C++] 纯文本查看 复制代码
#include "employee.h"

Employee::Employee(int id, string name, int deptId) {
    // 给从基类继承过来的属性赋值,不能使用参数列表的方式 id(id)
    this->id = id;
    this->name = name;
    this->deptId = deptId;
}

void Employee::showInfo() {
    cout << "职工编号:" << id
        << "\t职工姓名:" << name
        << "\t岗位:" << getDeptName()
        << "\t岗位职责:完成经理交给的任务" << endl;
}

免费评分

参与人数 6吾爱币 +12 热心值 +5 收起 理由
utnxh + 1 + 1 谢谢@Thanks!
yy20240721 + 1 + 1 鼓励转贴优秀软件安全工具和文档!
q7505413 + 1 + 1 我很赞同!
纯之风韵 + 1 + 1 我很赞同!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
ailmail + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

durongze 发表于 2024-11-26 18:00
大佬可以帮我看一下我这个工程为啥编译不过吗?有链接对应的库,但是就是编译不过。

链接: https://pan.baidu.com/s/1AHPhQ-33o3mtCbFPH418WA?pwd=4hrf 提取码: 4hrf
报错的工程是 test_3rdlib_app 和 test_3rdlib_shared。
-------------------------------------------------------------------------------------------
3:58:41:318        1>MSVCRTD.lib(init.obj) : error LNK2019: unresolved external symbol _CrtDbgReport referenced in function _CRT_RTC_INIT
13:58:41:318        1>MSVCRTD.lib(init.obj) : error LNK2019: unresolved external symbol _CrtDbgReportW referenced in function _CRT_RTC_INITW
13:58:41:318        1>MSVCRTD.lib(error.obj) : error LNK2019: unresolved external symbol strcpy_s referenced in function "void __cdecl _RTC_StackFailure(void *,char const *)" (?_RTC_StackFailure@@YAXPEAXPEBD@Z)
13:58:41:318        1>MSVCRTD.lib(error.obj) : error LNK2019: unresolved external symbol strcat_s referenced in function "void __cdecl _RTC_StackFailure(void *,char const *)" (?_RTC_StackFailure@@YAXPEAXPEBD@Z)
13:58:41:318        1>MSVCRTD.lib(error.obj) : error LNK2019: unresolved external symbol __stdio_common_vsprintf_s referenced in function _vsprintf_s_l
13:58:41:318        1>MSVCRTD.lib(error.obj) : error LNK2001: unresolved external symbol __C_specific_handler_noexcept
13:58:41:318        1>MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol _wmakepath_s referenced in function "int __cdecl GetPdbDllPathFromFilePath(wchar_t const *,wchar_t *,unsigned __int64)" (?GetPdbDllPathFromFilePath@@YAHPEB_WPEA_W_K@Z)
13:58:41:318        1>MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol _wsplitpath_s referenced in function "int __cdecl GetPdbDllPathFromFilePath(wchar_t const *,wchar_t *,unsigned __int64)" (?GetPdbDllPathFromFilePath@@YAHPEB_WPEA_W_K@Z)
13:58:41:318        1>MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol wcscpy_s referenced in function "int __cdecl GetPdbDllPathFromFilePath(wchar_t const *,wchar_t *,unsigned __int64)" (?GetPdbDllPathFromFilePath@@YAHPEB_WPEA_W_K@Z)
13:58:41:318        1>MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __vcrt_GetModuleFileNameW referenced in function "struct HINSTANCE__ * __cdecl GetPdbDll(void)" (?GetPdbDll@@YAPEAUHINSTANCE__@@XZ)
13:58:41:318        1>MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __vcrt_GetModuleHandleW referenced in function "struct HINSTANCE__ * __cdecl GetPdbDll(void)" (?GetPdbDll@@YAPEAUHINSTANCE__@@XZ)
13:58:41:318        1>MSVCRTD.lib(pdblkup.obj) : error LNK2019: unresolved external symbol __vcrt_LoadLibraryExW referenced in function "struct HINSTANCE__ * __cdecl GetPdbDll(void)" (?GetPdbDll@@YAPEAUHINSTANCE__@@XZ)
13:58:41:318        1>BuildSrc\Debug\test_3rdlib.dll : fatal error LNK1120: 17 unresolved externals
尚三宝 发表于 2024-8-29 16:54
hylkxj 发表于 2024-8-29 17:02
chh322 发表于 2024-8-29 17:05
效果图看看
someonewhite 发表于 2024-8-29 17:18
有点像之前看过的一个教学视频的例子
ccxk 发表于 2024-8-29 17:20
有点像初学者的实战第一个案例
Ditto 发表于 2024-8-29 17:27
想起了上课时学的学生管理系统
ailmail 发表于 2024-8-29 21:18
谢谢提供,收藏学习
xiaohui13 发表于 2024-8-29 21:21
初学者也能看懂
smallppgirl 发表于 2024-8-29 22:54
好像学完c语言后,老师暑假安排的作业。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-12-15 03:23

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表