C++ 公司员工管理系统
设计一个利用文件处理方式实现对公司人员(包括工程师、领导和主任工程师)进行管理,要求具有增加数据、更新数据、查询数据、删除数据以及实现数据功能。
实现提示: 设计一个虚基类 Staff(员工),包含编号、姓名和年龄成员以及相关的成员 函数;由 Staff 派生出工程师类 Engineer,包含专业和职称成员以及相关的成员 函数;再由 Staff 派生出领导类 Leader,包含职务和部门数据成员以及相关的成员函 数;然后由 Engineer 和 Leader 类派生出主任工程师类 Chairman。
实现的功能如下: (1) 增加公司人员; (2) 删除公司人员; (3) 查找公司人员; (4) 修改公司人员; (5)对员工进行排序
一.需求分析
1.1 问题描述
根据需求,该系统应包含的信息:员工的编号,姓名,年龄等信息
根据需求,该系统应实现的功能:信息的录入和保存,信息的删除,信息的修改,信息的查找,信息的增加,信息的排序
1.2 算法设计思想
根据需求,该系统首先需要包含一个虚基类Staff,由此派生工程师类 Engineer,领导类 Leader和主任工程师类 Chairman,再设置一个管理类Manager来实施系统中各个功能,如修改,查找,添加等,再用文件进行数据的保存和修改
二.源代码与运行结果
2.1 初始化和准备
定义员工类,工程师类,领导类,主任工程师类,管理类,以及主函数
#include
#include
#include
#define FILENAME "text.txt"
using namespace std;
class Staff //员工类
{
public:
virtual void info() = 0; //显示个人信息
int m_id;
string m_name;
int m_age;
int m_select;
};
class Engineer :virtual public Staff //工程师类
{
public:
Engineer(int id = 1, string name = " ", int age = 6, int select = 1, string major = " ", string status = " ");
void info() //显示个人信息
{
cout << "工程师编号" << this->m_id
<< "\t工程师姓名" << this->m_name
<< "\t工程师年龄" << this->m_age
<< "\t工程师专业" << this->m_major
<< "\t工程师职称" << this->m_status << endl;
}
string m_major; //专业
string m_status; //职称
};
Engineer::Engineer(int id, string name, int age, int select, string major, string status)
{
this->m_id = id;
this->m_name = name;
this->m_age = age;
this->m_major = major;
this->m_status = status;
}
class Leader :virtual public Staff //领导类
{
public:
Leader(int id = 2, string name = " ", int age = 5, int select = 2, string post = " ", string department = " ");
void info() //显示个人信息
{
cout << "领导编号" << this->m_id
<< "\t领导姓名" << this->m_name
<< "\t领导年龄" << this->m_age
<< "\t领导职务" << this->m_post
<< "\t领导部门" << this->m_department<< endl;
}
string m_post; //职务
string m_department; //部门
};
Leader::Leader(int id, string name, int age, int select, string post, string department)
{
this->m_id = id;
this->m_name = name;
this->m_age = age;
this->m_post = post;
this->m_department = department;
}
class Chairman :public Engineer, public Leader //主任工程师类
{
public:
Chairman(int id, string name, int age, int select)
{
this->m_id = id;
this->m_name = name;
this->m_age = age;
}
void info() //显示个人信息
{
cout << "主任工程师编号" << this->m_id
<< "\t主任工程师姓名" << this->m_name
<< "\t主任工程师年龄" << this->m_age << endl;
}
};
class Manager //管理类
{
public:
Manager()
{
//文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in); //读文件
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
this->m_num = 0;
this->m_array = NULL;
this->m_fileempty = true; //初始化文件是否为空
ifs.close();
return;
}
//初始化属性
this->m_num = 0;
this->m_array = NULL;
}
void show() //展示菜单
{
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 << "---------------------------" << endl;
cout << endl;
}
bool m_fileempty; //判断文件是否为空
void showstaff(); //显示员工
void add(); //添加员工
void save(); //保存文件
void del(); //删除员工
void mod(); //修改员工信息
void find(); //查找员工信息
void sort(); //排序
int staffexit(int id); //判断员工是否存在
int m_num; //记录员工人数
Staff ** m_array; //员工数组指针
};
int main()
{
Manager w;
w.save();
int c = 0; //用于存储用户的选项
while (1)
{
w.show();
cout << "请输入你的选择;" << endl;
cin >> c; //接收用户的选项
switch(c)
{
case 0:exit();
break;
case 1:w.showstaff();
break;
case 2:w.add();
break;
case 3:w.sort();
break;
case 4:w.find();
break;
case 5:w.mod();
break;
case 6:w.del();
break;
default:system("cls"); //清屏
break;
}
}
return 0;
}
2.2 功能的实现
1.退出管理系统
在操作后,按0可以退出系统
void exit() //退出系统
{
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);
}

2. 显示员工信息
在输入员工各个信息后,可以在界面显示员工的信息;如果文件不存在,则会显示“文件不存在”;如果无信息,则不会显示。
void Manager::showstaff() //显示员工
{
if (this->m_fileempty)
{
cout << "文件不存在!" << endl;
}
else
{
for (int i = 0; i < m_num; i++)
{
this->m_array[i]->info();
}
}
system("pause");
system("cls");
}

3. 增加员工信息
将员工的信息输入系统,系统可以将信息录入文件保存
void Manager::add() //添加员工
{
cout << "添加员工的数量;" << endl;
int addnum = 0; //保存用户输入的数量
cin >> addnum;
if (addnum > 0)
{
int newsize = this->m_num + addnum; //计算开辟新空间大小
Staff** newspace = new Staff * [newsize]; //开辟新空间
//将原来数据拷贝到新空间下
if (this->m_array != NULL)
{
for (int i = 0; i < this->m_num; i++)
{
newspace[i] = this->m_array[i];
}
}
//添加新数据
for (int i = 0; i < addnum; i++)
{
int id;
string name;
int age;
string major;
string status;
string post;
string department;
int select;
cout << "请输入第" << i + 1 << "个新员工编号;" << endl;
cin >> id;
cout << "请输入第" << i + 1 << "个新员工姓名;" << endl;
cin >> name;
cout << "请输入第" << i + 1 << "个新员工年龄;" << endl;
cin >> age;
cout << "请选择该员工岗位;" << endl;
cout << "1.工程师" << endl;
cout << "2.领导" << endl;
cout << "3.主任工程师" << endl;
cin >> select;
Staff* staff = NULL;
switch (select)
{
case 1:cout << "请输入工程师的专业;" << endl;
cin >> major;
cout << "请输入工程师的职称;" << endl;
cin >> status;
staff = new Engineer(id, name, age, 1, major, status);
break;
case 2:cout << "请输入领导的职务;" << endl;
cin >> post;
cout << "请输入领导的部门;" << endl;
cin >> department;
staff = new Leader(id,name, age, 2, post, department);
break;
case 3:
staff = new Chairman(id, name, age, 3);
break;
default:
break;
}
newspace[this->m_num + i] = staff; //保存到数组中
}
delete[] this->m_array; //释放原来的空间
this->m_array = newspace; //更改新空间的指向
this->m_num = newsize; //更新新的员工人数
this->save(); //保存数据
cout << "成功添加" << addnum << "名新员工" << endl;
}
else
{
cout << "添加有误!" << endl;
}
}
void Manager::save() //保存文件
{
ofstream ofs;
ofs.open(FILENAME , ios::out); //写文件
for (int i = 0; i < m_num; i++)
{
ofs << this->m_array[i]->m_id << " "
<< this->m_array[i]->m_name << " "
<< this->m_array[i]->m_age << endl;
}
ofs.close();
}

4 . 查找员工信息
可以输入员工编号,系统根据员工编号找到相应员工以及相关信息;如果文件不存在,则会显示“文件不存在”;如果输入的编号不存在,则会显示“查找失败!”
int Manager::staffexit(int id) //判断员工是否存在
{
int index = -1;
for (int i = 0; i < this->m_num; i++)
{
if (this->m_array[i]->m_id == id)
{
index = i;
break;
}
}
return index;
}
void Manager::find() //查找员工信息
{
if (this->m_fileempty)
{
cout << "文件不存在!" << endl;
}
else
{
int id;
cout << "请输入查找的员工编号:" << endl;
cin >> id;
int nw = this->staffexit(id);
if (nw != -1)
{
cout << "查找成功!信息如下:" << endl;
this->m_array[nw]->info();
}
else
{
cout << "查找失败" << endl;
}
}
}

5. 修改员工信息
在系统中输入要修改信息的员工的编号,系统可以找到对应的员工,然后可以修改员工的所有信息,并在文件中进行更改;如果文件不存在,则会显示“文件不存在”;如果输入的编号不存在,则会显示“修改失败”
void Manager::mod() //修改员工信息
{
if (this->m_fileempty)
{
cout << "文件不存在!" << endl;
}
else
{
cout << "请输入修改员工的编号: " << endl;
int id=0;
cin >> id;
int nw = this->staffexit(id);
if (nw != -1)
{
cout << "此人信息如下:" << endl;
this->m_array[nw]->info();
int new_id = 0;
string new_name = " ";
int new_age = 0;
int new_select = 0;
string new_major = " ";
string new_status = " ";
string new_post = " ";
string new_department = " ";
cout << "请输入新的员工号: " << endl;
cin >> new_id;
cout << "请输入新的员工姓名:" << endl;
cin >> new_name;
cout << "请输入新的员工年龄:" << endl;
cin >> new_age;
cout << "请选择该员工新岗位;" << endl;
cout << "1.工程师" << endl;
cout << "2.领导" << endl;
cout << "3.主任工程师" << endl;
cin >> new_select;
Staff* staff = NULL;
switch (new_select)
{
case 1:cout << "请输入工程师新的专业;" << endl;
cin >> new_major;
cout << "请输入工程师新的职称;" << endl;
cin >> new_status;
staff = new Engineer(new_id, new_name, new_age, 1,new_major, new_status);
break;
case 2:cout << "请输入领导新的职务;" << endl;
cin >> new_post;
cout << "请输入领导新的部门;" << endl;
cin >> new_department;
staff = new Leader(new_id, new_name, new_age, 2, new_post, new_department);
break;
case 3:
staff = new Chairman(new_id, new_name, new_age, 3);
break;
default:
break;
}
this->m_array[nw] = staff; //更新数据到数组中
cout << "修改成功!" << endl;
this->save();
}
else
{
cout << "修改失败" << endl;
}
}
system("pause");
system("cls");
}

6. 删除员工信息
输入要删除的员工的编号,系统可以将该员工的信息删除,并在文件中进行更改
;如果文件不存在,则会显示“文件不存在”;如果输入的编号不存在,则会显示“删除失败!”
void Manager::del() //删除员工
{
if (this->m_fileempty)
{
cout << "文件不存在!" << endl;
}
else
{
cout << "请输入删除的编号; " << endl;
int id = 0;
cin >> id;
int index = this->staffexit(id);
if (index != -1)
{
for (int i = index; i < this->m_num - 1; i++)
{
this->m_array[i] = this->m_array[i + 1];
}
this->m_num--;
this->save();
cout << "删除成功!" << endl;
}
else
{
cout << "删除失败!" << endl;
}
}
system("pause");
system("cls");
}

7. 对员工进行排序
可以将员工根据员工编号分别进行升序排序和降序排序,并在文件中进行更改;如果文件不存在,则会显示“文件不存在”。
void Manager::sort() //排序
{
if (this->m_fileempty)
{
cout << "文件不存在!" << endl;
}
else
{
cout << "选择排序方式:" << endl;
cout << "1.对员工号升序排序" << endl;
cout << "2.对员工号降序排序" << endl;
int select = 0;
cin >> select;
for (int i = 0; i < m_num-1; i++)
{
int m = 1;
for (int j = 1; j < m_num;j++)
{
if(select == 1)
{
if (m_array[m]->m_id > m_array[j]->m_id)
{
m = j;
}
}
else
{
if (m_array[m]->m_id < m_array[j]->m_id)
{
m = j;
}
}
}
if (i != m)
{
Staff* t = m_array[i];
m_array[i] = m_array[m];
m_array[m] = t;
}
}
cout << "排序成功,排序结果为:" << endl;
this->save();
this->showstaff();
}
}
}
}
}
if (i != m)
{
Staff* t = m_array[i];
m_array[i] = m_array[m];
m_array[m] = t;
}
}
cout << "排序成功,排序结果为:" << endl;
this->save();
this->showstaff();
}
}


PS:文件也随之更新,在此就不在展示
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
