#include" ;work_person.h"
work::work ()
{
ifstream ifs;
ifs.open (CONS, ios::in);
if (!ifs.is_open ())
{
cout << "文件为空" << endl;
this->m_empnum = 0;
this->m_emparray = NULL;
this->fileisempty = true;
ifs.close ();
return;
}
char ch;
ifs >> ch;
if (ifs.eof ())
{
cout << "文件为空" << endl;
this->m_empnum = 0;
this->m_emparray = NULL;
this->fileisempty = true;
ifs.close ();
return;
}
int num = this->get_empnum ();
cout << "职工的人数为" <<num<< endl;
this->m_empnum = num;
this->m_emparray = new worker * [this->m_empnum];
this->init_emp ();
}
void work::show_work ()
{
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 work::exit_work ()
{
cout << "欢迎下次使用" << endl;
exit (0);
}
void work::add_emp ()
{
cout << "请添加职工数量" << endl;
int addnum = 0;
cin >> addnum;
if (addnum>0)
{
int newsize = this->m_empnum + addnum;
worker ** newspce = new worker * [newsize];
if (this->m_emparray!=NULL)
{
for (int i = 0; i < this->m_empnum; i++)
{
newspce[i] = this->m_emparray[i];
}
}
for (int i = 0; i < addnum; i++)
{
int id;
string name;
int did;
cout << "请输入第" << i+1 << "个职工编号" << endl;
cin >> id;
cout << "请输入第" << i + 1 << "个职工姓名" << endl;
cin >> name;
cout << "请输入第" << i + 1 << "个职工岗位" << endl;
cout << "1、普通员工" << endl;
cout << "2、经理" << endl;
cout << "3、老板" << endl;
cin >> did;
worker* wor = NULL;
switch (did)
{
case 1:
wor = new worker_son1 (id, name, did);
break;
case 2:
wor = new worker_son2 (id, name, did);
break;
case 3:
wor = new worker_son2 (id, name, did);
break;
default:
break;
}
newspce[this->m_empnum + i] = wor;
}
delete[] this->m_emparray;
this->m_emparray = newspce;
this->m_empnum = newsize;
this->fileisempty = false;
cout << "成功添加" << addnum << "名职工" << endl;
work::save ();
}
else
{
cout << "输入有误" << endl;
}
system ("pause");
system ("cls");
}
void work::save ()
{
ofstream ofs;
ofs.open (CONS, 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 work::get_empnum ()
{
ifstream ifs;
ifs.open (CONS,ios::in);
int id;
string name;
int did;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> did)
{
num++;
}
ifs.close ();
return num;
}
void work::init_emp ()
{
ifstream ifs;
ifs.open (CONS, ios::in);
int id;
string name;
int did;
int index =0;
while (ifs >> id && ifs >> name && ifs >> did)
{
worker* wor = NULL;
if (did==1)
{
wor = new worker_son1 (id, name, did);
}
else if (did == 2)
{
wor = new worker_son2 (id, name, did);
}
else
{
wor = new worker_son3 (id, name, did);
}
this->m_emparray[index] = wor;
index++;
}
ifs.close ();
}
void work::show_emp ()
{
if (this->fileisempty)
{
cout << "还未录入职工信息" << endl;
}
else
{
for (int i = 0; i < this->m_empnum; i++)
{
this->m_emparray[i]->showinfo ();
}
}
system ("pause");
system ("cls");
}
void work::del_emp ()
{
if (this->fileisempty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
cout << "请输入要删除的职工编号" << endl;
int id;
cin >> id;
int index = isExist (id);
if (index !=-1)
{
for (int i = index; i < this->m_empnum -1; i++)
{
m_emparray[i] = m_emparray[i + 1];
}
this->m_empnum--;
this->save ();
cout << "删除成功" << endl;
}
else
{
cout << "删除失败,未找到该职工" << endl;
}
}
system ("pause");
system ("cls");
}
int work::isExist (int id)
{
int index = -1;
for (int i = 0; i < this->m_empnum; i++)
{
if (m_emparray[i]->m_id==id)
{
index = i;
break;
}
}
return index;
}
void work::mod_emp ()
{
if (this->fileisempty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
cout << "请输入要修改的职工编号" << endl;
int id;
cin >> id;
int ret = this->isExist (id);
if (ret != -1)
{
delete this->m_emparray[ret];
int new_id;
string new_name;
int new_did;
cout <<"查到:"<< id << "号职工,请输入新职工号" << endl;
cin >> new_id;
cout << "职工姓名" << endl;
cin >> new_name;
cout << "职工岗位" << endl;
cin >> new_did;
worker* wor = NULL;
switch (new_did)
{
case 1:
wor = new worker_son1 (new_id, new_name, new_did);
break;
case 2:
wor = new worker_son2 (new_id, new_name, new_did);
break;
case 3:
wor = new worker_son3 (new_id, new_name, new_did);
break;
default:
break;
}
m_emparray[ret] = wor;
cout << "修改成功" << endl;
this->save ();
}
else
{
cout << "修改失败,查无此人" << endl;
}
}
system ("pause");
system ("cls");
}
void work::find_emp ()
{
if (this->fileisempty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
cout << "1、按编号查找" << endl;
cout << "2、按姓名查找" << endl;
int id;
cin >> id;
if (id==1)
{
cout << "请输入要查找的职工编号" << endl;
int m_id;
cin >> m_id;
int ret = this->isExist (m_id);
if (ret != -1)
{
m_emparray[ret]->showinfo ();
}
else
{
cout << "查无此人" << endl;
}
}
else if (id == 2)
{
cout << "请输入要查找的职工姓名" << endl;
string name;
cin >> name;
bool flag = false;
for (int i = 0; i < this->m_empnum; i++)
{
if (m_emparray[i]->m_name==name)
{
cout << "您要找到的职工编号为:" << m_emparray[i]->m_id << "号信息如下:" << endl;
m_emparray[i]->showinfo ();
flag = true;
}
}
if (flag==false)
{
cout << "查无此人" << endl;
}
}
else
{
cout << "输入有误" << endl;
}
}
system ("pause");
system ("cls");
}
void work::sort_emp ()
{
if (this->fileisempty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
cout << "请选择排序方式" << endl;
cout << "1、升序" << endl;
cout << "2、降序" << endl;
int select;
cin >> select;
for (int i = 0; i < this->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 work::clean_File ()
{
cout << "确定要删除文件吗?" << endl;
cout << "1、确定" << endl;
cout << "2、返回" << endl;
int select;
cin >> select;
if (select==1)
{
ofstream ofs;
ofs.open (CONS, ios::trunc);
ofs.close ();
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];
this->m_emparray[i] = NULL;
}
}
delete[]this->m_emparray;
this->m_emparray = NULL;
this->m_empnum = 0;
this->fileisempty = true;
}
cout << "清空成功" << endl;
}
else
{
cout << "返回上层目录" << endl;
}
system ("pause");
system ("cls");
}
work::~work ()
{
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];
this->m_emparray[i] = NULL;
}
}
delete[] this->m_emparray;
this->m_emparray = NULL;
}
}
worker_son1::worker_son1 (int id, string name, int did)
{
this->m_id = id;
this->m_name = name;
this->m_deptid = did;
}
void worker_son1::showinfo ()
{
cout << "职工编号:" << this->m_id <<" "
<< "职工姓名:" << this->m_name << " "
<< "职工岗位:" << this->getdeptname () << " "
<< "岗位职责:完成经理交付的任务" << endl;
}
string worker_son1::getdeptname ()
{
return"员工";
}
worker_son2::worker_son2 (int id, string name, int did)
{
this->m_id = id;
this->m_name = name;
this->m_deptid = did;
}
void worker_son2::showinfo ()
{
cout << "职工编号:" << this->m_id << " "
<< "职工姓名:" << this->m_name << " "
<< "职工岗位:" << this->getdeptname () << " "
<< "岗位职责:完成老板交给的任务,并下发任务给员工" << endl;
}
string worker_son2::getdeptname ()
{
return"经理";
}
worker_son3::worker_son3 (int id, string name, int did)
{
this->m_id = id;
this->m_name = name;
this->m_deptid = did;
}
void worker_son3::showinfo ()
{
cout << "职工编号:" << this->m_id << " "
<< "职工姓名:" << this->m_name << " "
<< "职工岗位:" << this->getdeptname () << " "
<< "岗位职责:管理公司所有事物" << endl;
}
string worker_son3::getdeptname ()
{
return"老板";
}
</endl;