职工三类(部门编号):普通职工,经理,老板
职工编号,职工姓名,职工岗位,指责
普通职工 :完成经理交付任务
经理 :完成老板交付任务,并下发任务给员工
老板职责 :管理公司所有事务
1.退出管理程序
2.添加职工信息 实现批量添加,支持信息录入文件,(编号,姓名,部门编号)
3.显示职工信息
4.删除离职职工 按编号
5.修改职工信息 按编号
6.查找职工信息 按编号 或 姓名
7.按照编号排序 升序 降序
8.清空所有文档 (再确认功能)
效果
类内结构体
Code:
workerManager.h
#pragma once
#include<iostream>
#include<fstream>
#include"worker.h"
#define FILENAME "empFile.txt"
using namespace std;
class WorkerManager
{
private:
int m_EmpNum; //记录文件中人数
Worker** m_EmpArray; //员工指针数组 的指针
bool m_FileIsEmpty; //文件是否为空 的标示(1空 0非空)
bool FileIsEmpty(); //文件是否为空 的标识函数
bool IdUnique(int id); //ID唯一限制
void SelectSort(int s); //选择排序,s = 1升序 / 2降序
void init_Data(); //初始化数据
public:
WorkerManager(/* args */);
void Show_Menu(); //Menu
void ExitSys_0(); //00Exit
void Add_Emp_1(); //01添加员工
void saveData(); //数据txt 写入
int get_EmpNum(); //统计txt 的成员个数
void init_Emp(); //读取员工数据 到数组中
void show_Emp_2(); //02显示员工
void Del_Emp_3(); //03删除员工
int IsExist(int id); //判断职工ID是否存在( -1 不存在)
void Mod_Emp_4(); //04修改员工
void Find_Emp_5(); //05查找员工
void Sort_Emp_6(); //06选择排序
void Clea_Emp_7(); //07清空
~WorkerManager();
};
worker.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Worker
{
private:
int m_Id;
string m_Name;
int m_DeptId;
public:
int getId();
string getName();
int getDeptId();
void setFunc(int id, string name, int did);
virtual void showInfo() = 0; //显示个人信息
virtual string getDeptName() = 0; //获取岗位信息
//Worker(/* args */);
//~Worker();
};
worker.cpp
#include"worker.h"
int Worker::getId() {
return m_Id;
}
string Worker::getName() {
return m_Name;
}
int Worker::getDeptId() {
return m_DeptId;
}
void Worker::setFunc(int id, string name, int did) {
m_Id = id;
m_Name = name;
m_DeptId = did;
}
workerManager.cpp
#include"workerManager.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
WorkerManager::WorkerManager() {
//初始化属性
ifstream ifs;
if (this->FileIsEmpty())
{
cout << "FILE is NOT Exist!!!" << endl << "ReLoading..." << endl;
//初始化
this->m_EmpNum = 0;
this->m_EmpArray = NULL;
//this->m_FileIsEmpty = 1;
ifs.close();
return;
}
//3.文件存在,存在记录数据 初始化
this->m_EmpNum = this->get_EmpNum(); //获取职工人数,并更新员工人数
this->m_EmpArray = new Worker * [this->m_EmpNum]; //创建数组指针 并初始化长度,开辟空间
this->init_Emp(); //初始化数据,存到数组中
cout << "Staff Number : " << this->m_EmpNum << endl;
}
void WorkerManager::Show_Menu() {
for (int i = 0; i < 10; i++) {
if (0 == i || 9 == i)
cout << "**********************" << endl;
else if (i < 8 && i>0) {
cout << "******" << i;
if (1 == i)cout << "-Add\t";
if (2 == i)cout << "-Show\t";
if (3 == i)cout << "-Del\t";
if (4 == i)cout << "-Modify\t";
if (5 == i)cout << "-Search\t";
if (6 == i)cout << "-Sort\t";
if (7 == i)cout << "-Clean\t";
cout << "******" << endl;
}
else
cout << "******" << i - 8 << "-Quit\t" << "******" << endl;
}
cout << "Please Input Your Choice:" << endl;
}
void WorkerManager::ExitSys_0() {
cout << "Bye Bye!" << endl;
system("pause");
exit(0);
}
void WorkerManager::Add_Emp_1() {
int addNum = 0; //用户输入数量
cout << "Please Input the number of Staff which you want to add : ";
cin >> addNum;
if (addNum > 0) {
int id = -1;
string name;
int did = -1;
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++)
{
cout << "Please Input Num" << i + 1 << " Staff's ID:" << endl;
while (1) {
cin >> id;
if (this->IdUnique(id))
break;
else
cout << "ID is Exist, Input Again!" << endl;
}
cout << "Please Input Num" << i + 1 << " Staff's Name:" << endl;
cin >> name;
cout << "Please Input Num" << i + 1 << " Staff's Post" << endl
<< "(1.Empolyee ; 2.Manager ; 3.Boss)" << endl;
cin >> did;
Worker* worker = NULL;
switch (did)
{
case 1:
worker = new Employee(id, name, did);
case 2:
worker = new Manager(id, name, did);
case 3:
worker = new Boss(id, name, did);
default:
break;
}
newSpace[this->m_EmpNum + i] = worker; //根据原有的数据位置进行递增存储
}
delete[] this->m_EmpArray; //释放 原有数组空间
this->m_EmpArray = newSpace; //更改 新空间指向
this->m_EmpNum = newSize; //更新 新职工人数
//this->m_FileIsEmpty = 0; //更新 有职工标识
this->saveData(); //保存数据 在文件中
cout << "Done,Adding " << addNum << " New Staff" << endl;
}
else
{
cout << "ERROE input" << endl;
}
system("pause"); //任意键回到上级目录
system("cls");
}
void WorkerManager::saveData() {
ofstream ofs;
ofs.open(FILENAME, ios::out); //输出打开 写文件
for (int i = 0; i < this->m_EmpNum; i++) //每个人数据写入
{
ofs << this->m_EmpArray[i]->getId() << " "
<< this->m_EmpArray[i]->getName() << " "
<< this->m_EmpArray[i]->getDeptId() << endl;
}
ofs.close();
}
int WorkerManager::get_EmpNum() {
ifstream ifs;
int EmpNum = 0;
int id;
string name;
int did;
ifs.open(FILENAME, ios::in);
while (ifs >> id && ifs >> name && ifs >> did)
EmpNum++; //统计人数
ifs.close();
return EmpNum;
}
void WorkerManager::init_Emp() {
ifstream ifs;
int index = 0;
int id;
string name;
int did;
ifs.open(FILENAME, ios::in);
while (ifs >> id && ifs >> name && ifs >> did)
{
Worker* worker = NULL;
if (1 == did)
worker = new Employee(id, name, did);
else if (2 == did)
worker = new Manager(id, name, did);
else if (3 == did)
worker = new Boss(id, name, did);
else
cout << "ERROR init_Emp()" << endl;
this->m_EmpArray[index++] = worker;
}
ifs.close();
//this->init_Data();
}
void WorkerManager::show_Emp_2() {
if (this->FileIsEmpty())
{
cout << "FILE is NOT Exist!!!" << endl;
}
else {
this->init_Emp();
for (int i = 0; i < m_EmpNum; i++) { //利用多态调用程序接口
this->m_EmpArray[i]->showInfo();
}
}
system("pause");
system("cls");
}
bool WorkerManager::FileIsEmpty() {
ifstream ifs;
char ch;
ifs.open(FILENAME, ios::in); //读文件
ifs >> ch; //文件为空时,文件尾会有一个字符,ch读出接收后再用eof()判断是否为空
if ((!ifs.is_open()) || (ifs.eof())) { //1.未找到文件txt 2.文件为空
m_FileIsEmpty = 1;
}
else
m_FileIsEmpty = 0;
ifs.close();
return m_FileIsEmpty;
}
void WorkerManager::Del_Emp_3() {
if (this->FileIsEmpty())
cout << "FILE is NOT Exist!!!" << endl;
else { //按职工编号删除
int id = -1;
cout << "Please Input DEL_Staff's ID :" << endl;
cin >> id;
int index = this->IsExist(id);
if (-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->saveData();
cout << "Done! Del_Dtaff which id is " << id << endl;
}
else {
cout << "Staff is NOT Exist , Del_ERROR" << endl;
}
system("pause");
system("cls");
}
}
int WorkerManager::IsExist(int id) {
int index = -1;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (id == this->m_EmpArray[i]->getId()) {
index = i;
break;
}
}
return index;
}
void WorkerManager::Mod_Emp_4() {
if (this->FileIsEmpty())
cout << "FILE is NOT Exist!!!" << endl;
else {
int id = -1;
cout << "Please Input Mod_Staff's ID :" << endl;
cin >> id;
int index = this->IsExist(id);
if (-1 != index) { //存在 执行修改
int mod_id;
string mod_name;
int mod_did;
delete this->m_EmpArray[index];
cout << "Find ID : " << id << endl;
cout << "Please Input Staff's NewID:" << endl;
cin >> mod_id;
cout << "Please Input Staff's NewName:" << endl;
cin >> mod_name;
cout << "Please Input Staff's NewPost" << endl
<< "(1.Empolyee ; 2.Manager ; 3.Boss)" << endl;
cin >> mod_did;
Worker* worker = NULL;
switch (mod_did)
{
case 1:
worker = new Employee(mod_id, mod_name, mod_did);
case 2:
worker = new Manager(mod_id, mod_name, mod_did);
case 3:
worker = new Boss(mod_id, mod_name, mod_did);
default:
break;
}
this->m_EmpArray[index] = worker;
this->saveData();
cout << "Done! Modified Id : " << mod_id << endl;
}
else {
cout << "Staff is NOT Exist , Mod_ERROR" << endl;
}
system("pause");
system("cls");
}
}
void WorkerManager::Find_Emp_5() {
if (this->FileIsEmpty())
cout << "FILE is NOT Exist!!!" << endl;
else {
int select = -1;
int id;
bool flag = 0; //查找标识 默认未找到0
string name;
cout << "Please Choice the way to search :" << endl
<< "(1.ID Search)" << endl
<< "(2.Name Search)" << endl;
cin >> select;
if (1 == select) {
cout << "Please Input Searching ID:" << endl;
cin >> id;
int index = this->IsExist(id);
if (-1 != index) {
cout << "Find it! Data :" << endl;
this->m_EmpArray[index]->showInfo(); //多态 纯虚函数,子类重写父类即可
}
else {
cout << "Staff is NOT Exist , Search_ERROR" << endl;
}
}
else if (2 == select) {
cout << "Please Input Searching Name:" << endl;
cin >> name;
for (int i = 0; i < m_EmpNum; i++) {
if (name == this->m_EmpArray[i]->getName()) {
flag = 1;
cout << "Find it! Data :" << endl;
this->m_EmpArray[i]->showInfo(); //多态 纯虚函数,子类重写父类即可
}
}
if (0 == flag)
cout << "Staff is NOT Exist , Search_ERROR" << endl;
}
else
cout << "Choice ERROR!!!" << endl;
}
system("pause");
system("cls");
}
bool WorkerManager::IdUnique(int id) {
if (this->FileIsEmpty())
return 1;
else {
if (-1 == this->IsExist(id))
return 1;
else
return 0;
}
}
void WorkerManager::Sort_Emp_6() {
if (this->FileIsEmpty())
cout << "FILE is NOT Exist!!!" << endl;
else {
int select = -1;
cout << "Please Choice the way to Sort :" << endl
<< "(1.Ascend)" << endl
<< "(2.Descend)" << endl;
cin >> select;
this->SelectSort(select);
cout << "Done! Sorted!" << endl;
this->show_Emp_2();
}
system("pause");
system("cls");
}
void WorkerManager::SelectSort(int select) {
for (int i = 0; i < this->m_EmpNum; i++) {
int minOrMax = i;
for (int j = i + 1; j < this->m_EmpNum; j++) {
if (1 == select) { //升序
if (this->m_EmpArray[minOrMax]->getId() > this->m_EmpArray[j]->getId())
minOrMax = j;
}
else { //降序
if (this->m_EmpArray[minOrMax]->getId() < this->m_EmpArray[j]->getId())
minOrMax = j;
}
}
if (i != minOrMax) {
Worker* temp = this->m_EmpArray[i];
this->m_EmpArray[i] = this->m_EmpArray[minOrMax];
this->m_EmpArray[minOrMax] = temp;
}
}
this->saveData();
}
void WorkerManager::init_Data() {
//升序
this->SelectSort(1);
//顺序去重
for (int i = 0; i < this->m_EmpNum; i++) { //每次循环判断m_Array[i]的值是否重复
for (int j = i + 1; j < this->m_EmpNum;) { //暂时不给出循环条件,若出现重复数据,
if (this->m_EmpArray[i] == this->m_EmpArray[j]) { //判断某一个数据和后面的数据是否相同
for (int m = j; m < this->m_EmpNum - 1; m++) { //若相同,将后面的数据向前移动,覆盖前面的数据,则数组长度减小,即长度变为len - 1
this->m_EmpArray[m] = this->m_EmpArray[m + 1];
}
(this->m_EmpNum)--; //循环条件,数据长度减一
}
else { //如果某一个数据与后面的数据不同,则这个数据与后面数据的后面一个数据比较是否相同
j++;
}
}
}
this->saveData();
}
void WorkerManager::Clea_Emp_7() {
int select = -1;
cout << "Please For sure to Clean :" << endl
<< "(1.Yes)" << endl
<< "(2.No)" << endl;
cin >> select;
if (1 == select){
ofstream ofs(FILENAME, ios::trunc); //删除文件后重新创建
ofs.close();
if (0 == this->FileIsEmpty()){ //删除堆区每个职工对象
for (int i = 0; i < this->m_EmpNum; i++) {
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
delete[] this->m_EmpArray; //删除堆区数据指针
this->m_EmpArray = NULL;
this->m_EmpNum = 0;
cout << "Done! Clean ALL!" << 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;
}
}
WorkerStaff_sys.cpp
#include<iostream>
#include<string>
#include"workerManager.h"
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
using namespace std;
/*
职工三类(部门编号):普通职工,经理,老板
职工编号,职工姓名,职工岗位,指责
普通职工 :完成经理交付任务
经理 :完成老板交付任务,并下发任务给员工
老板职责 :管理公司所有事务
1.退出管理程序
2.添加职工信息 实现批量添加,支持信息录入文件,(编号,姓名,部门编号)
3.显示职工信息
4.删除离职职工 按编号
5.修改职工信息 按编号
6.查找职工信息 按编号 或 姓名
7.按照编号排序 升序 降序
8.清空所有文档 (再确认功能)
*/
int main() {
WorkerManager wm; //实例化对象
while (1) {
int select = -1;
wm.Show_Menu(); //展示菜单
cin >> select;
switch (select)
{
case 0: //Quit
wm.ExitSys_0();
break;
case 1: //Add
wm.Add_Emp_1();
break;
case 2: //Show
wm.show_Emp_2();
break;
case 3: //Del
wm.Del_Emp_3();
break;
case 4: //Modify
wm.Mod_Emp_4();
break;
case 5: //Search
wm.Find_Emp_5();
break;
case 6: //Sort
wm.Sort_Emp_6();
break;
case 7: //clean
wm.Clea_Emp_7();
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}