-
2021-02-20 13:51:23
职工管理系统
1、管理系统需求
职工管理系统可以用来管理公司内所有员工的信息
利用C++来实现一个基于多态的职工管理系统
公司中职工分为三类:普通员工、经理、老板,显示信息时,需要显示职工编号、职工姓名、职工岗位、以及职责
普通员工职责:完成经理交给的任务
经理职责:完成老板交给的任务,并下发任务给员工
老板职责:管理公司所有事务
管理系统中需要实现的功能如下:
- 退出管理程序:退出当前管理系统
- 增加职工信息:实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号
- 显示职工信息:显示公司内部所有职工的信息
- 删除离职职工:按照编号删除指定的职工
- 修改职工信息:按照编号修改职工个人信息
- 查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息
- 按照编号排序:按照职工编号,进行排序,排序规则由用户指定
- 清空所有文档:清空文件中记录的所有职工信息 (清空前需要再次确认,防止误删)
我们创建以下多个头文件和与其对应的 .cpp文件:
boss.h头文件
#pragma once #include <iostream> using namespace std; #include <string> #include "Worker.h" //老板类 class Boss :public Worker { public: Boss(int id, string name, int post); //显示个人信息 virtual void ShowInfo(); //获取岗位名称 virtual string getPost(); };
manager.h 头文件
class Manager :public Worker { public: Manager(int id, string name, int post); //显示个人信息 virtual void ShowInfo(); //获取岗位名称 virtual string getPost(); };
employee.h头文件
class Employee:public Worker { public: Employee(int id,string name,int post); //显示个人信息 virtual void ShowInfo(); //获取岗位名称 virtual string getPost(); };
worker.h头文件
//职工抽象类 class Worker { public: //显示个人信息 virtual void ShowInfo() = 0; //获取岗位名称 virtual string getPost() = 0; //职员编号 int m_Id; //职工姓名 string m_Name; //部门编号 int m_Post; };
worker_Manager.h头文件
#pragma once #include <iostream> using namespace std; #include "boss.h" #include "manager.h" #include "Worker.h" #include "employee.h" #include <fstream> #define TXT "empfine.txt" //各程序执行的类 class Worker_Manager { public: Worker_Manager(); //显示菜单 void ShowMenu(); //记录职工人数 int m_EmpNum; //职工指针数组 Worker** m_EmpArray; //添加职工 void Add_Emp(); //保存文件 void save(); //判断文件是否为空 bool m_File; //统计文件中的人数 int get_EmpNum(); //初始化员工 void In_Emp(); //显示员工 void Show_Emp(); //判断职工是否存在 int Ison_Emp(int id); //删除员工 void Del_Emp(); //修改员工 void Mod_Emp(); //查找员工 void Find_Emp(); //排序 void Sort_Emp(); //清空文件 void Clean_File(); ~Worker_Manager(); };
boss.cpp文件
#include "boss.h" Boss::Boss(int id, string name, int post) { this->m_Id = id; this->m_Name = name; this->m_Post = post; } //显示个人信息 void Boss::ShowInfo() { cout << "职工编号:" << this->m_Id << "\t职工姓名:" << this->m_Name << "\t职工部门:" << this->getPost() << "\t岗位职责:管理公司所有事务" << endl; } //获取岗位名称 string Boss::getPost() { return string("老板"); }
manager.cpp文件
#include "manager.h" Manager::Manager(int id, string name, int post) { this->m_Id = id; this->m_Name = name; this->m_Post = post; } //显示个人信息 void Manager::ShowInfo() { cout << "职工编号:" << this->m_Id << "\t职工姓名:" << this->m_Name << "\t职工部门:" << this->getPost() << "\t岗位职责:完成老板布置的任务,并下发任务给员工" << endl; } //获取岗位名称 string Manager::getPost() { return string("经理"); }
employee.cpp文件
#include "employee.h" Employee::Employee(int id, string name, int post) { this->m_Id = id; this->m_Name = name; this->m_Post = post; } //显示个人信息 void Employee::ShowInfo() { cout << "职工编号:" << this->m_Id << "\t职工姓名:" << this->m_Name << "\t职工部门:" << this->getPost() << "\t岗位职责:完成经理布置的任务" << endl; } //获取岗位名称 string Employee::getPost() { return string("员工"); }
worker_Manager.cpp文件
#include "worker_Manager.h" Worker_Manager::Worker_Manager() { //1、文件不存在 ifstream ifs; ifs.open(TXT, ios::in); if (!ifs.is_open()) { //cout << "文件不存在" << endl; //初始化属性 //初始化记录人数 this->m_EmpNum = 0; //初始化指针数组 this->m_EmpArray = NULL; //初始化文件是否为空 this->m_File = true; ifs.close(); return; } //2、文件存在,但为空 char ch; ifs >> ch; if (ifs.eof()) { //cout << "文件为空" << endl; //初始化属性 //初始化记录人数 this->m_EmpNum = 0; //初始化指针数组 this->m_EmpArray = NULL; //初始化文件是否为空 this->m_File = true; ifs.close(); return; } //3.文件存在且数据不为空 int num = this->get_EmpNum(); this->m_EmpNum = num; //开辟空间 this->m_EmpArray = new Worker * [this->m_EmpNum]; //将文件中的数据,存到数组中 this->In_Emp(); } void Worker_Manager::ShowMenu() { 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; } void Worker_Manager::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 post; cout << "请输入第 " << i + 1 << "个新职工的编号: " << endl; cin >> id; cout << "请输入第 " << i + 1 << "个新职工的姓名: " << endl; cin >> name; cout << "请选择职工岗位: " << endl; cout << "1、普通员工" << endl; cout << "2、经理" << endl; cout << "3、老板" << endl; cin >> post; Worker* worker = NULL; switch (post) { case 1: worker = new Employee(id, name, post); break; case 2: worker = new Manager(id, name, post); break; case 3: worker = new Boss(id, name, post); break; default: cout << "错误输入" << endl; break; } //将创建的职工指针,保存到数组中 newSpace[this->m_EmpNum + i] = worker; } //释放原指针 delete[] this->m_EmpArray; //更改新空间的指向 this->m_EmpArray = newSpace; //更新职工人数 this->m_EmpNum = newSize; cout << "成功添加" << addNum << "名新职工" << endl; this->m_File = false; //保存数据到文件中 this->save(); } else { cout << "输入有误" << endl; } system("pause"); system("cls"); } void Worker_Manager::save() { ofstream ofs; ofs.open(TXT, ios::out); for (int i = 0; i < this->m_EmpNum; i++) { ofs << this->m_EmpArray[i]->m_Id<<" " << this->m_EmpArray[i]->m_Name<<" " << this->m_EmpArray[i]->m_Post<<" " << endl; } ofs.close(); } int Worker_Manager::get_EmpNum() { ifstream ifs; ifs.open(TXT, ios::in); int id; string name; int post; int num = 0; while (ifs >> id && ifs >> name && ifs >> post) { num++; } return num; } void Worker_Manager::In_Emp() { ifstream ifs; ifs.open(TXT, ios::in); int id; string name; int post; int index = 0; while (ifs>>id && ifs>>name && ifs>>post) { Worker* worker = NULL; if (post == 1) { worker = new Employee(id, name, post);//普通员工 } else if (post == 2) { worker = new Manager(id, name, post);//经理 } else { worker = new Boss(id, name, post);//老板 } this->m_EmpArray[index] = worker; index++; } ifs.close(); } void Worker_Manager::Show_Emp() { if (this->m_File) { cout << "该文件不存在或为空" << endl; } else { for (int i = 0; i < this->m_EmpNum; i++) { //利用多态调用 this->m_EmpArray[i]->ShowInfo(); } } //清屏 system("pause"); system("cls"); } int Worker_Manager::Ison_Emp(int id) { 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 Worker_Manager::Del_Emp() { if (this->m_File) { cout << "该文件不存在或为空" << endl; } else { cout << "请输入你要删除员工的编号:" << endl; int id; cin >> id; int ret = this->Ison_Emp(id); //职工存在,且在index位置上 if (ret != -1) { for (int i = ret; i < this->m_EmpNum - 1; i++) { //数据前移 this->m_EmpArray[i] = this->m_EmpArray[i + 1]; } //更新人数 this->m_EmpNum--; cout << "删除成功" << endl; //同步到文件中 this->save(); } else { cout << "删除失败" << endl; } } system("pause"); system("cls"); } void Worker_Manager::Mod_Emp() { if (this->m_File) { cout << "该文件不存在或为空" << endl; } else { int id = 0; cout << "输入你要修改的员工编号:" << endl; cin >> id; int ret = this->Ison_Emp(id); if (ret != -1) { delete this->m_EmpArray[ret]; int Newid = 0; string name=""; int post = 0; cout << "查到: " << id << "号员工,请输入新的员工编号: " << endl; cin >> Newid; cout << "查到: " << id << "号员工,请输入新的员工姓名: " << endl; cin >> name; cout << "请输入新岗位" << endl; cout << "1、普通员工" << endl; cout << "2、经理" << endl; cout << "3、老板" << endl; cin >> post; Worker* worker = NULL; switch (post) { case 1: worker = new Employee(Newid, name, post); break; case 2: worker = new Manager(Newid, name, post); break; case 3: worker = new Boss(Newid, name, post); break; default: break; } //将修改的数据放回原来位置 this->m_EmpArray[ret] = worker; cout << "修改成功" << endl; this->save(); } else { cout << "修改失败" << endl; } } system("pause"); system("cls"); } void Worker_Manager::Find_Emp() { if (this->m_File) { cout << "该文件不存在或为空" << endl; } else { cout << "请选择查找的方式" << endl; cout << "1、按编号查找" << endl; cout << "2、按姓名查找" << endl; int input = 0; cin >> input; if (input == 1) { //按编号查找 int id = 0; cout << "请输入你要查找的编号为:" << endl; cin >> id; int ret = this->Ison_Emp(id); if (ret != -1) { cout << "查找到此人信息 如下:" << endl; this->m_EmpArray[ret]->ShowInfo(); } else { cout << "查无此人" << endl; } } else if(input==2) { //按姓名查找 string name; cout << "请输入你要查找人的姓名: " << endl; cin >> name; bool flag = false; for (int i = 0; i < this->m_EmpNum; i++) { if (this->m_EmpArray[i]->m_Name == name) { cout << "成功找到员工,员工编号为:" << this->m_EmpArray[i]->m_Id << "此员工信息如下" << endl; this->m_EmpArray[i]->ShowInfo(); flag = true; } if (flag == false) { cout << "查无此人" << endl; } } } else { cout << "输入选项有误" << endl; } } system("pause"); system("cls"); } void Worker_Manager::Sort_Emp() { if (this->m_File) { cout << "该文件不存在或为空" << endl; system("pause"); system("cls"); } else { cout << "请选择排序方式" << endl; cout << "1、按职工号升序" << endl; cout << "2、按职工号降序" << endl; int input = 0; cin >> input; for (int i = 0; i < this->m_EmpNum; i++) { int minOrmax = i; for (int j = i + 1; j < this->m_EmpNum; j++) { if (input == 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 (minOrmax != i) { 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 Worker_Manager::Clean_File() { cout << "确定清空?" << endl; cout << "1、确定" << endl; cout << "2、取消" << endl; int input = 0; cout << "请输入: " << endl; cin >> input; if (input == 1) { ofstream ofs; ofs.open(TXT, 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_File = true; this->m_EmpNum = 0; } cout << "清空成功" << endl; } system("pause"); system("cls"); } Worker_Manager::~Worker_Manager() { 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; } }
职工管理系统.cpp文件
#include <iostream> using namespace std; #include <string> #include "worker_Manager.h" #include "Worker.h" #include "employee.h" #include "boss.h" #include "manager.h" int main() { //测试代码 //Worker* worker = NULL; //worker = new Employee(1, "张三", 1); //worker->ShowInfo(); //delete worker; // worker = new Manager(2, "张三", 2); //worker->ShowInfo(); //delete worker; //worker = new Boss(3, "张三", 3); //worker->ShowInfo(); //delete worker; //实例化对象 Worker_Manager wm; int input = 0; do { wm.ShowMenu(); cout << "请输入你的选择:" << endl; cin >> input; switch (input) { case 0: cout << "欢迎下次使用!" << endl; 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; } } while (input); system("pause"); return 0; }
更多相关内容 -
C++员工管理系统
2018-07-07 16:13:58简单的员工管理系统,功能包括员工的增删以及查找,自己做的小demo~ -
c++职工管理系统
2018-10-13 21:48:34试设计一职工信息管理系统,使之能提供以下功能: 系统以菜单方式工作: 职工信息录入功能(职工信息用文件保存)--输入 职工信息浏览功能--输出 查询和排序功能:(至少一种查询方式)--算法 按工资查询 ... -
C++职工管理系统.rar
2021-07-29 11:17:13用数组开发的一个职工管理系统 -
C++职工管理系统
2020-06-13 21:14:24职工管理系统可以用来管理公司所有员工的信息,可以实现增、删、改、查职工信息,能对职工按照编号进行操作,清空所有文档等功能。 数据结构设计: 类别设计:职工抽象类(基类),普通员工类(派生类)、经理类...基于多态的职工管理系统
目录:
程序介绍
- 数据结构设计
- 函数功能分析
- 代码
程序介绍:
开发环境:Visual Studio Community.
软件介绍:
- 主要由c++面向对象基于多态设计、实现程序。
- 职工管理系统可以用来管理公司所有员工的信息,可以实现增、删、改、查职工信息,能对职工按照编号进行操作,清空所有文档等功能。
一、数据结构设计:
- 类别设计:职工抽象类(基类),普通员工类(派生类)、经理类(派生类)、老板类(派生类),功能操作类。
- 职工信息设计:显示职工编号、职工姓名、职工岗位(三种)、职责(职位对应)。
- 职工职责设计:普通员工职责是完成经理交给的任务,经理的职责是完成老板交给的任务,并下发给员工,老板的职责是管理公司所有事务。
二、函数功能分析理解
三、代码
头文件:
WorkerManger.h#pragma once // 防止头文件重复包含 #include <iostream> //包含输入输出流头文件 using namespace std; //使用标准命名空间 #include "worker.h" #include "employee.h" #include "manager.h" #include "boss.h" #include <fstream> #define FILENAME "empFile.txt" class WorkerManager { public: //构造函数 WorkerManager(); //展示菜单 void Show_Menu(); //退出系统 void ExitSystem(); //记录职工人数 int m_EmpNum; //职工数组指针 Worker** m_EmpArray; //添加职工 void Add_Emp(); //保存文件 void save(); //判断文件是否为空 标志 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(); //析构函数 ~WorkerManager(); };
manger.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(); };
employee.h
#pragma once #include<iostream> #include"worker.h" using namespace std; class Employee :public Worker { public: //构造函数 Employee( int id,string name,int dId); //普通员工重写显示个人信息 void showInfo(); //获取岗位名称 string getDeptName(); };
boss.h
#pragma once #include <iostream> using namespace std; #include "worker.h" //老板类 class Boss :public Worker { public: //构造函数 Boss(int id, string name, int dId); //显示个人信息 virtual void showInfo(); //获取岗位名称 virtual string getDeptName(); };
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; };
源文件:
WorkerManger.cpp#include "workerManger.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(); //测试代码 //for (int i = 0; i < this->m_EmpNum; i++) //{ // cout << "职工编号: " << this->m_EmpArray[i]->m_Id // << " 姓名: " << this->m_EmpArray[i]->m_Name // << " 部门编号: " << this->m_EmpArray[i]->m_DeptId << 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::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 dSelect; // 部门选择 cout << "请输入第 " << i + 1 << " 个新职工编号: " << endl; cin >> id; cout << "请输入第 " << i + 1 << " 个新职工姓名: " << endl; cin >> name; cout << "请选择该职工岗位: " << endl; cout << "1、普通职工" << endl; cout << "2、经理" << endl; cout << "3、老板" << endl; cin >> dSelect; Worker* worker = NULL; switch (dSelect) { 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; } //将创建职工职责 ,保存到数组中,就是数组的扩充 newSpace[this->m_EmpNum + i] = worker; } //释放原有空间 delete[] this->m_EmpArray; //更改新空间的指向 this->m_EmpArray = newSpace; //更新新的职工人数 this->m_EmpNum = newSize; //更新职工不为空标志 this->m_FileIsEmpty = 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->m_EmpNum; i++) { ofs << this->m_EmpArray[i]->m_Id << " " << this->m_EmpArray[i]->m_Name << " " << 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++; } 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 //老板 { worker = new Boss(id, name, dId); } this->m_EmpArray[index] = worker; index++; } //关闭文件 ifs.close(); } //显示职工 void WorkerManager::Show_Emp() { //判断文件是否为空 if (this->m_FileIsEmpty) { cout << "文件不存在或记录为空!" << endl; } else { for (int i = 0; i < 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); if (index != -1) //说明职工存在,并且要删除掉index位置上的职工 { for (int i = index; i < this->m_EmpNum - 1; i++) { //数据前移 this->m_EmpArray[i] = this->m_EmpArray[i + 1]; } this->m_EmpNum--; //更新数组中记录人员个数 //数据同步更新到文件中 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]->m_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) { //查找到编号的职工 delete this->m_EmpArray[ret]; int newId = 0; string newName = ""; int dSelect = 0; cout << "查到: " << id << "号职工,请输入新职工号: " << endl; cin >> newId; cout << "请输入新姓名: " << endl; cin >> newName; cout << "请输入岗位: " << endl; cout << "1、普通职工" << endl; cout << "2、经理" << endl; cout << "3、老板" << endl; cin >> dSelect; Worker* worker = NULL; switch (dSelect) { case1: worker = new Employee(newId, newName, dSelect); break; case2: worker = new Manager(newId, newName, dSelect); break; case 3: worker = new Boss(newId, newName, dSelect); break; default: break; } //更改数据 到数组中 this->m_EmpArray[ret] = worker; cout << "修改成功!" << this->m_EmpArray[ret]->m_DeptId << endl; //保存到文件中 this->save(); } else { cout << "修改失败,查无此人" << endl; } } //按任意键 清屏 system("pause"); system("cls"); } //查找职工 void WorkerManager::Find_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或记录为空!" << endl; } else { cout << "请输入查找的方式:" << endl; cout << "1、按职工编号查找 " << endl; cout << "2、按职工姓名查找 " << endl; int select = 0; cin >> select; if (select == 1) { //按照编号查 int id; cout << "请输入查找的职工编号: " << endl; cin >> id; int ret = IsExist(id); if (ret != -1) { //找到职工 cout << "查找成功!该职工信息如下:" << endl; this->m_EmpArray[ret]->showInfo(); } else { cout << "查找失败,查无此人" << endl; } } else if (select == 2) { //按照姓名查 string name; cout << "请输入查找的姓名:" << endl; cin >> name; //加入判断是否查到的标志 bool flag = false; //默认未找到职工 for (int i = 0; i < m_EmpNum; i++) { if (this->m_EmpArray[i]->m_Name == name) { cout << "查找成功,职工编号为: " << this->m_EmpArray[i]->m_Id << "号职工信息如下:" << endl; flag = true; //调用显示信息接口 this->m_EmpArray[i]->showInfo(); } } 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 select = 0; cin >> select; for (int i = 0; i < m_EmpNum; i++) { int minOrMax = i; //声明最小值 或 最大值下标 for (int j = i + 1; j < this->m_EmpNum; j++) { if (select == 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 << "确定清空?" << endl; cout << "1、确定" << endl; cout << "2、返回" << endl; int select = 0; cin >> select; if (select == 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; } }
manger.cpp
#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职工姓名: " << this->m_Name << "\t岗位: " << this->getDeptName() << "\t岗位职责: 完成老板交给的任务,并下发任务给员工" << endl; } //获取岗位名称 string Manager::getDeptName() { return string("经理"); }
employee.cpp
#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职工姓名: " << this->m_Name << "\t岗位: " << this->getDeptName() << "\t岗位职责: 完成经理交给的任务" << endl; } //获取岗位名称 string Employee::getDeptName() { return string("员工"); }
boss.cpp
#include "boss.h" //构造函数 Boss::Boss(int id, string name, int dId) { this->m_Id = id; this->m_Name = name; this->m_DeptId = dId; } //显示个人信息 void Boss::showInfo() { cout << "职工编号: " << this->m_Id << "\t职工姓名: " << this->m_Name << "\t岗位: " << this->getDeptName() << "\t岗位职责: 管理公司所有事务" << endl; } //获取岗位名称 string Boss::getDeptName() { return string("总裁"); }
main.cpp
#include <iostream> using namespace std; #include "workerManger.h" //#include "worker.h" //#include "employee.h" //#include "manager.h" //#include "boss.h" int main() { 测试代码: //Worker * worker = NULL; //worker = new Employee(1, "张三", 1); //worker->showInfo(); //delete worker; //worker = new Manager(2, "李四", 2); //worker->showInfo(); //delete worker; //worker = new Boss(3, "王五", 3); //worker->showInfo(); //delete worker; //实例化管理者对象 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"); return 0; }
参考:B站黑马
-
c++员工管理系统
2018-08-16 15:38:21c++员工管理系统(大一学习c++学期末的综合测评,相信很多人都做过,仅供参考) -
【C++职工管理系统案例】_VS2019.rar
2021-05-24 10:26:13c++初学练手案例,工程文件,可直接运行!!!复习案例,C++课程设计案例! -
c++职工管理系统c++职工管理系统.doc
2022-06-20 02:26:05c++职工管理系统c++职工管理系统 -
C++实现简单职工信息管理系统
2020-12-31 05:23:21本文实例为大家分享了C++职工信息管理系统的具体代码,供大家参考,具体内容如下 功能主模块描述 模块一:增加人员函数Add();增加职工基本信息。 模块二:删除人员函数Delete();删除指定的职工的基本信息以及... -
C++职工管理系统.pdf
2021-01-28 10:38:26职工管理系统.pdf -
C++职工管理系统(多态)
2020-10-09 20:05:27可以用来练习C++的继承、多态、抽象类等等,也可以作为课程实验的程序。简单易懂,分文件编写。。。。。。。。。。。。。。。。。。。。。。 -
C++数据结构课程设计大作业-员工管理系统(带完整报告)
2022-03-22 21:19:13C++数据结构课程设计大作业-员工管理系统(带完整报告),命令行版本,带36页完整报告,可以直接运行,linux环境也可以编译。涉及到文件保存、指针等常用知识点。 -
职工档案管理系统C++编程实验报告
2018-03-16 14:08:25C++职工档案管理系统课程设计报告,系统有添加删除查找读取恢复等功能。 -
C++职工管理系统编写
2022-06-25 15:29:47C++管理系统适合初学者使用,融合C++的基础知识 -
职工管理系统C++实现
2021-03-09 16:03:36职工管理系统可以用来管理公司内所有员工的信息(利用C++来实现一个基于多态的职工管理系统) 公司中职工分为三类:普通员工、经理、董事长,显示信息时,需要显示职工编号、职工姓名、职工岗位、以及职责 【普通... -
用c++实现职工管理系统
2022-04-18 13:37:34本教程主要利用c++来实现一个基于多态的职工管理系统 公司中职工分为三类:普通员工、经理、老板、显示信息时,需要显示职工编号、职工姓名、职工岗位、以及职责 普通员工职责:完成经理交给的任务 经理的职责...首先带大家看看文件的结构以及树状图
以下是具体的思路:
1、管理系统需求
职工管理系统可以用来管理公司内所有员工信息
本教程主要利用c++来实现一个基于多态的职工管理系统
公司中职工分为三类:普通员工、经理、老板、显示信息时,需要显示职工编号、职工姓名、职工岗位、以及职责
普通员工职责:完成经理交给的任务
经理的职责:完成老板交给的任务,并下发给任务给员工
老板职责:管理公司所有事物
管理系统中需要实现的功能如下
-
推出管理程序: 退出当前管理系统
-
增加职工信息: 实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号
-
显示职工信息: 显示公司内部所有职工的信息
-
删除离职职工:按照编号删除指定的职工
-
修改职工信息: 按照编号修改职工个人信息
-
查找职工信息: 按照职工的编号或者职工的姓名进行查找相关的人员信息
-
按照编号排序:按照职工编号、进行排序,排序规则由用户指定
-
清空所有文档:清空文件中记录的所有职工信息(清空前需要在此取人,防止误删)
系统界面效果如图:
2、创建项目
创建项目步骤如下:
-
创建新项目
-
添加文件
2.1创建项目
打开vs2017后,点击创建项目,创建新的C++项目
填写项目名称以及项目路径,点击确认
2.2添加文件
右键源文件,进行添加文件操作
至此项目已经创建完毕
3、创建管理类
管理类负责的内容如下:
-
与用户的沟通菜单界面
-
对职工增删改查的操作
-
与文件的读写交互
3.1创建文件
在头文件和源文件的文件下分别创建workerManager.h和workManager.cpp文件
3.2头文件实现
在workManager.h中设计管理类
代码如下:
#pragma once//防止头文件重复包含 #include <iostream>//包含输入输出流头文件 using namespace std;//使用标准命名空间 class WorkerManager { public: //构造函数 WorkerManager(); //析构函数 ~WorkerManager(); };
在workerManager.cpp中设计管理类
代码如下:
#include "workerManager.h" WorkerManager::WorkerManager() { } WorkerManager::~WorkerManager() { }
4、菜单功能
功能描述:与用户的沟通界面
4.1 添加成员函数
在管理类workerManager.h中添加成员函数 void Show_Menu():
#pragma once//防止头文件重复包含 #include <iostream>//包含输入输出流头文件 using namespace std;//使用标准命名空间 class WorkerManager { public: //构造函数 WorkerManager(); //展示菜单 void Show_Menu(); //析构函数 ~WorkerManager(); };
4.2 菜单功能实现
在管理类workerManager.cpp中实现Show_Menu()函数
#include "workerManager.h" WorkerManager::WorkerManager() { } //展示菜单 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; } WorkerManager::~WorkerManager() { if (this->m_EmArray != NULL) { delete[] this->m_EmArray; this->m_EmArray = NULL; } }
4.3 测试菜单功能
在职工管理系统.cpp中测试菜单功能
代码:
#include<iostream> using namespace std; #include "workerManager.h" int main() { //实例化管理者对象 WorkerManager wm; //调用展示菜单成员函数 wm.Show_Menu(); system("pause"); return 0; }
运行效果如图:
5、退出功能
5.1提供功能接口
在main函数中提供分支选择,提供每个功能接口
代码:
#include<iostream> using namespace std; #include "workerManager.h" int main() { //实例化管理者对象 WorkManager wm; int choice = 0;//用来存储用户的选项 while(true) { //调用展示菜单成员函数 wm.Show_Menu(); cout << "请输入您的选择" << endl; cin >> choice;//接受用户的选项 switch (choice) { 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://清屏 break; } } system("pause"); return 0; }
5.2实现退出功能
在workerManager.h中退出系统的成员函数 void exitSystem();
void ExitSystem();
在workerManager.cpp中提供具体的功能实现
代码:
void WorkerManager::ExitSystem() { cout << "欢饮下次再使用" << endl; system("pause"); exit(0);//退出程序 }
5.3测试功能
在main函数分支0选项中,调用退出程序的接口
运行测试截图
6、创建职工类
6.1创建职工抽象类
职工的分类为: 普通员工、经理、老板
将三种职工抽象到一个类(worker)中,利用多态管理不同职工种类
职工的属性为:职工编号、职工姓名、职工所在部门编号
职工的行为:岗位指责信息描述、获取岗位名称
头文件文件夹下 创建文件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; };
6.2创建普通员工类
普通员工类继承职工抽象类,并重写父类中的纯虚函数
在头文件和源文件的文件夹下分别船舰employee.h和employee.cpp文件
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(); };
employee.cpp中代码如下P:
#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职工姓名" << this->m_Name << "\t岗位:" << this->getDeptName() << "\t岗位职责:完成经理交给的任务" << endl; } //获取岗位名称 string Employee::getDeptName() { return string("员工"); }
测试代码:
#include<iostream> using namespace std; #include "workerManager.h" #include "worker.h" #include "employee.h" int main() { //测试代码 Worker *worker = NULL; worker = new Employee(1,"张三",1); worker->showInfo(); // 实例化管理者对象 // WorkManager wm; // // int choice = 0;//用来存储用户的选项 // while(true) // { // //调用展示菜单成员函数 // wm.Show_Menu(); // cout << "请输入您的选择" << endl; // cin >> choice;//接受用户的选项 // switch (choice) // { // 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://清屏 break; // } // } // system("pause"); return 0; }
运行效果截图
6.3创建经理类
经理类继承职工抽象类,并重写父类中纯虚函数,和普通员工类似
在头文件和源文件的文件夹下分别创建manager.h和manager.cpp文件
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(); };
manager.cpp中代码如下:
#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职工姓名" << this->m_Name << "\t岗位:" << this->getDeptName() << "\t岗位职责:完成老板交给的任务,并且下发任务给员工" << endl; } //获取岗位名称 string Manager::getDeptName() { return string("经理"); }
6.4创建老板类
老板类继承职工抽象类,并重写父类中的纯虚函数,和普通员工类似
在头文件和源文件的文件夹下分别创建boss.h和boss.cpp文件
boss.h中代码如下:
#pragma once #include <iostream> using namespace std; #include "worker.h" //老板类 class Boss :public Worker { public: //构造函数 Boss(int id, string name, int dId); //显示个人信息 virtual void showInfo(); //获取岗位名称 virtual string getDeptName(); };
boss.cpp中代码如下:
#include "boss.h" //构造函数 Boss::Boss(int id, string name, int dId) { this->m_Id = id; this->m_Name = name; this->m_DeptId = dId; } //显示个人信息 void Boss::showInfo() { cout << "职工编号:" << this->m_Id << "\t职工姓名" << this->m_Name << "\t岗位:" << this->getDeptName() << "\t岗位职责:管理公司所有事务" << endl; } //获取岗位名称 string Boss::getDeptName() { return string("总裁"); }
6.5测试多态
在职工管理系统.cpp中添加测试函数,并且运行能够产生多态
测试代码如下:
#include<iostream> using namespace std; #include "workerManager.h" #include "worker.h" #include "employee.h" #include "manager.h" #include "boss.h" int main() { //测试代码 Worker *worker = NULL; worker = new Employee(1,"张三",1); worker->showInfo(); delete worker; worker = new Manager(2, "李四", 2); worker->showInfo(); delete worker; worker = new Boss(2, "王五", 2); worker->showInfo(); delete worker; system("pause"); return 0; }
运行效果截图
7、添加职工
功能描述: 批量添加职工,并且保存到文件中
7.1功能分析
分析:
用户在批量创建时,可能会创建不同种类的职工
如果向将所有不同种类的员工都放入到一个数组中,可以将所有员工的指针维护到同一个数组里
如果想在程序中维护这个不定长度的数组,可以将数组创建到堆区,并利用Worker**的指针维护
7.2功能实现
在WorkerManager.h头文件中添加成员属性 代码:
#pragma once//防止头文件重复包含 #include <iostream>//包含输入输出流头文件 using namespace std;//使用标准命名空间 #include "worker.h" #include "employee.h" #include "manager.h" #include "boss.h" class WorkerManager { public: //记录职工人数 int m_EmpNum; //职工数组指针 Worker ** m_EmArray; //添加职工 void Add_Emp(); };
在WorkerManager构造函数中初始化属性
WorkerManager::WorkerManager() { //初始化属性 this->m_EmpNum = 0; this->m_EmArray = NULL; } //析构函数 WorkerManager::~WorkerManager() { if (this->m_EmArray != NULL) { delete[] this->m_EmArray; this->m_EmArray = NULL; } }
workManager.cpp中实现该函数
//添加职工 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_EmArray != NULL) { for (int i = 0; i < this->m_EmpNum; i++) { newSpace[i] = this->m_EmArray[i]; } } //批量添加新数据 for (int i=0;i<addNum;i++) { int id;//职工编号 string name;//职工姓名 int dSelect;//部门选择 cout << "请输入第" << i + 1 << "个新职工编号:" << endl; cin>>id; cout << "请输入第" << i + 1 << "个新职工姓名:" << endl; cin >> name; cout << "请选择该职工岗位:" << endl; cout << "1、普通职工" << endl; cout << "2、经理" << endl; cout << "3、老板" << endl; cin >> dSelect; Worker * worker = NULL; switch (dSelect) { 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); default: break; } //将创建职工职责,保存到数组中 newSpace[this->m_EmpNum + i] = worker; } //释放原有空间 delete[] this->m_EmArray; //更改新空间指向 this->m_EmArray = newSpace; //更新新的职工人数 this->m_EmpNum = newSize; //成功添加后,保存到文件中 //提示添加成功 cout << "成功添加" << addNum << "名新职工" << endl; } else { //输入有误 cout << "输入数据有误" << endl; } //按任意键后,清屏回到上级目录 system("pause"); system("cls"); }
7.3测试添加
运行效果:
8、文件交互-写文件
功能描述:对文件进行读写
在上一个添加功能中,我们只是将所有的数据台南佳到了内存中,一旦程序结束就无法保存了,因此文件管理类中需要一个与文件进行交互的功能,对于文件进行读写操作
8.1设计文件路径
首先我们将文件路径,在workerManager.h中添加宏常量,并且包含头文件fstream
#include <fstream> #define FILENAME "emFIle.txt"
8.2成员函数声明
在workerManager.h中类里添加成员函数 void save();
//保存文件 void save();
8.3保存文件功能实现
//保存文件 void WorkerManager::save() { ofstream ofs; ofs.open(FILENAME, ios::out);//用输出的方式打开文件--写文件 //将每个人的数据写入到文件中 for (int i = 0; i < this->m_EmpNum; i++) { ofs << this->m_EmArray[i]->m_Id << " " << this->m_EmArray[i]->m_Name << " " << this->m_EmArray[i]->m_DeptId << endl; } //关闭文件 ofs.close(); }
8.4保存文件功能测试
//释放原有空间 delete[] this->m_EmArray; //更改新空间指向 this->m_EmArray = newSpace; //更新新的职工人数 this->m_EmpNum = newSize; //成功添加后,保存到文件中 //提示添加成功 cout << "成功添加" << addNum << "名新职工" << endl; //保存数据到文件中 this->save(); }
再次运行代码,添加职工
同级目录下多出文件,并且保存了添加的信息
9、文件交互-读文件
功能描述: 将文件中的内容读取到程序中
虽然我们实现了添加职工后保存到文件的操作,但是每次开始运行程序,并没有将文件中数据读取到程序中
而我们的程序功能中还有清空文件的请求
因此狗哦早函数初始化数据的情况分为三种
1、第一次使用,文件未创建
2、文件存在,但是数据被用户清空
3、文件存在,并且保护职工的所有数据
9.1文件未创建
在workerManager.h中添加新的成员属性m_FileEmpty标志文件是否为空
//判断文件是否为空 标志 bool m_FileIsEmpty;
修改WorkerManager.cpp中构造函数代码
ifstream ifs; ifs.open(FILENAME, ios::in);//读文件 if (!ifs.is_open()) { cout << "文件不存在" << endl; //初始化属性 //初始化记录人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmArray = NULL; //初始化文件是否为空 this->m_FileIsEmpty=true; ifs.close(); return; }
9.2文件存在且数据为空
在workerManager.cpp中的构造函数追加代码:
//2、文件存在 数据为空 char ch; ifs >> ch; if (ifs.eof()) { //文件为空 cout << "文件为空" << endl; //初始化属性 //初始化记录人数 this->m_EmpNum = 0; //初始化数组指针 this->m_EmArray = NULL; //初始化文件是否为空 this->m_FileIsEmpty = true; ifs.close(); return; }
追加代码位置如图
将文件创建后清空文件内容,并测试该情况下初始化功能
我们发现文件不存在或者为空,清空m_FilesEmpty 判断文件是否为空的标志都为真,那何时为假?
成功添加职工后,应该更改文件不为空的标志
在void WorkerManager::Add_Emp()成员函数中添加:
//更新职工不为空标志 this->m_FileIsEmpty = false;
9.3文件存在且保存职工数据
9.3.1获取记录的职工人数
在workerManager.h中添加成员函数 int get_EmpNum();
//统计文件中人数 int get_EmpNum();
workerManager.cpp中实现
//统计文件中人数 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++; } return num; }
在workerManager.cpp构造函数中继续追加代码
//3、文件存在,并且记录数据 int num = this->get_EmpNum(); cout << "职工人数为 : " << num << endl; this->m_EmpNum = num;
手动添加一些职工数据,测试获取职工数量函数
9.3.2 初始化数组
根据职工的数据以及职工数量,初始化workerManager中的Worker ** m_EmpArray 指针
在WorkerManager.h中添加成员函数 void intit_Emp();
//初始化员工 void init_Emp();
在WorkerManager.cpp中实现
//初始化员工 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//老板 { worker = new Boss(id, name, dId); } this->m_EmArray[index] = worker; index++; } //关闭文件 ifs.close(); }
在workerManager.cpp构造函数中追加代码
//开辟空间 this->m_EmArray = new Worker*[this->m_EmpNum]; //将文件中的数据存到数组中 this->init_Emp(); //测试代码 for (int i = 0; i < this->m_EmpNum; i++) { cout << "职工编号" << this->m_EmArray[i]->m_Id << "姓名:" << this->m_EmArray[i]->m_Name << "部门编号:" << this->m_EmArray[i]->m_DeptId << endl; }
运行效果如图
10、显示职工
功能描述:显示当前所有职工信息
10.1 显示职工函数声明
在workerManager.h中添加成员函数 void Show_Emp();
//显示职工 void Show_Emp();
10.2 显示职工函数实现
在workerManager.cpp中实现成员函数 void Show_Emp();
//显示职工 void WorkerManager::Show_Emp() { //判断文件是否为空 if (this->m_FileIsEmpty) { cout << "文件不存在或记录为空!" << endl; } else { for (int i = 0; i < m_EmpNum; i++) { //利用多态调用程序接口 this->m_EmArray[i]->showInfo(); } } //按任意键后清屏 system("pause"); system("cls"); }
效果如图:
10.3 测试显示职工
case 2://显示职工 wm.Show_Emp();
测试时分别测试 文件为空和文件不为空两种情况
测试效果
测试1、文件不存在或者违抗情况
测试2、文件存在且有记录情况
测试完毕,至此,显示所有职工信息功能实现
11、删除职工
11.1删除职工函数声明
在workerManager.h中添加成员函数 void Del_Emp();
//删除职工 void Del_Emp();
11.2职工是否存在函数声明
很多功能都需要用到根据职工是否存在进行操作如:删除职工、修改职工、查找职工
因此添加该构造公告函数,以便后续调用
在workerManager.h中添加成员函数 int IsExist(int id);
//判断职工是否存在 如果存在返回职工所在数组中的位置,不存在返回-1 int IsExist(int id);
11.3职工是否存在函数实现
在workerManager.cpp中实现成员函数 int IsExist(int id);
//判断职工是否存在 如果存在返回职工所在数组中的位置,不存在返回-1 int WorkerManager::IsExist(int id) { int index = -1; for (int i = 0; i < this->m_EmpNum; i++) { if (this->m_EmArray[i]->m_Id==id) { //找到职工 index = 1; break; } } return index; }
11.4删除职工函数实现
//删除职工 void WorkerManager::Del_Emp() { if (this->m_FileIsEmpty) { cout << "请输入想要删除职工编号:" << endl; int id = 0; cin >> id; int index = this->IsExist(id); if (index !=-1) //说明职工存在,并且要删除掉index位置上的职工 { for (int i=index;i<this->m_EmpNum-1;i++) { //数据前移 this->m_EmArray[i] = this->m_EmArray[i + 1]; } this->m_EmpNum--;//更新数组中记录人员个数 //同步更新到文件中 this->save(); cout << "删除成功" << endl; } else { cout << "删除失败,未找到该职工" << endl; } } //按任意键清屏 system("pause"); system("cls"); }
11.5测试删除职工
case 3://删除职工 // { // //测试 // // int ret = wm.IsExist(8); // if (ret != -1) // { // cout << "职工存在" << endl; // } // else // { // cout << "职工不存在" << endl; // } wm.Del_Emp(); break;
测试1、删除不存在职工情况
测试2、删除存在职工情况
再次显示所有职工信息,确保已经删除
查看文件中信息,再次核实员工已被完全删除
12、修改职工
功能描述:能够按照职工的编号对职工信息进行修改并保存
12.1修改职工函数声明
在workerManager.h中添加成员函数 void Mod_Emp();
//修改员工 void Mod_Emp();
12.2修改职工函数实现
在workerManager.cpp中实现成员函数 void Mod_Emp();
//修改员工 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) { //查找到编号的职工 delete this->m_EmArray[ret]; int newId = 0; string newName = ""; int dSelect = 0; cout << "查到 ;" << id << "号职工,请输入新职工号:" << endl; cin >> newId; cout << "请输入新姓名:" << endl; cin >> newName; cout << "请输入岗位" << endl; cout << "1、普通岗位" << endl; cout << "2、经理" << endl; cout << "3、老板" << endl; cin >> dSelect; Worker *worker = NULL; switch (dSelect) { case 1: worker = new Employee(newId, newName, dSelect); break; case 2: worker = new Manager(newId, newName, dSelect); break; case 3: worker = new Boss(newId, newName, dSelect); break; default: break; } //更新数据 到数组中 this->m_EmArray[ret] = worker; cout << "修改成功" << endl; //保存到文件中 this->save(); } else { cout << "修改失败,查无此人" << endl; } } system("pause"); system("cls"); }
测试:
无此人:
13、查找职工
功能描述:提供两种查找职工方式,一种按照职工编号,一种按照职工姓名
13.1 查找职工函数声明
在workerManager.h中添加成员函数 void Find_Emp();
//查找职工 void Find_Emp();
13.2查找职工函数实现
在workerManager.cpp中实现成员函数 void Find_Emp();
//查找职工 void WorkerManager::Find_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或记录为空" << endl; } else { cout << "请输入查找的方式:" << endl; cout << "1、按照职工编号查找" << endl; cout << "2、按照姓名查找" << endl; int select = 0; cin >> select; if (select == 1) { //按照编号查 int id; cout << "请输入查找的职工编号:" << endl; cin >> id; int ret = IsExist(id); if (ret != -1) { //找到职工 cout << "查找成功!该职工信息如下:" << endl; this->m_EmArray[ret]->showInfo(); } else { cout << "查找失败,查无此人" << endl; } } else if (select == 2) { //按照姓名查 string name; cout << "请输入查找的姓名:" << endl; cin >> name; //介入判断是否查到的标志 bool flag = false;//默认未找到职工 for (int i = 0; i < m_EmpNum; i++) { if (this->m_EmArray[i]->m_Name == name) { cout << "查找成功,职工编号为:" << this->m_EmArray[i]->m_Id << "号职工信息如下:" << endl; flag = true; //调用显示信息接口 this->m_EmArray[i]->showInfo(); } } if (flag == false) { cout << "查找失败,查无此人!" << endl; } } else { cout << "输入选项有误" << endl; } } //按任意键清屏 system("pause"); system("cls"); }
13.3测试查找职工
case 5://查找职工 wm.Find_Emp(); break;
1.按编号
2、按姓名
14、排序
功能描述:按照职工编号进行排序,排序的顺序由用户指定
14.1排序函数声明
在workerManager.h中添加成员函数 void Sort_Emp();
//按照编号排序 void Sort_Emp();
14.2 排序函数实现
在workerManager.cpp中实现成员函数 void Sort_Emp();
//按照编号排序 void WorkerManager::Sort_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或记录为空" << endl; system("pause"); system("cls"); } else { cout << "请输入排序方式:" << endl; cout << "1、按照工号进行升序" << endl; cout << "2、按照工号进行降序" << endl; int select = 0; cin >> select; for (int i = 0; i < m_EmpNum; i++) { int minOrMax = i;//声明最小值或最大值下标 for (int j = i + 1; j < this->m_EmpNum; j++) { if (select == 1)//升序 { if (this->m_EmArray[minOrMax]->m_Id>this->m_EmArray[j]->m_Id) { minOrMax = j; } } else//降序 { if (this->m_EmArray[minOrMax]->m_Id < this->m_EmArray[j]->m_Id) { minOrMax = j; } } } //判断一开始认定 最小值或最大值 是不是计算的最大值或最小值 ,如果不是 交换数据 if (i != minOrMax) { Worker * temp = this->m_EmArray[i]; this->m_EmArray[i] = this->m_EmArray[minOrMax]; this->m_EmArray[minOrMax] = temp; } } cout << "排序成功!排序后的结果为:" << endl; this->save();//排序后结果保存到文件中 this->Show_Emp();//展示所有职工 } }
测试:
case 6://排序职工 wm.Sort_Emp(); break;
升序:
降序:
15、清空文件
功能描述:将文件记录数据清空
15.1 清空函数声明
在workerManager.h中添加成员函数 void Clean_File();
//清空文件操作 void Clean_File();
15.2清空函数实现
在workerManager.cpp中实现成员函数 void Clean_File();
//清空文件操作 void WorkerManager::Clean_File() { cout << "确定清空?" << endl; cout << "1、确定" << endl; cout << "2、返回" << endl; int select = 0; cin >> select; if (select == 1) { //清空文件 ofstream ofs(FILENAME, ios::trunc);//删除文件后重新创建 ofs.close(); if (this->m_EmArray != NULL) { //删除堆区的每个职工对象 for (int i=0;i<this->m_EmpNum;i++) { delete this->m_EmArray[i]; this->m_EmArray[i] = NULL; } //删除堆区数组指针 delete[] this->m_EmArray; this->m_EmArray = NULL; this->m_EmpNum = 0; this->m_FileIsEmpty = true; } cout << "清空成功!" << endl; } system("pause"); system("cls"); }
15.3 测试清空文件
在main函数分支7选项中,调用清空文件窗口
case 7://清空文档 wm.Clean_File(); break; default://清屏 system("cls"); break;
测试确认清空文件
再次查看文件中数据,记录已空
打开文件,里面数据已确保清空,该功能需要慎用
随着文件功能实现,本案例制作完毕~
-
-
C++职工管理系统.zip
2020-04-08 21:22:44退出管理程序:退出当前管理系统 增加职工信息:实现批量添加职工功能,将信息录入到文件中,职工信息为:编号,姓名,部门编号 显示职工信息:显示公司内部所有职工的信息 删除离职职工:按照编号修改职工个人信息 ... -
C++职工管理系统源代码工程(VS2019)
2021-08-30 15:28:53C++职工管理系统 -
C++职工管理系统源码.zip
2022-06-10 10:09:37C++职工管理系统源码.zip -
C++实现简单的职工管理系统实训代码
2020-09-02 12:42:02主要为大家详细介绍了C++职工管理系统实训代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
黑马C++职工管理系统
2021-10-07 15:49:03参考博客:链接 代码结构: ...//职工抽象类 class Worker { public: //显示个人信息 virtual void showInfo() = 0; //获取岗位名称 virtual string getDeptName() = 0; //职工编号 int m_Id参考博客:链接
代码结构:
以下是所有代码:
头文件:
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; };
boss.h
#pragma once #include<iostream> using namespace std; #include "worker.h" //Boss类 class Boss :public Worker { public: //构造函数 Boss(int id, string name, int did); //显示个人信息 void showInfo(); //获取岗位名称 string getDeptName(); };
manager.h
#pragma once #include<iostream> using namespace std; #include "worker.h" //经理类 class Manager :public Worker { public: //构造函数 Manager(int id, string name, int did); //显示个人信息 void showInfo(); //获取岗位名称 string getDeptName(); };
employee.h
//普通员工文件 #pragma once #include<iostream> using namespace std; #include "worker.h" class Employee :public Worker { public: //构造函数 Employee(int id, string name, int did); //显示个人信息 void showInfo(); //获取岗位名称 string getDeptName(); };
workerManager.h
#pragma once //放置头文件重复包含 #include<iostream> //包含输入输出流头文件 using namespace std; //使用标准命名空间 #include"worker.h" #include"employee.h" #include"manager.h" #include"boss.h" #include<fstream> #define FILENAME "empFile.txt" class WorkerManager { public: //构造函数 WorkerManager(); //展示菜单 void Show_Menu(); //退出系统 void ExitSystem(); //记录职工人数 int m_EmpNum; //职工数组指针 Worker** m_EmpArray; //添加职工 void Add_Emp(); //保存文件 void save(); //判断文件是否为空 标志 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(); //析构函数 ~WorkerManager(); };
源文件
boss.cpp#include<iostream> using namespace std; #include"boss.h" //构造函数 Boss::Boss(int id, string name, int did) { this->m_Id = id; this->m_Name = name; this->m_DeptId = did; } //显示个人信息 void Boss::showInfo() { cout << "职工编号:" << this->m_Id << "\t职工姓名:" << this->m_Name << "\t岗位:" << this->getDeptName() << "\t岗位职责:管理公司所有事务" << endl; } //获取岗位名称 string Boss::getDeptName() { return string("老板"); }
manager.cpp
#include<iostream> 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职工姓名:" << this->m_Name << "\t岗位:" << this->getDeptName() << "\t岗位职责:完成老板交给的任务,并且下发任务给员工" << endl; } //获取岗位名称 string Manager::getDeptName() { return string("经理"); }
employee.cpp
#include<iostream> 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职工姓名:" << this->m_Name << "\t岗位:" << this->getDeptName() << "\t岗位职责:完成经理交给的任务" << endl; } //获取岗位名称 string Employee::getDeptName() { return string("员工"); }
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(); //测试代码 //for (int i = 0; i < this->m_EmpNum; i++) { // cout << "职工编号:" << this->m_EmpArray[i]->m_Id // << "\t姓名:" << this->m_EmpArray[i]->m_Name // << "\t部门编号:" << this->m_EmpArray[i]->m_DeptId << 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::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 dSelect; //部门选择 cout << "请输入第" << i + 1 << "个新职工编号:" << endl; cin >> id; cout << "请输入第" << i + 1 << "个新职工姓名:" << endl; cin >> name; cout << "请选择该职工岗位:" << endl; cout << "1、普通职工" << endl; cout << "2、经理" << endl; cout << "3、老板" << endl; cin >> dSelect; Worker* worker = NULL; switch (dSelect) { 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; } //将创建的职工职责,保存到数组中 newSpace[this->m_EmpNum + i] = worker; } //释放原有的空间 delete[] this->m_EmpArray; //更改新空间的指向 this->m_EmpArray = newSpace; //更新新的职工人数 this->m_EmpNum = newSize; //更新职工不为空标志 this->m_FileIsEmpty = 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->m_EmpNum; i++) { ofs << this->m_EmpArray[i]->m_Id << " " << this->m_EmpArray[i]->m_Name << " " << 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++; } 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 { //老板 worker = new Boss(id, name, did); } this->m_EmpArray[index] = worker; index++; } //关闭文件 ifs.close(); } //显示职工 void WorkerManager::Show_Emp() { //判断文件是否为空 if (this->m_FileIsEmpty) { cout << "文件不存在或者记录为空!" << endl; } else { for (int i = 0; i < 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); 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--; //更新人员个数 //数据同步更新到文件中 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]->m_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) { //查找到编号的职工 delete this->m_EmpArray[ret]; int newId = 0; string newName = ""; int dSelect = 0; cout << "查到:" << id << "号职工,请输入新的职工号:" << endl; cin >> newId; cout << "请输入新姓名:" << endl; cin >> newName; cout << "请输入岗位:" << endl; cout << "1、普通职工" << endl; cout << "2、经理" << endl; cout << "3、老板" << endl; cin >> dSelect; Worker* worker = NULL; switch (dSelect) { case 1: worker = new Employee(newId, newName, dSelect); break; case 2: worker = new Manager(newId, newName, dSelect); break; case 3: worker = new Boss(newId, newName, dSelect); 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; } else { cout << "请选择查找的方式:" << endl; cout << "1、按职工编号查找:" << endl; cout << "2、按职工姓名查找:" << endl; int select = 0; cin >> select; if (select == 1) { //按照编号查 int id; cout << "请输入要查找的编号:" << endl; cin >> id; int ret = IsExist(id); if (ret != -1) { //找到职工 cout << "查找成功!该职工的信息如下:" << endl; this->m_EmpArray[ret]->showInfo(); } else { cout << "查找失败!查无此人!" << endl; } } else if (select == 2) { //按照姓名查 string name; cout << "请输入查找的姓名:" << endl; cin >> name; //加入判断查找是否查到的标志 bool flag = false; for (int i = 0; i < m_EmpNum; i++) { if (this->m_EmpArray[i]->m_Name == name) { cout << "查找成功!职工编号为:" << this->m_EmpArray[i]->m_Id << "号职工信息如下:" << endl; flag = true; //调用显示信息接口 this->m_EmpArray[i]->showInfo(); } } 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 select = 0; cin >> select; for (int i = 0; i < m_EmpNum; i++) { int minOrMax = i; //声明 最小值 或 最大值下标 for (int j = i + 1; j < this->m_EmpNum; j++) { if (select == 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 << "确定清空?" << endl; cout << "1、确定" << endl; cout << "2、返回" << endl; int select = 0; cin >> select; if (select == 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; } }
职工管理系统.cpp
#include<iostream> using namespace std; #include "workerManager.h" #include "worker.h" #include "employee.h" #include"manager.h" #include"boss.h" int main() { //测试代码: //Worker* worker = NULL; //worker = new Employee(1, "张三", 1); //worker->showInfo(); //delete worker; //worker = new Manager(2, "李四", 2); //worker->showInfo(); //delete worker; //worker = new Boss(3, "王五", 3); //worker->showInfo(); //delete worker; //return 0; //实例化管理者对象 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"); //按任意键继续 return 0; }
-
C++ 员工管理系统
2012-11-30 12:05:22用C++做的一个小系统,实现了保存等功能 -
C++实现简单职工管理系统
2021-01-01 14:11:52本文实例为大家分享了C++职工管理系统实例代码,供大家参考,具体内容如下 1.单个职工的头文件 staff.h #ifndef STAFF_H_INCLUDED #define STAFF_H_INCLUDED //结构体创建 struct staff { char ID[10]; char name...