一、题目要求
说明:本题来自黑马程序员在b站的C++教程视频,由于代码较长,并且对于初学者比较复杂,在此不做讲解,直接放代码。如有需要可以去b站搜索黑马程序员观看视频教程,讲解还是很详细的。
题目:
某公司职工分为三类:普通员工、经理、老板。显示信息时需要显示职工编号、职工姓名、职工岗位、岗位职责等信息。
普通员工职责:完成经理交给的任务。
经理职责:完成老板交给的任务,并下发给员工。
老板职责:管理公司所有事务。
管理系统中需要实现的功能如下:
0.退出管理程序:鬼畜当前管理系统
1.增加职工信息:实现不批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号。
2.显示职工信息:显示公司内所有职工信息。
3.删除离职职工:按照编号删除指定的职工。
4.修改职工信息:按照编号删除指定的职工。
5.查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息。
6.按照编号排序:按照职工编号,进行排序,排序规则由用户指定。
7.清空所有文档:清空文件中记录的所有职工信息(请空前需要再次确认)。
二、代码
代码分为头文件和源文件两部分。
2.1头文件
2.1.1 boos.h
//老板类
#pragma once
#include<iostream>
using namespace std;
#include"worker.h"
class Boos :public Worker {
public:
Boos(int id, string name, int did); //构造函数
virtual void showInfo(); //显示个人信息
virtual string getDeptName(); //获取岗位名称
};
2.1.2 employee.h
//普通员工类
#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(); //获取岗位名称
};
2.1.3 manager.h
//经理类
#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(); //获取职工岗位
};
2.1.4 worker.h
#pragma once
#include<iostream>
using namespace std;
#include<string>
//职工抽象类
class Worker {
public:
virtual void showInfo() = 0; //显示个人信息
virtual string getDeptName() = 0; //获取岗位名称
int m_Id; //职工编号
string m_Name; //职工姓名
int m_DeptId; //部门编号
};
2.1.5 workerManager.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boos.h"
#include<fstream>
#define FILENAME "empFile.txt" //宏定义文件名
class WorkerManager {
public:
WorkerManager();
void Show_Menu(); //展示菜单
void ExitSystem(); //退出系统
int m_EmpNum; //记录职工人数
Worker** m_EmpArray; //职工数组指针,因为职工数组是worker*类型,所以要用worker**类型接收
bool m_FileIsEmpty; //标志文件是否为空
int get_EmpNum(); //统计文件中人数
void init_Emp(); //初始化员工
void Show_Emp(); //显示员工
void Del_Emp(); //删除职工
//判断职工是否存在,如果存在 返回职工所在数组中的位置,不存在返回-1
int IsExist(int id);
void Mod_Emp(); //修改职工
void Find_Emp(); //查找职工
void Sort_Emp(); //排序
void Clean_File(); //清空文件
void Add_Emp(); //添加职工
void save(); //保存文件
~WorkerManager();
};
2.2源文件
2.2.1 boos.cpp
#include"boos.h"
Boos::Boos(int id, string name, int did) { //构造函数,对自身内容做初始化操作
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = did;
}
void Boos::showInfo() { //显示个人信息
cout << "职工编号:" << this->m_Id
<< "\t\t姓名:" << this->m_Name
<< "\t\t岗位:" << this->getDeptName()
<< "\t\t职责:" << "管理公司所有事务" << endl;
}
string Boos::getDeptName() { //获取岗位名称
return string("老板");
}
2.2.2 employee.cpp
#include<iostream>
#include<string>
using namespace std;
#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\t姓名:" << this->m_Name
<< "\t\t岗位:" << this->getDeptName()
<< "\t\t岗位:" << "完成经理交给的任务" << endl;
}
string Employee::getDeptName() { //获取岗位名称
return string("普通员工");
}
2.2.3 manager.cpp
#include<iostream>
#include<string>
using namespace std;
#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\t姓名:" << this->m_Name
<< "\t\t岗位:" << this->getDeptName()
<< "\t\t职责:" << "完成老板交给的任务,并下发任务给员工" << endl;
}
string Manager::getDeptName() { //显示职工岗位
return string("经理");
}
2.2.4 workerManager.cpp
#include"workerManager.h"
//管理类。负责和用户沟通,记录的增删改查等功能
WorkerManager::WorkerManager() {
//1.文件不存在
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;
}
//2.文件存在但数据为空
char ch;
ifs >> ch;
if (ifs.eof()) { //如果读取为空
//cout << "文件为空!" << endl;
//初始化属性
this->m_EmpNum = 0; //初始化记录人数
this->m_EmpArray = NULL; //初始化数组指针
this->m_FileIsEmpty = true; //初始化文件是否为空(为空)
ifs.close();
return;
}
//3.文件存在并记录数据
int num = this->get_EmpNum();
//cout << "职工人数为:" << num << endl;
this->m_EmpNum = num;
//开辟空间
this->m_EmpArray = new Worker * [this->m_EmpNum];
//将文件中的数据存放到数组中
this->init_Emp();
}
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::ExitSystem() {
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0); //退出程序
}
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 dSelete; //部门选择
cout << "请输入第" << i + 1 << "个新职工的编号:" << endl;
cin >> id;
cout << "请输入第" << i + 1 << "个新职工的姓名:" << endl;
cin >> name;
cout << "请选择该职工的岗位:" << endl;
cout << "1.普通职工" << endl;
cout << "2.经理" << endl;
cout << "3.老板" << endl;
cin >> dSelete;
Worker* worker = NULL;
switch (dSelete) {
case 1:
worker = new Employee(id, name, 1);
break;
case 2:
worker = new Manager(id, name, 2);
break;
case 3:
worker = new Boos(id, name, 3);
break;
default:
break;
}
//将创建的职工指针,保存到数组中
newSpace[this->m_EmpNum + i] = worker; //添加的位置下标为 原来的职工人数再加上i
}
//释放原有空间
delete[] this->m_EmpArray;
//更改新空间的指向
this->m_EmpArray = newSpace;
//更新新的职工人数
this->m_EmpNum = newSize;
//文件不为空
this->m_FileIsEmpty = false;
//保存数据
this->save();
//提示成功添加
cout << "添加成功!" << endl;
}
else {
cout << "输入有误!" << endl;
}
system("pause");
system("cls");
}
//保存文件
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 << "\t"
<< this->m_EmpArray[i]->m_Name << "\t"
<< this->m_EmpArray[i]->m_DeptId << endl;
}
//关闭文件
ofs.close();
}
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加一
num++;
}
return num;
}
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;
if (did == 1) {
worker = new Employee(id, name, did);
}
else if (did == 2) {
worker = new Manager(id, name, did);
}
else if (did == 3) {
worker = new Boos(id, name, did);
}
this->m_EmpArray[index] = worker; //在m_EmpArray数组中存放数据
index++;
}
ifs.close();
}
void WorkerManager::Show_Emp() {
//判断文件是否为空
if (this->m_FileIsEmpty) {
cout << "文件不存在或记录为空!" << endl;
}
else {
for (int i = 0; i < this->m_EmpNum; i++) {
//利用多态调用程序接口
this->m_EmpArray[i]->showInfo();
}
}
//按任意键清屏
system("pause");
system("cls");
}
void WorkerManager::Del_Emp() { //删除职工
if (this->m_FileIsEmpty) {
cout << "文件为空或不存在!" << endl;
}
else {
//按照职工编号删除
cout << "请输入要删除的职工编号:" << endl;
int id = 0;
cin >> id;
int index = this->IsExist(id); //判断职工是否存在,如果存在 返回职工所在数组中的位置,不存在返回-1
if (index != -1) {
for (int i = 0; i < this->m_EmpNum; i++) {
this->m_EmpArray[i] = this->m_EmpArray[i + 1]; //数据前移
}
this->m_EmpNum--; //更新人数
this->save(); //同步更新
cout << "删除成功!" << endl;
}
else {
cout << "删除失败,未找到该职工!" << endl;
}
}
system("pause");
system("cls");
}
int WorkerManager::IsExist(int id) { //判断职工是否存在,如果存在 返回职工所在数组中的位置,不存在返回-1
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;
}
void WorkerManager::Mod_Emp() {
if (this->m_FileIsEmpty) {
cout << "文件为空或不存在!" << endl;
}
else {
cout << "请输入要查找的职工编号:" << endl;
int id = 0;
cin >> id;
int ret = this->IsExist(id);
if (ret != -1) {
//查找到编号的职工
delete this->m_EmpArray[ret];
int newId = 0;
string newName = "";
int newDid = 0;
int dSelete;
cout << "查到:" << id << "号职工,请输入新工号:" << endl;
cin >> newId;
cout << "请输入新姓名:" << endl;
cin >> newName;
cout << "请输入新岗位:" << endl;
cout << "1.普通职工" << endl;
cout << "2.经理" << endl;
cout << "3.老板" << endl;
cin >> dSelete;
Worker* worker = NULL;
switch (dSelete) {
case 1:
worker = new Employee(newId, newName, dSelete);
break;
case 2:
worker = new Manager(newId, newName, dSelete);
break;
case 3:
worker = new Boos(newId, newName, dSelete);
break;
default:
break;
}
//更新数据到数组中
this->m_EmpArray[ret] = worker;
cout << "修改成功!" << endl;
//保存到文件中
this->save();
}
else {
cout << "查无此人!" << endl;
}
}
system("pause");
system("cls");
}
void WorkerManager::Find_Emp() {
if (this->m_FileIsEmpty) {
cout << "文件为空或不存在!" << endl;
system("pause");
system("cls");
}
else {
cout << "请输入查找方法:" << endl;
cout << "1.按职工编号查找" << endl;
cout << "2.按职工姓名查找" << endl;
int selete = 0;
cin >> selete;
if (selete == 1) { //按职工编号查找
int id;
cout << "请输入要查找的职工编号:" << endl;
cin >> id;
int ret = IsExist(id);
if (ret != -1) {
//找到职工
cout << "查找成功!该职工信息如下:" << endl;
this->Show_Emp();
}
}
else if (selete == 2) { //按职工姓名查找
string name;
bool flag = false; //判断是否找到,默认未找到
cout << "请输入要查找的职工姓名:" << endl;
cin >> name;
for (int i = 0; i < this->m_EmpNum; i++) {
cout << "查找成功!该职工信息如下:" << endl;
this->m_EmpArray[i]->showInfo();
flag = true;
}
if (flag == false) {
cout << "查找失败,查无此人!" << endl;
}
}
else {
cout << "输入有误!" << endl;
}
system("pause");
system("cls");
}
}
void WorkerManager::Sort_Emp() {
if (this->m_FileIsEmpty) {
cout << "文件为空或不存在!" << endl;
system("pause");
system("cls");
}
else {
cout << "请选择排序方式:" << endl;
cout << "1.按职工编号升序排列" << endl;
cout << "2.按职工姓名降序排列" << endl;
int selete;
cin >> selete;
for (int i = 0; i < this->m_EmpNum; i++) {
int minOrMax = i; //声明最小值或最大值下标
for (int j = i + 1; j < this->m_EmpNum; j++) {
if (selete == 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(); //展示所有职工
}
void WorkerManager::Clean_File() { //清空文件
cout << "确定清空文件吗?(输入1确认清空)" << endl;
int selete;
cin >> selete;
if (selete == 1) {
//清空文件
ofstream ofs(FILENAME, ios::trunc); //删除文件后重新创建
ofs.close();
if (this->m_EmpArray != NULL) {
//删除堆区每个职工对象
for (int i = 0; i < this->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_FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
WorkerManager::~WorkerManager() {
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];
}
}
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
2.2.5 职工管理系统
#include<iostream>
#include<string>
#include"workerManager.h"
using namespace std;
int main() {
//实例化管理者对象
WorkerManager wm;
int choice = 0;
while (true) {
//调用展示菜单函数
wm.Show_Menu();
cout << "请输入您的选择:" << endl;
cin >> choice;
switch (choice) {
case 0: //退出系统
wm.ExitSystem();
break;
case 1: //增加职工
wm.Add_Emp();
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");
}