项目结构

  1. 头文件(Header Files)

    • employee.h: 定义了一个基本的职工类。
    • manage.h: 继承自 Employee 类,用于管理员工。
    • boss.h: 同样继承自 Employee 类,代表公司的高层管理人员。
    • workermanager.h: 主要管理类,负责整体的职工信息管理。
  2. 源文件(Source Files)

    • employee.cpp: 实现了 Employee 类的方法。
    • manage.cpp: 实现了 Manage 类的方法。
    • boss.cpp: 实现了 Boss 类的方法。
    • workermanager.cpp: 实现了 WorkerManager 类的方法。
  3. 其他文件

    • main.cpp: 项目入口点,通常用于创建 WorkerManager 对象,并启动系统。

主要功能

  • 显示菜单 (ShowMenu): 提供用户交互界面,展示可用的操作选项。
  • 添加职工 (Add_Emp): 允许用户输入新的职工信息,并将其添加到系统中。
  • 删除职工 (Del_Emp): 根据输入的职工编号删除相应的职工信息。
  • 修改职工信息 (Mode_Emp): 更改现有职工的信息。
  • 查找职工 (Find_Emp): 根据编号或姓名搜索特定的职工。
  • 排序职工 (Sort_Emp): 按编号升序或降序排列职工列表。
  • 退出系统 (exitSystem): 清理资源并退出程序。

代码示例

Boss.h

#pragma once
#include<iostream>
#include<string>
#include"Woker.h"
class Boss :public Woker
{
public:
	//构造函数
	Boss(int id, string name, int dId);
	//显示个人信息
	virtual void showInfo();
	//获取岗位名称
	virtual string getDeptName();
};

Employee.h

#pragma once
#include<iostream>
#include<string>
#include"Woker.h"
class Employee:public Woker
{
public:
	//构造函数
	Employee(int id, string name, int dId);
    //显示个人信息
	virtual void showInfo();
	//获取岗位名称
	virtual string getDeptName();
};

 Manager.h

#pragma once
#include<iostream>
#include<string>
#include"Woker.h"
class Manage :public Woker
{
public:
	//构造函数
	Manage(int id, string name, int dId);
	//显示个人信息
	virtual void showInfo();
	//获取岗位名称
	virtual string getDeptName();
};

 Worker.h

#pragma once
#include<iostream>
#include<string>
using namespace std;
class Woker {
public:
	//个人信息
	virtual void showInfo() = 0;

	//岗位名称
	virtual string getDeptName() = 0;

	//职工编号
	int m_Id;

	//职工姓名
	string m_Name;

	//职工所在部门名称编号
	int m_DeptId;
};

WorkerManager.h 

#pragma once
#include<iostream>
#include<string>
#include<fstream>
#define FILENAME "empFile.txt"
using namespace std;

class WorkerMananger
{
public:
	//记录文件中的人数
	int m_EmpNum;
	//员工的指针
	Woker** m_EmpArray;
	WorkerMananger();
	void ShowMenu();
	void exitSystem();
	void Add_Emp();
	void save();
	//判断文件是否为空
	bool m_FileIsEmpty;
	//统计文件中的人数
	int getm_EmpNum();
	//初始化员工
	void init_Emp();
	//显示职工
	void Show_Emp();
	//删除职工
	void Del_Emp();
	//按照职工编号判断职工是否存在,若存在返回职工在数组中的位置,不存在返回-1
	int IsExist(int id);
	//修改职工
	void Mode_Emp();
	//查找职工
	void Find_Emp();
	//排序职工
	void Sort_Emp();
	//清空文件
	void Clean_File();


	~WorkerMananger();
};

Boss.cpp 

#include "boss.h"
Boss::Boss(int id, string name, int dId) {
	this->m_Id = id;
	this->m_Name = name;
	this->m_DeptId = dId;
}
void Boss::showInfo() {
	cout << "职工编号:" << this->m_Id
		<< "\t职工姓名:" << this->m_Name
		<< "\t岗位:" << this->m_DeptId
		<< "\t岗位职责:管理公司所有事务" << endl;
}
string Boss::getDeptName() {
	return string("总裁");
}

Employee.cpp 

#include "employee.h"
Employee::Employee(int id, string name, int dId) {
	this->m_Id = id;
	this->m_Name = name;
	this->m_DeptId = dId;
}
void Employee::showInfo() {
	cout << "职工编号:" << this->m_Id
		<< "\t职工姓名:" << this->m_Name
		<< "\t岗位:" << this->m_DeptId
		<< "\t岗位职责:完成经理交代的任务" << endl;
}
string Employee::getDeptName() {
	return string("员工");
}

 Manager.cpp

 

#include "manage.h"
Manage::Manage(int id, string name, int dId) {
	this->m_Id = id;
	this->m_Name = name;
	this->m_DeptId = dId;
}
void Manage::showInfo() {
	cout << "职工编号:" << this->m_Id
		<< "\t职工姓名:" << this->m_Name
		<<"\t岗位:"<<this->m_DeptId
		<< "\t岗位职责:完成老板交代的任务,并下发给员工" << endl;
}
string Manage::getDeptName() {
	return string("经理");
}

 ManagerWorker.cpp

#include <iostream>
#include<string>
#include"employee.h"
#include"WorkerManange.h"
#include"Woker.h"
#include"manage.h"
#include"boss.h"
#include<fstream>
#define FILENAME "empFilet.txt"
using namespace std;
WorkerMananger::WorkerMananger(){
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    if (!ifs.is_open()) {
        //cout << "文件不存在!" << endl;
        //初始化属性
        //初始化人数
        this->m_EmpNum = 0;
        //初始化数组指针为空
        this->m_EmpArray = NULL;
        //初始化文件是否为空
        this->m_FileIsEmpty = true;
        ifs.close();
        return;
    }
    //文件存在 数据为空
    char ch;
    ifs>>ch;
    if (ifs.eof()) {
        //cout << "文件为空!" << endl;
        //初始化属性
        //初始化人数
        this->m_EmpNum = 0;
        //初始化数组指针为空
        this->m_EmpArray = NULL;
        //初始化文件是否为空
        this->m_FileIsEmpty = true;
        ifs.close();
        return;
    }
    //文件存在 并且记录数据
    int num = this->getm_EmpNum();
    //cout << "职工人数为:" << num << endl;
    this->m_EmpNum = num;

    //开辟空间
    this->m_EmpArray = new Woker * [this->m_EmpNum];
    //将文件中的数据,存放到数组中
    init_Emp();

    /*for (int i = 0; i < this->m_EmpNum; i++) {
        cout << "职工编号:" << this->m_EmpArray[i]->m_Id
            << "姓名:" << this->m_EmpArray[i]->m_Name
            << "部门编号:" << this->m_EmpArray[i]->m_DeptId << endl;
    }*/
}
void WorkerMananger:: ShowMenu() {
    cout << "*********************************" << std::endl;
    cout << "* 欢迎使用职工管理系统!          *" << std::endl;
    cout << "*********************************" << std::endl;
    cout << "* 0. 退出管理程序                *" << std::endl;
    cout << "* 1. 增加职工信息                *" << std::endl;
    cout << "* 2. 显示职工信息                *" << std::endl;
    cout << "* 3. 删除离职职工                *" << std::endl;
    cout << "* 4. 修改职工信息                *" << std::endl;
    cout << "* 5. 查找职工信息                *" << std::endl;
    cout << "* 6. 按照编号排序                *" << std::endl;
    cout << "* 7. 清空所有文档                *" << std::endl;
    cout << "*********************************" << std::endl;
}
void WorkerMananger::exitSystem() {
    cout << "欢迎下次使用!" << endl;
    system("pause");
    exit(0);
}
void WorkerMananger::Add_Emp() {
    cout << "请输入增加职工的数量:" << endl;
    int addNum=0;
    cin >> addNum;
    if (addNum > 0) {
        //计算新空间的大小
        int newSize = this->m_EmpNum + addNum;
        //开辟新空间
        Woker** newSpace = new Woker * [newSize];
        //将原空间的内容放到新空间
        if (this->m_EmpArray != NULL) {
            for (int i = 0; i < m_EmpNum; i++) {
                newSpace[i] = this->m_EmpArray[i];
            }
        }
        for (int i = 0; i < addNum; i++) {
            int id;
            string name;
            int dSelect;
            cout << "请输入第"<<i+1<<"个新职工编号" << endl;
            cin >> id;

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

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

            Woker* worker = NULL;
            switch (dSelect) {
            case 1:
               worker = new Employee(id, name, dSelect);
                break;
            case 2:
                worker = new Manage(id, name, dSelect);
                break;
            case 3:
                worker = new Boss(id, name, dSelect);
                break;
            default:
                break;
            }
            newSpace[this->m_EmpNum + i] = worker;
        }
        //释放原有空间
        delete[] this->m_EmpArray;
        //更改新空间的指向
        this->m_EmpArray = newSpace;
        //更新新的个数
        this->m_EmpNum = newSize;
        //文件不为空
        this->m_FileIsEmpty = false;
        //成功添加
        cout << "成功添加" << addNum << "名新职工" << endl;
        //保存数据
        this->save();
    }
    else {
        cout << "输入有误" << endl;
    }
}
void WorkerMananger::save() {
    ofstream ofs;
    ofs.open(FILENAME, ios::out);
    for (int i = 0; i < this->m_EmpNum; i++) {
        ofs << this->m_EmpArray[i]->m_Id << " "
            << this->m_EmpArray[i]->m_Name << " "
            << this->m_EmpArray[i]->m_DeptId << endl;
            
    }
    ofs.close();
}
//统计文件中的人数
int WorkerMananger::getm_EmpNum() {
    ifstream ifs;
    ifs.open(FILENAME, ios::in);//打开文件 读
    int id;
    string name;
    int dId;
    //定义人数
    int num=0;
    while (ifs >> id && ifs >> name && ifs >> dId) {
        //统计人数
        num++;
    }
    return num;
}
//初始化员工
void WorkerMananger::init_Emp() {
    ifstream ifs;
    ifs.open(FILENAME, ios::in);//打开文件 读
    int id;
    string name;
    int dId;
    //数组索引
    int index=0;
    while (ifs >> id && ifs >> name && ifs >> dId) {
        Woker* worker = NULL;
        if (dId == 1) {
            worker = new Employee(id, name, dId);
        }
        else if (dId == 2) {
            worker = new Manage(id, name, dId);
        }
        else {
            worker = new Boss(id, name, dId);
        }
        this->m_EmpArray[index] = worker;
        index++;
    }
    ifs.close();
}
//显示职工
void WorkerMananger::Show_Emp() {
    if (this->m_FileIsEmpty) {
        cout << "文件不存在或记录为空!" << endl;
    }
    else {
        for (int i = 0; i < m_EmpNum; i++) {
            //利用多态调用接口
            this->m_EmpArray[i]->showInfo();
        }
    }
    system("pause");
    system("cls");
}
//删除职工
void WorkerMananger::Del_Emp() {
    if (this->m_FileIsEmpty) {
        cout << "文件不存在或记录为空!" << endl;
    }
    else {
        cout << "请输入想要删除的职工号:" << endl;
        int id = 0;
        cin >> id;
        int index = IsExist(id);
        if (index != -1) {
            for (int i = index; i < m_EmpNum - 1; i++) {
                this->m_EmpArray[i] = this->m_EmpArray[i + 1];
            }
            this->m_EmpNum--;
            this->save();
            cout << "删除成功!" << endl;
        }
        else {
            cout << "未找到员工!" << endl;
        }
    }
    //按任意键清屏
    system("pause");
    system("cls");
}

//按照职工编号判断职工是否存在,若存在返回职工在数组中的位置,不存在返回-1
int WorkerMananger::IsExist(int id) {
    int index = -1;
    for (int i = 0; i < this->m_EmpNum; i++) {
        if (this->m_EmpArray[i]->m_Id == id) {
            index = i;
            break;
        }
    }
    return index;
}
//修改职工
void WorkerMananger::Mode_Emp() {
    if (this->m_FileIsEmpty) {
        cout << "文件不存在或记录为空!" << endl;
    }
    else {
        cout << "请输入想要修改的职工号:" << endl;
        int id;
        cin >> id;

        int ret = this->IsExist(id);
        if (ret != -1){
            delete this->m_EmpArray[ret];
            int newId = 0;
            string newName = "";
            int dSelect = 0;
            cout << "查到:" << id << "号职工,请输入新的职工号:" << endl;
            cin >> newId;

            cout << "请输入想要修改的职工姓名:" << endl;
           
            cin >> newName;

            cout << "请选择新的职工的岗位:" << endl;
            cout << "1.普通职工" << endl;
            cout << "2.经理" << endl;
            cout << "3.老板" << endl;
         
            cin >> dSelect;
            Woker* worker = NULL;
            if (dSelect == 1) {
                worker = new Employee(newId, newName, dSelect);
            }
            else if (dSelect == 2) {
                worker = new Manage(newId, newName, dSelect);
            }
            else {
                worker = new Boss(newId, newName, dSelect);
            }
            this->m_EmpArray[ret] = worker;
            cout << "修改成功!" << endl;
            this->save();
        }
        else {
            cout << "修改失败,查无此人!" << endl;
        }
        
    }
    system("pause");
    system("cls");
}
//查找职工
void WorkerMananger::Find_Emp() {
    if (this->m_FileIsEmpty) {
        cout << "文件不存在或记录为空!" << endl;
    }
    else {
        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) {
                cout << "已查到职工信息如下:" << endl;
                this->m_EmpArray[ret]->showInfo();
            }
            else {
                cout << "查无此人!" << endl;
            }

        }
        else if (select == 2) {
            cout << "请输入想要查找的职工姓名:" << endl;
            string name;
            cin >> name;
            bool flag = false;
            for (int i = 0; i < m_EmpNum; i++) {
                if (this->m_EmpArray[i]->m_Name == name) {
                    cout << "查找成功,职工编号为:"
                        << this->m_EmpArray[i]->m_Id
                        << "职工信息如下:" << endl;
                    this->m_EmpArray[i]->showInfo();
                    flag = true;
                }
                if (flag == false) {
                    cout << "查无此人!" << endl;
                }
            }
        }
        else {
            cout << "输入错误!" << endl;
        }
    }
    system("pause");
    system("cls");
}
//排序职工
void WorkerMananger::Sort_Emp() {
    if (this->m_FileIsEmpty) {
        cout << "文件不存在或记录为空!" << endl;
        system("pause");
        system("cls");
    }
    else{
        cout << "请选择排序方式:" << endl;
        cout << "1.按职工编号升序" << endl;
        cout << "2.按职工编号降序" << endl;

        int select = 0;
        cin >> select;
        for (int i = 0; i < m_EmpNum; i++) {
            int minOrMax = i;//声明最小值或最大值下标
            for (int j = i + 1; j < this->m_EmpNum; j++) {
                if (select == 1) {//升序
                    if (m_EmpArray[minOrMax]->m_Id > m_EmpArray[j]->m_Id) {
                        minOrMax = j;
                    }
                }
                else {//降序
                    if(m_EmpArray[minOrMax]->m_Id < m_EmpArray[j]->m_Id) {
                        minOrMax = j;
                    }
                }
            }
            if (i != minOrMax) {//判定i是不是预定的最小值或最大值,如果不是,交换数据
                Woker* temp = m_EmpArray[i];
                m_EmpArray[i] = m_EmpArray[minOrMax];
                m_EmpArray[minOrMax] = temp;
            }
        }
        cout << "排序成功,排序结果为:" << endl;
        this->save();
        this->Show_Emp();
    }
}
//清空文件
void WorkerMananger::Clean_File() {
    cout << "确认清空?" << endl;
    cout << "1.确认" << endl;
    cout << "2.返回" << endl;

    int select = 0;
    cin >> select;

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

        if (this->m_EmpArray != NULL) {
            for (int i = 0; i < this->m_EmpNum; i++) {
                if (this->m_EmpArray[i] != NULL) {
                    delete this->m_EmpArray[i];
                }
            }
            this->m_EmpNum = 0;
            delete[]this->m_EmpArray;
            this->m_EmpArray = NULL;
            this->m_FileIsEmpty = true;
        }
        cout << "清除成功!" << endl;
    }
    system("pause");
    system("cls");
}


WorkerMananger::~WorkerMananger() {
    if (this->m_EmpArray != NULL) {
        delete[]this->m_EmpArray;
        this->m_EmpArray = NULL;
    }
}


int main()
{
    WorkerMananger wm;
    wm.ShowMenu();
    while (true) {
        int choice;
        cout << "请输入你的选择" << endl;
        cin >> choice;
        if (!cin) {
            cerr << "无效输入。" << endl;
            cin.clear(); // 清除错误标志
            cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略错误输入直到下一个换行符
            continue;
        }
        switch (choice) {
        case 1://增加职工信息
            wm.Add_Emp();
            break;
        case 2://显示职工信息
            wm.Show_Emp();
            break;
        case 3://修改职工信息
            wm.Del_Emp();
            break;
        case 4://更新职工信息
            wm.Mode_Emp();
            break;
        case 5://查询职工信息
            wm.Find_Emp();
            break;
        case 6://排序职工信息
            wm.Sort_Emp();
            break;
        case 7://清空职工信息
            wm.Clean_File();
            break;
        case 0://退出系统
            wm.exitSystem();
            break;
        default:
            cout << "无效的选择。" << endl;
            system("cls");
            break;
        }
    }
    system("pause");
}


 

Logo

欢迎加入西安开发者社区!我们致力于为西安地区的开发者提供学习、合作和成长的机会。参与我们的活动,与专家分享最新技术趋势,解决挑战,探索创新。加入我们,共同打造技术社区!

更多推荐