您好,登录后才能下订单哦!
这篇文章主要介绍了基于C++如何实现职工管理系统的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇基于C++如何实现职工管理系统文章都会有所收获,下面我们一起来看看吧。
职工管理系统可以用来管理公司内所有员工的信息
利用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++如何实现职工管理系统”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“基于C++如何实现职工管理系统”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。