[C++] C++ employee information management system

foreword

This is a phased exercise of C++ core programming. It mainly uses small projects as the carrier to review and consolidate the knowledge points learned in the core part, laying a foundation for future learning and improving programming.

1. Management System Requirements

The system can manage the information of all employees in the company.
It mainly uses c++ to implement a polymorphic-based employee management system.

Employees in the company are divided into three categories: ordinary employees, managers, and bosses. When displaying information, it is necessary to display the employee number, employee name, employee position and responsibilities. Ordinary
employee responsibilities: to complete various tasks assigned by the manager.
Manager responsibilities: to complete the tasks assigned by the boss. , and disassembled and distributed to employees
Boss Responsibilities: Overall management of all affairs of the company

The management system needs to implement the following functions:

  • Exit management system: exit the current system
  • Add employee information: realize the function of adding employees in batches, and enter the information into the file. The employee information is: employee number, name, department number
  • Display employee information: display the information of all employees in the company
  • Delete resigned employees: delete the specified employees according to the number
  • Modify employee information: modify employee personal information according to the number
  • Find employee information: search for the corresponding information of the employee according to the employee's serial number and employee name
  • Sort by number: sort by employee number, and the sorting rules are specified by the user

2. Create a management class

The management class is responsible for the following:

  • Communication menu interface with users
  • Add, delete, modify and query operations on employees
  • Reading and writing interactions with files

2.1 Create a file

  1. Create WokerManager.h and WokerManager.cpp files under the header file and source file folders respectively

insert image description here

2.2 header file implementation

#pragma once //防止头文件重复包含
#include<iostream> //包含输入输出流的头文件
using namespace std; //使用标准的命名空间

class WokerManager
{
    
    
	//构造函数
	WokerManager();

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

2.3 Source file implementation

#include "wokerManager.h"

WokerManager::WokerManager() {
    
    

}

WokerManager::~WokerManager() {
    
    

}

3. Menu function

Function description: interface for communicating with users

  1. Add the member function void show_Menu() in the management class WokerManager.h;
#pragma once //防止头文件重复包含
#include<iostream> //包含输入输出流的头文件
using namespace std; //使用标准的命名空间

class WokerManager
{
    
    
public:
	//构造函数
	WokerManager();

	//展示菜单
	void Show_Menu();

	//析构函数
	~WokerManager();
};
  1. Implement menu member functions
void WokerManager::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;
}
  1. Test menu function

insert image description here

4. Exit function

Function description: realize the exit of the program

  1. Provide branch selection in the main function, providing each functional interface
int main() {
    
    
	//实例化管理者对象
	WokerManager 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;
}
  1. Provide member function void exitSystem() to exit the system in WokerManager.h
void WokerManager::exitSystem() {
    
    
	cout << "欢迎下次使用!" << endl;
	system("pause");
	exit(0);
}
  1. Run the system, type 0, and check whether the system exits normally

insert image description here

5. Create an employee class

5.1 Create worker abstract class

The classification of employees is: ordinary employees, managers, and bosses.
Abstract the three types of employees into one class (woker), and use polymorphism to manage different types of employees. The
attributes of employees are: employee ID, employee name, employee’s department number,
employee behavior For: Job Responsibilities Information Description, Get Job Title

Create worker.h under the header file

#pragma once
#include<iostream>
#include<stdlib.h>
using namespace std;

//抽象职工类
class Worker
{
    
    
public:
	//显示个人信息
	virtual void showInfo() = 0;
	//获取岗位名称
	virtual string getDeptName() = 0;

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

5.2 Create a common employee class

The ordinary employee class inherits the employee abstract class and overrides the pure virtual function in the parent class

Create employee.h and employee.cpp files in header file and source file add db

The employee.h file is as follows:

#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();
};

The employee.cpp file is as follows:

#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("员工");
}

Run the test:

insert image description here

5.3 Create a manager class

The manager class inherits the employee abstract class and rewrites the pure virtual function in the parent class, similar to ordinary employees

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

manager.h is as follows:

#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 is as follows:

#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("经理");
}

5.4 Create a boss class

The boss class inherits the employee abstract class and rewrites the pure virtual function in the parent class, similar to ordinary employees

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

The boss.h code is as follows:

#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();
};

The boss.cpp code is as follows:

#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("总裁");
}

5.5 Testing Polymorphism

Add a test function in the employee management system.cpp, and run it to generate polymorphism

The test code is as follows:

#include<iostream>
#include<stdlib.h>
using namespace std;
#include "wokerManager.h"
#include "boss.h"
#include "employee.h"
#include "manager.h"
#include "worker.h"

int main() {
    
    
	//测试代码
	Worker* worker = NULL;
	Worker* worker1 = NULL;
	worker = new Employee(1, "张三", 0001);
	worker->showInfo();
	worker1 = new Boss(2, "BOSS", 0000);
	worker1->showInfo();

	system("pause");
	return 0;
}

Test Results:

insert image description here

6. Add employees

Function description: Add employees in batches and save them to a file

6.1 Function Analysis

Analysis:
When users create batches, they may create different types of employees.
If you put all different types of employees in the same array, you can maintain the pointers of all employees in an array.
If you want to maintain this uncertainty in the program The length of the array, the array can be created to the heap area, and use the pointer of Worker ** to maintain

insert image description here

6.2 Function realization

workerManager.cpp:

//添加职工
void WokerManager::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.h:

#pragma once
#include <iostream>
using namespace std;
#include <worker.h>
#include <employee.h>
#include <manager.h>
#include <boss.h>
#include <fstream>

#define FILENAME "empFile.txt"

class WorkerManager
{
    
    
public:
    WorkerManager();

    void showMenu();

    void ExitSystem();

    void Add_Num();

    ~WorkerManager();

    int m_EmpNum;

    bool m_FileIsEmpty;

    Worker** m_EmpArray;

};

Staff management system.cpp:

#include <iostream>
using namespace std;
#include <workerManager.h>
#include <worker.h>
#include <employee.h>
#include <manager.h>
#include <boss.h>

int main()
{
    
    
    WorkerManager wm;
    /*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;*/

    int choice = 0;

    while (true)
    {
    
    
        wm.showMenu();
        cout << "请输入你的操作:" << endl;
        cin >> choice;

        switch (choice)
        {
    
    
        case 0://退出系统
            wm.ExitSystem();
            break;
        case 1://添加职工
            wm.Add_Num();
            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;
}

6.1 Test addition

Run the program, follow the prompts and select 1 to add employees

insert image description here

7. File interaction - write file

Function description: read and write files

In the last added function, we just added all the data to the memory. Once the program finishes running, the data cannot be saved. Therefore, the file management class needs a function to interact with the file, to read and write the file.

  1. First we add the file path, add macro constants in workerManager.h, and include the header file fstream
#include <fstream>//文件操作
#define FILENAME "empFile.txt"
  1. Add the member function void save(); in the class in workManage.h
//保存文件
void save();
  1. The overall function of saving files is realized
//保存文件功能实现
void WokerManager::save() {
    
    
	ofstream ofs;
	ofs.open(FILENAME,ios::out);//写的方式打开文件

	//for循环中把我们记录的数据进行写入
	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();
}
  1. Call the save file function after adding workers

insert image description here

  1. Save file function test

insert image description here
insert image description here

8. File interaction - write file

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

Although we realized the operation of adding employees and saving them to the file, but every time we start running the program, the data in the file is not read into the program memory, and our program function still needs to clear the file, so we The constructor initialization is divided into three cases

  1. On first use, the file is not created
  2. The file exists, but the data is cleared by the user
  3. The file exists and holds all the data of the employee

8.1 File not created

  1. Add a new member attribute m_FilesEmpty flag file is empty in wokerManager.h
//判断文件是否为空的标志
bool m_FilesIsEmpty;
  1. Modify WOK and the constructor code in Manager.cpp
//1.文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);//读文件
if (ifs.is_open()) {
    
    
	cout << "文件不存在!" << endl;
	//初始化属性
	//初始化记录数
	this->m_EmpNum = 0;
	//初始化数组指针
	this->m_EmpArray = NULL;
	//初始化我呢间是否为空
	this->m_FilesIsEmpty = true;
	ifs.close();
	return;
}

8.2 The file exists and the data is empty

Add code to the constructor in WOK and Manager.cpp:

// 2.文件存在,但是数据为空
WorkerManager::WorkerManager()
{
    
    
	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;
	}
}

8.3 File exists and saves employee data

  1. Add member function int get_EmpNum() in wokerManager.h;
//统计文件中的人数
int get_EmpNum();
  1. Implemented in wokerManager.cpp
//统计文件中的人数
int WokerManager::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++;
	}
	return num;

}

According to the worker's data, initialize the Woker** m_Emparray pointer in the wokerManager

  1. Add member function void init_Emp() in wokerManager.h;
//初始化职工
void init_Emp();
  1. Implemented in wokerManager.cpp
//初始化职工
void WokerManager::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;
		if (dId == 1) {
    
    //普通职工
			worker = new Employee(id, name, dId);
		}
		else if (dId == 2) {
    
    //经理
			worker = new Manager(id ,name ,dId);
		}
		else {
    
    //老板
			worker = new Boss(id, name, dId);
		}
		this->m_EmpArray[index] = worker;
		index++;

	}
	cout << "共从文件中初始化[" << index << "]条数据" << endl;

	//关闭文件
	ifs.close();
}

9. Show workers

Function description: Display all current employee information

  1. Add a member function void Show_Emp() in workerManager.h;
//显示职工信息
void Show_Emp();
  1. Implement the member function void Show_Emp() in workerManager.cpp;
//显示职工信息
void WokerManager::Show_Emp() {
    
    
	//判断文件是否为空或者不存在
	if (this->m_FilesIsEmpty) {
    
    
		cout << "文件不存在或者记录为空!" << endl;
	}
	else {
    
    
		for (int i = 0; i < m_EmpNum; i++) {
    
    
			//利用多态调用显示的接口
			this->m_EmpArray[i]->showInfo();
		}
	}

	//按任意键后清屏
	//system("paus");
	//system("cls");
}
  1. Main function implementation

insert image description here

  1. test run

insert image description here

10. Delete employees

Function description: Delete operation according to employee number

  1. Add a member function void Del_Emp() in workerManager.h;
//删除职工
void Del_Emp();

Many functions need to be performed according to the existence of employees, such as deleting employees, modifying employees, finding employees, etc., so add this function

  1. Add a member function int IsExit() in workerManager.h;
//判断职工是否存在,存在则返回职工在数组中的位置,不存在返回-1
int IsExit(int id);
  1. Does the employee exist to achieve
int WorkerManager::is_Exist(int i)
{
    
    
	int index = -1;

	for (int j = 0;j < this->m_EmpNum;j++)
	{
    
    
		if (this->m_EmpArray[j]->m_Id == i)
		{
    
    
			index = j;
		}
	}

	return index;
}
  1. delete worker implementation
void WorkerManager::Del_Emp()
{
    
    
	if (this->m_FileIsEmpty) {
    
    
		cout << "文件不存在或者记录为空!" << endl;
	}
	else {
    
    
		//按照职工编号进行删除
		cout << "请输入需要删除的职工编号" << endl;
		int id = 0;
		cin >> id;

		int index = this->is_Exist(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");
}
  1. The main function adds the delete function

insert image description here

  1. delete worker demo
    insert image description here
    insert image description here
    insert image description here

11. Modify workers

Function description: It can modify the employee information according to the employee number

  1. Add a member function void Mod_Emp() in workerManager.h;
//修改职工
void Mod_Emp();
  1. Implement the member function void Mod_Emp() in workerManager.cpp;
//修改职工
void WokerManager::Mod_Emp() {
    
    
	if (this->m_FilesIsEmpty) {
    
    
		cout << "文件不存在或者记录为空!" << endl;
	}
	else {
    
    
		cout << "请输入需要【修改】的职工编号" << endl;
		int id;
		cin >> id;
		int ret = this->IsExit(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* woker = NULL;
			switch (dSelect)
			{
    
    
			case 1:
				woker = new Employee(newId, newName, dSelect);
			case 2:
				woker = new Manager(newId, newName, dSelect);
			case 3:
				woker = new Boss(newId, newName, dSelect);
			}
			//数据更新到数组中
			this->m_EmpArray[ret] = woker;
			cout << "职工信息修改成功!" << endl;

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

	//按任意键清屏
	system("pause");
	system("cls");
}
  1. Add the modification function to the main function

insert image description here

  1. Modify function demo

insert image description here
insert image description here

12. Find employees

Function description: Provide two employee query methods, one is based on the employee number, and the other is based on the employee name

  1. Add a member function void Find_Emp() in workerManager.h;
//查找职工
void Find_Emp();
  1. Implement the member function void Find_Emp() in workerManager.cpp;
//查找职工
void WokerManager::Find_Emp() {
    
    
	if (this->m_FilesIsEmpty) {
    
    
		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 = IsExit(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 select_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;

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

	//按任意键清屏
	system("pause");
	system("cls");
}
  1. Add a search function to the main function

insert image description here

  1. Find feature demos

insert image description here

13. Staff sorting

Function description: Sort in ascending or descending order by employee number

  1. Add a member function void Sort_Emp() in workerManager.h;
//按照编号排序
void Sort_Emp();
  1. Implement the member function void Sort_Emp() in workerManager.cpp;
//职工排序
void WokerManager::Sort_Emp() {
    
    
	if (this->m_FilesIsEmpty) {
    
    
		cout << "文件不存在或者记录为空!" << endl;
	}
	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 (this->m_EmpArray[minOrMax]->m_Id > this->m_EmpArray[j]->m_Id) {
    
    
						minOrMax = j;
					}

				}
				else {
    
    //降序
					if (this->m_EmpArray[minOrMax]->m_Id < this->m_EmpArray[j]->m_Id) {
    
    
						minOrMax = j;
					}
				}
			}

			//判断一开始认定的最小值或者最大值 是不是计算的最小值或者最大值,如果不是则就交换数据
			if (i != minOrMax) {
    
    
				Worker* temp = this->m_EmpArray[i];
				this->m_EmpArray[i]  = this->m_EmpArray[minOrMax];
				this->m_EmpArray[minOrMax] = temp;
			}
			
		}
	}
	cout << "排序成功,排序后结果为:" << endl;
	this->save();//排序后的结果保存到文件中
	this->Show_Emp();
}
  1. Sort function demo

insert image description here

insert image description here

14. Empty workers

Function description: clear the records in the file

  1. Add a member function void Clean_File() in workerManager.h;
//清空文件
void Clean_File();
  1. Implement the member function void Clean_File() in workerManager.cpp;
// 清空文件
void WokerManager::Clean_File() {
    
    
	cout << "确定清空文件?" << endl;
	cout << "1、确认" << endl;
	cout << "2、返回" << endl;

	int selcet = 0;
	cin >> selcet;
	if (selcet == 1) {
    
    
		//清空文件
		ofstream ofs(FILENAME, ios::trunc);//删除文件后重新创建
		if (this->m_EmpArray != NULL) {
    
    
			//删除堆区的每个职工对象
			for (int i = 0; i < m_EmpNum; i++) {
    
    
				delete this->m_EmpArray[i];
				this->m_EmpArray[i] = NULL;

			}
			//删除堆区数组指针
			delete[] this->m_EmpArray;
			this->m_EmpArray = NULL;
			this->m_EmpNum = 0;
			this->m_FilesIsEmpty = true;
		}

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

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

}
  1. Add a clear function to the main function

insert image description here

  1. Clear Effect Demo

insert image description here
insert image description here
insert image description here

15. Main function code:

#include <iostream>
using namespace std;
#include <workerManager.h>
#include <worker.h>
#include <employee.h>
#include <manager.h>
#include <boss.h>

int main()
{
    
    
    WorkerManager wm;
    /*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;*/

    int choice = 0;

    while (true)
    {
    
    
        wm.showMenu();
        cout << "请输入你的操作:" << endl;
        cin >> choice;

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

    }

    

    system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_62361050/article/details/127715773