黑马C++笔记——职工管理系统(C++实现)

1.功能拆解

在这里插入图片描述

2.代码实现

2.1 实现管理类

#pragma once //防止头文件被重复包含
#include<iostream>
#include<fstream>
using namespace std;

#include"Worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"

#define FILENAME "empfile.txt"

class WorkerManager {
public:
	//员工数组的size
	int empNum;
	//指向员工数组的指针
	Worker** empArray;

	//文件是否存在的标志
	bool isFileEmpty;

	WorkerManager();

	//显示菜单
	void show_menu();

	//添加新职工
	void add_emp();

	//将员工信息写入文件中
	void save();

	//获取文件中职工数量
	int getEmpNum();

	//初始化职工数组
	void init_emp();

	//显示职工信息
	void show_emp();

	//删除职工
	void delete_emp();

	//职工是否存在,存在返回其位置,否则返回-1
	int isEmpExist(int id);

	//修改职工信息
	void update_emp();

	//查找职工
	void find_emp();

	//对职工按编号排序
	void sort_emp();

	//清空文件
	void clear_emp();

	//退出系统
	void exitSystem();

	~WorkerManager();
};

2.2 实现菜单功能

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;
}

2.3 实现退出功能

	//退出系统
void exitSystem();

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

2.4 创建职工类

Worker.h

#pragma once
#include<string>
using namespace std;

class Worker {
public:
	int w_id;
	string w_name;
	int w_deptId;

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

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

employee.h

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

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

	virtual void showInfo();

	virtual string getDeptName();
};

employee.cpp

#include "employee.h"
#include "Worker.h"
using namespace std;

Employee::Employee(int id, string name, int deptId) {
	this->w_id = id;
	this->w_name = name;
	this->w_deptId = deptId;
}

//显示员工个人信息
void Employee::showInfo() {
	cout << "职工编号:  " << this->w_id
		<< "\t职工姓名:  " << this->w_name
		<< "\t岗位:  " << this->getDeptName()
		<< "\t岗位职责:  完成经理交给的任务" << endl;
}

//显示岗位名称
string Employee::getDeptName() {
	return string("员工");
}

manager.h

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

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

	virtual void showInfo();

	virtual string getDeptName();
};

manager.cpp

#include "manager.h"
#include "Worker.h"
using namespace std;

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

//显示员工个人信息
void Manager::showInfo() {
	cout << "职工编号:  " << this->w_id
		<< "\t职工姓名:  " << this->w_name
		<< "\t岗位:  " << this->getDeptName()
		<< "\t岗位职责:  完成老板交给的任务,并下发任务给员工" << endl;
}

//显示岗位名称
string Manager::getDeptName() {
	return string("经理");
}

boss.h

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

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

	virtual void showInfo();

	virtual string getDeptName();
};

boss.cpp

#include "boss.h"
#include "Worker.h"
using namespace std;

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

//显示员工个人信息
void Boss::showInfo() {
	cout << "职工编号:  " << this->w_id
		<< "\t职工姓名:  " << this->w_name
		<< "\t岗位:  " << this->getDeptName()
		<< "\t岗位职责:  管理公司所有的事务" << endl;
}

//显示岗位名称
string Boss::getDeptName() {
	return string("老板");
}

2.5 添加职工

在这里插入图片描述

//添加新员工
void WorkerManager::add_emp() {
	cout << "请输入添加要添加员工的数量" << endl;
	int addNum;
	cin >> addNum;
	if (addNum > 0) {
		int newSize = this->empNum + addNum;
		Worker** newSpace = new Worker * [newSize];
		
		//原来的数组还有元素,需要将其复制到新的数组中
		if (this->empArray != nullptr) {
			for (int i = 0; i < this->empNum; i++) {
				newSpace[i] = this->empArray[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 = nullptr;

			switch (deptId) {
			case 1:
				worker = new Employee(id, name, deptId);
				break;
			case 2:
				worker = new Manager(id, name, deptId);
				break;
			case 3:
				worker = new Boss(id, name, deptId);
				break;
			default:
				break;
			}

			newSpace[this->empNum + i] = worker;
		}
		delete[] this->empArray;
		this->empArray = newSpace;
		this->empNum = newSize;

		this->isFileEmpty = false;
		cout << "成功添加了 " << addNum << " 位新职工" << endl;
		this->save();
	}
	else {
		cout << "输入有误,请重新输入" << endl;
	}
	system("pause");
	system("cls");
}

2.6 初始化

WorkerManager::WorkerManager() {
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	if (!ifs.is_open()) {
		//cout << "初始化时文件不存在" << endl;
		this->empArray = nullptr;
		this->empNum = 0;
		this->isFileEmpty = true;
		ifs.close();

		return;
	}

	//文件存在,但是文件为空
	char ch;
	ifs >> ch;
	if (ifs.eof()) {
		//cout << "初始化时文件为空" << endl;
		this->empArray = nullptr;
		this->empNum = 0;
		this->isFileEmpty = true;
		ifs.close();

		return;
	}


	/*int num = this->getEmpNum();
	cout << "文件中职工的数量为:" << num << endl;*/

	this->isFileEmpty = false;
	this->empNum = this->getEmpNum();
	this->empArray = new Worker * [this->empNum];

	//初始化职工数组
	this->init_emp();

	/*for (int i = 0; i < this->empNum; i++) {
		cout << "职工编号:  " << this->empArray[i]->w_id
			<< "\t姓名:  " << this->empArray[i]->w_name
			<< "\t职位:  " << this->empArray[i]->getDeptName() << endl;
	}*/
}

2.7 显示职工信息

void WorkerManager::show_emp() {
	if (this->isFileEmpty) {
		cout << "文件不存在或者为空" << endl;
	}
	else {
		for (int i = 0; i < this->empNum; i++) {
			this->empArray[i]->showInfo();
		}
	}
	system("pause");
	system("cls");
}

2.8 删除职工

void WorkerManager::delete_emp() {
	if (this->isFileEmpty) {
		cout << "文件不存在或者为空" << endl;
	}
	else {
		cout << "请输入要删除的职工编号:" << endl;
		int id;
		cin >> id;
		int index = this->isEmpExist(id);
		if (index == -1) {
			cout << "该员工不存在" << endl;
		}
		else {
			for (int i = index; i < this->empNum - 1; i++) {
				this->empArray[i] = this->empArray[i + 1];
			}
			this->empNum--;

			//更新到文件
			this->save();
			cout << "删除成功" << endl;
		}
	}
	system("pause");
	system("cls");
}


//职工是否存在,存在返回其位置,否则返回-1
int WorkerManager::isEmpExist(int id) {
	int index = -1;
	for (int i = 0; i < this->empNum; i++) {
		if (this->empArray[i]->w_id == id) {
			index = i;
			break;
		}
	}
	return index;
}

2.9 修改职工

//修改职工信息
void WorkerManager::update_emp() {
	if (this->isFileEmpty) {
		cout << "文件不存在或者为空" << endl;
	}
	else {
		cout << "请输入要修改的员工编号:" << endl;
		int id;
		cin >> id;
		int index = this->isEmpExist(id);
		if (index == -1) {
			cout << "查无此人" << endl;
		}
		else {
			int newId;
			string newName;
			int newDeptId;

			//释放原来的空间
			delete this->empArray[index];

			Worker* worker = nullptr;
			cout << "请输入新的职工编号:" << endl;
			cin >> newId;
			cout << "请输入新的职工姓名:" << endl;
			cin >> newName;
			cout << "请输入新的职工岗位:" << endl;
			cin >> newDeptId;

			if (newDeptId == 1) worker = new Employee(newId, newName, newDeptId);
			else if(newDeptId == 2) worker = new Manager(newId, newName, newDeptId);
			else if(newDeptId == 3) worker = new Boss(newId, newName, newDeptId);

			this->empArray[index] = worker;
			cout << "修改成功" << endl;

			//保存到文件
			this->save();
		}
	}
	system("pause");
	system("cls");
}

2.10 查找职工

//查找职工
void WorkerManager::find_emp() {
	if (this->isFileEmpty) {
		cout << "文件不存在或者为空" << endl;
	}
	else {
		int select;
		cout << "1、按照职工编号查找" << endl;
		cout << "2、按照职工姓名查找" << endl;
		cin >> select;
		if (select == 1) {
			int id;
			cout << "请输入要查找的职工编号:" << endl;
			cin >> id;

			int index = this->isEmpExist(id);
			if (index == -1) {
				cout << "查无此人" << endl;
			}
			else {
				cout << "查找成功,职工信息如下:" << endl;
				this->empArray[index]->showInfo();
			}
		}
		else if (select == 2) {
			string name;
			cout << "请输入要查找职工的姓名:" << endl;
			cin >> name;
			bool ok = false;
			for (int i = 0; i < this->empNum; i++) {
				if (this->empArray[i]->w_name == name) {
					cout << "查找成功,职工信息如下:" << endl;
					this->empArray[i]->showInfo();
					ok = true;
				}
			}
			if (!ok) {
				cout << "查无此人" << endl;
			}
		}
		else {
			cout << "输入有误" << endl;
		}
	}

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

2.11 按照职工编号排序

//对职工按编号排序
void WorkerManager::sort_emp() {
	if (this->isFileEmpty) {
		cout << "文件不存在或者为空" << endl;
		system("pause");
		system("cls");
	}
	else {
		int select;
		cout << "请输入排序方式:" << endl;
		cout << "1、按职工编号升序排序" << endl;
		cout << "2、按职工编号降序排序" << endl;
		cin >> select;

		for (int i = 0; i < this->empNum; i++) {
			int minOrMax = i;
			for (int j = i + 1; j < this->empNum; j++) {
				if (select == 1) {
					if (this->empArray[j]->w_id < this->empArray[minOrMax]->w_id) minOrMax = j;
				}
				else {
					if (this->empArray[j]->w_id > this->empArray[minOrMax]->w_id) minOrMax = j;
				}
			}
			if (i != minOrMax) {
				Worker* temp = this->empArray[i];
				this->empArray[i] = this->empArray[minOrMax];
				this->empArray[minOrMax] = temp;
			}
		}

		this->save();
		cout << "排序后的结果为:" << endl;
		this->show_emp();
	}
}

2.12 清空文件内容

//清空文件
void WorkerManager::clear_emp() {
	if (this->isFileEmpty) {
		cout << "文件不存在或者为空" << endl;
	}
	else {
		int select = 0;
		cout << "确认是否清空文件" << endl;
		cout << "1、确认" << endl;
		cout << "2、返回" << endl;
		cin >> select;
		if (select == 1) {
			if (this->empArray != nullptr) {
				for (int i = 0; i < this->empNum; i++) {
					if (this->empArray[i] != nullptr) {
						delete this->empArray[i];
						this->empArray[i] = nullptr;
					}
				}

				delete[] this->empArray;
				this->empArray = nullptr;
				this->empNum = 0;
				this->isFileEmpty = true;
				
				this->save();
				cout << "清空成功" << endl;
			}
		}
	}
	system("pause");
	system("cls");
}

3.完整代码

在这里插入图片描述

Worker.h

#pragma once
#include<string>
using namespace std;

class Worker {
public:
	int w_id;
	string w_name;
	int w_deptId;

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

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

WorkerManager.h

#pragma once //防止头文件被重复包含
#include<iostream>
#include<fstream>
using namespace std;

#include"Worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"

#define FILENAME "empfile.txt"

class WorkerManager {
public:
	//员工数组的size
	int empNum;
	//指向员工数组的指针
	Worker** empArray;

	//文件是否存在的标志
	bool isFileEmpty;

	WorkerManager();

	//显示菜单
	void show_menu();

	//添加新职工
	void add_emp();

	//将员工信息写入文件中
	void save();

	//获取文件中职工数量
	int getEmpNum();

	//初始化职工数组
	void init_emp();

	//显示职工信息
	void show_emp();

	//删除职工
	void delete_emp();

	//职工是否存在,存在返回其位置,否则返回-1
	int isEmpExist(int id);

	//修改职工信息
	void update_emp();

	//查找职工
	void find_emp();

	//对职工按编号排序
	void sort_emp();

	//清空文件
	void clear_emp();

	//退出系统
	void exitSystem();

	~WorkerManager();
};

employee.h

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

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

	virtual void showInfo();

	virtual string getDeptName();
};

manager.h

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

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

	virtual void showInfo();

	virtual string getDeptName();
};

boss.h

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

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

	virtual void showInfo();

	virtual string getDeptName();
};

employee.cpp

#include "employee.h"
#include "Worker.h"
using namespace std;

Employee::Employee(int id, string name, int deptId) {
	this->w_id = id;
	this->w_name = name;
	this->w_deptId = deptId;
}

//显示员工个人信息
void Employee::showInfo() {
	cout << "职工编号:  " << this->w_id
		<< "\t职工姓名:  " << this->w_name
		<< "\t岗位:  " << this->getDeptName()
		<< "\t岗位职责:  完成经理交给的任务" << endl;
}

//显示岗位名称
string Employee::getDeptName() {
	return string("员工");
}

manager.cpp

#include "manager.h"
#include "Worker.h"
using namespace std;

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

//显示员工个人信息
void Manager::showInfo() {
	cout << "职工编号:  " << this->w_id
		<< "\t职工姓名:  " << this->w_name
		<< "\t岗位:  " << this->getDeptName()
		<< "\t岗位职责:  完成老板交给的任务,并下发任务给员工" << endl;
}

//显示岗位名称
string Manager::getDeptName() {
	return string("经理");
}

boss.cpp

#include "boss.h"
#include "Worker.h"
using namespace std;

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

//显示员工个人信息
void Boss::showInfo() {
	cout << "职工编号:  " << this->w_id
		<< "\t职工姓名:  " << this->w_name
		<< "\t岗位:  " << this->getDeptName()
		<< "\t岗位职责:  管理公司所有的事务" << endl;
}

//显示岗位名称
string Boss::getDeptName() {
	return string("老板");
}

WorkerManager.cpp

#include "WorkerManager.h"

WorkerManager::WorkerManager() {
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	if (!ifs.is_open()) {
		//cout << "初始化时文件不存在" << endl;
		this->empArray = nullptr;
		this->empNum = 0;
		this->isFileEmpty = true;
		ifs.close();

		return;
	}

	//文件存在,但是文件为空
	char ch;
	ifs >> ch;
	if (ifs.eof()) {
		//cout << "初始化时文件为空" << endl;
		this->empArray = nullptr;
		this->empNum = 0;
		this->isFileEmpty = true;
		ifs.close();

		return;
	}


	/*int num = this->getEmpNum();
	cout << "文件中职工的数量为:" << num << endl;*/

	this->isFileEmpty = false;
	this->empNum = this->getEmpNum();
	this->empArray = new Worker * [this->empNum];

	//初始化职工数组
	this->init_emp();

	/*for (int i = 0; i < this->empNum; i++) {
		cout << "职工编号:  " << this->empArray[i]->w_id
			<< "\t姓名:  " << this->empArray[i]->w_name
			<< "\t职位:  " << this->empArray[i]->getDeptName() << endl;
	}*/
}

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;
}

//添加新员工
void WorkerManager::add_emp() {
	cout << "请输入添加要添加员工的数量" << endl;
	int addNum;
	cin >> addNum;
	if (addNum > 0) {
		int newSize = this->empNum + addNum;
		Worker** newSpace = new Worker * [newSize];
		
		//原来的数组还有元素,需要将其复制到新的数组中
		if (this->empArray != nullptr) {
			for (int i = 0; i < this->empNum; i++) {
				newSpace[i] = this->empArray[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 = nullptr;

			switch (deptId) {
			case 1:
				worker = new Employee(id, name, deptId);
				break;
			case 2:
				worker = new Manager(id, name, deptId);
				break;
			case 3:
				worker = new Boss(id, name, deptId);
				break;
			default:
				break;
			}

			newSpace[this->empNum + i] = worker;
		}
		delete[] this->empArray;
		this->empArray = newSpace;
		this->empNum = newSize;

		this->isFileEmpty = false;
		cout << "成功添加了 " << addNum << " 位新职工" << endl;
		this->save();
	}
	else {
		cout << "输入有误,请重新输入" << endl;
	}
	system("pause");
	system("cls");
}

void WorkerManager::save() {
	ofstream ofs;
	ofs.open(FILENAME, ios::out);

	for (int i = 0; i < this->empNum; i++) {
		ofs << this->empArray[i]->w_id << "  "
			<< this->empArray[i]->w_name << "  "
			<< this->empArray[i]->w_deptId << endl;
	}

	ofs.close();
}

int WorkerManager::getEmpNum() {
	int num = 0;
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	int id;
	string name;
	int deptId;

	while (ifs >> id && ifs >> name && ifs >> deptId) {
		num++;
	}
	ifs.close();
	return num;
}

void WorkerManager::init_emp() {
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int idx = 0;
	int id;
	string name;
	int deptId;
	while (ifs >> id && ifs >> name && ifs >> deptId) {
		if (deptId == 1) this->empArray[idx] = new Employee(id, name, deptId);
		else if (deptId == 2) this->empArray[idx] = new Manager(id, name, deptId);
		else if (deptId == 3) this->empArray[idx] = new Boss(id, name, deptId);
		idx++;
	}

	ifs.close();
}

void WorkerManager::show_emp() {
	if (this->isFileEmpty) {
		cout << "文件不存在或者为空" << endl;
	}
	else {
		for (int i = 0; i < this->empNum; i++) {
			this->empArray[i]->showInfo();
		}
	}
	system("pause");
	system("cls");
}

//删除职工
void WorkerManager::delete_emp() {
	if (this->isFileEmpty) {
		cout << "文件不存在或者为空" << endl;
	}
	else {
		cout << "请输入要删除的职工编号:" << endl;
		int id;
		cin >> id;
		int index = this->isEmpExist(id);
		if (index == -1) {
			cout << "该员工不存在" << endl;
		}
		else {
			for (int i = index; i < this->empNum - 1; i++) {
				this->empArray[i] = this->empArray[i + 1];
			}
			this->empNum--;

			//更新到文件
			this->save();
			cout << "删除成功" << endl;
		}
	}
	system("pause");
	system("cls");
}

//职工是否存在,存在返回其位置,否则返回-1
int WorkerManager::isEmpExist(int id) {
	int index = -1;
	for (int i = 0; i < this->empNum; i++) {
		if (this->empArray[i]->w_id == id) {
			index = i;
			break;
		}
	}
	return index;
}

//修改职工信息
void WorkerManager::update_emp() {
	if (this->isFileEmpty) {
		cout << "文件不存在或者为空" << endl;
	}
	else {
		cout << "请输入要修改的员工编号:" << endl;
		int id;
		cin >> id;
		int index = this->isEmpExist(id);
		if (index == -1) {
			cout << "查无此人" << endl;
		}
		else {
			int newId;
			string newName;
			int newDeptId;

			//释放原来的空间
			delete this->empArray[index];

			Worker* worker = nullptr;
			cout << "请输入新的职工编号:" << endl;
			cin >> newId;
			cout << "请输入新的职工姓名:" << endl;
			cin >> newName;
			cout << "请输入新的职工岗位:" << endl;
			cin >> newDeptId;

			if (newDeptId == 1) worker = new Employee(newId, newName, newDeptId);
			else if(newDeptId == 2) worker = new Manager(newId, newName, newDeptId);
			else if(newDeptId == 3) worker = new Boss(newId, newName, newDeptId);

			this->empArray[index] = worker;
			cout << "修改成功" << endl;

			//保存到文件
			this->save();
		}
	}
	system("pause");
	system("cls");
}

//查找职工
void WorkerManager::find_emp() {
	if (this->isFileEmpty) {
		cout << "文件不存在或者为空" << endl;
	}
	else {
		int select;
		cout << "1、按照职工编号查找" << endl;
		cout << "2、按照职工姓名查找" << endl;
		cin >> select;
		if (select == 1) {
			int id;
			cout << "请输入要查找的职工编号:" << endl;
			cin >> id;

			int index = this->isEmpExist(id);
			if (index == -1) {
				cout << "查无此人" << endl;
			}
			else {
				cout << "查找成功,职工信息如下:" << endl;
				this->empArray[index]->showInfo();
			}
		}
		else if (select == 2) {
			string name;
			cout << "请输入要查找职工的姓名:" << endl;
			cin >> name;
			bool ok = false;
			for (int i = 0; i < this->empNum; i++) {
				if (this->empArray[i]->w_name == name) {
					cout << "查找成功,职工信息如下:" << endl;
					this->empArray[i]->showInfo();
					ok = true;
				}
			}
			if (!ok) {
				cout << "查无此人" << endl;
			}
		}
		else {
			cout << "输入有误" << endl;
		}
	}

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

//对职工按编号排序
void WorkerManager::sort_emp() {
	if (this->isFileEmpty) {
		cout << "文件不存在或者为空" << endl;
		system("pause");
		system("cls");
	}
	else {
		int select;
		cout << "请输入排序方式:" << endl;
		cout << "1、按职工编号升序排序" << endl;
		cout << "2、按职工编号降序排序" << endl;
		cin >> select;

		for (int i = 0; i < this->empNum; i++) {
			int minOrMax = i;
			for (int j = i + 1; j < this->empNum; j++) {
				if (select == 1) {
					if (this->empArray[j]->w_id < this->empArray[minOrMax]->w_id) minOrMax = j;
				}
				else {
					if (this->empArray[j]->w_id > this->empArray[minOrMax]->w_id) minOrMax = j;
				}
			}
			if (i != minOrMax) {
				Worker* temp = this->empArray[i];
				this->empArray[i] = this->empArray[minOrMax];
				this->empArray[minOrMax] = temp;
			}
		}

		this->save();
		cout << "排序后的结果为:" << endl;
		this->show_emp();
	}
}

//清空文件
void WorkerManager::clear_emp() {
	if (this->isFileEmpty) {
		cout << "文件不存在或者为空" << endl;
	}
	else {
		int select = 0;
		cout << "确认是否清空文件" << endl;
		cout << "1、确认" << endl;
		cout << "2、返回" << endl;
		cin >> select;
		if (select == 1) {
			if (this->empArray != nullptr) {
				for (int i = 0; i < this->empNum; i++) {
					if (this->empArray[i] != nullptr) {
						delete this->empArray[i];
						this->empArray[i] = nullptr;
					}
				}

				delete[] this->empArray;
				this->empArray = nullptr;
				this->empNum = 0;
				this->isFileEmpty = true;
				
				this->save();
				cout << "清空成功" << endl;
			}
		}
	}
	system("pause");
	system("cls");
}

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

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

职工管理系统.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 = new Employee(1, "wurusai", 1);
	worker->showInfo();
	delete worker;

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

	worker = new Boss(3, "Elon", 3);
	worker->showInfo();
	delete worker;*/

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

		switch (choice)
		{
	    //退出系统
		case 0:
			wm.exitSystem();
			break;
        //增加职工
		case 1:
			wm.add_emp();
			break;
        //显示职工  s
		case 2:
			wm.show_emp();
			break;
        //删除职工
		case 3:
			wm.delete_emp();
			break;
        //修改职工
		case 4:
			wm.update_emp();
			break;
        //查找职工
		case 5:
			wm.find_emp();
			break;
        //按照编号排序
		case 6:
			wm.sort_emp();
			break;
        //清空所有内容
		case 7:
			wm.clear_emp();
			break;
		default:
			system("cls");
			break;
		}
	}

	system("pause");
	return 0;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值