黑马C++笔记——职工管理系统(C++实现)
2023-09-14 09:14:58 时间
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;
}
相关文章
- 【C/C++学院】(4)c++开篇/类和对象/命名空间/类型增强/三目运算符/const专题/引用专题/函数增强
- 鸡啄米:C++编程之十四学习之构造函数和析构函数
- linux c++下遍历文件
- Effective C++笔记(二):构造/析构/赋值运算
- C++随笔
- C++ Primer读书笔记(2)
- 第十二届蓝桥杯省赛第二场C++B组真题(节选)
- Stable Matching-稳定匹配问题【G-S算法,c++】
- C/C++每日一练(20230228)
- 数制转换-栈的应用(C++实现)
- 【华为OD机试 2023】 匿名信(C++ Java JavaScript Python)
- 解答私信@被c++折磨头秃的花季美少女 //C++ 利用指针数组输入10个单词,编写函数对10个单词进行排序并输出,要求判断是否有相同的单词,如果有相同的单词在输出时该单词只输出一次。
- 解答私信@被c++折磨头秃的花季美少女 //C++ 写一个带命令行参数的程序,可以实现将参数求和、求平均值以及排序之后输出(参数的数量不确定)。
- 精心为学弟学妹整理了60个适合练手的C语言/C++项目,值得收藏!
- C++ muduo网络库 学习笔记之:Reactor框架总览
- C++ 命名空间 实战(二)之 直接数组访问&迭代器访问
- PAT 1128 C++版
- Foxit PDF SDK 9.0.0 linux for C++ Crack
- C++运算符优先级