最近博主也是花了两三天时间跟着视频学了一个c++实践项目--职工管理系统。完成后也是迫不及待地给大家分享,大家感兴趣可以自己上机试试(分文件有点多)。
1.Boss.cpp
代码:#include"boss.h"
Boss::Boss(int id, string name, int did)
{
this->id = id;
this->name = name;
this->did = did;
}
void Boss::showInfo()
{
cout << "职工编号: " << this->id
<< "\t职工姓名: " << this->name
<< "\t岗位: " << this->getDeptName()
<< "\t岗位职责:管理公司所有事务" << endl;
}
string Boss::getDeptName()
{
if (did == 1)
{
return string("普通员工");
}
else if (did == 2)
{
return string("经理");
}
else
{
return string("总裁");
}
}
2.boss.h
代码:
#pragma once
#include<iostream>
#include<string>
using namespace std;
#include"Worker.h"
class Boss :public Worker
{
public:
//构造函数
Boss(int id, string name, int did);
//显示个人信息
virtual void showInfo();
//获取职工岗位名称
virtual string getDeptName();
};
3.Employee.cpp
代码:
#include"employee.h"
Employee::Employee(int id, string name, int did)
{
this->id = id;
this->name = name;
this->did = did;
}
void Employee::showInfo()
{
cout << "职工编号: " << this->id
<< "\t职工姓名: " << this->name
<< "\t岗位: " << this->getDeptName()
<< "\t岗位职责:完成经理交给的任务" << endl;
}
string Employee::getDeptName()
{
if (did == 1)
{
return string("普通员工");
}
else if (did == 2)
{
return string("经理");
}
else
{
return string("总裁");
}
}
4.employee.h
代码:
#include"employee.h"
Employee::Employee(int id, string name, int did)
{
this->id = id;
this->name = name;
this->did = did;
}
void Employee::showInfo()
{
cout << "职工编号: " << this->id
<< "\t职工姓名: " << this->name
<< "\t岗位: " << this->getDeptName()
<< "\t岗位职责:完成经理交给的任务" << endl;
}
string Employee::getDeptName()
{
if (did == 1)
{
return string("普通员工");
}
else if (did == 2)
{
return string("经理");
}
else
{
return string("总裁");
}
}
5.Manager.cpp
代码:
#include"manager.h"
Manager::Manager(int id, string name, int did)
{
this->id = id;
this->name = name;
this->did = did;
}
void Manager::showInfo()
{
cout << "职工编号: " << this->id
<< "\t职工姓名: " << this->name
<< "\t岗位: " << this->getDeptName()
<< "\t岗位职责:完成老板交给的任务,并下发给员工" << endl;
}
string Manager::getDeptName()
{
if (did == 1)
{
return string("普通员工");
}
else if (did == 2)
{
return string("经理");
}
else
{
return string("总裁");
}
}
6.manager.h
代码:
#pragma once
#include<iostream>
#include<string>
using namespace std;
#include"Worker.h"
class Manager :public Worker
{
public:
//构造函数
Manager(int id, string name, int did);
//显示个人信息
virtual void showInfo();
//获取职工岗位名称
virtual string getDeptName();
};
7.WorkerManager.cpp
代码:
#include"workerMnager.h"//直接调用写好的头函数
#include"employee.h"
#include"manager.h"
#include"boss.h"
//类外实现构造函数
WorkerManager::WorkerManager()
{
//1。文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);//读文件
if (!ifs.is_open())
{
//cout<<"文件不存在"<<endl;
this->EmpNum = 0;
this->Emp_Arr = NULL;
this->FileIsEmpty = true;
ifs.close();
return;
}
//2.文件存在,数据为空
char ch;
ifs >> ch;
if (ifs.eof())
{
//cout<<"文件为空"<<endl;
this->EmpNum = 0;
this->Emp_Arr = NULL;
this->FileIsEmpty = true;
ifs.close();
return;
}
//3.文件存在且数据不为空
int num = this->getEmpNum();
this->EmpNum = num;
this->Emp_Arr = new Worker * [this->EmpNum];//开辟空间
this->initEmp();//将文件中的数据读到数组中
};
//类外实现析构函数
WorkerManager::~WorkerManager()
{
if (this->Emp_Arr != NULL)
{
delete[]this->Emp_Arr;
this->Emp_Arr = NULL;
}
};
//类外实现菜单函数
void WorkerManager::Show_Menu()
{
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 WorkerManager::exitSystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
//实现员工添加功能
void WorkerManager::Add_Emp()
{
cout << "请输入添加的员工数量" << endl;
int addNum = 0;//添加人数
cin >> addNum;
if (addNum > 0)
{
int newSize = this->EmpNum + addNum;//计算添加新空间大小
Worker **newSpace = new Worker * [newSize];//开辟空间
//将原来的空间的数据拷贝到新空间上
if (this->Emp_Arr != NULL)
{
for (int i = 0; i < this->EmpNum; i++)
{
newSpace[i] = this->Emp_Arr[i];
}
}
//开始添加新的数据
for (int i = 0; i < addNum; i++)
{
int id;
string name;
int did;
cout << "请输入第" << i + 1 << "个新职工编号:" << endl;
cin >> id;
cout << "请输入职工姓名:" << endl;
cin >> name;
cout << "请选择该职工岗位" << endl;
cout << "1.普通员工" << endl;
cout << "2.经理" << endl;
cout << "3.老板" << endl;
Worker* worker_1 = NULL;
cin >> did;
switch (did)
{
case 1:
worker_1 = new Employee(id,name,did);
case 2:
worker_1 = new Manager(id, name, did);
case 3:
worker_1 = new Boss(id, name, did);
deault:
break;
}
newSpace[this->EmpNum + i] = worker_1;//将创建的职工指针保存到数组中
}
delete[]this->Emp_Arr;//释放原有空间
this->Emp_Arr = newSpace;//更改新空间的指向
this->EmpNum=newSize;//更改新空间人数
this->FileIsEmpty = false;//更新一下文件不为空的情况
cout << "成功添加" << addNum << "名新职工" << endl;
this->save();
}
else
{
cout << "数据有误" << endl;
}
system("pause");//按任意建后清屏回到上一目录
system("cls");
}
//实现保存数据到文件
void WorkerManager::save()
{
ofstream ofs;
ofs.open(FILENAME, ios:: out);//用输出的方式打开文件,就是写入到文件
for (int i = 0; i < this->EmpNum; i++)
{
ofs << this->Emp_Arr[i]->id << " "
<< this->Emp_Arr[i]->name << " "
<< this->Emp_Arr[i]->did << " " << endl;
}
ofs.close();//关闭文件
}
//统计文件中的人数
int WorkerManager::getEmpNum()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int did;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> did)
{
num++;
}
return num;
}
//初始化员工
void WorkerManager::initEmp()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);//读文件,接受数据
int id;
string name;
int did;
int index = 0;
while (ifs >> id && ifs >> name && ifs >> did)
{
Worker *worker = NULL;
if (did == 1)
{
worker = new Employee(id, name, 1);
}
else if (did == 2)
{
worker = new Manager(id, name, 2);
}
else if (did == 3)
{
worker = new Boss(id, name, 3);
}
//存放在数组中
this->Emp_Arr[index] = worker;
index++;
}
}
//显示职工
void WorkerManager::showEmp()
{
if (FileIsEmpty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
for (int i = 0; i < this->EmpNum; i++)
{
//利用多态调用接口
this->Emp_Arr[i]->showInfo();
}
}
system("pause");
system("cls");//按任意键以后回到菜单选择状态
}
//删除员工
void WorkerManager::DelEmp()
{
if (FileIsEmpty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
//按照职工编号删除
cout << "请输入想要删除职工编号:" << endl;
int id = 0;
cin >> id;
int index = this->IsExit(id);
if (index != -1)
{
for (int i = index; i < this->EmpNum - 1; i++)
{
this->Emp_Arr[i] = this->Emp_Arr[i + 1];
}
this->EmpNum--;//更新一下数组中记录的人员个数
//同步更新到文件
this->save();
cout << "删除成功" << endl;
}
else
{
cout << "删除失败,未找到该职工" << endl;
}
}
system("pause");
system("cls");//按任意键以后回到菜单选择状态
}
//判断员工是否存在
int WorkerManager::IsExit(int id)
{
int index = -1;
for (int i = 0; i < this->EmpNum; i++)
{
if (this->Emp_Arr[i]->id == id)
{
index = i;
break;
}
}
return index;
}
//修改职工
void WorkerManager::modEmp()
{
if (FileIsEmpty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
//按照职工编号删除
cout << "请输入想要修改职工编号" << endl;
int id= 0;
cin >> id;
int ret = this->IsExit(id);
if (ret != -1)
{
//查找到该编号的职工
delete this->Emp_Arr[ret];
int newid = 0;
string newname="";
int newdid = 0;
cout << "查到" << id << "号职工,请重新输入新职工号" << endl;
cin >> newid;
cout << "请输入新名字" << endl;
cin >> newname;
cout << "请输入岗位:" << endl;
cout << "1.普通职工" << endl;
cout << "2.经理" << endl;
cout << "3.老板" << endl;
cin >> newdid;
Worker* worker = NULL;
switch (newdid)
{
case 1:
worker = new Employee(newid, newname, newdid);
case 2:
worker = new Manager(newid, newname, newdid);
case 3:
worker = new Boss(newid, newname, newdid);
deault:
break;
}
//更新数据到数组中
this->Emp_Arr[ret] = worker;
cout << "修改成功" << Emp_Arr[ret]->did << endl;
this->save();
cout << "删除成功" << endl;
}
else {
cout << "修改失败,未找到该职工" << endl;
}
}
system("pause");
system("cls");
}
//查找职工
void WorkerManager::FindEmp()
{
if (FileIsEmpty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
//按照职工编号删除
cout << "请输入查找方式:" << endl;
cout << "1、按职工号查找" << endl;
cout << "2、按职工姓名查找" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
cout <<"请输入想要查找的职工编号" << endl;
int id = 0;
cin >> id;
int ret = this->IsExit(id);
if (ret != -1)
{
cout << "查找成功,该职工信息如下:" << endl;
this->Emp_Arr[ret]->showInfo();
}
else
{
cout << "修改失败,未找到该职工" << endl;
}
}
else if (select == 2)
{
cout << "请输入想要查找职工姓名:" << endl;
string name;
cin >> name;
bool flag = false;//查找到标志
for (int i = 0; i < this->EmpNum; i++)
{
if (Emp_Arr[i]->name == name)
{
cout << "查找成功,职工编号为:" << Emp_Arr[i]->id
<< ",职工的信息如下" << endl;
flag = true;
this->Emp_Arr[i]->showInfo();
}
}
if (flag == false)
{
cout << "修改i失败,未找到该职工" << endl;
}
}
else
{
cout << "输入项有误" << endl;
}
}
system("pause");
system("cls");
}
//排序职工
void WorkerManager::sortEmp()
{
if (FileIsEmpty)
{
cout << "文件不存在或记录为空" << endl;
system("pause");
system("cls");
}
else
{
cout << "请选择排序方式:" << endl;
cout << "1、按职工号升序" << endl;
cout << "2、按职工号降序:" << endl;
int select;
cin >> select;
for (int i = 0; i < this->EmpNum - 1; i++)
{
int minOrmax = i;
for (int j = i + 1; j < this->EmpNum; j++)
{
if (select == 1)//升序
{
if (this->Emp_Arr[minOrmax]->id > Emp_Arr[j]->id)
{
minOrmax = j;
}
}
else//降序
{
if (this->Emp_Arr[minOrmax]->id < Emp_Arr[j]->id)
{
minOrmax = j;
}
}
}
if (i != minOrmax)
{
Worker *temp = Emp_Arr[i];
Emp_Arr[i] = Emp_Arr[minOrmax];
Emp_Arr[minOrmax] = temp;
}
}
cout<<"排序成功,排序后结果为:"<<endl;
this->save();
this->showEmp();
}
}
//清空文件
void WorkerManager::clean_File()
{
cout << "取人清空?" << endl;
cout << "1、确认" << endl;
cout << "2、返回" << endl;
int select;
cin >> select;
if (select == 1)
{
//打开模式ios::trunc,如果存在删除文件并重新创建
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
if (this->Emp_Arr != NULL)
{
for (int i = 0; i < this->EmpNum; i++)
{
if (this->Emp_Arr[i] != NULL)
{
delete this->Emp_Arr[i];
}
}
this->EmpNum = 0;
delete[]this->Emp_Arr;
this->Emp_Arr = NULL;
this->FileIsEmpty = true;
}
cout << "清空成功" << endl;
}
system("pause");
system("cls");
}。
8.workerManager.h
代码:
#pragma once
#include<iostream>
#include<fstream>
#include"worker.h"
#define FILENAME "empFile.txt"
using namespace std;
class WorkerManager
{
public:
//构造函数
WorkerManager();
//展示菜单函数
void Show_Menu();//头函数里不能开大括号,不然系统会以为已经有了主体
//退出系统函数
void exitSystem();
//添加职工
void Add_Emp();
//记录职工人数
int EmpNum;
//职工数组指针
Worker **Emp_Arr;
//保存文件函数
void save();
//统计文件中的人数
int getEmpNum();
//判断文件是否为空
bool FileIsEmpty;
//初始化员工
void initEmp();
//显示职工
void showEmp();
//删除员工
void DelEmp();
//判断职工是否存在
int IsExit(int id);
//修改员工
void modEmp();
//查找职工
void FindEmp();
//排序员工
void sortEmp();
//清空文件
void clean_File();
//析构函数
~WorkerManager();
};
9.Worker.h
代码:#pragma once
#include<iostream>
#include<string>
using namespace std;
//职工抽象基类
class Worker
{
public:
//显示个人信息
virtual void showInfo() = 0;
//获取岗位名称
virtual string getDeptName() = 0;
int id;//职工编号
string name;//职工姓名
int did;//职工所在部门编号
};
10.职工管理系统
代码:
#include<iostream>
using namespace std;
#include"workerMnager.h"
#include"Worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
int main()
{
WorkerManager wm;
int choice = 0;
while (true)
{
//展示菜单
wm.Show_Menu();
cout << "请输入你的选择:" << endl;
cin >> choice;
switch (true)
{
case 0://退出系统
wm.exitSystem();
break;
case 1://添加职工信息
wm.Add_Emp();
break;
case 2://显示职工信息
wm.showEmp();
break;
case 3://删除职工信息
wm.DelEmp();
break;
case 4://修改职工信息
wm.modEmp();
case 5://查找职工信息
wm.FindEmp();
break;
case 6://按照编号排序
wm.sortEmp();
break;
case 7://清空所有文件
wm.clean_File();
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}
本文详细介绍了如何使用C++实现一个职工管理系统的全流程,包括创建Boss、Employee和Manager类,实现增删改查功能,并演示了如何读写文件存储数据。通过实例学习,读者可以掌握面向对象编程在实际项目中的应用。

348

被折叠的 条评论
为什么被折叠?



