公司员工信息管理系统:(使用C++实现)
1、输入:txt文本,包含员工信息、性别、年龄、工号、部门、职位(职员、部门经理、总经理)等信息。
2、要求功能: 添加、删除、修改员工信息;
员工显示排序(能够根据任意信息排序)。
根据某员工查询其上下级员工;
能够输出所有信息。
3、需要使用知识点:类继承、多态、vector/map容器。
源代码:
1、头文件:
#include
#include
#include
#include
#include
using namespace std;
#define EMP "employee"
#define MAG "manage"
#define BOS "boss"
#define Type_Employee 1
#define Type_Manage 2
#define Type_Boss 3
class persion
{
public:
virtual char GetType() = 0;
};
class employee : public persion //普通员工
{
public:
virtual char GetType(){ return Type_Employee; };
virtual ~employee(){};
void operator=(employee& b);
public:
string position; //职位
string name; //姓名
int age; //年龄
string gender; //性别
int number; //员工编号
string depart; //部门
};
class manage : public employee //部门经理:手机号、邮箱、负责部门
{
public:
virtual char GetType(){ return Type_Manage; }
virtual ~manage(){}
void operator=(manage& b);
public:
string tele;
string mail;
};
class boss : public employee //总经理:手机号、邮箱、通讯地址
{
public:
virtual char GetType(){ return Type_Boss; }
virtual ~boss(){}
void operator=(boss& b);
public:
string tele;
string mail;
string addr;
};
class ManageSystem
{
public:
ManageSystem();
~ManageSystem();
void Add(employee* pInfo);
size_t GetSize();
bool Delete(string& name);
char GetType(int& num);
char GetType(string& name);
bool GetInformation(int& num, employee** ppEmp);
bool GetInformation(string& name, employee** ppEmp);
void NumberSort();
void NameSort();
void ModifyDepart(string& type,string& depart);
void ModifyMail(string& type, string& mail);
void ModifyTele(string& type, string& tele);
void ModifyAddr(string& type, string& addr);
private:
size_t FindName(string& name);
size_t FindNumber(int& num);
private:
void LoadInformationToSys(); //文件加载员工信息----->系统
void LoadInformationToFile(); //系统加载员工信息----->文件
vector _employee;
FILE* pFile;
};
2、实现文件(.cpp)
#include "Manage_System.h"
void employee::operator=(employee& e)
{
this->age = e.age;
this->depart = e.depart;
this->gender = e.gender;
this->name = e.name;
this->number = e.number;
this->position = e.position;
}
void manage::operator=(manage& m)
{
this->age = m.age;
this->depart = m.depart;
this->gender = m.gender;
this->mail = m.mail;
this->name = m.name;
this->number = m.number;
this->tele = m.tele;
this->position = m.position;
}
void boss::operator=(boss& b)
{
this->addr = b.addr;
this->age = b.age;
this->depart = b.depart;
this->gender = b.gender;
this->mail = b.mail;
this->name = b.name;
this->number = b.number;
this->tele = b.tele;
this->position = b.position;
}
ManageSystem::ManageSystem()
{
LoadInformationToSys();
}
ManageSystem::~ManageSystem()
{
LoadInformationToFile(); //将信息加载到文件中
for (size_t i = 0; i < GetSize(); i++)
{
switch (_employee[i]->GetType())
{
case 1:
delete _employee[i];
break;
case 2:
delete (manage*)_employee[i];
break;
case 3:
delete (boss*)_employee[i];
break;
default:
break;
}
_employee[i] = NULL;
}
}
bool IsNumberString(string& str) //判断输入的字符串是否为数字字符串
{
int sz = str.size();
for (int i = 0; i < sz; i++)
{
if (str[i] < 48 || str[i] > 57)
{
return false;
}
}
return true;
}
string i_ReadPart(char* buf, int& index) //
{
string strRt = "";
char szTemp[64] = "";
int i = 0;
while (buf[index] != ' ' && buf[index] != '\n' && buf[index] != '\0')
{
szTemp[i++] = buf[index];
index++;
}
if (buf[index] == ' ')
{
index++;
}
strRt = szTemp;
return strRt;
}
void i_WritePart(vector& em, FILE* fout, int idx)
{
string sz = "";
char age[20] = { 0 };
char number[20] = { 0 };
itoa(em[idx]->age, age, 10);
itoa(em[idx]->number, number, 10);
sz += em[idx]->position;
sz += " ";
sz += em[idx]->name;
sz += " ";
sz += age;
sz += " ";
sz += em[idx]->gender;
sz += " ";
sz += number;
sz += " ";
sz += em[idx]->depart;
if (em[idx]->GetType() == Type_Employee)
{
sz += "\n";
}
if (em[idx]->GetType() == Type_Manage)
{
sz += " ";
sz += ((manage*)em[idx])->tele;
sz += " ";
sz += ((manage*)em[idx])->mail;
sz += "\n";
}
if (em[idx]->GetType() == Type_Boss)
{
sz += " ";
sz += ((boss*)em[idx])->tele;
sz += " ";
sz += ((boss*)em[idx])->mail;
sz += " ";
sz += ((boss*)em[idx])->addr;
sz += "\n";
}
fputs(sz.c_str(), fout);
}
void ReadFile(vector& _employee, char* buf, int index)
{
employee* emply = NULL;
char chType = 0;
string strTemp = "";
strTemp = i_ReadPart(buf, index);
if (strTemp == EMP)
{
emply = new employee();
chType = Type_Employee;
}
else if (strTemp == MAG)
{
emply = new manage();
chType = Type_Manage;
}
else if (strTemp == BOS)
{
emply = new boss();
chType = Type_Boss;
}
if (!emply) return; //不空
emply->position = strTemp; //职位已经提取出来
strTemp = i_ReadPart(buf, index);
if (!strTemp.empty()) emply->name = strTemp;
strTemp = i_ReadPart(buf, index);
if (!strTemp.empty()){ int age = atoi(strTemp.c_str()); emply->age = age; }
strTemp = i_ReadPart(buf, index);
if (!strTemp.empty()) emply->gender = strTemp;
strTemp = i_ReadPart(buf, index);
if (!strTemp.empty()){ int num = atoi(strTemp.c_str()); emply->number = num; }
strTemp = i_ReadPart(buf, index);
if (!strTemp.empty()) emply->depart = strTemp;
if (chType == Type_Manage)
{
strTemp = i_ReadPart(buf, index);
if (!strTemp.empty()) ((manage*)emply)->tele = strTemp;
strTemp = i_ReadPart(buf, index);
if (!strTemp.empty()) ((manage*)emply)->mail = strTemp;
}
if (chType == Type_Boss)
{
strTemp = i_ReadPart(buf, index);
if (!strTemp.empty()) ((boss*)emply)->tele = strTemp;
strTemp = i_ReadPart(buf, index);
if (!strTemp.empty()) ((boss*)emply)->mail = strTemp;
strTemp = i_ReadPart(buf, index);
if (!strTemp.empty()) ((boss*)emply)->addr = strTemp;
}
_employee.push_back(emply);
}
void ManageSystem::LoadInformationToSys() //加载员工信息到系统
{
pFile = fopen("EmployeeInformation.txt", "r");
assert(pFile);
char buf[128];
int index = 0;
while (!feof(pFile))
{
index = 0;
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pFile); //fgets会读取一行,并在最后加上'\0';
ReadFile(_employee, buf,index);
}
if (fclose(pFile) != 0) //关闭文件
{
cout << "关闭失败" << endl;
return;
}
}
void ManageSystem::LoadInformationToFile() //将信息加载到文件中
{
FILE* fout = fopen("EmployeeInformation.txt","w");
size_t i = 0;
while (i < GetSize())
{
i_WritePart(_employee,fout, i);
i++;
}
fclose(fout);
}
void ManageSystem::Add(employee* pInfo) //添加新员工
{
if (pInfo == NULL)
return;
char type = pInfo->GetType();
if (1 == type)
{
employee* pEm = new employee();
*pEm = *pInfo;
_employee.push_back(pEm);
}
if (2 == type)
{
employee* pManage = new manage();
*((manage*)pManage) = *((manage*)pInfo);
_employee.push_back(pManage);
}
if (3 == type)
{
employee* pBoss = new boss();
*((boss*)pBoss) = *((boss*)pInfo);
_employee.push_back(pBoss);
}
}
size_t ManageSystem::GetSize()
{
return _employee.size();
}
bool ManageSystem::Delete(string& name) //姓名或员工编号均可删除
{
int ret = -1;
int num = -1;
if (IsNumberString(name)) //输入是员工编号,,找下标
{
num = atoi(name.c_str());
ret = FindNumber(num);
}
else //输入是员工姓名,,找下标
{
ret = FindName(name);
}
if (ret == -1 )
return false;
delete _employee[ret];
_employee[ret] = NULL;
for (size_t i = ret; i < _employee.size() - 1; i++)
{
_employee[i] = _employee[i + 1];
}
_employee.pop_back();
return true;
}
size_t ManageSystem::FindName(string& name)
{
for (size_t i = 0; i < _employee.size(); i++)
{
if (name == _employee[i]->name)
{
return i;
}
}
return -1;
}
size_t ManageSystem::FindNumber(int& num)
{
for (size_t i = 0; i < _employee.size(); i++)
{
if (num == _employee[i]->number)
{
return i;
}
}
return -1;
}
char ManageSystem::GetType(int& num)
{
int nIndex = FindNumber(num);
if (nIndex != -1)
{
return _employee[nIndex]->GetType();
}
return 0;
}
char ManageSystem::GetType(string& name)
{
int nIndex = FindName(name);
if (nIndex != -1)
{
return _employee[nIndex]->GetType();
}
return 0;
}
bool ManageSystem::GetInformation(int& num, employee** ppEmp) //传编号
{
if (!ppEmp) return false;
int nIndex = FindNumber(num);
if (nIndex != -1)
{
if (GetType(num) == Type_Employee) **ppEmp = *(_employee[nIndex]);
if (GetType(num) == Type_Manage) *((manage*)(*ppEmp)) = *((manage*)_employee[nIndex]);
if (GetType(num) == Type_Boss) *((boss*)(*ppEmp)) = *((boss*)_employee[nIndex]);
return true;
}
return false;
}
bool ManageSystem::GetInformation(string& name, employee** ppEmp) //传编号
{
if (!ppEmp) return false;
int nIndex = FindName(name);
if (nIndex != -1)
{
if (GetType(name) == Type_Employee) **ppEmp = *(_employee[nIndex]);
if (GetType(name) == Type_Manage) *((manage*)(*ppEmp)) = *((manage*)_employee[nIndex]);
if (GetType(name) == Type_Boss) *((boss*)(*ppEmp)) = *((boss*)_employee[nIndex]);
return true;
}
return false;
}
void ManageSystem::NumberSort() //按员工编号排序
{
for (size_t i = 0; i < GetSize(); i++)
{
for (size_t j = 0; j < GetSize() - 1 - i; j++)
{
if (_employee[j]->number > _employee[j + 1]->number)
{
swap(_employee[j], _employee[j + 1]);
}
}
}
}
void ManageSystem::NameSort() //按姓名编号排序
{
for (size_t i = 0; i < GetSize(); i++)
{
for (size_t j = 0; j < GetSize() - 1 - i; j++)
{
if (strcmp(_employee[j]->name.c_str(), _employee[j + 1]->name.c_str()) > 0)
{
swap(_employee[j], _employee[j + 1]);
}
}
}
}
void ManageSystem::ModifyDepart(string& type,string& depart) //type为姓名或员工编号
{
if (IsNumberString(type))
{
int number = atoi(type.c_str());
_employee[FindNumber(number)]->depart = depart;
return;
}
_employee[FindName(type)]->depart = depart;
}
void ManageSystem::ModifyAddr(string& type,string& addr)
{
if (IsNumberString(type))
{
int number = atoi(type.c_str());
((boss*)_employee[FindNumber(number)])->addr = addr;
return;
}
((boss*)(_employee[FindName(type)]))->addr = addr;
}
void ManageSystem::ModifyMail(string& type,string& mail)
{
if (IsNumberString(type))
{
int number = atoi(type.c_str());
((manage*)_employee[FindNumber(number)])->mail = mail;
return;
}
((manage*)(_employee[FindName(type)]))->mail = mail;
}
void ManageSystem::ModifyTele(string& type, string& tele)
{
if (IsNumberString(type))
{
int number = atoi(type.c_str());
((manage*)_employee[FindNumber(number)])->tele = tele;
return;
}
((manage*)(_employee[FindName(type)]))->tele = tele;
}
3、测试文件(test.cpp)
#include "Manage_System.h"
struct Base
{
int in;
int mode = 0;
int number = -1;
int age = -1;
int newNumber;
int newAge;
char gender[10];
char name[20];
char position[20];
char depart[15];
char tele[15];
char mail[20];
char addr[25];
char type[20];
string _name;
string newTele;
string newMail;
string newAddr;
string newPosition;
string newDepart;
string newGender;
string _type;
};
Base Temp;
bool IsNumberString(string& str); //判断输入的字符串是否为数字字符串
enum
{
QUIT,
ADD,
DELETE,
MODFIY,
SORT,
FIND
};
void Menu()
{
cout << endl;
cout << " ==========菜单==========" << endl;
cout << "|" << " ";
cout << "1 add 2 delete " << "|" << endl;
cout << "|" << " ";
cout << "3 modfiy 4 sort " << "|" << endl;
cout << "|" << " ";
cout << "5 find 0 quit " << "|" << endl;
cout << " =========================" << endl;
}
void BaseInformation()
{
cout << "请输入姓名: ";
gets(Temp.name);
cout << "请输入年龄: ";
scanf("%d", &Temp.age);
cout << "请输入性别: ";
getchar();
gets(Temp.gender);
cout << "请输入员工编号: ";
scanf("%d", &Temp.number);
cout << "请输入部门: ";
getchar();
gets(Temp.depart);
}
void AddEmployee(ManageSystem& s)
{
employee* pEm = new employee;
BaseInformation();
pEm->name = Temp.name;
pEm->number = Temp.number;
pEm->gender = Temp.gender;
pEm->age = Temp.age;
pEm->position = Temp.position;
pEm->depart = Temp.depart;
s.Add(pEm);
delete pEm;
pEm = NULL;
}
void AddManage(ManageSystem& s)
{
BaseInformation();
cout << "请输入电话: ";
getchar();
gets(Temp.tele);
cout << "请输入邮箱: ";
gets(Temp.mail);
manage* pMa = new manage;
pMa->depart = Temp.depart;
pMa->name = Temp.name;
pMa->age = Temp.age;
pMa->tele = Temp.tele;
pMa->position = Temp.position;
pMa->number = Temp.number;
pMa->gender = Temp.gender;
pMa->mail = Temp.mail;
s.Add(pMa);
delete pMa;
}
void AddBoss(ManageSystem& s)
{
boss* pBo = new boss;
BaseInformation();
cout << "请输入电话: ";
gets(Temp.tele);
cout << "请输入邮箱: ";
gets(Temp.mail);
cout << "请输入地址: ";
gets(Temp.addr);
pBo->addr = Temp.addr;
pBo->age = Temp.age;
pBo->mail = Temp.mail;
pBo->tele = Temp.tele;
pBo->depart = Temp.depart;
pBo->position = Temp.position;
pBo->number = Temp.number;
pBo->gender = Temp.gender;
pBo->name = Temp.name;
s.Add(pBo);
delete pBo;
}
void Delete(ManageSystem& s) //删除
{
getchar();
cout << "请输入要删除的姓名或员工编号:" << endl;
gets(Temp.name);
Temp._name = Temp.name;
if (s.Delete(Temp._name) == true)
cout << "删除成功!!!" << endl;
else
cout << "你要删除的名字不存在!!!" << endl;
}
void Print(employee* pEm)
{
if (pEm == NULL)
{
cout << "你要查找的员工不存在!!!" << endl;
return;
}
cout << pEm->position << " " << pEm->name << " " << pEm->gender << " " << pEm->depart
<< " " << pEm->number << " " << pEm->age ;
if (pEm->GetType() == Type_Employee) { cout << endl; return; }
if (pEm->GetType() == Type_Manage)
{
cout << " " << ((manage*)pEm)->tele << " " << ((manage*)pEm)->mail << endl;;
return;
}
if (pEm->GetType() == Type_Boss)
{
cout << " " << ((boss*)pEm)->tele << " " << ((boss*)pEm)->mail << " " << ((boss*)pEm)->addr << endl;
return;
}
}
void ShowInfo(ManageSystem& s, string& str)
{
if (IsNumberString(str)) //是数字字符串的话,转成数字
{
int num = atoi(str.c_str());
if (s.GetType(num) == Type_Employee)
{
employee* pEm = new employee();
bool ret = s.GetInformation(num, &pEm);
if (ret == false)
{
cout << "输入错误!!!" << endl;
return;
}
Print(pEm);
delete pEm;
return;
}
if (s.GetType(num) == Type_Manage)
{
manage* pMa = new manage();
bool ret = s.GetInformation(num, (employee**)&pMa);
if (ret == false)
{
cout << "输入错误!!!" << endl;
return;
}
Print(pMa);
delete pMa;
return;
}
if (s.GetType(num) == Type_Boss)
{
boss* pBo = new boss();
bool ret = s.GetInformation(num, (employee**)&pBo);
if (ret == false)
{
cout << "输入错误!!!" << endl;
return;
}
Print(pBo);
delete pBo;
return;
}
}
if (s.GetType(str) == Type_Employee)
{
employee* pEm = new employee();
bool ret = s.GetInformation(str, &pEm);
if (ret == false)
{
cout << "输入错误!!!" << endl;
return;
}
Print(pEm);
delete pEm;
return;
}
if (s.GetType(str) == Type_Manage)
{
manage* pMa = new manage();
bool ret = s.GetInformation(str, (employee**)&pMa);
if (ret == false)
{
cout << "输入错误!!!" << endl;
return;
}
Print(pMa);
delete pMa;
return;
}
if (s.GetType(str) == Type_Boss)
{
boss* pBo = new boss();
bool ret = s.GetInformation(str, (employee**)&pBo);
if (ret == false)
{
cout << "输入错误!!!" << endl;
return;
}
Print(pBo);
delete pBo;
return;
}
cout << "输入错误!!!" << endl;
}
void Show(ManageSystem& s)
{
cout << "请输入要查看的员工姓名或编号:" << endl;
char ch[20] = { 0 };
getchar();
gets(ch);
string str = ch; //str为员工姓名或员工编号
ShowInfo(s, str);
}
void ModflyInformation(ManageSystem& s, int num, string& input) //num为修改的内容,input为姓名或编号
{
if (num == 1)
{
cout << "请输入修改后的部门->:" << endl;
getchar();
gets(Temp.depart);
Temp.newDepart = Temp.depart;
s.ModifyDepart(input, Temp.newDepart);
}
if (num == 2)
{
cout << "请输入修改后邮箱->:" << endl;
getchar();
gets(Temp.mail);
Temp.newDepart = Temp.mail;
s.ModifyMail(input, Temp.newDepart);
}
if (num == 3)
{
cout << "请输入修改后电话->:" << endl;
getchar();
gets(Temp.tele);
Temp.newTele = Temp.tele;
s.ModifyMail(input, Temp.newTele);
}
if (num == 4)
{
cout << "请输入修改后地址->:" << endl;
getchar();
gets(Temp.addr);
Temp.newAddr = Temp.addr;
s.ModifyMail(input, Temp.newAddr);
}
}
void Modfly(ManageSystem& s)
{
cout << "请输入要修改的员工姓名或员工编号->:" << endl;
getchar();
gets(Temp.type);
Temp._type = Temp.type;
cout << "基本信息: " << endl;
ShowInfo(s, Temp._type);
cout << "请选择修改内容: " << endl;
cout << "1、修改部门 2、修改邮箱 3、修改电话 4、修改地址 " << endl;
scanf("%d", &Temp.mode);
ModflyInformation(s, Temp.mode, Temp._type);
}
void Sort(ManageSystem& s)
{
int ret = -1;
cout << "请选择排序类型: 1、按员工姓名排序 2、按员工编号排序" << endl;
scanf("%d", &ret);
switch (ret)
{
case 1: s.NameSort(); break;
case 2: s.NumberSort(); break;
default:
break;
}
}
void Add(ManageSystem& s)
{
cout << "请输入员工职位->: ";
getchar();
gets(Temp.position);
if (strcmp(Temp.position, EMP) == 0) AddEmployee(s); //添加员工
if (strcmp(Temp.position, MAG) == 0) AddManage(s); //添加经理
if (strcmp(Temp.position, BOS) == 0) AddBoss(s); //添加老板
}
int main()
{
ManageSystem s;
while (1)
{
Menu();
int input = 0;
cout << "请输入操作: "<