管理系统需求

职工管理系统可以用来管理公司内所有员工的信息

本教程主要利用C++来实现一个基于多态的职工管理系统

公司中职工分为三类:普通员工、经理、老板,显示信息时,需要显示职工编号、职工姓名、职工岗位、以及职责

普通员工职责:完成经理交给的任务

经理职责:完成老板交给的任务,并下发任务给员工

老板职责:管理公司所有事务

管理系统中需要实现的功能如下:

  • 退出管理程序:退出当前管理系统
  • 增加职工信息:实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号
  • 显示职工信息:显示公司内部所有职工的信息
  • 删除离职职工:按照编号删除指定的职工
  • 修改职工信息:按照编号修改职工个人信息
  • 查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息
  • 按照编号排序:按照职工编号,进行排序,排序规则由用户指定
  • 清空所有文档:清空文件中记录的所有职工信息 (清空前需要再次确认,防止误删)

系统界面效果图如下:

1546511409198

需根据用户不同的选择,完成不同的功能!

创建项目

创建项目步骤如下:

  • 创建新项目
  • 添加文件

创建项目

打开vs2017后,点击创建新项目,创建新的C++项目

1544151201465

填写项目名称以及项目路径,点击确定

1546349209805

添加文件

右键源文件,进行添加文件操作

1546349360960

1546349421496

1546349488752

至此,项目已创建完毕

创建管理类

​ 管理类负责的内容如下:

  • 与用户的沟通菜单界面
  • 对职工增删改查的操作
  • 与文件的读写交互

创建文件

在头文件和源文件的文件夹下分别创建workerManager.h 和 workerManager.cpp文件

1546349904944

头文件实现

在workerManager.h中设计管理类

代码如下:

System Management Requirements

The worker management system can be used to manage the information of all employees in the company

This tutorial mainly uses C++ to implement a worker management system based on polymorphism

The workers in the company are divided into three categories: ordinary employees, managers and bosses. When displaying information, the worker ID, worker name, worker position and responsibilities need to be shown

Responsibilities of ordinary employees: complete the tasks assigned by the manager

Responsibilities of the manager: complete the tasks assigned by the boss and assign tasks to employees

Responsibilities of the boss: manage all company affairs

The functions that need to be implemented in the management system are as follows:

  • Exit the management program: exit the current management system
  • Add worker information: implement batch adding of workers and record the information into a file. The worker information is: worker ID, name, department ID
  • Display worker information: display the information of all workers in the company
  • Delete resigned workers: delete the specified worker by ID
  • Modify worker information: modify a worker’s personal information by ID
  • Search worker information: search related personnel information by worker ID or worker name
  • Sort by ID: sort by worker ID, and the sorting rule is specified by the user
  • Clear all documents: clear all worker information recorded in the file (confirmation is required before clearing to prevent accidental deletion)

The system interface effect is shown below:

1546511409198

Different functions need to be completed according to different user selections!

Create the Project

The steps to create the project are as follows:

  • Create a new project
  • Add files

Create a Project

After opening vs2017, click Create New Project to create a new C++ project

1544151201465

Fill in the project name and project path, then click OK

1546349209805

Add Files

Right-click Source Files to add a file

1546349360960

1546349421496

1546349488752

At this point, the project has been created

Create the Management Class

The management class is responsible for the following:

  • The menu interface for communicating with the user
  • Operations for adding, deleting, modifying and searching workers
  • Read/write interaction with files

Create the File

Create the workerManager.h and workerManager.cpp files in the header file folder and the source file folder respectively

1546349904944

Header File Implementation

Design the management class in workerManager.h

The code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once
#include<iostream>
using namespace std;


class WorkerManager
{
public:

//构造函数
WorkerManager();

//析构函数
~WorkerManager();

};

源文件实现

在workerManager.cpp中将构造和析构函数空实现补全

Source File Implementation

In workerManager.cpp, complete the empty implementations of the constructor and destructor

1
2
3
4
5
6
7
8
9
10
#include "workerManager.h"

WorkerManager::WorkerManager()
{
}

WorkerManager::~WorkerManager()
{
}

至此职工管理类已创建完毕

菜单功能

功能描述:与用户的沟通界面

添加成员函数

在管理类workerManager.h中添加成员函数 void Show_Menu();

1546351543942

菜单功能实现

在管理类workerManager.cpp中实现 Show_Menu()函数

At this point, the worker management class has been created

Function description: the interface for communicating with the user

Add a Member Function

Add the member function void Show_Menu(); to the management class workerManager.h

1546351543942

Implement the Show_Menu() function in workerManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
}

测试菜单功能

在职工管理系统.cpp中测试菜单功能

代码:

Test the Menu Function

Test the menu function in 职工管理系统.cpp

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;
#include "workerManager.h"

int main() {

WorkerManager wm;

wm.Show_Menu();

system("pause");

return 0;
}

运行效果如图:

1546352771191

退出功能

提供功能接口

在main函数中提供分支选择,提供每个功能接口

代码:

The running effect is shown below:

1546352771191

Exit Function

Provide the Function Interface

In the main function, provide branch selection and an interface for each function

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
int main() {

WorkerManager wm;
int choice = 0;
while (true)
{
//展示菜单
wm.Show_Menu();
cout << "请输入您的选择:" << endl;
cin >> choice;

switch (choice)
{
case 0: //退出系统
break;
case 1: //添加职工
break;
case 2: //显示职工
break;
case 3: //删除职工
break;
case 4: //修改职工
break;
case 5: //查找职工
break;
case 6: //排序职工
break;
case 7: //清空文件
break;
default:
system("cls");
break;
}
}

system("pause");
return 0;
}

实现退出功能

在workerManager.h中提供退出系统的成员函数 void exitSystem();

在workerManager.cpp中提供具体的功能实现

Implement the Exit Function

In workerManager.h, provide the member function exitSystem(); for exiting the system

In workerManager.cpp, provide the concrete implementation

1
2
3
4
5
6
void WorkerManager::exitSystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}

测试功能

在main函数分支 0 选项中,调用退出程序的接口

1546353199424

运行测试效果如图:

1546353155490

创建职工类

创建职工抽象类

职工的分类为:普通员工、经理、老板

将三种职工抽象到一个类(worker)中,利用多态管理不同职工种类

职工的属性为:职工编号、职工姓名、职工所在部门编号

职工的行为为:岗位职责信息描述,获取岗位名称

头文件文件夹下 创建文件worker.h 文件并且添加如下代码:

Test the Function

In option 0 of the main function branch, call the interface for exiting the program

1546353199424

The running test effect is shown below:

1546353155490

Create the Worker Class

Create the Worker Abstract Class

The workers are divided into: ordinary employees, managers and bosses

Abstract the three kinds of workers into one class (worker) and use polymorphism to manage different worker types

The attributes of a worker are: worker ID, worker name, and the ID of the department the worker belongs to

The behaviors of a worker are: description of position responsibilities and getting the position name

In the header file folder, create the file worker.h and add the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include<iostream>
#include<string>
using namespace std;

//职工抽象基类
class Worker
{
public:

//显示个人信息
virtual void showInfo() = 0;
//获取岗位名称
virtual string getDeptName() = 0;

int m_Id; //职工编号
string m_Name; //职工姓名
int m_DeptId; //职工所在部门名称编号
};

创建普通员工类

普通员工类继承职工抽象类,并重写父类中纯虚函数

在头文件和源文件的文件夹下分别创建employee.h 和 employee.cpp文件

employee.h中代码如下:

Create the Ordinary Employee Class

The ordinary employee class inherits the worker abstract class and overrides the pure virtual functions of the parent class

Create the employee.h and employee.cpp files in the header file folder and the source file folder respectively

The code in employee.h is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once 
#include<iostream>
using namespace std;
#include "worker.h"

//员工类
class Employee :public Worker
{
public:

//构造函数
Employee(int id, string name, int dId);

//显示个人信息
virtual void showInfo();

//获取职工岗位名称
virtual string getDeptName();
};

employee.cpp中代码如下:

The code in employee.cpp is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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->getDeptName()
<< " \t岗位职责:完成经理交给的任务" << endl;
}


string Employee::getDeptName()
{
return string("员工");
}


创建经理类

经理类继承职工抽象类,并重写父类中纯虚函数,和普通员工类似

在头文件和源文件的文件夹下分别创建manager.h 和 manager.cpp文件

manager.h中代码如下:

Create the Manager Class

The manager class inherits the worker abstract class and overrides the pure virtual functions of the parent class, similar to the ordinary employee class

Create the manager.h and manager.cpp files in the header file folder and the source file folder respectively

The code in manager.h is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include<iostream>
using namespace std;
#include "worker.h"

//经理类
class Manager :public Worker
{
public:

Manager(int id, string name, int dId);

//显示个人信息
virtual void showInfo();

//获取职工岗位名称
virtual string getDeptName();
};

manager.cpp中代码如下:

The code in manager.cpp is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "manager.h"

Manager::Manager(int id, string name, int dId)
{
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = dId;

}

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

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


创建老板类

老板类继承职工抽象类,并重写父类中纯虚函数,和普通员工类似

在头文件和源文件的文件夹下分别创建boss.h 和 boss.cpp文件

boss.h中代码如下:

Create the Boss Class

The boss class inherits the worker abstract class and overrides the pure virtual functions of the parent class, similar to the ordinary employee class

Create the boss.h and boss.cpp files in the header file folder and the source file folder respectively

The code in boss.h is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include<iostream>
using namespace std;
#include "worker.h"

//老板类
class Boss :public Worker
{
public:

Boss(int id, string name, int dId);

//显示个人信息
virtual void showInfo();

//获取职工岗位名称
virtual string getDeptName();
};

boss.cpp中代码如下:

The code in boss.cpp is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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->getDeptName()
<< " \t岗位职责:管理公司所有事务" << endl;
}

string Boss::getDeptName()
{
return string("总裁");
}

测试多态

在职工管理系统.cpp中添加测试函数,并且运行能够产生多态

测试代码如下:

Test Polymorphism

Add a test function in 职工管理系统.cpp and run it so that polymorphism is produced

The test code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"


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

worker = new Manager(2, "李四", 2);
worker->showInfo();
delete worker;

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

运行效果如图:

1546398236081

测试成功后,测试代码可以注释保留,或者选择删除

添加职工

功能描述:批量添加职工,并且保存到文件中

功能分析

分析:

用户在批量创建时,可能会创建不同种类的职工

如果想将所有不同种类的员工都放入到一个数组中,可以将所有员工的指针维护到一个数组里

如果想在程序中维护这个不定长度的数组,可以将数组创建到堆区,并利用Worker **的指针维护

1546399491099

功能实现

在WokerManager.h头文件中添加成员属性 代码:

The running effect is shown below:

1546398236081

After the test succeeds, the test code can be kept as comments or deleted

Add Workers

Function description: add workers in batches and save them to a file

Function Analysis

Analysis:

When creating workers in batches, the user may create different kinds of workers

If you want to put all kinds of employees into one array, you can maintain the pointers of all employees in an array

If you want to maintain this dynamic-length array in the program, you can create the array on the heap and maintain it with a Worker ** pointer

1546399491099

Function Implementation

In the WokerManager.h header file, add the member attribute. Code:

1
2
3
4
5
//记录文件中的人数个数
int m_EmpNum;

//员工数组的指针
Worker ** m_EmpArray;

在WorkerManager构造函数中初始化属性

Initialize the attributes in the WorkerManager constructor

1
2
3
4
5
6
7
8
WorkerManager::WorkerManager()
{
//初始化人数
this->m_EmpNum = 0;

//初始化数组指针
this->m_EmpArray = NULL;
}

在workerManager.h中添加成员函数

Add the member function in workerManager.h

1
2
//增加职工
void Add_Emp();

workerManager.cpp中实现该函数

Implement this function in workerManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//增加职工
void WorkerManager::Add_Emp()
{
cout << "请输入增加职工数量: " << endl;

int addNum = 0;
cin >> addNum;

if (addNum > 0)
{
//计算新空间大小
int newSize = this->m_EmpNum + addNum;

//开辟新空间
Worker ** newSpace = new Worker*[newSize];

//将原空间下内容存放到新空间下
if (this->m_EmpArray != NULL)
{
for (int i = 0; i < this->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;


Worker * worker = NULL;
switch (dSelect)
{
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;
}


newSpace[this->m_EmpNum + i] = worker;
}

//释放原有空间
delete[] this->m_EmpArray;

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

//更新新的个数
this->m_EmpNum = newSize;

//提示信息
cout << "成功添加" << addNum << "名新职工!" << endl;
}
else
{
cout << "输入有误" << endl;
}

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

在WorkerManager.cpp的析构函数中,释放堆区数据

In the destructor of WorkerManager.cpp, release the heap data

1
2
3
4
5
6
7
8
WorkerManager::~WorkerManager()
{
if (this->m_EmpArray != NULL)
{
delete[] this->m_EmpArray;
}
}

测试添加

在main函数分支 1 选项中,调用添加职工接口

1546401705277

效果如图:

1546401763461

至此,添加职工到程序中功能实现完毕

文件交互 - 写文件

功能描述:对文件进行读写

​ 在上一个添加功能中,我们只是将所有的数据添加到了内存中,一旦程序结束就无法保存了

​ 因此文件管理类中需要一个与文件进行交互的功能,对于文件进行读写操作

设定文件路径

首先我们将文件路径,在workerManager.h中添加宏常量,并且包含头文件 fstream

Test Adding

In option 1 of the main function branch, call the interface for adding workers

1546401705277

The effect is shown below:

1546401763461

At this point, the function of adding workers to the program is fully implemented

File Interaction - Writing Files

Function description: read and write files

In the previous add function, we only added all the data to memory; once the program ends, it cannot be saved

Therefore, the file management class needs a function to interact with the file for reading and writing operations

Set the File Path

First, add a macro constant for the file path in workerManager.h and include the header file fstream

1
2
#include <fstream>
#define FILENAME "empFile.txt"

成员函数声明

在workerManager.h中类里添加成员函数 void save()

Member Function Declaration

Add the member function void save() to the class in workerManager.h

1
2
//保存文件
void save();

保存文件功能实现

Save File Function Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void WorkerManager::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();
}

保存文件功能测试

在添加职工功能中添加成功后添加保存文件函数

1546432469465

再次运行代码,添加职工

1546401763461

同级目录下多出文件,并且保存了添加的信息

1546432343078

文件交互 - 读文件

功能描述:将文件中的内容读取到程序中

虽然我们实现了添加职工后保存到文件的操作,但是每次开始运行程序,并没有将文件中数据读取到程序中

而我们的程序功能中还有清空文件的需求

因此构造函数初始化数据的情况分为三种

  1. 第一次使用,文件未创建
  2. 文件存在,但是数据被用户清空
  3. 文件存在,并且保存职工的所有数据

文件未创建

在workerManager.h中添加新的成员属性 m_FileIsEmpty标志文件是否为空

Save File Function Test

After adding workers successfully in the add-worker function, add the save-file function

1546432469465

Run the code again and add workers

1546401763461

An extra file appears in the same directory, and the added information is saved in it

1546432343078

File Interaction - Reading Files

Function description: read the content of the file into the program

Although we implemented saving to a file after adding workers, every time the program starts running, the data in the file is not read into the program

And our program functions also include the need to clear the file

Therefore, the initialization of data in the constructor is divided into three cases

  1. First use: the file has not been created
  2. The file exists, but the data has been cleared by the user
  3. The file exists and contains all worker data

File Does Not Exist

In workerManager.h, add a new member attribute m_FileIsEmpty to mark whether the file is empty

1
2
//标志文件是否为空
bool m_FileIsEmpty;

修改WorkerManager.cpp中构造函数代码

Modify the constructor code in WorkerManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WorkerManager::WorkerManager()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);

//文件不存在情况
if (!ifs.is_open())
{
cout << "文件不存在" << endl; //测试输出
this->m_EmpNum = 0; //初始化人数
this->m_FileIsEmpty = true; //初始化文件为空标志
this->m_EmpArray = NULL; //初始化数组
ifs.close(); //关闭文件
return;
}
}

删除文件后,测试文件不存在时初始化数据功能

文件存在且数据为空

在workerManager.cpp中的构造函数追加代码:

After deleting the file, test the data initialization function when the file does not exist

File Exists and Data Is Empty

Append the following code to the constructor in WorkerManager.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
//文件存在,并且没有记录
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "文件为空!" << endl;
this->m_EmpNum = 0;
this->m_FileIsEmpty = true;
this->m_EmpArray = NULL;
ifs.close();
return;
}

追加代码位置如图:

1546435197575

将文件创建后清空文件内容,并测试该情况下初始化功能

我们发现文件不存在或者为空清空 m_FileIsEmpty 判断文件是否为空的标志都为真,那何时为假?

成功添加职工后,应该更改文件不为空的标志

void WorkerManager::Add_Emp()成员函数中添加:

The position of the appended code is shown in the figure:

1546435197575

After creating the file, clear the file content and test the initialization function in this case

We found that when the file does not exist or is empty, the m_FileIsEmpty flag that determines whether the file is empty is always true. When does it become false?

After successfully adding workers, the flag that the file is not empty should be changed

In the void WorkerManager::Add_Emp() member function, add:

1
2
//更新职工不为空标志
this->m_FileIsEmpty = false;

1546656256176

文件存在且保存职工数据

获取记录的职工人数

在workerManager.h中添加成员函数 int get_EmpNum();

1546656256176

File Exists and Contains Worker Data

Get the Number of Workers Recorded

In workerManager.h, add the member function int get_EmpNum();

1
2
//统计人数
int get_EmpNum();

workerManager.cpp中实现

Implement it in workerManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int WorkerManager::get_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++;
}
ifs.close();

return num;
}

在workerManager.cpp构造函数中继续追加代码:

Continue to append code in the constructor of WorkerManager.cpp:

1
2
3
int num =  this->get_EmpNum();
cout << "职工个数为:" << num << endl; //测试代码
this->m_EmpNum = num; //更新成员属性

手动添加一些职工数据,测试获取职工数量函数

1546436429055

1546436385793

初始化数组

根据职工的数据以及职工数据,初始化workerManager中的Worker ** m_EmpArray 指针

在WorkerManager.h中添加成员函数 void init_Emp();

Manually add some worker data and test the function for getting the number of workers

1546436429055

1546436385793

Initialize the Array

According to the worker data, initialize the Worker ** m_EmpArray pointer in workerManager

In WorkerManager.h, add the member function void init_Emp();

1
2
//初始化员工
void init_Emp();

在WorkerManager.cpp中实现

Implement it in WorkerManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void WorkerManager::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)
{
Worker * worker = NULL;
//根据不同的部门Id创建不同对象
if (dId == 1) // 1普通员工
{
worker = new Employee(id, name, dId);
}
else if (dId == 2) //2经理
{
worker = new Manager(id, name, dId);
}
else //总裁
{
worker = new Boss(id, name, dId);
}
//存放在数组中
this->m_EmpArray[index] = worker;
index++;
}
}

在workerManager.cpp构造函数中追加代码

Append code in the constructor of workerManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
//根据职工数创建数组
this->m_EmpArray = new Worker *[this->m_EmpNum];
//初始化职工
init_Emp();

//测试代码
for (int i = 0; i < m_EmpNum; i++)
{
cout << "职工号: " << this->m_EmpArray[i]->m_Id
<< " 职工姓名: " << this->m_EmpArray[i]->m_Name
<< " 部门编号: " << this->m_EmpArray[i]->m_DeptId << endl;
}

运行程序,测试从文件中获取的数据

1546436938152

至此初始化数据功能完毕,测试代码可以注释或删除掉!

显示职工

功能描述:显示当前所有职工信息

显示职工函数声明

在workerManager.h中添加成员函数 void Show_Emp();

Run the program and test the data read from the file

1546436938152

At this point, the data initialization function is complete. The test code can be commented out or deleted!

Display Workers

Function description: display the information of all current workers

Display Worker Function Declaration

In workerManager.h, add the member function void Show_Emp();

1
2
//显示职工
void Show_Emp();

显示职工函数实现

在workerManager.cpp中实现成员函数 void Show_Emp();

Display Worker Function Implementation

Implement the member function void Show_Emp(); in workerManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//显示职工
void WorkerManager::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");
}

测试显示职工

在main函数分支 2 选项中,调用显示职工接口

1546497336465

测试时分别测试 文件为空和文件不为空两种情况

测试效果:

测试1-文件不存在或者为空情况

1546497082135

测试2 - 文件存在且有记录情况

1546496947671

测试完毕,至此,显示所有职工信息功能实现

删除职工

功能描述:按照职工的编号进行删除职工操作

删除职工函数声明

在workerManager.h中添加成员函数 void Del_Emp();

Test Displaying Workers

In option 2 of the main function branch, call the interface for displaying workers

1546497336465

When testing, test the two cases of an empty file and a non-empty file separately

Test effect:

Test 1 - the file does not exist or is empty

1546497082135

Test 2 - the file exists and has records

1546496947671

The test is complete. At this point, the function of displaying all worker information is implemented

Delete Workers

Function description: delete workers by their IDs

Delete Worker Function Declaration

In workerManager.h, add the member function void Del_Emp();

1
2
//删除职工
void Del_Emp();

职工是否存在函数声明

很多功能都需要用到根据职工是否存在来进行操作如:删除职工、修改职工、查找职工

因此添加该公告函数,以便后续调用

在workerManager.h中添加成员函数 int IsExist(int id);

Worker Existence Function Declaration

Many functions need to operate based on whether a worker exists, such as deleting workers, modifying workers and searching workers

Therefore, add this common function for later calls

In workerManager.h, add the member function int IsExist(int id);

1
2
//按照职工编号判断职工是否存在,若存在返回职工在数组中位置,不存在返回-1
int IsExist(int id);

职工是否存在函数实现

在workerManager.cpp中实现成员函数 int IsExist(int id);

Worker Existence Function Implementation

Implement the member function int IsExist(int id); in workerManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int WorkerManager::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;
}

删除职工函数实现

在workerManager.cpp中实现成员函数 void Del_Emp();

Delete Worker Function Implementation

Implement the member function void Del_Emp(); in workerManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//删除职工
void WorkerManager::Del_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
//按职工编号删除
cout << "请输入想要删除的职工号:" << endl;
int id = 0;
cin >> id;

int index = this->IsExist(id);

if (index != -1) //说明index上位置数据需要删除
{
for (int i = index; i < this->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");
}

测试删除职工

在main函数分支 3 选项中,调用删除职工接口

1546502698622

测试1 - 删除不存在职工情况

1546500324196

测试2 - 删除存在的职工情况

删除成功提示图:

1546500350526

再次显示所有职工信息,确保已经删除

1546500361889

查看文件中信息,再次核实员工已被完全删除

1546500383570

至此,删除职工功能实现完毕!

修改职工

功能描述:能够按照职工的编号对职工信息进行修改并保存

修改职工函数声明

在workerManager.h中添加成员函数 void Mod_Emp();

Test Deleting Workers

In option 3 of the main function branch, call the interface for deleting workers

1546502698622

Test 1 - delete a non-existent worker

1546500324196

Test 2 - delete an existing worker

Success prompt:

1546500350526

Display all worker information again to make sure it has been deleted

1546500361889

Check the information in the file again to verify that the employee has been completely deleted

1546500383570

At this point, the delete worker function is fully implemented!

Modify Workers

Function description: modify worker information by worker ID and save it

Modify Worker Function Declaration

In workerManager.h, add the member function void Mod_Emp();

1
2
//修改职工
void Mod_Emp();

修改职工函数实现

在workerManager.cpp中实现成员函数 void Mod_Emp();

Modify Worker Function Implementation

Implement the member function void Mod_Emp(); in workerManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//修改职工
void WorkerManager::Mod_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;

Worker * worker = NULL;
switch (dSelect)
{
case1:
worker = new Employee(newId, newName, dSelect);
break;
case2:
worker = new Manager(newId, newName, dSelect);
break;
case 3:
worker = new Boss(newId, newName, dSelect);
break;
default:
break;
}

//更改数据 到数组中
this->m_EmpArray[ret]= worker;

cout << "修改成功!" << endl;

//保存到文件中
this->save();
}
else
{
cout << "修改失败,查无此人" << endl;
}
}

//按任意键 清屏
system("pause");
system("cls");
}

测试修改职工

在main函数分支 4 选项中,调用修改职工接口

1546502651922

测试1 - 修改不存在职工情况

1546502759643

测试2 - 修改存在职工情况,例如将职工 “李四” 改为 “赵四”

1546502830350

修改后再次查看所有职工信息,并确认修改成功

1546502865443

再次确认文件中信息也同步更新

1546502898653

至此,修改职工功能已实现!

查找职工

功能描述:提供两种查找职工方式,一种按照职工编号,一种按照职工姓名

查找职工函数声明

在workerManager.h中添加成员函数 void Find_Emp();

Test Modifying Workers

In option 4 of the main function branch, call the interface for modifying workers

1546502651922

Test 1 - modify a non-existent worker

1546502759643

Test 2 - modify an existing worker, for example change the worker “李四” to “赵四”

1546502830350

After modification, view all worker information again and confirm the modification is successful

1546502865443

Confirm again that the information in the file is also updated synchronously

1546502898653

At this point, the modify worker function is implemented!

Search Workers

Function description: provide two ways to search workers, one by worker ID and one by worker name

Search Worker Function Declaration

In workerManager.h, add the member function void Find_Emp();

1
2
//查找职工
void Find_Emp();

查找职工函数实现

在workerManager.cpp中实现成员函数 void Find_Emp();

Search Worker Function Implementation

Implement the member function void Find_Emp(); in workerManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//查找职工
void WorkerManager::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) //按职工号查找
{
int id;
cout << "请输入查找的职工编号:" << endl;
cin >> id;

int ret = IsExist(id);
if (ret != -1)
{
cout << "查找成功!该职工信息如下:" << endl;
this->m_EmpArray[ret]->showInfo();
}
else
{
cout << "查找失败,查无此人" << endl;
}
}
else if(select == 2) //按姓名查找
{
string name;
cout << "请输入查找的姓名:" << endl;
cin >> name;

bool flag = false; //查找到的标志
for (int i = 0; i < m_EmpNum; i++)
{
if (m_EmpArray[i]->m_Name == name)
{
cout << "查找成功,职工编号为:"
<< m_EmpArray[i]->m_Id
<< " 号的信息如下:" << endl;

flag = true;

this->m_EmpArray[i]->showInfo();
}
}
if (flag == false)
{
//查无此人
cout << "查找失败,查无此人" << endl;
}
}
else
{
cout << "输入选项有误" << endl;
}
}


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

测试查找职工

在main函数分支 5 选项中,调用查找职工接口

1546504714318

测试1 - 按照职工编号查找 - 查找不存在职工

1546504767229

测试2 - 按照职工编号查找 - 查找存在职工

1546505046521

测试3 - 按照职工姓名查找 - 查找不存在职工

1546505115610

测试4 - 按照职工姓名查找 - 查找存在职工(如果出现重名,也一并显示,在文件中可以添加重名职工)

例如 添加两个王五的职工,然后按照姓名查找王五

1546507850441

1546507760284

至此,查找职工功能实现完毕!

排序

功能描述:按照职工编号进行排序,排序的顺序由用户指定

排序函数声明

在workerManager.h中添加成员函数 void Sort_Emp();

Test Searching Workers

In option 5 of the main function branch, call the interface for searching workers

1546504714318

Test 1 - search by worker ID - search for a non-existent worker

1546504767229

Test 2 - search by worker ID - search for an existing worker

1546505046521

Test 3 - search by worker name - search for a non-existent worker

1546505115610

Test 4 - search by worker name - search for an existing worker (if there are duplicate names, they are all displayed; workers with duplicate names can be added in the file)

For example, add two workers named 王五, then search by name for 王五

1546507850441

1546507760284

At this point, the search worker function is fully implemented!

Sort

Function description: sort by worker ID, and the sorting order is specified by the user

Sort Function Declaration

In workerManager.h, add the member function void Sort_Emp();

1
2
//排序职工
void Sort_Emp();

排序函数实现

在workerManager.cpp中实现成员函数 void Sort_Emp();

Sort Function Implementation

Implement the member function void Sort_Emp(); in workerManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//排序职工
void WorkerManager::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 < 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)
{
Worker * temp = m_EmpArray[i];
m_EmpArray[i] = m_EmpArray[minOrMax];
m_EmpArray[minOrMax] = temp;
}

}

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

}

测试排序功能

在main函数分支 6 选项中,调用排序职工接口

1546510145181

测试:

首先我们添加一些职工,序号是无序的,例如:

1546658169987

测试 - 升序排序

1546658190479

文件同步更新

1546658273581

测试 - 降序排序

1546658288936

文件同步更新

1546658313704

至此,职工按照编号排序的功能实现完毕!

清空文件

功能描述:将文件中记录数据清空

清空函数声明

在workerManager.h中添加成员函数 void Clean_File();

Test the Sort Function

In option 6 of the main function branch, call the interface for sorting workers

1546510145181

Test:

First we add some workers with unordered IDs, for example:

1546658169987

Test - ascending sort

1546658190479

The file is updated synchronously

1546658273581

Test - descending sort

1546658288936

The file is updated synchronously

1546658313704

At this point, the function of sorting workers by ID is fully implemented!

Clear the File

Function description: clear the data recorded in the file

Clear Function Declaration

In workerManager.h, add the member function void Clean_File();

1
2
//清空文件
void Clean_File();

清空函数实现

在workerManager.cpp中实现员函数 void Clean_File();

Clear Function Implementation

Implement the member function void Clean_File(); in workerManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//清空文件
void WorkerManager::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");
}

测试清空文件

在main函数分支 7 选项中,调用清空文件接口

1546511085541

测试:确认清空文件

1546510976745

再次查看文件中数据,记录已为空

1546510994196

打开文件,里面数据已确保清空,该功能需要慎用!

1546511018517

随着清空文件功能实现,本案例制作完毕 ^ _ ^

我的实现

头文件

管理类头文件–workerManager.h:

Test Clearing the File

In option 7 of the main function branch, call the interface for clearing the file

1546511085541

Test: confirm clearing the file

1546510976745

Check the data in the file again; the records are empty now

1546510994196

Open the file; the data is confirmed to be cleared. This function should be used with caution!

1546511018517

With the clear-file function implemented, this case study is complete ^ _ ^

My Implementation

Header Files

Management class header file – workerManager.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#pragma once
#include<iostream>
#include<fstream>
#include<string>
#include "Manager.h"
#include "Boss.h"
#include "Employee.h"
using namespace std;

#define FILENAME "empFile.txt"

class workerManager {
public:
void showMenu();
workerManager();
~workerManager();
void exitSystem();
bool fileIsExist();
void addEmp();
void initEmpArray();
void save();
void showEmpInfo();
void delEmp();
int empIsExist(int id);
void alterEmp();
void findEmp();
void sortEmp();
void cleanEmp();
public:
int empNum; // 员工数量
Worker** empArray; //员工数组
};

职工抽象类头文件–Worker.h:

Worker abstract class header file – Worker.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma once
#include <string>
using namespace std;

// 职工抽象类
class Worker {
public:
int id; // 职工编号
string name; // 姓名
int dId; // 部门编号

virtual void showInfo() = 0; // 显示信息纯虚函数
//virtual string getInfo() = 0;
};

老板类头文件–Boss.h:

Boss class header file – Boss.h:

1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#include <string>
#include "Worker.h"
using namespace std;

class Boss : public Worker {
public:
virtual void showInfo();
//virtual string getInfo();
Boss(int id, string name, int dId);
~Boss();
};

经理类头文件–Manager.h:

Manager class header file – Manager.h:

1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#include <string>
#include "Worker.h"
using namespace std;

class Manager : public Worker {
public:
virtual void showInfo();
//virtual string getInfo();
Manager(int id, string name, int dId);
~Manager();
};

职工类头文件–Employee.h:

Employee class header file – Employee.h:

1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#include <string>
#include "Worker.h"
using namespace std;

class Employee : public Worker {
public:
virtual void showInfo();
//virtual string getInfo();
Employee(int id, string name, int dId);
~Employee();
};

源文件

管理类源文件–workerManager.cpp:

Source Files

Management class source file – workerManager.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <iostream>
using namespace std;
#include "workerManager.h"
#include <fstream>


workerManager::workerManager(){

// 初始化数据
this->empArray = NULL;
this->empNum = 0;
}

workerManager::~workerManager() {
// 此处应释放堆区数据
}
void workerManager::initEmpArray() {
// 从文件 初始化 员工数组
if (this->fileIsExist()) {
ifstream ifs;
ifs.open(FILENAME, ios::in);

int id;
string name;
int dId;
int num = 0;

// 每行读入三个数据 先计算文件中的员工人数num 用于开辟空间
while (ifs >> id && ifs >> name && ifs >> dId) {
num++;
}
ifs.close();

// 重新打开文件 用于创建员工
ifs.open(FILENAME, ios::in);

// 数组中存放的是 Worker* 类型
Worker** newSpace = new Worker * [num];
num = 0;
while (ifs >> id && ifs >> name && ifs >> dId) {
Worker* worker = NULL;
switch (dId) {
case 1:
worker = new Employee(id, name, dId);
break;
case 2:
worker = new Manager(id, name, dId);
break;
case 3:
worker = new Boss(id, name, dId);
break;
default:
break;
}
newSpace[num++] = worker;
}
ifs.close();

// 更新成员属性
this->empArray = newSpace;
this->empNum = num;
}
else {
this->empNum = 0;
this->empArray = NULL;
}
return;
}
void workerManager::save() {
ofstream ofs;
ofs.open(FILENAME, ios::out);
for (int i = 0; i < this->empNum; i++) {
ofs << this->empArray[i]->id << " "
<< this->empArray[i]->name << " "
<< this->empArray[i]->dId << endl;
}
ofs.close();
}
void workerManager::exitSystem() {
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);
}
void workerManager::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 << endl;
}
bool workerManager::fileIsExist() {
// 判断文件是否存在 且 不为空
ifstream ifs;
ifs.open(FILENAME, ios::in);

if (!ifs.is_open()) {
ifs.close();
return false;
}
else {
char c = ifs.get();
ifs.close();
return (c != EOF) ? true : false;
}
}
void workerManager::addEmp() {

cout << "请输入要添加的员工数量:" << endl;
int num;
cin >> num;
if (num > 0) {
// 先从文件初始化数据
this->initEmpArray();

// 根据新添加员工数量 开辟更大的空间
Worker** newSpace = new Worker * [this->empNum + num];

// 将原数据转移到新空间
for (int i = 0; i < this->empNum; i++) {
newSpace[i] = this->empArray[i];
}

int id;
string name;
int dId;
for (int i = 0; i < num; i++) {
cout << "请输入第 " << i + 1 << " 个员工的编号:" << endl;
cin >> id;
cout << "请输入第 " << i + 1 << " 个员工的姓名:" << endl;
cin >> name;
cout << "请输入第 " << i + 1 << " 个员工的职位:" << endl;
cout << "1. 普通职工" << endl;
cout << "2. 经理" << endl;
cout << "3. 老板" << endl;
cin >> dId;

Worker* worker = NULL;
switch (dId) {
case 2:
worker = new Manager(id, name, dId);
break;
case 3:
worker = new Boss(id, name, dId);
break;
default:
worker = new Employee(id, name, dId);
break;
}
// 在新空间中添加员工
newSpace[this->empNum + i] = worker;
}

// 更新数据并保存到文件中
this->empNum += num;
this->empArray = newSpace;
this->save();
cout << endl << "添加成功!" << endl;
}
else {
cout << "输入有误!" << endl << endl;
}
system("pause");
system("cls");
return;
}
void workerManager::showEmpInfo() {
if (!this->fileIsExist()) {
cout << endl << "文件不存在或数据为空!" << endl;
system("pause");
system("cls");
return;
}

// 从文件初始化数据
this->initEmpArray();
for (int i = 0; i < this->empNum; i++) {

// 利用多态输出 每个员工的信息
this->empArray[i]->showInfo();
cout << endl;
}
system("pause");
system("cls");

}
void workerManager::delEmp() {
if (!this->fileIsExist()) {
cout << endl << "文件不存在或数据为空!" << endl;
system("pause");
system("cls");
return;
}
cout << endl << "请输入要删除职工的编号:" << endl;
int id,index;
cin >> id;

// 根据 id 查找员工在数组中的位置
index = this->empIsExist(id);
if (index != -1) {
if (index == this->empNum - 1) {
this->empNum -= 1;
this->save();
}
else {

// 删除员工
for (int i = index; i < this->empNum - 1; i++) {
this->empArray[i] = this->empArray[i + 1];
}

// 更新数据并保存
this->empNum -= 1;
this->save();
}
cout << "删除成功!" << endl;
}
else {
cout << "输入有误,查无此人!" << endl;
}
system("pause");
system("cls");
return;
}
int workerManager::empIsExist(int id) {
// 根据 id 查找员工数组中的下标 查找失败返回 -1
this->initEmpArray();
int index = -1;
for (int i = 0; i < this->empNum; i++) {
index = (this->empArray[i]->id == id) ? i : index;
}
return index;
}
void workerManager::alterEmp() {


if (!this->fileIsExist()) {
cout << endl << "文件不存在或数据为空!" << endl;
system("pause");
system("cls");
return;
}
int id, index;
cout << endl << "请输入要修改的职工编号:" << endl;
cin >> id;
index = this->empIsExist(id);
if (index != -1) {
cout << endl << "请输入新的职工编号:" << endl ;
cin >> this->empArray[index]->id;
cout << endl << "请输入新的职工姓名:" << endl;
cin >> this->empArray[index]->name;
cout << endl << "请输入新的职工岗位:" << endl;
cout << "1. 普通职工" << endl;
cout << "2. 经理" << endl;
cout << "3. 老板" << endl;
cin >> this->empArray[index]->dId;
this->save();
cout << endl << "修改成功!" << endl;
}
else {
cout << "输入有误,查无此人!" << endl << endl;
}
system("pause");
system("cls");
return;
}
void workerManager::findEmp() {
if (!this->fileIsExist()) {
cout << endl << "文件不存在或数据为空!" << endl;
system("pause");
system("cls");
return;
}
int select,index,flag = 0;

while (true) {
cout << "请选择查找方式:" << endl <<
"1. 按职工编号查找" << endl <<
"2. 按职工姓名查找" << endl;
cin >> select;
if (select == 1) {
int id;
cout << endl << "请输入职工编号:" << endl;
cin >> id;
index = this->empIsExist(id);
if (index != -1) {
cout << "查找成功,职工信息如下:" << endl;
this->empArray[index]->showInfo();
break;
}
else {
cout << "查无此人!" << endl;
break;
}
}
else if (select == 2) {
string name;
cout << endl << "请输入职工姓名 :" << endl;
cin >> name;
for (int i = 0; i < this->empNum; i++) {
if (name == this->empArray[i]->name) {
cout << "查找成功!" << endl;
this->empArray[i]->showInfo();
flag = 1;
}
}
if (flag == 0) {
cout << "查无此人!" << endl;
}
break;
}
else {
cout << "输入有误,请重新输入!" << endl;
}
}
system("pause");
system("cls");
return;
}
void workerManager::sortEmp() {
if (!this->fileIsExist()) {
cout << endl << "文件不存在或数据为空!" << endl;
system("pause");
system("cls");
return;
}
this->initEmpArray();
int flag = 1;
while (true) {
cout << "请选择排序方式: " << endl;
cout << "1. 升序排序" << endl;
cout << "0. 降序排序" << endl;
cin >> flag;
if (flag == 1) {
int min_index;

// 应用选择排序算法
for (int i = 0; i < this->empNum; i++) {
min_index = i;
for (int j = i; j < this->empNum; j++) {
if (this->empArray[j]->id < this->empArray[min_index]->id) {
min_index = j;
}
}
if (min_index != i) {
Worker* worker = this->empArray[i];
this->empArray[i] = this->empArray[min_index];
this->empArray[min_index] = worker;
}
}
break;
}
else if(flag == 0){
int max_index;
for (int i = 0; i < this->empNum; i++) {
max_index = i;
for (int j = i; j < this->empNum; j++) {
if (this->empArray[j]->id > this->empArray[max_index]->id) {
max_index = j;
}
}
if (max_index != i) {
Worker* worker = this->empArray[i];
this->empArray[i] = this->empArray[max_index];
this->empArray[max_index] = worker;
}
}
break;
}
else {
cout << "输入有误,请重新输入!" << endl;
}
}
this->save();
system("pause");
system("cls");
}
void workerManager::cleanEmp() {
cout << "确定清除吗?" << endl;
cout << "1. 是" << endl;
cout << "2. 否" << endl;
int flag;
cin >> flag;

// 从文件中读入 清除后再写回文件
if (flag == 1) {
this->initEmpArray();
if (this->empArray) {
delete[] this->empArray;
this->empArray = NULL;
}
this->empNum = 0;
this->save();
}
system("pause");
system("cls");
}

老板类源文件–Boss.cpp:

Boss class source file – Boss.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include <iostream>
#include <string>
#include "Boss.h"

void Boss::showInfo() {
cout << "员工编号:" << this->id << "\t" << "员工姓名:" << this->name<<"\t\t"
<< "岗位名称:" << "老板 " << "\t" << "岗位职责:" << "管理公司所有事务"
<< endl;
}
Boss::Boss(int id, string name, int dId) {
this->id = id;
this->name = name;
this->dId = dId;
}
Boss::~Boss() {

}

经理类源文件–Manager.cpp:

Manager class source file – Manager.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include <iostream>
#include <string>
#include "Manager.h"

void Manager::showInfo() {
cout << "员工编号:" << this->id << "\t" << "员工姓名:" << this->name << "\t\t"
<< "岗位名称:" << "经理 " << "\t" << "岗位职责:" << "完成老板交给的任务,并下发任务给员工"
<< endl;
}
Manager::Manager(int id, string name, int dId) {
this->id = id;
this->name = name;
this->dId = dId;
}
Manager::~Manager() {

}

职工类源文件–Employee.cpp:

Employee class source file – Employee.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include <iostream>
#include <string>
#include "Employee.h"

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


Employee::Employee(int id, string name, int dId) {
this->id = id;
this->name = name;
this->dId = dId;
}
Employee::~Employee() {

}

主函数文件–职工管理系统.cpp:

Main function file – 职工管理系统.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
using namespace std;
#include "workerManager.h"

int main() {

int select;
workerManager wm;

while (true) {

// 显示菜单
wm.showMenu();

cout << "请输入您的选择:" << endl;
cin >> select;
switch (select) {
case 0:
// 退出系统
wm.exitSystem();
break;
case 1:
// 添加职工
wm.addEmp();
break;
case 2:
// 显示职工信息
wm.showEmpInfo();
break;
case 3:
// 删除职工
wm.delEmp();
break;
case 4:
// 修改职工
wm.alterEmp();
break;
case 5:
// 查找职工
wm.findEmp();
break;
case 6:
// 排序
wm.sortEmp();
break;
case 7:
// 清除职工信息
wm.cleanEmp();
break;
default:
system("cls");
break;
}
}

system("pause");
return 0;
}