CMakeList.txt
[C++] 纯文本查看 复制代码 cmake_minimum_required(VERSION 3.19)
project(main)
set(CMAKE_CXX_STANDARD 14)
include_directories(.)
add_executable(main
main.cpp
worker.h
workerManager.cpp
workerManager.h employee.cpp employee.h manager.cpp manager.h boss.cpp boss.h)
main.cpp
[C++] 纯文本查看 复制代码 #include "workerManager.h"
int main() {
WorkerManager wm;
int choice = 0;
while (true) {
wm.showMenu();
cout << "请输入您的选择:";
cin >> choice;
switch (choice) {
case 0:
wm.exitSystem();
break;
case 1:
wm.addEmp();
break;
case 2:
wm.showEmpInfo();
break;
case 3:
wm.delEmp();
break;
case 4:
wm.modEmp();
break;
case 5:
wm.findEmp();
break;
case 6:
wm.sortEmp();
break;
case 7:
wm.cleanEmp();
break;
default:
system("clear");
break;
}
}
return 0;
}
workerManager.h
[C++] 纯文本查看 复制代码 #pragma once
#include "worker.h"
#include <iostream>
#include <string>
#include <fstream>
#define FILENAME "empFile.txt"
using namespace std;
class WorkerManager {
public:
WorkerManager();
void showMenu();
void exitSystem();
void addEmp();
void saveFile();
int getEmpNumFromFile();
void initEmpArray();
void showEmpInfo();
int isExist(int id); // 根据职工编号判断是否存在,返回数组下标,-1表示不存在
void delEmp();
void modEmp();
void findEmp();
void sortEmp();
void cleanEmp();
~WorkerManager();
private:
int empNum;
Worker **empArr; // Worker *的指针数组,多态保存每种职工
};
workerManager.cpp
[C++] 纯文本查看 复制代码 #include "workerManager.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"
WorkerManager::WorkerManager() {
int num = this->getEmpNumFromFile();
// 文件不存在或者内容为空
if (num == 0) {
this->empNum = 0;
this->empArr = NULL;
return;
}
this->empNum = num;
this->empArr = new Worker*[num]; // 申请数组空间
this->initEmpArray(); // 初始化职工信息
}
void WorkerManager::showMenu() {
cout << "*************************" << endl;
cout << "*** 欢迎使用职工管理系统 ***" << endl;
cout << "***** 1、增加职工信息 *****" << endl;
cout << "***** 2、显示职工信息 *****" << endl;
cout << "***** 3、删除离职职工 *****" << endl;
cout << "***** 4、修改职工信息 *****" << endl;
cout << "***** 5、查找职工信息 *****" << endl;
cout << "***** 6、按照编号排序 *****" << endl;
cout << "***** 7、清空所有文档 *****" << endl;
cout << "***** 0、退出管理程序 *****" << endl;
cout << "*************************" << endl;
}
void WorkerManager::exitSystem() {
cout << "欢迎下次使用" << endl;
exit(0);
}
Worker *workerFactory(int id, string name, int deptId) {
Worker *worker = NULL;
switch (deptId) {
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;
}
return worker;
}
void WorkerManager::addEmp() {
cout << "请输入增加职工数量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum <= 0) {
cout << "输入有误" << endl;
return;
}
// 计算新空间大小
int newSize = this->empNum + addNum;
// 开辟新空间
Worker **newSpace = new Worker *[newSize];
// 将原空间里内容拷贝到新空间下
if (this->empArr != NULL) {
for (int i = 0; i < this->empNum; ++i) {
newSpace[i] = this->empArr[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 = workerFactory(id, name, deptId);
newSpace[this->empNum + i] = worker;
}
// 释放原有空间
delete[] this->empArr;
// 更改新空间指向
this->empArr = newSpace;
// 更新员工个数
this->empNum = newSize;
// 保存数据到文件
this->saveFile();
cout << "成功添加" << addNum << "名新职工" << endl;
}
void WorkerManager::saveFile() {
ofstream ofs(FILENAME, ios::out);
for (int i = 0; i < this->empNum; ++i) {
ofs << this->empArr[i]->getId() << " "
<< this->empArr[i]->getName() << " "
<< this->empArr[i]->getDeptId() << endl;
}
ofs.close();
}
int WorkerManager::getEmpNumFromFile() {
int empNumFromFile = 0;
ifstream ifs(FILENAME, ios::in);
// 文件不存在,返回职工数0
if (!ifs.is_open()) {
ifs.close();
return empNumFromFile;
}
// 读一个字符,看文件是否到eof,判断文件是否为空
// char ch;
// ifs >> ch;
// if (ifs.eof())
int id;
string name;
int deptId;
while (ifs >> id && ifs >> name && ifs >> deptId) {
empNumFromFile++;
}
ifs.close();
return empNumFromFile;
}
void WorkerManager::initEmpArray() {
ifstream ifs(FILENAME, ios::in);
int id;
string name;
int deptId;
int index = 0;
while (ifs >> id && ifs >> name && ifs >> deptId) {
Worker *worker = workerFactory(id, name, deptId);
this->empArr[index++] = worker;
}
ifs.close();
}
void WorkerManager::showEmpInfo() {
if (this->empNum == 0) {
cout << "文件不存在或记录为空!" << endl;
return;
}
for (int i = 0; i < this->empNum; ++i) {
// 利用多态调用接口
this->empArr[i]->showInfo();
}
}
int WorkerManager::isExist(int id) {
int index = -1;
for (int i = 0; i < this->empNum; ++i) {
if (this->empArr[i]->getId() == id) {
index = i;
break;
}
}
return index;
}
void WorkerManager::delEmp() {
if (this->empNum == 0) {
cout << "职工记录为空" << endl;
return;
}
cout << "请输入想要删除的职工编号:" << endl;
int id = 0;
cin >> id;
int index = this->isExist(id);
if (index == -1) {
cout << "删除失败,未找到该职工" << endl;
return;
}
// 删除 index 位置的数据
for (int i = index; i < this->empNum - 1; i++) {
this->empArr[i] = this->empArr[i + 1];
}
this->empNum--;
// 删除后数据同步到文件中
this->saveFile();
cout << "删除成功" << endl;
}
void WorkerManager::modEmp() {
if (this->empNum == 0) {
cout << "职工记录为空" << endl;
return;
}
cout << "请输入修改职工的编号:" << endl;
int id;
cin >> id;
int ret = this->isExist(id);
if (ret == -1) {
cout << "修改失败,查无此人" << endl;
return;
}
// 找到编号的职工,先释放空间
delete this->empArr[ret];
int newId;
string name;
int deptId;
cout << "查到:" << id << "号职工,请输入新职工号:" << endl;
cin >> newId;
cout << "请输入新职工姓名:" << endl;
cin >> name;
cout << "请选择该职工的岗位:" << endl;
cout << "1、普通职工" << endl;
cout << "2、经理" << endl;
cout << "3、老板" << endl;
cin >> deptId;
Worker *worker = workerFactory(newId, name, deptId);
this->empArr[ret] = worker;
this->saveFile();
cout << "修改成功" << endl;
}
void WorkerManager::findEmp() {
if (this->empNum == 0) {
cout << "职工记录为空" << endl;
return;
}
cout << "请输入查找的方式:" << endl;
cout << "1、按职工编号查找" << endl;
cout << "2、按姓名查找" << endl;
int select = 0;
cin >> select;
if (select == 1) {
cout << "请输入查找的职工编号:" << endl;
int id;
cin >> id;
int ret = this->isExist(id);
if (ret != -1) {
this->empArr[ret]->showInfo();
} else {
cout << "查无此人" << endl;
}
} else if (select == 2) {
cout << "请输入查找的姓名:" << endl;
string name;
cin >> name;
bool found = false;
for (int i = 0; i < this->empNum; i++) {
if (this->empArr[i]->getName() == name) {
found = true;
this->empArr[i]->showInfo();
}
}
if (!found) {
cout << "查无此人" << endl;
}
} else {
cout << "输入选项有误" << endl;
}
}
void WorkerManager::sortEmp() {
if (this->empNum == 0) {
cout << "职工记录为空" << endl;
return;
}
cout << "请选择排序方式:" << endl;
cout << "1、按职工号进行升序" << endl;
cout << "2、按职工号进行降序" << endl;
int select = 0;
cin >> select;
// 选择排序
for (int i = 0; i < this->empNum - 1; i++) {
int minOrMax = i;
for (int j = i + 1; j < this->empNum; j++) {
if (select == 1) { // 升序
if (this->empArr[minOrMax]->getId() > this->empArr[j]->getId()) {
minOrMax = j;
}
} else { // 降序
if (this->empArr[minOrMax]->getId() < this->empArr[j]->getId()) {
minOrMax = j;
}
}
}
// 交换位置
if (minOrMax != i) {
Worker *tmp = this->empArr[i];
this->empArr[i] = this->empArr[minOrMax];
this->empArr[minOrMax] = tmp;
}
}
cout << "排序成功,排序后结果为:" << endl;
this->saveFile();
this->showEmpInfo();
}
void WorkerManager::cleanEmp() {
cout << "确认清空?" << endl;
cout << "1、确认" << endl;
cout << "2、返回" << endl;
int select = 0;
cin >> select;
if (select != 1) {
return;
}
// 打开模式 trunc 如果存在删除并重新创建文件
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
if (this->empArr != NULL) {
for (int i = 0; i < this->empNum; i++) {
if (this->empArr[i] != NULL) {
delete this->empArr[i];
}
}
this->empNum = 0;
delete[] this->empArr;
this->empArr = NULL;
}
cout << "清空成功!" << endl;
}
WorkerManager::~WorkerManager() {
if (this->empArr != NULL) {
for (int i = 0; i < this->empNum; ++i) {
if (this->empArr[i] != NULL) {
delete this->empArr[i];
}
}
delete[] this->empArr;
this->empArr = NULL;
}
}
worker.h
[C++] 纯文本查看 复制代码 #pragma once
#include <iostream>
#include <string>
using namespace std;
class Worker {
public:
// 显示个人信息
virtual void showInfo() = 0;
int getId() {
return id;
}
string getName() {
return name;
}
int getDeptId() {
return deptId;
}
// 获取岗位名称
string getDeptName() {
string deptName;
switch (deptId) {
case 1:
deptName = "普通职工";
break;
case 2:
deptName = "经理";
break;
case 3:
deptName = "老板";
break;
default:
break;
}
return deptName;
}
protected:
int id; // 职工编号
string name; // 职工姓名
int deptId; // 职工所在部门名称编号
};
boss.h
[C++] 纯文本查看 复制代码 #ifndef MAIN_BOSS_H
#define MAIN_BOSS_H
#include "worker.h"
class Boss : public Worker {
public:
Boss(int id, string name, int deptId);
void showInfo() override;
};
#endif // MAIN_BOSS_H
boss.cpp
[C++] 纯文本查看 复制代码 #include "boss.h"
Boss::Boss(int id, string name, int deptId) {
this->id = id;
this->name = name;
this->deptId = deptId;
}
void Boss::showInfo() {
cout << "职工编号:" << id
<< "\t职工姓名:" << name
<< "\t岗位:" << getDeptName()
<< "\t岗位职责:管理公司所有事务" << endl;
}
manager.h
[C++] 纯文本查看 复制代码 #ifndef MAIN_MANAGER_H
#define MAIN_MANAGER_H
#include "worker.h"
class Manager : public Worker {
public:
Manager(int id, string name, int deptId);
void showInfo() override;
};
#endif //MAIN_MANAGER_H
manager.cpp
[C++] 纯文本查看 复制代码 #include "manager.h"
Manager::Manager(int id, string name, int deptId) {
this->id = id;
this->name = name;
this->deptId = deptId;
}
void Manager::showInfo() {
cout << "职工编号:" << id
<< "\t职工姓名:" << name
<< "\t岗位:" << getDeptName()
<< "\t岗位职责:完成老板交给的任务,并下发任务给员工" << endl;
}
employee.h
[C++] 纯文本查看 复制代码 #ifndef MAIN_EMPLOYEE_H
#define MAIN_EMPLOYEE_H
#include "worker.h"
class Employee: public Worker{
public:
Employee(int id, string name, int deptId);
void showInfo() override;
};
#endif //MAIN_EMPLOYEE_H
employee.cpp
[C++] 纯文本查看 复制代码 #include "employee.h"
Employee::Employee(int id, string name, int deptId) {
// 给从基类继承过来的属性赋值,不能使用参数列表的方式 id(id)
this->id = id;
this->name = name;
this->deptId = deptId;
}
void Employee::showInfo() {
cout << "职工编号:" << id
<< "\t职工姓名:" << name
<< "\t岗位:" << getDeptName()
<< "\t岗位职责:完成经理交给的任务" << endl;
}
|