如何设计一个基于多态的职工管理系统来应对长尾词查询需求?

2026-04-12 00:352阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计8364个文字,预计阅读时间需要34分钟。

如何设计一个基于多态的职工管理系统来应对长尾词查询需求?

一、管理系统需求公司中员工分为三类:普通员工、经理、老板

显示信息时,需展示以下内容:

1.职工编号、姓名、岗位、职责

1. 普通员工职责:

完成经理交办的任务

2. 经理职责:

2.经理职责:

一、管理系统需求

公司中职工分为三类:普通员工、经理、老板

显示信息时,需要显示职工编号、职工姓名、职工岗位、以及职责

1、普通员工职责:完成经理交给的任务

2、经理职责:完成老板交给的任务,并下发任务给员工

3、老板职责:管理公司所有事务


管理系统中需要实现的功能如下:

1、退出管理程序:退出当前管理系统

2、增加职工信息:实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号

3、显示职工信息:显示公司内部所有职工的信息

4、删除离职职工:按照编号删除指定的职工

5、修改职工信息:按照编号修改职工个人信息

6、查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息

7、按照编号排序:按照职工编号,进行排序,排序规则由用户指定

8、清空所有文档:清空文件中记录的所有职工信息 (清空前需要再次确认,防止误删)

如何设计一个基于多态的职工管理系统来应对长尾词查询需求?


界面效果图如下:

二、创建项目

以vs2022为例

项目创建完成


三、创建管理类

管理类内容:

1、与用户交流页面

2、对职工进行增删改查

3、与文件的读写交互


1、创建文件

创建两个文件:

源文件:workerManager.cpp

头文件:workerManager.h

2、头文件实现

#pragma once #include<iostream> using namespace std; class workerManager { public: workerManager(); ~workerManager(); };


3、源文件实现

#include "workerManager.h" workerManager::workerManager() { } workerManager::~workerManager() { }

四、菜单功能

1、添加成员函数

2、菜单功能实现

3、测试

在主函数中,常见对象,调用成员方法

五、退出功能

1、提供功能结构

在main函数中提供分支选择,提供每个函数的接口

int main() { workerManager wm; int chioce; while (true) { wm.show_Menu(); cout << "请做出选择(数字):" << endl; cin >> chioce; switch (chioce){ case 0 : //退出功能 wm.exitSystem(); 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; } }


2、编写退出功能

六、创建职工类

1、创建职工抽象类

职工分为:老板,普通员工,经理

在头文件文件夹下,创建woker.h头文件,编写如下代码:

#pragma once #include<iostream> using namespace std; class Woker { public: //方法 //显示信息 virtual void showInfo() = 0; virtual string getDeptName() = 0; //属性 //员工编号 int w_id; //姓名 string w_name; //部门编号 int w_deptId; };

2、创建普通员工抽象类

创建员工employee.h头文件和employee.cpp源文件

employee.h代码:

#pragma once #include<iostream> using namespace std; #include"woker.h" class Employee :public Woker{ public: //构造函数 Employee(int id,string name,int dId); //显示信息 void showInfo(); //获取职位名称 string getDeptName(); };


employee.cpp代码:

#define _CRT_SECURE_NO_WARNINGS 1 #include"employee.h" //构造函数 Employee::Employee(int id, string name, int dId) { this->w_id = id; this->w_name = name; this->w_deptId = dId; } //显示信息 void Employee::showInfo() { cout << "职工编号:" << this->w_id << "\t职工姓名:" << this->w_name << "\t岗位:" << this->getDeptName() << "\t岗位职能:完成经理的任务!" << endl; } //获取职位名称 string Employee::getDeptName() { return string("员工"); }

3、创建经理抽象类

创建经理manager.h头文件和manager.cpp源文件

manager.h代码:

#pragma once #include<iostream> using namespace std; #include"woker.h" //经理类 class Manager :public Woker { public: //构造函数 Manager(int id, string name, int dId); //显示信息 void showInfo(); //获取职位名称 string getDeptName(); };

manager.cpp代码:

#define _CRT_SECURE_NO_WARNINGS 1 #include"manager.h" //构造函数 Manager::Manager(int id, string name, int dId) { this->w_id = id; this->w_name = name; this->w_deptId = dId; } //显示信息 void Manager::showInfo() { cout << "职工编号:" << this->w_id << "\t职工姓名:" << this->w_name << "\t岗位:" << this->getDeptName() << "\t岗位职能:完成老板的任务,并下发任务给员工!" << endl; } //获取职位名称 string Manager::getDeptName() { return string("经理"); }

4、创建老板抽象类

创建老板boss.h头文件和boss.cpp源文件

boss.h代码:

#pragma once #include<iostream> using namespace std; #include"woker.h" //经理类 class Boss :public Woker { public: //构造函数 Boss(int id, string name, int dId); //显示信息 void showInfo(); //获取职位名称 string getDeptName(); };

boss.cpp代码:

#define _CRT_SECURE_NO_WARNINGS 1 #include"boss.h" //构造函数 Boss::Boss(int id, string name, int dId) { this->w_id = id; this->w_name = name; this->w_deptId = dId; } //显示信息 void Boss::showInfo() { cout << "职工编号:" << this->w_id << "\t职工姓名:" << this->w_name << "\t岗位:" << this->getDeptName() << "\t岗位职能:管理公司所有事务!" << endl; } //获取职位名称 string Boss::getDeptName() { return string("总裁"); }

5、测试多态

正常显示!

七、添加职工

1、功能分析

功能描述:批量添加员工,并保存到文件中

如果想将所有不同种类的员工都放入到一个数组中,可以将所有员工的指针维护到一个数组里

如果想在程序中维护这个不定长度的数组,可以将数组创建到堆区,并利用Worker **的指针维护

2、功能实现

在wokerManager.h头文件中添加成员属性 :

//员工数量 int m_EmpNum; //员工数组指针 Woker** m_EmpArray;


在wokerManager.cpp源文件构造函数中,初始化属性:

workerManager::workerManager() { //初始化人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmpArray = NULL; }

实现add_Emp()函数

void workerManager::add_Emp() { cout << "请输入新添加职工的数量(整数):" << endl; int add_num; cin >> add_num; if (add_num > 0) { //1、计算新空间大小 int newSize = this->m_EmpNum + add_num; //2、开辟新的空间 Woker** newSpace = new Woker* [newSize]; //3、将原空间的内容放入新空间 if (this->m_EmpArray != NULL) { for (int j =0 ; j < this->m_EmpNum; j++) { newSpace[j] = this->m_EmpArray[j]; } } //4、输入新的数据 for (int i = 0; i < add_num; i++) { int id; string name; int dselete; cout << "请输入第" << i + 1 << "个新职员的编号:" << endl; cin >> id; cout << "请输入第" << i + 1 << "个新职员的姓名:" << endl; cin >> name; cout << "请输入第" << i + 1 << "个新职员的职位(数字):" << endl; cout << "1、员工" << endl; cout << "2、经理" << endl; cout << "3、总裁" << endl; cin >> dselete; if (dselete == 1 || dselete == 2 || dselete == 3) { Woker* woker = NULL; switch (dselete) { case 1: woker = new Employee(id, name, 1); break; case 2: woker = new Manager(id, name, 2); break; case 3: woker = new Boss(id, name, 3); break; } newSpace[this->m_EmpNum + i + 1] = woker; } else { cout << "选择错误!录入失败" << endl; cin >> dselete; } //5、释放原来的空间 delete[] this->m_EmpArray; //6、更改指针指向 this->m_EmpArray = newSpace; //7、更新员工个数 this->m_EmpNum = newSize; //8、提示信息 cout << "添加成功!" << endl; } } else { cout << "输入有误!" << endl; } system("pause"); system("cls"); }

在WorkerManager.cpp的析构函数中,释放堆区数据

workerManager::~workerManager() { if (this->m_EmpArray!=NULL) { delete[] this->m_EmpArray; } }

3、测试添加

八、文件交互-写文件

功能描述:对文件进行读写

1、设置文件路径

2、保存文件函数声明

3、保存文件函数实现

//保存文件 void workerManager::save() { //2、创建文件流对象 ofstream ofs; //3、打开文件 ofs.open(FILENAME, ios::out); //4、写入文件 for (int i = 0; i < this->m_EmpNum; i++) { ofs << this->m_EmpArray[i]->w_id << " " << this->m_EmpArray[i]->w_name << " " << this->m_EmpArray[i]->w_deptId << endl; } //5、关闭文件 ofs.close(); }

4、测试

显示成功!


九、文件交互-读文件

功能描述:将文件中的内容读取到程序

因此构造函数初始化数据的情况分为三种:

1、第一次使用,文件未创建

2、文件存在,但是数据被用户清空

3、文件存在,并且保存职工的所有数据


1、文件未创建

workerManager.h中添加属性 m_FileIsEmpty,标志文件是否为空

修改workerManager.cpp中构造函数代码

workerManager::workerManager() { ifstream ifs; ifs.open(FILENAME,ios::in); //打开文件 //1、文件不存在 if (!ifs.is_open()) { cout << "文件不存在!" << endl; //初始化人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmpArray = NULL; //置为空标志为true this->m_FileIsEmpty = true; ifs.close(); return; } }

2、文件存在,数据为空

更新workerManager.cpp中构造函数代码

workerManager::workerManager() { ifstream ifs; ifs.open(FILENAME,ios::in); //打开文件 //1、文件不存在 if (!ifs.is_open()) { cout << "文件不存在!" << endl; //初始化人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmpArray = NULL; //置为空标志为true 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; //置为空标志为true this->m_FileIsEmpty = true; ifs.close(); return; } }

3、文件存在且保存职工数据

1、获取记录的职工人数

在workerManager.h中添加成员函数 int get_EmpNum();

在workerManager.cpp中实现get_EmpNum()方法

//获取职工数量 int workerManager::get_EmpNum() { ifstream ifs; ifs.open(FILENAME,ios::in); int num = 0; int id; string name; int did; while(ifs>>id && ifs>>name && ifs>>did) { num++; } return num; }


更新workerManager.cpp中构造函数代码

workerManager::workerManager() { ifstream ifs; ifs.open(FILENAME,ios::in); //打开文件 //1、文件不存在 if (!ifs.is_open()) { cout << "文件不存在!" << endl; //初始化人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmpArray = NULL; //置为空标志为true 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; //置为空标志为true this->m_FileIsEmpty = true; ifs.close(); return; } //3、文件存在,保存数据 int num1 = get_EmpNum(); this->m_EmpNum = num1; cout << "职工数量为:" << this->m_EmpNum << endl; }

2、初始化数组

在workerManager.h中添加成员函数 init_Emp();

在workerManager.cpp中实现init_Emp()方法

//初始化职工数组 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) { Woker* woker = NULL; if (did == 1) { //员工 woker = new Employee(id, name, did); } else if (did == 2) {//经理 woker = new Manager(id, name, did); } else { woker = new Boss(id, name, did); } this->m_EmpArray[index] = woker; index++; } ifs.close(); }


更新workerManager.cpp中构造函数代码

workerManager::workerManager() { ifstream ifs; ifs.open(FILENAME,ios::in); //打开文件 //1、文件不存在 if (!ifs.is_open()) { cout << "文件不存在!" << endl; //初始化人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmpArray = NULL; //置为空标志为true 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; //置为空标志为true this->m_FileIsEmpty = true; ifs.close(); return; } //3、文件存在,保存数据 int num1 = get_EmpNum(); this->m_EmpNum = num1; cout << "职工数量为:" << this->m_EmpNum << endl; //创建堆区数据 this->m_EmpArray = new Woker * [this->m_EmpNum]; this->init_Emp(); for (int i = 0; i < this->m_EmpNum;i++) { cout << "职工编号:" << this->m_EmpArray[i]->w_id << " 姓名:" << this->m_EmpArray[i]->w_name << " 岗位编号:" << this->m_EmpArray[i]->w_deptId << endl; } }


1、不存在

2、存在但为空

3、存在且保存

十、显示职工

1、显示职工函数声明

2、函数实现

在workerManager.cpp中实现显示函数

//显示职工信息 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"); }

十一、删除职工

功能描述:按照职工的编号进行删除职工操作

1、删除职工函数声明

2、判断职工是否存在函数声明

3、判断职工是否存在函数实现

在workerManager.cpp中实现显示函数

//判断职工是否存在 //存在返回所占索引,不存在返回-1 int workerManager::isExist(int id) { int index = -1; for (int i = 0; i < this->m_EmpNum;i++) { if (i == id) { //职工存在 index = i; break; } } return index; }

4、删除职工函数实现

//删除职工 void workerManager::del_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者数据为空" << endl; } else { int id = 0; cout << "请输入你想删除的职工编号:" << endl; cin >> id; int index = this->isExist(id); if (index != -1) { for (int i = index; i < this->m_EmpNum - 1; i++) { this->m_EmpArray[i] = this->m_EmpArray[i + 1]; } this->m_EmpNum -= 1; this->save();//保存文件 cout << "删除成功!" << endl; } else cout << "该职工不存在" << endl; } system("pause"); system("cls"); }

5、测试

十二、修改职工

功能描述:能够按照职工编号对职工信息进行修改和保存

1、修改职工函数声明

2、修改职工函数实现

//修改职工 void workerManager::mod_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者数据为空!" << endl; } else{ cout << "请输入你想修改的职工编号:" << endl; int id; cin >> id; int ret = this->isExist(id); if (ret != -1) { int newId = 0; string newName = ""; int newDid = 0; cout << "查找到编号为:" << id << "的职工" << endl; cout << "请输入新的职工编号:" << endl; cin >> newId; cout << "请输入新的职工姓名:" << endl; cin >> newName; cout << "请输入新的职工岗位编号:" << endl; cout << "1、员工" << endl; cout << "2、经理" << endl; cout << "3、总裁" << endl; cin >> newDid; Woker* woker = NULL; switch(newDid) { case 1: woker = new Employee(newId, newName, 1); break; case 2: woker = new Manager(newId, newName, 2); break; case 3: woker = new Boss(newId, newName, 3); break; } this->m_EmpArray[ret] = woker; cout << "修改成功!" << endl; this->save(); } else { cout << "修改失败,该员工不存在" << endl; } this->save(); } system("pause"); system("cls"); }


十三、查找职工

功能描述:提供两种查找职工方式,

1、按照职工编号

2、按照职工姓名

1、查找职工函数声明

2、查找职工函数实现

//查找职工 void workerManager::find_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者数据为空!" << endl; } else { cout << "请输入查找的方式:" << endl; cout << "1、按职工编号查找" << endl; cout << "2、按姓名查找" << endl; int selete = 0; cin >> selete; switch (selete) { case 1: { int id; cout << "请输入查找的职工编号:" << endl; cin >> id; int ret = this->isExist(id); if (ret != -1) { cout << "查找成功!该职工信息如下:" << endl; this->m_EmpArray[ret]->showInfo(); } else cout << "查找失败,查无此人" << endl; break; } case 2: { string name; cout << "请输入查找的职工姓名:" << endl; cin >> name; bool flag = false; for (int i = 0; i < this->m_EmpNum;i++) { if (this->m_EmpArray[i]->w_name == name) { cout << "查找成功!该职工信息如下:" << endl; this->m_EmpArray[i]->showInfo(); flag = true; } } if (!flag) { cout << "查找失败,查无此人" << endl; } break; } default: cout << "输入错误!查找失败" << endl; break; } } system("pause"); system("cls"); }

十四、排序

功能描述:按照职工编号进行排序,排序的顺序由用户指定

1、升序:冒泡排序法

2、降序:快速排序法


1、排序函数声明

2、排序函数实现

在workerManager.cpp中实现成员函数 void sort_Emp();

//排序 void workerManager::sort_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者数据为空!" << endl; } else{ cout << "请输入你选择的排序方式:" << endl; cout << "1、按职工编号升序排列" << endl; cout << "2、按职工编号降序排列" << endl; int selete = 0; cin >> selete; switch (selete) { case 1: { //冒泡排序 for (int i = 0; i < this->m_EmpNum;i++) { for (int j = this->m_EmpNum - 1; j >= i;j--) { if (this->m_EmpArray[i]->w_id > this->m_EmpArray[j]->w_id) { Woker* temp = m_EmpArray[i]; m_EmpArray[i] = m_EmpArray[j]; m_EmpArray[j] = temp; } } } cout << "冒泡排序升序成功!" << endl; break; } case 2: { //快速排序 for (int i = 0; i < this->m_EmpNum; i++) { int min = i; for (int j = i + 1; j < this->m_EmpNum;j++) { if (this->m_EmpArray[min]->w_id < this->m_EmpArray[j]->w_id) { min = j; } } if ( i != min) { Woker* temp = m_EmpArray[i]; m_EmpArray[i] = m_EmpArray[min]; m_EmpArray[min] = temp; } } cout << "快速排序降序完成!" << endl; break; } default: cout << "排序方式输入有误!" << endl; } this->save(); cout << "排序成功,排序后结果为:" << endl; this->show_Emp(); } }


十五、清空文件

功能描述:将文件中的记录数据清空

1、清空函数声明

2、清空函数实现

//清空 void workerManager::clean_Emp() { cout << "确定清空?" << endl; cout << "1、确定" << endl; cout << "2、退出" << endl; int select = 0; cin >> select; switch (select) { case 1: { //打开模式 ios::trunc 如果存在删除文件并重新创建 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; this->m_EmpNum = 0; this->m_FileIsEmpty = true; } cout << "清空完成!" << endl; system("pause"); system("cls"); break; } case 2: cout << "已退出,文件未清空!" << endl; system("pause"); system("cls"); break; default: cout << "选择有误!" << endl; system("pause"); system("cls"); break; } }

至此,职工管理系统所有功能完成!


附源码:

1、目录结构:

2、头文件

1、boss.h

#pragma once #include<iostream> using namespace std; #include"woker.h" //经理类 class Boss :public Woker { public: //构造函数 Boss(int id, string name, int dId); //显示信息 void showInfo(); //获取职位名称 string getDeptName(); };


2、employee.h

#pragma once #include<iostream> using namespace std; #include"woker.h" class Employee :public Woker{ public: //构造函数 Employee(int id,string name, int dId); //显示信息 void showInfo(); //获取职位名称 string getDeptName(); };


3、manager.h

#pragma once #include<iostream> using namespace std; #include"woker.h" //经理类 class Manager :public Woker { public: //构造函数 Manager(int id, string name, int dId); //显示信息 void showInfo(); //获取职位名称 string getDeptName(); };


4、woker.h

#pragma once #include<iostream> using namespace std; class Woker { public: //方法 //显示信息 virtual void showInfo() = 0; //获取职位名称 virtual string getDeptName() = 0; //属性 //员工编号 int w_id; //姓名 string w_name; //部门编号 int w_deptId; };


5、wokerManager.h

#pragma once #include<iostream> using namespace std; #include"woker.h" class workerManager { public: workerManager(); ~workerManager(); //展示菜单 void show_Menu(); //退出功能 void exitSystem(); //添加职工 void add_Emp(); //保存文件 void save(); //获取职工数量 int get_EmpNum(); //初始化职工数组 void init_Emp(); //显示职工信息 void show_Emp(); //删除职工 void del_Emp(); //修改职工 void mod_Emp(); //查找职工 void find_Emp(); //排序 void sort_Emp(); //清空 void clean_Emp(); //判断职工是否存在 //存在返回所占索引,不存在返回-1 int isExist(int id); //属性 //员工数量 int m_EmpNum; //员工数组指针 Woker** m_EmpArray; //文件是否为空 bool m_FileIsEmpty; };


3、源文件

1、boss.cpp

#define _CRT_SECURE_NO_WARNINGS 1 #include"boss.h" //构造函数 Boss::Boss(int id, string name, int dId) { this->w_id = id; this->w_name = name; this->w_deptId = dId; } //显示信息 void Boss::showInfo() { cout << "职工编号:" << this->w_id << "\t职工姓名:" << this->w_name << "\t岗位:" << this->getDeptName() << "\t岗位职能:管理公司所有事务!" << endl; } //获取职位名称 string Boss::getDeptName() { return string("总裁"); }


2、employee.cpp

#define _CRT_SECURE_NO_WARNINGS 1 #include"employee.h" //构造函数 Employee::Employee(int id, string name,int dId) { this->w_id = id; this->w_name = name; this->w_deptId = dId; } //显示信息 void Employee::showInfo() { cout << "职工编号:" << this->w_id << "\t职工姓名:" << this->w_name << "\t岗位:" << this->getDeptName() << "\t岗位职能:完成经理的任务!" << endl; } //获取职位名称 string Employee::getDeptName() { return string("员工"); }


3、main.cpp

#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; //自定义头文件 #include"workerManager.h" #include"woker.h" #include"employee.h" #include"manager.h" #include"boss.h" int main() { workerManager wm; int chioce; while (true) { wm.show_Menu(); cout << "请做出选择(数字):" << endl; cin >> chioce; switch (chioce){ 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_Emp(); break; default: system("cls"); break; } } system("pause"); return 0; }


4、manager.cpp

#define _CRT_SECURE_NO_WARNINGS 1 #include"manager.h" //构造函数 Manager::Manager(int id, string name, int dId) { this->w_id = id; this->w_name = name; this->w_deptId = dId; } //显示信息 void Manager::showInfo() { cout << "职工编号:" << this->w_id << "\t职工姓名:" << this->w_name << "\t岗位:" << this->getDeptName() << "\t岗位职能:上传下达!" << endl; } //获取职位名称 string Manager::getDeptName() { return string("经理"); }


5、wokerManager.cpp

#define _CRT_SECURE_NO_WARNINGS 1 #include"workerManager.h" #include"boss.h" #include"employee.h" #include"manager.h" #include<fstream> #define FILENAME "empInfo.txt" workerManager::workerManager() { ifstream ifs; ifs.open(FILENAME,ios::in); //打开文件 //1、文件不存在 if (!ifs.is_open()) { cout << "文件不存在!" << endl; //初始化人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmpArray = NULL; //置为空标志为true 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; //置为空标志为true this->m_FileIsEmpty = true; ifs.close(); return; } //3、文件存在,保存数据 int num1 = get_EmpNum(); this->m_EmpNum = num1; cout << "职工数量为:" << this->m_EmpNum << endl; //创建堆区数据 this->m_EmpArray = new Woker * [this->m_EmpNum]; this->init_Emp(); /*for (int i = 0; i < this->m_EmpNum;i++) { cout << "职工编号:" << this->m_EmpArray[i]->w_id << " 姓名:" << this->m_EmpArray[i]->w_name << " 岗位编号:" << this->m_EmpArray[i]->w_deptId << endl; }*/ } workerManager::~workerManager() { if (this->m_EmpArray != NULL) { for (int i = 0; i < this->m_EmpNum; i++) { delete this->m_EmpArray[i]; this->m_EmpArray[i] = NULL; } } } //菜单 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::save() { //2、创建文件流对象 ofstream ofs; //3、打开文件 ofs.open(FILENAME,ios::out); //4、写入文件 for (int i = 0; i < this->m_EmpNum; i++) { ofs << this->m_EmpArray[i]->w_id << " " << this->m_EmpArray[i]->w_name << " " << this->m_EmpArray[i]->w_deptId << endl; } //5、关闭文件 ofs.close(); } //获取职工数量 int workerManager::get_EmpNum() { ifstream ifs; ifs.open(FILENAME,ios::in); int num = 0; int id; string name; int did; while(ifs>>id && ifs>>name && ifs>>did) { 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) { Woker* woker = NULL; if (did == 1) { //员工 woker = new Employee(id, name, did); } else if (did == 2) {//经理 woker = new Manager(id, name, did); } else { woker = new Boss(id, name, did); } this->m_EmpArray[index] = woker; 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 { int id = 0; cout << "请输入你想删除的职工编号:" << endl; cin >> id; int index = this->isExist(id); if (index != -1) { for (int i = index; i < this->m_EmpNum - 1; i++) { this->m_EmpArray[i] = this->m_EmpArray[i + 1]; } this->m_EmpNum -= 1; this->save();//保存文件 cout << "删除成功!" << endl; } else cout << "该职工不存在" << endl; } system("pause"); system("cls"); } //判断职工是否存在 //存在返回所占索引,不存在返回-1 int workerManager::isExist(int id) { int index = -1; for (int i = 0; i < this->m_EmpNum;i++) { if (this->m_EmpArray[i]->w_id == id) { //职工存在 index = i; break; } } return index; } //修改职工 void workerManager::mod_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者数据为空!" << endl; } else{ cout << "请输入你想修改的职工编号:" << endl; int id; cin >> id; int ret = this->isExist(id); if (ret != -1) { int newId = 0; string newName = ""; int newDid = 0; cout << "查找到编号为:" << id << "的职工" << endl; cout << "请输入新的职工编号:" << endl; cin >> newId; cout << "请输入新的职工姓名:" << endl; cin >> newName; cout << "请输入新的职工岗位编号:" << endl; cout << "1、员工" << endl; cout << "2、经理" << endl; cout << "3、总裁" << endl; cin >> newDid; Woker* woker = NULL; switch(newDid) { case 1: woker = new Employee(newId, newName, 1); break; case 2: woker = new Manager(newId, newName, 2); break; case 3: woker = new Boss(newId, newName, 3); break; } this->m_EmpArray[ret] = woker; cout << "修改成功!" << endl; this->save(); } else { cout << "修改失败,该员工不存在" << endl; } this->save(); } system("pause"); system("cls"); } //查找职工 void workerManager::find_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者数据为空!" << endl; } else { cout << "请输入查找的方式:" << endl; cout << "1、按职工编号查找" << endl; cout << "2、按姓名查找" << endl; int selete = 0; cin >> selete; switch (selete) { case 1: { int id; cout << "请输入查找的职工编号:" << endl; cin >> id; int ret = this->isExist(id); if (ret != -1) { cout << "查找成功!该职工信息如下:" << endl; this->m_EmpArray[ret]->showInfo(); } else cout << "查找失败,查无此人" << endl; break; } case 2: { string name; cout << "请输入查找的职工姓名:" << endl; cin >> name; bool flag = false; for (int i = 0; i < this->m_EmpNum;i++) { if (this->m_EmpArray[i]->w_name == name) { cout << "查找成功!该职工信息如下:" << endl; this->m_EmpArray[i]->showInfo(); flag = true; } } if (!flag) { cout << "查找失败,查无此人" << endl; } break; } default: cout << "输入错误!查找失败" << endl; break; } } 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 = 0; cin >> selete; switch (selete) { case 1: { //冒泡排序 for (int i = 0; i < this->m_EmpNum;i++) { for (int j = this->m_EmpNum - 1; j >= i;j--) { if (this->m_EmpArray[i]->w_id > this->m_EmpArray[j]->w_id) { Woker* temp = m_EmpArray[i]; m_EmpArray[i] = m_EmpArray[j]; m_EmpArray[j] = temp; } } } cout << "冒泡排序升序成功!" << endl; break; } case 2: { //快速排序 for (int i = 0; i < this->m_EmpNum; i++) { int min = i; for (int j = i + 1; j < this->m_EmpNum;j++) { if (this->m_EmpArray[min]->w_id < this->m_EmpArray[j]->w_id) { min = j; } } if ( i != min) { Woker* temp = m_EmpArray[i]; m_EmpArray[i] = m_EmpArray[min]; m_EmpArray[min] = temp; } } cout << "快速排序降序完成!" << endl; break; } default: cout << "排序方式输入有误!" << endl; } this->save(); cout << "排序成功,排序后结果为:" << endl; this->show_Emp(); } } //清空 void workerManager::clean_Emp() { cout << "确定清空?" << endl; cout << "1、确定" << endl; cout << "2、退出" << endl; int select = 0; cin >> select; switch (select) { case 1: { //打开模式 ios::trunc 如果存在删除文件并重新创建 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; this->m_EmpNum = 0; this->m_FileIsEmpty = true; } cout << "清空完成!" << endl; system("pause"); system("cls"); break; } case 2: cout << "已退出,文件未清空!" << endl; system("pause"); system("cls"); break; default: cout << "选择有误!" << endl; system("pause"); system("cls"); break; } } //添加 void workerManager::add_Emp() { cout << "请输入新添加职工的数量(整数):" << endl; int add_num = 0; cin >> add_num; if (add_num > 0) { //1、计算新空间大小 int newSize = this->m_EmpNum + add_num; //2、开辟新的空间 Woker** newSpace = new Woker* [newSize+4]; //3、将原空间的内容放入新空间 if (this->m_EmpArray != NULL) { for (int i =0 ; i < this->m_EmpNum; i++) { newSpace[i] = this->m_EmpArray[i]; } } //4、输入新的数据 for (int i = 0; i < add_num; i++) { int id; string name; int dselete; cout << "请输入第" << i + 1 << "个新职员的编号:" << endl; cin >> id; int j = 0; while (j < this->m_EmpNum) { if (m_EmpArray != NULL) { if (id == this->m_EmpArray[j]->w_id) { cout << "输入错误,请重新输入:" << endl; cin >> id; j = 0; } else { j++; } } } cout << "请输入第" << i + 1 << "个新职员的姓名:" << endl; cin >> name; cout << "请输入第" << i + 1 << "个新职员的岗位(数字):" << endl; cout << "1、员工" << endl; cout << "2、经理" << endl; cout << "3、总裁" << endl; cin >> dselete; if (dselete == 1 || dselete == 2 || dselete == 3) { Woker* woker = NULL; switch (dselete) { case 1: woker = new Employee(id, name, 1); break; case 2: woker = new Manager(id, name, 2); break; case 3: woker = new Boss(id, name, 3); break; } newSpace[this->m_EmpNum + i] = woker; } else { cout << "选择错误!录入失败,请重新输入:" << endl; cin >> dselete; } } //5、释放原来的空间 delete[] this->m_EmpArray; //6、更改指针指向 this->m_EmpArray = newSpace; //7、更新员工个数 this->m_EmpNum = newSize; //置文件不为空 this->m_FileIsEmpty = false; //8、提示信息 cout << "成功添加"<<add_num<<"名职员" << endl; //保存文件 this->save(); } else { cout << "输入有误!" << endl; } system("pause"); system("cls"); }

喜欢就点赞收藏吧~

本文共计8364个文字,预计阅读时间需要34分钟。

如何设计一个基于多态的职工管理系统来应对长尾词查询需求?

一、管理系统需求公司中员工分为三类:普通员工、经理、老板

显示信息时,需展示以下内容:

1.职工编号、姓名、岗位、职责

1. 普通员工职责:

完成经理交办的任务

2. 经理职责:

2.经理职责:

一、管理系统需求

公司中职工分为三类:普通员工、经理、老板

显示信息时,需要显示职工编号、职工姓名、职工岗位、以及职责

1、普通员工职责:完成经理交给的任务

2、经理职责:完成老板交给的任务,并下发任务给员工

3、老板职责:管理公司所有事务


管理系统中需要实现的功能如下:

1、退出管理程序:退出当前管理系统

2、增加职工信息:实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号

3、显示职工信息:显示公司内部所有职工的信息

4、删除离职职工:按照编号删除指定的职工

5、修改职工信息:按照编号修改职工个人信息

6、查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息

7、按照编号排序:按照职工编号,进行排序,排序规则由用户指定

8、清空所有文档:清空文件中记录的所有职工信息 (清空前需要再次确认,防止误删)

如何设计一个基于多态的职工管理系统来应对长尾词查询需求?


界面效果图如下:

二、创建项目

以vs2022为例

项目创建完成


三、创建管理类

管理类内容:

1、与用户交流页面

2、对职工进行增删改查

3、与文件的读写交互


1、创建文件

创建两个文件:

源文件:workerManager.cpp

头文件:workerManager.h

2、头文件实现

#pragma once #include<iostream> using namespace std; class workerManager { public: workerManager(); ~workerManager(); };


3、源文件实现

#include "workerManager.h" workerManager::workerManager() { } workerManager::~workerManager() { }

四、菜单功能

1、添加成员函数

2、菜单功能实现

3、测试

在主函数中,常见对象,调用成员方法

五、退出功能

1、提供功能结构

在main函数中提供分支选择,提供每个函数的接口

int main() { workerManager wm; int chioce; while (true) { wm.show_Menu(); cout << "请做出选择(数字):" << endl; cin >> chioce; switch (chioce){ case 0 : //退出功能 wm.exitSystem(); 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; } }


2、编写退出功能

六、创建职工类

1、创建职工抽象类

职工分为:老板,普通员工,经理

在头文件文件夹下,创建woker.h头文件,编写如下代码:

#pragma once #include<iostream> using namespace std; class Woker { public: //方法 //显示信息 virtual void showInfo() = 0; virtual string getDeptName() = 0; //属性 //员工编号 int w_id; //姓名 string w_name; //部门编号 int w_deptId; };

2、创建普通员工抽象类

创建员工employee.h头文件和employee.cpp源文件

employee.h代码:

#pragma once #include<iostream> using namespace std; #include"woker.h" class Employee :public Woker{ public: //构造函数 Employee(int id,string name,int dId); //显示信息 void showInfo(); //获取职位名称 string getDeptName(); };


employee.cpp代码:

#define _CRT_SECURE_NO_WARNINGS 1 #include"employee.h" //构造函数 Employee::Employee(int id, string name, int dId) { this->w_id = id; this->w_name = name; this->w_deptId = dId; } //显示信息 void Employee::showInfo() { cout << "职工编号:" << this->w_id << "\t职工姓名:" << this->w_name << "\t岗位:" << this->getDeptName() << "\t岗位职能:完成经理的任务!" << endl; } //获取职位名称 string Employee::getDeptName() { return string("员工"); }

3、创建经理抽象类

创建经理manager.h头文件和manager.cpp源文件

manager.h代码:

#pragma once #include<iostream> using namespace std; #include"woker.h" //经理类 class Manager :public Woker { public: //构造函数 Manager(int id, string name, int dId); //显示信息 void showInfo(); //获取职位名称 string getDeptName(); };

manager.cpp代码:

#define _CRT_SECURE_NO_WARNINGS 1 #include"manager.h" //构造函数 Manager::Manager(int id, string name, int dId) { this->w_id = id; this->w_name = name; this->w_deptId = dId; } //显示信息 void Manager::showInfo() { cout << "职工编号:" << this->w_id << "\t职工姓名:" << this->w_name << "\t岗位:" << this->getDeptName() << "\t岗位职能:完成老板的任务,并下发任务给员工!" << endl; } //获取职位名称 string Manager::getDeptName() { return string("经理"); }

4、创建老板抽象类

创建老板boss.h头文件和boss.cpp源文件

boss.h代码:

#pragma once #include<iostream> using namespace std; #include"woker.h" //经理类 class Boss :public Woker { public: //构造函数 Boss(int id, string name, int dId); //显示信息 void showInfo(); //获取职位名称 string getDeptName(); };

boss.cpp代码:

#define _CRT_SECURE_NO_WARNINGS 1 #include"boss.h" //构造函数 Boss::Boss(int id, string name, int dId) { this->w_id = id; this->w_name = name; this->w_deptId = dId; } //显示信息 void Boss::showInfo() { cout << "职工编号:" << this->w_id << "\t职工姓名:" << this->w_name << "\t岗位:" << this->getDeptName() << "\t岗位职能:管理公司所有事务!" << endl; } //获取职位名称 string Boss::getDeptName() { return string("总裁"); }

5、测试多态

正常显示!

七、添加职工

1、功能分析

功能描述:批量添加员工,并保存到文件中

如果想将所有不同种类的员工都放入到一个数组中,可以将所有员工的指针维护到一个数组里

如果想在程序中维护这个不定长度的数组,可以将数组创建到堆区,并利用Worker **的指针维护

2、功能实现

在wokerManager.h头文件中添加成员属性 :

//员工数量 int m_EmpNum; //员工数组指针 Woker** m_EmpArray;


在wokerManager.cpp源文件构造函数中,初始化属性:

workerManager::workerManager() { //初始化人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmpArray = NULL; }

实现add_Emp()函数

void workerManager::add_Emp() { cout << "请输入新添加职工的数量(整数):" << endl; int add_num; cin >> add_num; if (add_num > 0) { //1、计算新空间大小 int newSize = this->m_EmpNum + add_num; //2、开辟新的空间 Woker** newSpace = new Woker* [newSize]; //3、将原空间的内容放入新空间 if (this->m_EmpArray != NULL) { for (int j =0 ; j < this->m_EmpNum; j++) { newSpace[j] = this->m_EmpArray[j]; } } //4、输入新的数据 for (int i = 0; i < add_num; i++) { int id; string name; int dselete; cout << "请输入第" << i + 1 << "个新职员的编号:" << endl; cin >> id; cout << "请输入第" << i + 1 << "个新职员的姓名:" << endl; cin >> name; cout << "请输入第" << i + 1 << "个新职员的职位(数字):" << endl; cout << "1、员工" << endl; cout << "2、经理" << endl; cout << "3、总裁" << endl; cin >> dselete; if (dselete == 1 || dselete == 2 || dselete == 3) { Woker* woker = NULL; switch (dselete) { case 1: woker = new Employee(id, name, 1); break; case 2: woker = new Manager(id, name, 2); break; case 3: woker = new Boss(id, name, 3); break; } newSpace[this->m_EmpNum + i + 1] = woker; } else { cout << "选择错误!录入失败" << endl; cin >> dselete; } //5、释放原来的空间 delete[] this->m_EmpArray; //6、更改指针指向 this->m_EmpArray = newSpace; //7、更新员工个数 this->m_EmpNum = newSize; //8、提示信息 cout << "添加成功!" << endl; } } else { cout << "输入有误!" << endl; } system("pause"); system("cls"); }

在WorkerManager.cpp的析构函数中,释放堆区数据

workerManager::~workerManager() { if (this->m_EmpArray!=NULL) { delete[] this->m_EmpArray; } }

3、测试添加

八、文件交互-写文件

功能描述:对文件进行读写

1、设置文件路径

2、保存文件函数声明

3、保存文件函数实现

//保存文件 void workerManager::save() { //2、创建文件流对象 ofstream ofs; //3、打开文件 ofs.open(FILENAME, ios::out); //4、写入文件 for (int i = 0; i < this->m_EmpNum; i++) { ofs << this->m_EmpArray[i]->w_id << " " << this->m_EmpArray[i]->w_name << " " << this->m_EmpArray[i]->w_deptId << endl; } //5、关闭文件 ofs.close(); }

4、测试

显示成功!


九、文件交互-读文件

功能描述:将文件中的内容读取到程序

因此构造函数初始化数据的情况分为三种:

1、第一次使用,文件未创建

2、文件存在,但是数据被用户清空

3、文件存在,并且保存职工的所有数据


1、文件未创建

workerManager.h中添加属性 m_FileIsEmpty,标志文件是否为空

修改workerManager.cpp中构造函数代码

workerManager::workerManager() { ifstream ifs; ifs.open(FILENAME,ios::in); //打开文件 //1、文件不存在 if (!ifs.is_open()) { cout << "文件不存在!" << endl; //初始化人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmpArray = NULL; //置为空标志为true this->m_FileIsEmpty = true; ifs.close(); return; } }

2、文件存在,数据为空

更新workerManager.cpp中构造函数代码

workerManager::workerManager() { ifstream ifs; ifs.open(FILENAME,ios::in); //打开文件 //1、文件不存在 if (!ifs.is_open()) { cout << "文件不存在!" << endl; //初始化人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmpArray = NULL; //置为空标志为true 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; //置为空标志为true this->m_FileIsEmpty = true; ifs.close(); return; } }

3、文件存在且保存职工数据

1、获取记录的职工人数

在workerManager.h中添加成员函数 int get_EmpNum();

在workerManager.cpp中实现get_EmpNum()方法

//获取职工数量 int workerManager::get_EmpNum() { ifstream ifs; ifs.open(FILENAME,ios::in); int num = 0; int id; string name; int did; while(ifs>>id && ifs>>name && ifs>>did) { num++; } return num; }


更新workerManager.cpp中构造函数代码

workerManager::workerManager() { ifstream ifs; ifs.open(FILENAME,ios::in); //打开文件 //1、文件不存在 if (!ifs.is_open()) { cout << "文件不存在!" << endl; //初始化人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmpArray = NULL; //置为空标志为true 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; //置为空标志为true this->m_FileIsEmpty = true; ifs.close(); return; } //3、文件存在,保存数据 int num1 = get_EmpNum(); this->m_EmpNum = num1; cout << "职工数量为:" << this->m_EmpNum << endl; }

2、初始化数组

在workerManager.h中添加成员函数 init_Emp();

在workerManager.cpp中实现init_Emp()方法

//初始化职工数组 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) { Woker* woker = NULL; if (did == 1) { //员工 woker = new Employee(id, name, did); } else if (did == 2) {//经理 woker = new Manager(id, name, did); } else { woker = new Boss(id, name, did); } this->m_EmpArray[index] = woker; index++; } ifs.close(); }


更新workerManager.cpp中构造函数代码

workerManager::workerManager() { ifstream ifs; ifs.open(FILENAME,ios::in); //打开文件 //1、文件不存在 if (!ifs.is_open()) { cout << "文件不存在!" << endl; //初始化人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmpArray = NULL; //置为空标志为true 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; //置为空标志为true this->m_FileIsEmpty = true; ifs.close(); return; } //3、文件存在,保存数据 int num1 = get_EmpNum(); this->m_EmpNum = num1; cout << "职工数量为:" << this->m_EmpNum << endl; //创建堆区数据 this->m_EmpArray = new Woker * [this->m_EmpNum]; this->init_Emp(); for (int i = 0; i < this->m_EmpNum;i++) { cout << "职工编号:" << this->m_EmpArray[i]->w_id << " 姓名:" << this->m_EmpArray[i]->w_name << " 岗位编号:" << this->m_EmpArray[i]->w_deptId << endl; } }


1、不存在

2、存在但为空

3、存在且保存

十、显示职工

1、显示职工函数声明

2、函数实现

在workerManager.cpp中实现显示函数

//显示职工信息 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"); }

十一、删除职工

功能描述:按照职工的编号进行删除职工操作

1、删除职工函数声明

2、判断职工是否存在函数声明

3、判断职工是否存在函数实现

在workerManager.cpp中实现显示函数

//判断职工是否存在 //存在返回所占索引,不存在返回-1 int workerManager::isExist(int id) { int index = -1; for (int i = 0; i < this->m_EmpNum;i++) { if (i == id) { //职工存在 index = i; break; } } return index; }

4、删除职工函数实现

//删除职工 void workerManager::del_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者数据为空" << endl; } else { int id = 0; cout << "请输入你想删除的职工编号:" << endl; cin >> id; int index = this->isExist(id); if (index != -1) { for (int i = index; i < this->m_EmpNum - 1; i++) { this->m_EmpArray[i] = this->m_EmpArray[i + 1]; } this->m_EmpNum -= 1; this->save();//保存文件 cout << "删除成功!" << endl; } else cout << "该职工不存在" << endl; } system("pause"); system("cls"); }

5、测试

十二、修改职工

功能描述:能够按照职工编号对职工信息进行修改和保存

1、修改职工函数声明

2、修改职工函数实现

//修改职工 void workerManager::mod_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者数据为空!" << endl; } else{ cout << "请输入你想修改的职工编号:" << endl; int id; cin >> id; int ret = this->isExist(id); if (ret != -1) { int newId = 0; string newName = ""; int newDid = 0; cout << "查找到编号为:" << id << "的职工" << endl; cout << "请输入新的职工编号:" << endl; cin >> newId; cout << "请输入新的职工姓名:" << endl; cin >> newName; cout << "请输入新的职工岗位编号:" << endl; cout << "1、员工" << endl; cout << "2、经理" << endl; cout << "3、总裁" << endl; cin >> newDid; Woker* woker = NULL; switch(newDid) { case 1: woker = new Employee(newId, newName, 1); break; case 2: woker = new Manager(newId, newName, 2); break; case 3: woker = new Boss(newId, newName, 3); break; } this->m_EmpArray[ret] = woker; cout << "修改成功!" << endl; this->save(); } else { cout << "修改失败,该员工不存在" << endl; } this->save(); } system("pause"); system("cls"); }


十三、查找职工

功能描述:提供两种查找职工方式,

1、按照职工编号

2、按照职工姓名

1、查找职工函数声明

2、查找职工函数实现

//查找职工 void workerManager::find_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者数据为空!" << endl; } else { cout << "请输入查找的方式:" << endl; cout << "1、按职工编号查找" << endl; cout << "2、按姓名查找" << endl; int selete = 0; cin >> selete; switch (selete) { case 1: { int id; cout << "请输入查找的职工编号:" << endl; cin >> id; int ret = this->isExist(id); if (ret != -1) { cout << "查找成功!该职工信息如下:" << endl; this->m_EmpArray[ret]->showInfo(); } else cout << "查找失败,查无此人" << endl; break; } case 2: { string name; cout << "请输入查找的职工姓名:" << endl; cin >> name; bool flag = false; for (int i = 0; i < this->m_EmpNum;i++) { if (this->m_EmpArray[i]->w_name == name) { cout << "查找成功!该职工信息如下:" << endl; this->m_EmpArray[i]->showInfo(); flag = true; } } if (!flag) { cout << "查找失败,查无此人" << endl; } break; } default: cout << "输入错误!查找失败" << endl; break; } } system("pause"); system("cls"); }

十四、排序

功能描述:按照职工编号进行排序,排序的顺序由用户指定

1、升序:冒泡排序法

2、降序:快速排序法


1、排序函数声明

2、排序函数实现

在workerManager.cpp中实现成员函数 void sort_Emp();

//排序 void workerManager::sort_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者数据为空!" << endl; } else{ cout << "请输入你选择的排序方式:" << endl; cout << "1、按职工编号升序排列" << endl; cout << "2、按职工编号降序排列" << endl; int selete = 0; cin >> selete; switch (selete) { case 1: { //冒泡排序 for (int i = 0; i < this->m_EmpNum;i++) { for (int j = this->m_EmpNum - 1; j >= i;j--) { if (this->m_EmpArray[i]->w_id > this->m_EmpArray[j]->w_id) { Woker* temp = m_EmpArray[i]; m_EmpArray[i] = m_EmpArray[j]; m_EmpArray[j] = temp; } } } cout << "冒泡排序升序成功!" << endl; break; } case 2: { //快速排序 for (int i = 0; i < this->m_EmpNum; i++) { int min = i; for (int j = i + 1; j < this->m_EmpNum;j++) { if (this->m_EmpArray[min]->w_id < this->m_EmpArray[j]->w_id) { min = j; } } if ( i != min) { Woker* temp = m_EmpArray[i]; m_EmpArray[i] = m_EmpArray[min]; m_EmpArray[min] = temp; } } cout << "快速排序降序完成!" << endl; break; } default: cout << "排序方式输入有误!" << endl; } this->save(); cout << "排序成功,排序后结果为:" << endl; this->show_Emp(); } }


十五、清空文件

功能描述:将文件中的记录数据清空

1、清空函数声明

2、清空函数实现

//清空 void workerManager::clean_Emp() { cout << "确定清空?" << endl; cout << "1、确定" << endl; cout << "2、退出" << endl; int select = 0; cin >> select; switch (select) { case 1: { //打开模式 ios::trunc 如果存在删除文件并重新创建 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; this->m_EmpNum = 0; this->m_FileIsEmpty = true; } cout << "清空完成!" << endl; system("pause"); system("cls"); break; } case 2: cout << "已退出,文件未清空!" << endl; system("pause"); system("cls"); break; default: cout << "选择有误!" << endl; system("pause"); system("cls"); break; } }

至此,职工管理系统所有功能完成!


附源码:

1、目录结构:

2、头文件

1、boss.h

#pragma once #include<iostream> using namespace std; #include"woker.h" //经理类 class Boss :public Woker { public: //构造函数 Boss(int id, string name, int dId); //显示信息 void showInfo(); //获取职位名称 string getDeptName(); };


2、employee.h

#pragma once #include<iostream> using namespace std; #include"woker.h" class Employee :public Woker{ public: //构造函数 Employee(int id,string name, int dId); //显示信息 void showInfo(); //获取职位名称 string getDeptName(); };


3、manager.h

#pragma once #include<iostream> using namespace std; #include"woker.h" //经理类 class Manager :public Woker { public: //构造函数 Manager(int id, string name, int dId); //显示信息 void showInfo(); //获取职位名称 string getDeptName(); };


4、woker.h

#pragma once #include<iostream> using namespace std; class Woker { public: //方法 //显示信息 virtual void showInfo() = 0; //获取职位名称 virtual string getDeptName() = 0; //属性 //员工编号 int w_id; //姓名 string w_name; //部门编号 int w_deptId; };


5、wokerManager.h

#pragma once #include<iostream> using namespace std; #include"woker.h" class workerManager { public: workerManager(); ~workerManager(); //展示菜单 void show_Menu(); //退出功能 void exitSystem(); //添加职工 void add_Emp(); //保存文件 void save(); //获取职工数量 int get_EmpNum(); //初始化职工数组 void init_Emp(); //显示职工信息 void show_Emp(); //删除职工 void del_Emp(); //修改职工 void mod_Emp(); //查找职工 void find_Emp(); //排序 void sort_Emp(); //清空 void clean_Emp(); //判断职工是否存在 //存在返回所占索引,不存在返回-1 int isExist(int id); //属性 //员工数量 int m_EmpNum; //员工数组指针 Woker** m_EmpArray; //文件是否为空 bool m_FileIsEmpty; };


3、源文件

1、boss.cpp

#define _CRT_SECURE_NO_WARNINGS 1 #include"boss.h" //构造函数 Boss::Boss(int id, string name, int dId) { this->w_id = id; this->w_name = name; this->w_deptId = dId; } //显示信息 void Boss::showInfo() { cout << "职工编号:" << this->w_id << "\t职工姓名:" << this->w_name << "\t岗位:" << this->getDeptName() << "\t岗位职能:管理公司所有事务!" << endl; } //获取职位名称 string Boss::getDeptName() { return string("总裁"); }


2、employee.cpp

#define _CRT_SECURE_NO_WARNINGS 1 #include"employee.h" //构造函数 Employee::Employee(int id, string name,int dId) { this->w_id = id; this->w_name = name; this->w_deptId = dId; } //显示信息 void Employee::showInfo() { cout << "职工编号:" << this->w_id << "\t职工姓名:" << this->w_name << "\t岗位:" << this->getDeptName() << "\t岗位职能:完成经理的任务!" << endl; } //获取职位名称 string Employee::getDeptName() { return string("员工"); }


3、main.cpp

#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; //自定义头文件 #include"workerManager.h" #include"woker.h" #include"employee.h" #include"manager.h" #include"boss.h" int main() { workerManager wm; int chioce; while (true) { wm.show_Menu(); cout << "请做出选择(数字):" << endl; cin >> chioce; switch (chioce){ 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_Emp(); break; default: system("cls"); break; } } system("pause"); return 0; }


4、manager.cpp

#define _CRT_SECURE_NO_WARNINGS 1 #include"manager.h" //构造函数 Manager::Manager(int id, string name, int dId) { this->w_id = id; this->w_name = name; this->w_deptId = dId; } //显示信息 void Manager::showInfo() { cout << "职工编号:" << this->w_id << "\t职工姓名:" << this->w_name << "\t岗位:" << this->getDeptName() << "\t岗位职能:上传下达!" << endl; } //获取职位名称 string Manager::getDeptName() { return string("经理"); }


5、wokerManager.cpp

#define _CRT_SECURE_NO_WARNINGS 1 #include"workerManager.h" #include"boss.h" #include"employee.h" #include"manager.h" #include<fstream> #define FILENAME "empInfo.txt" workerManager::workerManager() { ifstream ifs; ifs.open(FILENAME,ios::in); //打开文件 //1、文件不存在 if (!ifs.is_open()) { cout << "文件不存在!" << endl; //初始化人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmpArray = NULL; //置为空标志为true 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; //置为空标志为true this->m_FileIsEmpty = true; ifs.close(); return; } //3、文件存在,保存数据 int num1 = get_EmpNum(); this->m_EmpNum = num1; cout << "职工数量为:" << this->m_EmpNum << endl; //创建堆区数据 this->m_EmpArray = new Woker * [this->m_EmpNum]; this->init_Emp(); /*for (int i = 0; i < this->m_EmpNum;i++) { cout << "职工编号:" << this->m_EmpArray[i]->w_id << " 姓名:" << this->m_EmpArray[i]->w_name << " 岗位编号:" << this->m_EmpArray[i]->w_deptId << endl; }*/ } workerManager::~workerManager() { if (this->m_EmpArray != NULL) { for (int i = 0; i < this->m_EmpNum; i++) { delete this->m_EmpArray[i]; this->m_EmpArray[i] = NULL; } } } //菜单 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::save() { //2、创建文件流对象 ofstream ofs; //3、打开文件 ofs.open(FILENAME,ios::out); //4、写入文件 for (int i = 0; i < this->m_EmpNum; i++) { ofs << this->m_EmpArray[i]->w_id << " " << this->m_EmpArray[i]->w_name << " " << this->m_EmpArray[i]->w_deptId << endl; } //5、关闭文件 ofs.close(); } //获取职工数量 int workerManager::get_EmpNum() { ifstream ifs; ifs.open(FILENAME,ios::in); int num = 0; int id; string name; int did; while(ifs>>id && ifs>>name && ifs>>did) { 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) { Woker* woker = NULL; if (did == 1) { //员工 woker = new Employee(id, name, did); } else if (did == 2) {//经理 woker = new Manager(id, name, did); } else { woker = new Boss(id, name, did); } this->m_EmpArray[index] = woker; 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 { int id = 0; cout << "请输入你想删除的职工编号:" << endl; cin >> id; int index = this->isExist(id); if (index != -1) { for (int i = index; i < this->m_EmpNum - 1; i++) { this->m_EmpArray[i] = this->m_EmpArray[i + 1]; } this->m_EmpNum -= 1; this->save();//保存文件 cout << "删除成功!" << endl; } else cout << "该职工不存在" << endl; } system("pause"); system("cls"); } //判断职工是否存在 //存在返回所占索引,不存在返回-1 int workerManager::isExist(int id) { int index = -1; for (int i = 0; i < this->m_EmpNum;i++) { if (this->m_EmpArray[i]->w_id == id) { //职工存在 index = i; break; } } return index; } //修改职工 void workerManager::mod_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者数据为空!" << endl; } else{ cout << "请输入你想修改的职工编号:" << endl; int id; cin >> id; int ret = this->isExist(id); if (ret != -1) { int newId = 0; string newName = ""; int newDid = 0; cout << "查找到编号为:" << id << "的职工" << endl; cout << "请输入新的职工编号:" << endl; cin >> newId; cout << "请输入新的职工姓名:" << endl; cin >> newName; cout << "请输入新的职工岗位编号:" << endl; cout << "1、员工" << endl; cout << "2、经理" << endl; cout << "3、总裁" << endl; cin >> newDid; Woker* woker = NULL; switch(newDid) { case 1: woker = new Employee(newId, newName, 1); break; case 2: woker = new Manager(newId, newName, 2); break; case 3: woker = new Boss(newId, newName, 3); break; } this->m_EmpArray[ret] = woker; cout << "修改成功!" << endl; this->save(); } else { cout << "修改失败,该员工不存在" << endl; } this->save(); } system("pause"); system("cls"); } //查找职工 void workerManager::find_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者数据为空!" << endl; } else { cout << "请输入查找的方式:" << endl; cout << "1、按职工编号查找" << endl; cout << "2、按姓名查找" << endl; int selete = 0; cin >> selete; switch (selete) { case 1: { int id; cout << "请输入查找的职工编号:" << endl; cin >> id; int ret = this->isExist(id); if (ret != -1) { cout << "查找成功!该职工信息如下:" << endl; this->m_EmpArray[ret]->showInfo(); } else cout << "查找失败,查无此人" << endl; break; } case 2: { string name; cout << "请输入查找的职工姓名:" << endl; cin >> name; bool flag = false; for (int i = 0; i < this->m_EmpNum;i++) { if (this->m_EmpArray[i]->w_name == name) { cout << "查找成功!该职工信息如下:" << endl; this->m_EmpArray[i]->showInfo(); flag = true; } } if (!flag) { cout << "查找失败,查无此人" << endl; } break; } default: cout << "输入错误!查找失败" << endl; break; } } 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 = 0; cin >> selete; switch (selete) { case 1: { //冒泡排序 for (int i = 0; i < this->m_EmpNum;i++) { for (int j = this->m_EmpNum - 1; j >= i;j--) { if (this->m_EmpArray[i]->w_id > this->m_EmpArray[j]->w_id) { Woker* temp = m_EmpArray[i]; m_EmpArray[i] = m_EmpArray[j]; m_EmpArray[j] = temp; } } } cout << "冒泡排序升序成功!" << endl; break; } case 2: { //快速排序 for (int i = 0; i < this->m_EmpNum; i++) { int min = i; for (int j = i + 1; j < this->m_EmpNum;j++) { if (this->m_EmpArray[min]->w_id < this->m_EmpArray[j]->w_id) { min = j; } } if ( i != min) { Woker* temp = m_EmpArray[i]; m_EmpArray[i] = m_EmpArray[min]; m_EmpArray[min] = temp; } } cout << "快速排序降序完成!" << endl; break; } default: cout << "排序方式输入有误!" << endl; } this->save(); cout << "排序成功,排序后结果为:" << endl; this->show_Emp(); } } //清空 void workerManager::clean_Emp() { cout << "确定清空?" << endl; cout << "1、确定" << endl; cout << "2、退出" << endl; int select = 0; cin >> select; switch (select) { case 1: { //打开模式 ios::trunc 如果存在删除文件并重新创建 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; this->m_EmpNum = 0; this->m_FileIsEmpty = true; } cout << "清空完成!" << endl; system("pause"); system("cls"); break; } case 2: cout << "已退出,文件未清空!" << endl; system("pause"); system("cls"); break; default: cout << "选择有误!" << endl; system("pause"); system("cls"); break; } } //添加 void workerManager::add_Emp() { cout << "请输入新添加职工的数量(整数):" << endl; int add_num = 0; cin >> add_num; if (add_num > 0) { //1、计算新空间大小 int newSize = this->m_EmpNum + add_num; //2、开辟新的空间 Woker** newSpace = new Woker* [newSize+4]; //3、将原空间的内容放入新空间 if (this->m_EmpArray != NULL) { for (int i =0 ; i < this->m_EmpNum; i++) { newSpace[i] = this->m_EmpArray[i]; } } //4、输入新的数据 for (int i = 0; i < add_num; i++) { int id; string name; int dselete; cout << "请输入第" << i + 1 << "个新职员的编号:" << endl; cin >> id; int j = 0; while (j < this->m_EmpNum) { if (m_EmpArray != NULL) { if (id == this->m_EmpArray[j]->w_id) { cout << "输入错误,请重新输入:" << endl; cin >> id; j = 0; } else { j++; } } } cout << "请输入第" << i + 1 << "个新职员的姓名:" << endl; cin >> name; cout << "请输入第" << i + 1 << "个新职员的岗位(数字):" << endl; cout << "1、员工" << endl; cout << "2、经理" << endl; cout << "3、总裁" << endl; cin >> dselete; if (dselete == 1 || dselete == 2 || dselete == 3) { Woker* woker = NULL; switch (dselete) { case 1: woker = new Employee(id, name, 1); break; case 2: woker = new Manager(id, name, 2); break; case 3: woker = new Boss(id, name, 3); break; } newSpace[this->m_EmpNum + i] = woker; } else { cout << "选择错误!录入失败,请重新输入:" << endl; cin >> dselete; } } //5、释放原来的空间 delete[] this->m_EmpArray; //6、更改指针指向 this->m_EmpArray = newSpace; //7、更新员工个数 this->m_EmpNum = newSize; //置文件不为空 this->m_FileIsEmpty = false; //8、提示信息 cout << "成功添加"<<add_num<<"名职员" << endl; //保存文件 this->save(); } else { cout << "输入有误!" << endl; } system("pause"); system("cls"); }

喜欢就点赞收藏吧~