//职工管理系统
//公司中职工分为三类:普通员工、经理、董事长,显示信息时,需要显示职工编号、
// 职工姓名、职工岗位、以及职责。
#include<iostream>
#include<fstream>
using namespace std;
#define File "file1.dat"//操作文件
class Worker//员工基类
{
public:
Worker(int d, string nam, int dI) :Id(d), name(nam), depty_Id(dI) {}
virtual void Show_information() = 0;//显示个人信息
string Get_deptyname(int& depty_Id);//获取岗位名称
int Id;//职工编号
string name;//职工姓名
int depty_Id;//职工所在部门名称编号
};
string Worker::Get_deptyname(int& depty_Id)//获取岗位名称
{
switch (depty_Id)
{
case 1:
return "普通员工";
break;
case 2:
return"经理";
break;
case 3:
return "董事长";
break;
default:
break;
}
}
class Employee :public Worker//普通员工类
{
public:
Employee(int d, string nam, int dI) :Worker(d, nam, dI) {}
void Show_information();
};
void Employee::Show_information()
{
cout << "职工编号:" << this->Id << '\t';
cout << "职工姓名:" << this->name << '\t';
cout << "职工岗位:" << this->Get_deptyname(depty_Id) << endl;
}
class Employee_manager :public Worker//经理类
{
public:
Employee_manager(int d, string nam, int dI) :Worker(d, nam, dI) {}
void Show_information();
};
void Employee_manager::Show_information()
{
cout << "职工编号:" << this->Id << '\t';
cout << "职工姓名:" << this->name << '\t';
cout << "职工岗位:" << this->Get_deptyname(depty_Id) << endl;
}
class Employer :public Worker//董事长类
{
public:
Employer(int d, string nam, int dI) :Worker(d, nam, dI) {}
void Show_information();
};
void Employer::Show_information()
{
cout << "职工编号:" << this->Id << '\t';
cout << "职工姓名:" << this->name << '\t';
cout << "职工岗位:" << this->Get_deptyname(depty_Id) << endl;
}
class System_Manager
{
public:
System_Manager();
void Menu_display();//显示菜单
void Exit_system();//退出系统
int Employ_num;//记录职工人数
Worker** Employ_arr;//职工记载数组
void Add_Employ();//添加职员信息
void Save_Information();//保存职员信息
int Get_Employnum();//统计文件记载信息的职工人数
void Get_Employarr();//拷贝文件中记载的职工信息
void Show_Employinformation();//显示所有已记载的职工信息
void Delete_Employinformation();//按照编号删除指定的职工信息
void Modify_Employinformation();//修改按照编号指定的职工信息
void Search_Employinformation();//按照编号查找指定职工信息并显示
void Sort_Employinformation();//按照编号排序职工信息
void Clean_Empoyinformation();//清空存储文件中的所有职工信息数据
~System_Manager();
};
System_Manager::System_Manager()
{
ifstream ifile(File, ios::in);
if (!ifile)//文件不存在的情况
{
cout << "文件不存在" << endl;
Employ_num = 0;
Employ_arr = NULL;
ifile.close();
return;
}
char c;//文件存在但无记录的情况
ifile >> c;
if (ifile.eof())
{
cout << "文件存在但空记录" << endl;
Employ_num = 0;
Employ_arr = NULL;
ifile.close();
return;
}
else//文件存在且有记录的情况
{
cout << "文件存在且有记录" << endl;
Employ_num = Get_Employnum();
cout << "已记录信息的职工人数为:" << Get_Employnum();
Employ_arr =new Worker * [Employ_num];
Get_Employarr();
ifile.close();
}
}
void System_Manager::Menu_display()
{
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;
cout << "*********************************" << endl;
cout << endl;
}
void System_Manager::Exit_system()
{
cout << "欢迎下次使用" << endl;
exit(0);
}
void System_Manager::Add_Employ()
{
cout << "请输入需要批量添加职工信息的数量" << endl;
int Add_num = 0;//所需批量添加的职工信息数量
cin >> Add_num;
if (Add_num > 0)//添加职工信息
{
int newsize = this->Employ_num + Add_num;
Worker** newspace = new Worker * [newsize];//堆区上开辟空间
if (this->Employ_arr != NULL)//将原空间数据拷贝到新空间下
{
for (int i=0; i < this->Employ_num; i++)
{
newspace[i] = this->Employ_arr[i];
}
}
for (int i = 0;i < Add_num;i++)//批量添加职工信息
{
int id;//职工编号
string nam;//职工姓名
int depty_select;//部门选择
cout << "请输入第" << i + 1 << "个新职工编号:" << endl;
cin >> id;
cout << "请输入第" << i + 1 << "个新职工姓名:" << endl;
cin >> nam;
cout << "请选择第" << i + 1 << "个新职工岗位:" << endl;
cout << "1.普通员工" << endl;
cout << "2.经理" << endl;
cout << "3.董事长" << endl;
cin >> depty_select;
Worker* worker = NULL;
switch (depty_select)
{
case 1:
worker = new Employee(id, nam, 1);
break;
case 2:
worker = new Employee_manager(id, nam, 2);
break;
case 3:
worker = new Employee_manager(id, nam, 3);
break;
default:
break;
}
newspace[this->Employ_num + i] = worker;//在新开辟空间存储新添加职工信息
}
delete[] this->Employ_arr;//更新存储信息
this->Employ_arr = newspace;
this->Employ_num = newsize;
cout << "成功添加" << Add_num << "名职工信息!" << endl;
cout << "职工信息保存到文件" << endl;
Save_Information();//数据保存在文件中
}
else
{
cout << "不允许添加人数小于0的职工信息" << endl;
}
}
void System_Manager::Save_Information()
{
ofstream ofile(File, ios::out);//文本文件读写
if (!ofile)
{
cout << "文件打开失败" << endl;
perror("错误原因:");
exit(0);
}
for (int i = 0;i < Employ_num; i++)//向文件中输入职工信息并保存
{
ofile << Employ_arr[i]->Id << " " << Employ_arr[i]->name << " " << Employ_arr[i]->depty_Id << endl;
}
ofile.close();//关闭文件
}
int System_Manager::Get_Employnum()
{
ifstream ifile(File, ios::in);
int id;//职工编号
string nam;//职工姓名
int depty_select;//部门选择
int num = 0;//记录职工人数
while (ifile >> id && ifile >> nam && ifile>>depty_select)//读文件
{
num++;
}
ifile.close();
return num;
}
void System_Manager::Get_Employarr()
{
ifstream ifile(File, ios::in);
int id;//职工编号
string nam;//职工姓名
int depty_select;//部门选择
int num = 0;//记录职工人数
while (ifile >> id && ifile >> nam && ifile >> depty_select)//读文件
{
Worker* worker = NULL;
switch (depty_select)
{
case 1:
worker = new Employee(id, nam, 1);
break;
case 2:
worker = new Employee_manager(id, nam, 2);
break;
case 3:
worker = new Employee_manager(id, nam, 3);
break;
default:
break;
}
Employ_arr[num] = worker;
num++;
}
ifile.close();
}
void System_Manager::Show_Employinformation()
{
for (int i = 0;i < Employ_num;i++)
{
Employ_arr[i]->Show_information();
}
}
void System_Manager::Delete_Employinformation()
{
cout << "请输入所要删除职工信息的编号:" << endl;
int del_id;
cin >> del_id;
for (int i = 0;i < Employ_num;i++)//查找用户输入的所指定职工是否存在,再进行删除操作
{
if (Employ_arr[i]->Id == del_id)
{
cout << "所指定的职工信息存在,进行删除" << endl;
for (int j = i;j < Employ_num ;j++)
{
Employ_arr[j] = Employ_arr[j + 1];
}
Employ_num--;
Save_Information();//更新保存到文件中
cout << "删除操作成功" << endl;
}
else
{
cout << "所指定的职工信息不存在,删除操作失败" << endl;
}
}
}
void System_Manager::Modify_Employinformation()
{
cout << "请输入所要修改职工信息的编号:" << endl;
int modi_id;
cin >> modi_id;
for (int i = 0;i < Employ_num;i++)//查找用户输入的所指定职工是否存在,再进行修改操作
{
if (Employ_arr[i]->Id == modi_id)
{
cout << "所指定的职工信息存在,进行修改信息" << endl;
int n = 0;
do
{
cout << "请输入所要修改的信息项" << endl;
cout << "********************" << endl;
cout << "*****1.职工编号*****" << endl;
cout << "*****2.职工姓名*****" << endl;
cout << "*****3.职工岗位*****" << endl;
cout << "********************" << endl;
int choose;
cin >> choose;
int id;//职工编号
string nam;//职工姓名
int depty_select;//部门选择
switch (choose)
{
case 1:
cout << "请输入新职工编号:" << endl;
cin >> id;
Employ_arr[i]->Id = id;
break;
case 2:
cout << "请输入新职工姓名:" << endl;
cin >> nam;
Employ_arr[i]->name = nam;
break;
case 3:
cout << "请输入新职工岗位编号(1.普通员工/2.经理/3.董事长):" << endl;
cin >> depty_select;
Employ_arr[i]->depty_Id = depty_select;
break;
default:
break;
}
cout << "该职工信息是否修改完毕:(是/否)" << endl;
string chioce;
cin >> chioce;
if (chioce == "是")
{
n = 0;
}
else if (chioce == "否")
{
n = 1;
}
else {
n = 0;
cout << "输入选项有误,系统默认用户输入‘否’" << endl;
}
} while (n);
Save_Information();//更新保存到文件中
cout << "修改信息操作成功" << endl;
}
else
{
cout << "所指定的职工信息不存在,修改信息操作失败" << endl;
}
}
}
void System_Manager::Search_Employinformation()
{
cout << "请输入所要修改职工信息的编号:" << endl;
int sear_id;
cin >> sear_id;
for (int i = 0;i < Employ_num;i++)//查找用户输入的所指定职工是否存在,再进行查找操作
{
if (Employ_arr[i]->Id == sear_id)
{
cout << "所指定的职工信息查找" << endl;
cout << "所指定的职工信息位于文件中第" << i + 1 << "处" << endl;
Employ_arr[i]->Show_information();
}
else
{
cout << "所指定的职工信息不存在,未查找到" << endl;
}
}
}
void System_Manager::Sort_Employinformation()
{
cout << "请选择排序方式:" << endl;
cout << "1.按职工号进行升序" << endl;
cout << "2.按职工号进行降序" << endl;
int select = 0;
cin >> select;
if (select == 1 || select == 2)
{
for (int i = 0;i < Employ_num;i++)
{
int Max_Min = i;
for (int j = i + 1;j < Employ_num;j++)
{
switch (select)
{
case 1://升序
if (Employ_arr[i]->Id > Employ_arr[j]->Id)
{
Max_Min = j;
}
break;
case 2://降序
if (Employ_arr[i]->Id < Employ_arr[j]->Id)
{
Max_Min = j;
}
break;
default:
break;
}
}
if (i != Max_Min)
{
Worker* temp = Employ_arr[i];
Employ_arr[i] = Employ_arr[Max_Min];
Employ_arr[Max_Min] = temp;
}
}
}
else
{
cout << "无该选项,请重新选择" << endl;
}
cout << "排序操作成功,排序结果为:" << endl;
Save_Information();
Show_Employinformation();
}
void System_Manager::Clean_Empoyinformation()
{
cout << "确定是否清空存储文件中所有数据" << endl;
cout << " 1.确定" << endl;
cout << " 2.返回" << endl;
int select = 0;
cin >> select;
if (select == 1 || select == 2)
{
if (select == 1)
{
ofstream ofile(File, ios::trunc);//打开模式ios::trunc 如果存在删除文件并重新创建
ofile.close();
if (Employ_arr != NULL)//清空记录参数
{
for (int i = 0;i < Employ_num;i++)
{
if (Employ_arr[i] != NULL)
{
delete Employ_arr[i];
}
Employ_num = 0;
delete[] Employ_arr;
Employ_arr = NULL;
}
}
cout << "清空成功!" << endl;
}
}
else
{
cout << "无该选项,无法进行清空操作" << endl;
}
}
System_Manager::~System_Manager()
{
if (Employ_arr != NULL)//清空记录参数
{
for (int i = 0;i < Employ_num;i++)
{
if (Employ_arr[i] != NULL)
{
delete Employ_arr[i];
}
delete[] Employ_arr;
Employ_arr = NULL;
}
}
}
int main()
{
System_Manager Work_manager;//职工管理系统对象
int choice = 0;//存储用户选项
while (true)
{
Work_manager.Menu_display();
cout << "请输入你的选择: " << endl;
cin >> choice;
switch(choice)
{
case 0://退出管理程序
Work_manager.Exit_system();
break;
case 1://批量增加职工信息,保存至文件中
Work_manager.Add_Employ();
break;
case 2://显示职工信息
Work_manager.Show_Employinformation();
break;
case 3://删除离职职工
Work_manager.Delete_Employinformation();
break;
case 4://修改职工信息
Work_manager.Modify_Employinformation();
break;
case 5://查找职工信息
Work_manager.Search_Employinformation();
break;
case 6://按照编号排序
Work_manager.Sort_Employinformation();
break;
case 7://清理所有文档
Work_manager.Clean_Empoyinformation();
break;
default:
break;
}
}
return 0;
}
c++职工信息系统
于 2022-03-17 11:43:49 首次发布