#include<iostream>
#include<string>
#include<fstream>
#define FILENAME "test.txt"
using namespace std;//.hpp包含的头文件
#include<iostream>
#include<string>
#include"类与方法.hpp"
using namespace std;//.cpp所包含的头文件
整个案例分为多个大块:
首先是头文件 类与方法.hpp内
1.三种职工的父类
class Job
{
private:
int Job_id;
string name;
string duty;//职责
public:
string post;//岗位
int department;//部门编号
int getjob_id()
{
return this->Job_id;
}
string getname()
{
return this->name;
}
string getpost()
{
return this->post;
}
string getDuty()
{
return this->duty;
}
void setjob_id(int id)
{
this->Job_id = id;
}
void setname(string name)
{
this->name = name;
}
void setpost(string post)
{
this->post = post;
}
void setduty(string duty)
{
this->duty = duty;
}
};
2.普通职工
class Employee :public Job
{
public:
Employee(int id, string name,int e_id)
{
setjob_id(id);
setname(name);
setpost("普通员工");
this->department = e_id;
setduty("完成经理布置的任务");
}
};
3.经理类
class manger :public Job
{
public:
manger(int id, string name, int e_id)
{
setjob_id(id);
setname(name);
setpost("经理");
this->department = e_id;
setduty("完成老板布置的任务");
}
};
4.老板类
class Boss :public Job
{
public:
Boss(int id, string name, int e_id)
{
setjob_id(id);
setname(name);
setpost("老板");
this->department = e_id;
setduty("管理公司");
}
};
5.整个案例所有方法以及对象类
class workermanager
{
public:
workermanager();
void menu();
void add();
Job** m_EmpArray;
int number=0;
//保存文件
void save();
//判断文件是否为空标志
bool m_FileIsEmpty;
统计文件中的人数
int get_EmpNum();
// 初始化员工
void init_Emp();
//显示职工
void Show_Emp();
//删除职工
void Del_Emp();
判断职工是否存在,如果存在返回职工所在数组中的位置,不存在返回-1
int IsExist(int id);
//修改职工
void Mod_Emp();
//查找员工
void Find_Emp();
//按照编号排序
void Sort_Emp();
//清空文件
void Clean_File();
};
测试test.cpp
1.构造器与初始化
//构造器
workermanager::workermanager()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
//初始化属性,设置初始化记录人数为0
this->number = 0;
//初始化数组指针
this->m_EmpArray = NULL;
//初始化文件是否为空
this->m_FileIsEmpty = true;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
//文件为空
cout << "文件为空!" << endl;
//初始化属性,设置初始化记录人数为0
this->number = 0;
//初始化数组指针
this->m_EmpArray = NULL;
//初始化文件是否为空
this->m_FileIsEmpty = true;
ifs.close();//关闭文件
return;
}
int num = this->get_EmpNum();
cout << "职工人数为:" << num << endl;
this->number = num;
//开辟空间
this->m_EmpArray = new Job * [this->number];
//将文件中的数据存放到数组中
this->init_Emp();
}
//初始化
void workermanager::init_Emp()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int e_id;
for (int i = 0;i < this->get_EmpNum();i++)
{
ifs >> id;
ifs >> name;
ifs >> e_id;
Job *job = NULL;
if (e_id == 1)
{
job = new Employee(id, name, 1);
}
else if(e_id == 2)
{
job = new manger(id, name, 2);
}
else if (e_id == 3)
{
job = new Boss(id, name, 3);
}
this->m_EmpArray[i] = job;
}
this->number = this->get_EmpNum();
ifs.close();
}
2.菜单栏
void workermanager::menu()
{
int flag=1;
while (flag!=0)
{
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;
cout << "请输入你的选择" << endl;
cin >> flag;
switch (flag)
{
case 1:
add();
break;
case 2:
Show_Emp();
break;
case 3:
Del_Emp();
break;
case 4:
Mod_Emp();
break;
case 5:
Find_Emp();
break;
case 6:
Sort_Emp();
break;
case 7:
Clean_File();
break;
default:
break;
}
}
}
3.通过职工编号判断是否存在和在文件中保存方法
int workermanager::IsExist(int id)
{
for(int i=0;i<this->get_EmpNum();i++)
{
if (this->m_EmpArray[i]->getjob_id() == id)
{
return i;
}
}
return - 1;
}
void workermanager::save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);
for (int i = 0;i < this->number;i++)
{
ofs << this->m_EmpArray[i]->getjob_id() << " "
<< this->m_EmpArray[i]->getname() << " "
<< this->m_EmpArray[i]->department<< endl;
}
ofs.close();
}
4.批量添加职工
void workermanager::add()
{
int flag;
string name;
int id;
int e_id;
int E_number;
int e_number;
cout << "请输入你要添加的人数";
cin >> E_number;
if (E_number > 0)
{
int newsize = E_number + number;
Job** newspace = new Job * [newsize];
if(this->m_EmpArray != NULL)
{
for (int i = 0;i<this->number;i++)
{
newspace[i] = this->m_EmpArray[i];
}
}
for(int i=0;i<E_number;i++)
{
cout << "请输入你要添加的职工的岗位;1普通职工 2经理 3老板";
cin >> flag;
cout << "请输入职工姓名";
cin >> name;
cout << "请输入职工职工编号";
cin >> id;
Job* job = NULL;
switch (flag)
{
case 1:
job = new Employee(id, name, 1);
break;
case 2:
job = new manger(id, name, 2);
break;
case 3:
job = new Boss(id, name, 3);
break;
default:
cout << "输入有误"<<endl;
break;
}
newspace[this->number + i] = job;
}
delete[] this->m_EmpArray;
this->number = newsize;
this->m_EmpArray = newspace;
this->m_FileIsEmpty = false;
cout << "添加成功" << endl;
}
else {
cout << "输入有误" << endl;
}
this->save();
}
5.查找职工
void workermanager::Find_Emp()
{
int flag;
int id;
string name;
int sign=0;
cout << "若按编号查找请按1;若按姓名查找请按2" << endl;
cin >> flag;
if (flag == 1)
{
cout << "请输入要查找的编号" << endl;
cin >> id;
for (int i = 0;i < this->get_EmpNum();i++)
{
if (this->m_EmpArray[i]->getjob_id() == id)
{
cout << "部门:" << m_EmpArray[i]->getpost()<<" " << "部门编号:" << m_EmpArray[i]->department << " " << "职工编号:" << m_EmpArray[i]->getjob_id() << " " << "职工姓名:" << m_EmpArray[i]->getname() << " " << "职工职责:" << m_EmpArray[i]->getDuty() << endl;
sign = 1;
}
}
}
else if(flag == 2)
{
cout << "请输入要查询的职工姓名" << endl;
cin >> name;
for (int i = 0;i < this->get_EmpNum();i++)
{
if (this->m_EmpArray[i]->getname() == name)
{
cout << "部门:" << m_EmpArray[i]->getpost() << "部门编号:" << m_EmpArray[i]->department << " " << "职工编号:" << m_EmpArray[i]->getjob_id() << " " << "职工姓名:" << m_EmpArray[i]->getname() << " " << "职工职责:" << m_EmpArray[i]->getDuty() << endl;
sign = 1;
}
}
}
else
{
cout << "输入有误" << endl;
sign = 1;
}
if (sign==0)
{
cout << "未查找到该职工" << endl;
}
}
6.展示文件中职工信息
void workermanager::Show_Emp()
{
for (int i = 0;i <this->number;i++)
{
cout << "部门:" << m_EmpArray[i]->getpost() << " " << "部门编号:" << m_EmpArray[i]->department << " " << "职工编号:" << m_EmpArray[i]->getjob_id() << " " << "职工姓名:" << m_EmpArray[i]->getname() << " " << "职工职责:" << m_EmpArray[i]->getDuty() << endl;
}
}
7.修改职工
void workermanager::Mod_Emp()
{
int flag;
int sign=0;
string name;
int id;
int e_id;
cout << "请输入要修改的职工的编号" << endl;
cin >> flag;
for (int i = 0;i < this->get_EmpNum();i++)
{
if (this->m_EmpArray[i]->getjob_id() == flag)
{
cout << "请输入你要添加的职工的岗位;1普通职工 2经理 3老板";
cin >> e_id;
cout << "请输入职工姓名";
cin >> name;
cout << "请输入职工职工编号";
cin >> id;
this->m_EmpArray[i]->setjob_id(id);
this->m_EmpArray[i]->department = e_id;
this->m_EmpArray[i]->setname(name);
if (e_id == 1)
{
this->m_EmpArray[i]->setpost("普通职工");
this->m_EmpArray[i]->setduty("完成经理布置的任务");
}
else if (e_id == 2)
{
this->m_EmpArray[i]->setpost("经理");
this->m_EmpArray[i]->setduty("完成老板布置的任务");
}
else if (e_id == 3)
{
this->m_EmpArray[i]->setpost("老板");
this->m_EmpArray[i]->setduty("管理公司");
}
else
{
cout << "输入错误" << endl;
}
cout << "修改成功" << endl;
sign = 1;
}
}
if (sign == 0)
{
cout << "修改失败" << endl;
}
this->save();
}
8.得到文件中职工人数
int workermanager::get_EmpNum()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);//打开文件,读取数据
int id;
string name;
int E_id;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> E_id)
{
//记录人数
num++;
}
ifs.close();
return num;
}
9.删除离职职工
void workermanager::Del_Emp()
{
if (this->m_FileIsEmpty)//为空
{
cout << "文件不存在或者记录为空!" << endl;
}
else
{
int flag;
cout << "请输入你要删除的职工编号" << endl;
cin >> flag;
for (int i = 0;i < this->number;i++)
{
if (this->m_EmpArray[i]->getjob_id() == flag)
{
for (int j = i;j < this->number - 1;j++)
{
this->m_EmpArray[j] = this->m_EmpArray[j + 1];
}
this->number--;
cout << "删除成功" <<endl;
}
}
this->save();
}
}
10排序职工,按升序职工编号
void workermanager::Sort_Emp()
{
for (int i = 0;i < this->number;i++)
{
for (int j = 0;j < this->number - i-1;j++)
{
if (this->m_EmpArray[j]->getjob_id() > this->m_EmpArray[j + 1]->getjob_id())
{
Job* job = this->m_EmpArray[j];
this->m_EmpArray[j] = this->m_EmpArray[j + 1];
this->m_EmpArray[j + 1] = job;
}
}
}
cout << "排序成功" << endl;
this->save();
}
11清除文件所有职工
void workermanager::Clean_File()
{
cout << "确认清空?" << endl;
cout << "1-是" << endl;
cout << "2-否" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//清空文件
ofstream ofs(FILENAME, ios::trunc); //打开模式ios::trunc,如果文件存在,删除文件并重新创建
ofs.close();
if (this->m_EmpArray != NULL)
{
//删除堆区的每个职工对象
for (int i = 0; i < this->number; i++)//释放堆区的数据
{
this->m_EmpArray[i] == NULL;
delete this->m_EmpArray[i];
}
//删除堆区数组指针
this->number = 0;
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
this->m_FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
12.main方法
int main()
{
workermanager wk;
wk.menu();
}
整个源文件
类与方法.hpp
#include<iostream>
#include<string>
#include<fstream>
#define FILENAME "test.txt"
using namespace std;
class Job
{
private:
int Job_id;
string name;
string duty;
public:
string post;//岗位
int department;
int getjob_id()
{
return this->Job_id;
}
string getname()
{
return this->name;
}
string getpost()
{
return this->post;
}
string getDuty()
{
return this->duty;
}
void setjob_id(int id)
{
this->Job_id = id;
}
void setname(string name)
{
this->name = name;
}
void setpost(string post)
{
this->post = post;
}
void setduty(string duty)
{
this->duty = duty;
}
};
class workermanager
{
public:
workermanager();
void menu();
void add();
Job** m_EmpArray;
int number=0;
//保存文件
void save();
//判断文件是否为空标志
bool m_FileIsEmpty;
统计文件中的人数
int get_EmpNum();
// 初始化员工
void init_Emp();
//显示职工
void Show_Emp();
//删除职工
void Del_Emp();
判断职工是否存在,如果存在返回职工所在数组中的位置,不存在返回-1
int IsExist(int id);
//修改职工
void Mod_Emp();
//查找员工
void Find_Emp();
//按照编号排序
void Sort_Emp();
//清空文件
void Clean_File();
};
class Employee :public Job
{
public:
Employee(int id, string name,int e_id)
{
setjob_id(id);
setname(name);
setpost("普通员工");
this->department = e_id;
setduty("完成经理布置的任务");
}
};
class manger :public Job
{
public:
manger(int id, string name, int e_id)
{
setjob_id(id);
setname(name);
setpost("经理");
this->department = e_id;
setduty("完成老板布置的任务");
}
};
class Boss :public Job
{
public:
Boss(int id, string name, int e_id)
{
setjob_id(id);
setname(name);
setpost("老板");
this->department = e_id;
setduty("管理公司");
}
};
test.cpp
#include<iostream>
#include<string>
#include"类与方法.hpp"
using namespace std;
workermanager::workermanager()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
//初始化属性,设置初始化记录人数为0
this->number = 0;
//初始化数组指针
this->m_EmpArray = NULL;
//初始化文件是否为空
this->m_FileIsEmpty = true;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
//文件为空
cout << "文件为空!" << endl;
//初始化属性,设置初始化记录人数为0
this->number = 0;
//初始化数组指针
this->m_EmpArray = NULL;
//初始化文件是否为空
this->m_FileIsEmpty = true;
ifs.close();//关闭文件
return;
}
int num = this->get_EmpNum();
cout << "职工人数为:" << num << endl;
this->number = num;
//开辟空间
this->m_EmpArray = new Job * [this->number];
//将文件中的数据存放到数组中
this->init_Emp();
}
void workermanager::init_Emp()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int e_id;
for (int i = 0;i < this->get_EmpNum();i++)
{
ifs >> id;
ifs >> name;
ifs >> e_id;
Job *job = NULL;
if (e_id == 1)
{
job = new Employee(id, name, 1);
}
else if(e_id == 2)
{
job = new manger(id, name, 2);
}
else if (e_id == 3)
{
job = new Boss(id, name, 3);
}
this->m_EmpArray[i] = job;
}
this->number = this->get_EmpNum();
ifs.close();
}
void workermanager::menu()
{
int flag=1;
while (flag!=0)
{
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;
cout << "请输入你的选择" << endl;
cin >> flag;
switch (flag)
{
case 1:
add();
break;
case 2:
Show_Emp();
break;
case 3:
Del_Emp();
break;
case 4:
Mod_Emp();
break;
case 5:
Find_Emp();
break;
case 6:
Sort_Emp();
break;
case 7:
Clean_File();
break;
default:
break;
}
}
}
int workermanager::IsExist(int id)
{
for(int i=0;i<this->get_EmpNum();i++)
{
if (this->m_EmpArray[i]->getjob_id() == id)
{
return i;
}
}
return - 1;
}
void workermanager::save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);
for (int i = 0;i < this->number;i++)
{
ofs << this->m_EmpArray[i]->getjob_id() << " "
<< this->m_EmpArray[i]->getname() << " "
<< this->m_EmpArray[i]->department<< endl;
}
ofs.close();
}
void workermanager::add()
{
int flag;
string name;
int id;
int e_id;
int E_number;
int e_number;
cout << "请输入你要添加的人数";
cin >> E_number;
if (E_number > 0)
{
int newsize = E_number + number;
Job** newspace = new Job * [newsize];
if(this->m_EmpArray != NULL)
{
for (int i = 0;i<this->number;i++)
{
newspace[i] = this->m_EmpArray[i];
}
}
for(int i=0;i<E_number;i++)
{
cout << "请输入你要添加的职工的岗位;1普通职工 2经理 3老板";
cin >> flag;
cout << "请输入职工姓名";
cin >> name;
cout << "请输入职工职工编号";
cin >> id;
Job* job = NULL;
switch (flag)
{
case 1:
job = new Employee(id, name, 1);
break;
case 2:
job = new manger(id, name, 2);
break;
case 3:
job = new Boss(id, name, 3);
break;
default:
cout << "输入有误"<<endl;
break;
}
newspace[this->number + i] = job;
}
delete[] this->m_EmpArray;
this->number = newsize;
this->m_EmpArray = newspace;
this->m_FileIsEmpty = false;
cout << "添加成功" << endl;
}
else {
cout << "输入有误" << endl;
}
this->save();
}
void workermanager::Find_Emp()
{
int flag;
int id;
string name;
int sign=0;
cout << "若按编号查找请按1;若按姓名查找请按2" << endl;
cin >> flag;
if (flag == 1)
{
cout << "请输入要查找的编号" << endl;
cin >> id;
for (int i = 0;i < this->get_EmpNum();i++)
{
if (this->m_EmpArray[i]->getjob_id() == id)
{
cout << "部门:" << m_EmpArray[i]->getpost()<<" " << "部门编号:" << m_EmpArray[i]->department << " " << "职工编号:" << m_EmpArray[i]->getjob_id() << " " << "职工姓名:" << m_EmpArray[i]->getname() << " " << "职工职责:" << m_EmpArray[i]->getDuty() << endl;
sign = 1;
}
}
}
else if(flag == 2)
{
cout << "请输入要查询的职工姓名" << endl;
cin >> name;
for (int i = 0;i < this->get_EmpNum();i++)
{
if (this->m_EmpArray[i]->getname() == name)
{
cout << "部门:" << m_EmpArray[i]->getpost() << "部门编号:" << m_EmpArray[i]->department << " " << "职工编号:" << m_EmpArray[i]->getjob_id() << " " << "职工姓名:" << m_EmpArray[i]->getname() << " " << "职工职责:" << m_EmpArray[i]->getDuty() << endl;
sign = 1;
}
}
}
else
{
cout << "输入有误" << endl;
sign = 1;
}
if (sign==0)
{
cout << "未查找到该职工" << endl;
}
}
void workermanager::Show_Emp()
{
for (int i = 0;i <this->number;i++)
{
cout << "部门:" << m_EmpArray[i]->getpost() << " " << "部门编号:" << m_EmpArray[i]->department << " " << "职工编号:" << m_EmpArray[i]->getjob_id() << " " << "职工姓名:" << m_EmpArray[i]->getname() << " " << "职工职责:" << m_EmpArray[i]->getDuty() << endl;
}
}
void workermanager::Mod_Emp()
{
int flag;
int sign=0;
string name;
int id;
int e_id;
cout << "请输入要修改的职工的编号" << endl;
cin >> flag;
for (int i = 0;i < this->get_EmpNum();i++)
{
if (this->m_EmpArray[i]->getjob_id() == flag)
{
cout << "请输入你要添加的职工的岗位;1普通职工 2经理 3老板";
cin >> e_id;
cout << "请输入职工姓名";
cin >> name;
cout << "请输入职工职工编号";
cin >> id;
this->m_EmpArray[i]->setjob_id(id);
this->m_EmpArray[i]->department = e_id;
this->m_EmpArray[i]->setname(name);
if (e_id == 1)
{
this->m_EmpArray[i]->setpost("普通职工");
this->m_EmpArray[i]->setduty("完成经理布置的任务");
}
else if (e_id == 2)
{
this->m_EmpArray[i]->setpost("经理");
this->m_EmpArray[i]->setduty("完成老板布置的任务");
}
else if (e_id == 3)
{
this->m_EmpArray[i]->setpost("老板");
this->m_EmpArray[i]->setduty("管理公司");
}
else
{
cout << "输入错误" << endl;
}
cout << "修改成功" << endl;
sign = 1;
}
}
if (sign == 0)
{
cout << "修改失败" << endl;
}
this->save();
}
int workermanager::get_EmpNum()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);//打开文件,读取数据
int id;
string name;
int E_id;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> E_id)
{
//记录人数
num++;
}
ifs.close();
return num;
}
void workermanager::Del_Emp()
{
if (this->m_FileIsEmpty)//为空
{
cout << "文件不存在或者记录为空!" << endl;
}
else
{
int flag;
cout << "请输入你要删除的职工编号" << endl;
cin >> flag;
for (int i = 0;i < this->number;i++)
{
if (this->m_EmpArray[i]->getjob_id() == flag)
{
for (int j = i;j < this->number - 1;j++)
{
this->m_EmpArray[j] = this->m_EmpArray[j + 1];
}
this->number--;
cout << "删除成功" <<endl;
}
}
this->save();
}
}
void workermanager::Sort_Emp()
{
for (int i = 0;i < this->number;i++)
{
for (int j = 0;j < this->number - i-1;j++)
{
if (this->m_EmpArray[j]->getjob_id() > this->m_EmpArray[j + 1]->getjob_id())
{
Job* job = this->m_EmpArray[j];
this->m_EmpArray[j] = this->m_EmpArray[j + 1];
this->m_EmpArray[j + 1] = job;
}
}
}
cout << "排序成功" << endl;
this->save();
}
void workermanager::Clean_File()
{
cout << "确认清空?" << endl;
cout << "1-是" << endl;
cout << "2-否" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//清空文件
ofstream ofs(FILENAME, ios::trunc); //打开模式ios::trunc,如果文件存在,删除文件并重新创建
ofs.close();
if (this->m_EmpArray != NULL)
{
//删除堆区的每个职工对象
for (int i = 0; i < this->number; i++)//释放堆区的数据
{
this->m_EmpArray[i] == NULL;
delete this->m_EmpArray[i];
}
//删除堆区数组指针
this->number = 0;
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
this->m_FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
int main()
{
workermanager wk;
wk.menu();
}