需求概述:
既然设计这个系统,就要考虑对应需求,职工管理系统,顾名思义,是为了管理职工而设计,系统中最基本的“增删改查”自然少不了。
首先设计头文件
首先,我们定义多个头文件(设立头文件的目的主要是:提供全局变量、全局函数的声明或公用数据类型的定义,从而实现分离编译和代码复用。 同时,也有利于强化安全性。)
分别如下(以下每个头文件与每种身份的人对应):
boss.h
#pragma once
#include<iostream>
using namespace std;
class boss :public worker
{
public:
boss(int id, string name, int post);
virtual void showinfo();
virtual string getpost();
};
employee.h
#pragma once
class employee :public worker
{
public:
employee(int id, string name, int post);
virtual void showinfo();
virtual string getpost();
};
manager.h
#pragma once
class Manager :public Worker
{
public:
Manager(int id, string name, int post);
virtual void ShowInfo();
virtual string getPost();
};
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();
};
worker.h
#pragma once
class worker
{
public:
virtual void showinfo() = 0;
virtual string getpost() = 0;
int m_id;
string m_name;
int m_post;
};
源文件如下:
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("老板");
}
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("员工");
}
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("经理");
}
workmanager.h
#include "worker manager.h"
worker_manager::Worker_Manager()
{
ifstream ifs;
ifs.open(TXT, Ios::in);
if (!ifs.is_open())
{
this->m_EmpNum = 0;
this->m_EmpArray = NULL;
this->m_File = true;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
this->m_EmpNum = 0;
this->m_EmpNum = NULL;
this->m_File = true;
ifs.close();
return;
}
int num = this->get_EmpNum();
this->m_EmpNum = num;
this->m_EmpArray = new ew 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_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;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。