功能要求:
(1)添加功能:程序能够任意添加上述四类人员的记录,可提供选择界面供用户选择所要添加的人员类别,要求员工的编号要唯一,如果添加了重复编号的记录时,则提示数据添加重复并取消添加。
(2)查询功能:可根据编号、姓名等信息对已添加的记录进行查询,如果未找到,给出相应的提示信息,如果找到,则显示相应的记录信息;
(3)显示功能:可显示当前系统中所有记录,每条记录占据一行。
(4)编辑功能:可根据查询结果对相应的记录进行修改,修改时注意编号的唯一性。
(5)删除功能:主要实现对已添加的人员记录进行删除。如果当前系统中没有相应的人员记录,则提示“记录为空!”并返回操作;否则,输入要删除的人员的编号或姓名,根据所输入的信息删除该人员记录,如果没有找到该人员信息,则提示相应的记录不存。
(6)统计功能:能根据多种参数进行人员的统计。例如,统计四类人员数量以及总数, 或者统计男、女员工的数量等信息。
(7)保存功能:可将当前系统中各类人员记录存入文件中,存入方式任意。
(8)读取功能:可将保存在文件中的人员信息读入到当前系统中,供用户进行使用。
一、管理类负责的内容如下:
-
与用户的沟通菜单界面
-
对职工增删改查的操作
-
与文件的读写交互
1.1 创建文件
在头文件和源文件的文件夹下面分别创建workerManager.cpp和workerManager.h文件
workerManager.h
#pragma once//防止头文件重复包含
#include<iostream>//包含输入输出流头文件
#include<string.h>//使用标准命名空间
#include"Worker.h"
#include<fstream>
#define FILENAME "empFile.txt"
using namespace std;
class WorkManager
{
public:
//构造函数
WorkManager();
//展示菜单函数
void Show_Menu();
//退出系统
void ExitSystem();
//记录职工人数
int m_EmpNum;
//职工数组指针
Worker** m_EmpArray;
//添加职工
void Add_Emp();
//保存文件
void save();
//判断文件是否为空
bool m_FileIsEmpty;
//统计文件中的人数
int get_EmpNum();
//初始化职工
void init_Emp();
//显示职工
void Show_Emp();
//删除职工
void Del_Emp();
//判断职工是否存在
int IsExist(int id);
//修改职工
void Mod_Emp();
//查找职工
void Find_Emp();
//按照编号进行排序
void Sort_Emp();
//清空文件
void Clean_File();
//统计人数
void displayEmployeeCount();
//析构函数
~WorkManager();
};
二、菜单功能
2.1 添加成员函数
在管理类workerManager.h中添加成员函数void Show_Menu();
2.2 菜单功能实现
在管理类workerManager.cpp中实现Show_Menu()函数
void WorkManager::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;
}
三、退出功能
3.1 提供退出接口
在main函数中提供分支选择,提供每个功能接口
int main()
{
//实例化管理者对象
WorkManager wm;
int choice = 0;//用来存储用户选项
while (true)
{
//调用展示菜单成员函数
wm.Show_Menu();
cout << "请输入您的选择:" << endl;
cin >> choice;
switch (choice)
{
case 0://退出系统
wm.ExitSystem();
break;
case 1://增加职工
wm.Add_Emp();
break;
case 2://显示职工
wm.Show_Emp();
break;
case 3://删除职工
wm.Del_Emp();
break;
case 4://修改职工
wm.Mod_Emp();
break;
case 5://查找职工
wm.Find_Emp();
break;
case 6://排序职工
wm.Sort_Emp();
break;
case 7://淸空文档
wm.Clean_File();
break;
default:
system("cls");//清屏
break;
}
}
system("pause");
return 0;
}
3.2实现退出功能
在workerManager.h中提供退出系统的成员函数void ExitSystem();
在workerManager.cpp中提供具体的功能实现
void WorkManager::ExitSystem()
{
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);//退出程序
}
四、创建职工类
4.1 创建职工抽象类
职工的分类分为:经理、技术人员、销售人员、销售经理
将三种职工抽象到一个类(worker)中,利用多态管理不同职工种类
职工属性为:职工编号、职工姓名、职工所在部门编号、职工的行为:并根据不同职位计算各自的工资。
例如:
经理:拿固定工资
技术人员:按小时领取月薪
销售人员:按其当月销售额的提成领取工资
销售经理:即拿固定月薪也领取销售提成
头文件文件夹下创建文件worker.h文件并且添加如下代码
#pragma once
#include<iostream>
using namespace std;
#include<string.h>
//职工抽象类
class Worker
{
public:
//显示所有个人信息
virtual void showInfo() = 0;
//获取岗位名称
virtual string getDeptName() = 0;
//职工编号
int m_Id;
//职工姓名
string m_Name;
//部门编号
int m_DeptId;
//职工性别
string m_sex;
//职工所在部门
string m_department;
//职工所得工资
double m_hourlyRate;//小时工资
int m_hoursWorked;//工作时长
double m_monthlySalary;//月薪
double m_salesAmount;//销售额
double m_commissionRate;//提成率
virtual double calculateSalary() = 0;
virtual void save(ofstream& ofs) = 0;
};
4.2 实现
经理类:
Manager.h
#pragma once
#include<iostream>
using namespace std;
#include"Worker.h"
#include<string.h>
#include <fstream>
class Manager : public Worker
{
public:
//构造函数
Manager(int id, string name,string sex, string department, double monthlySalary, int did);
//显示所有个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
//计算工资
virtual double calculateSalary();
virtual void save(ofstream& ofs);
double monthlySalary;
};
Manager.cpp
#include"Manager.h"
//构造函数
Manager::Manager(int id, string name, string sex, string department, double monthlySalary,int did)
{
this->m_Id = id;
this->m_DeptId = did;
this->m_sex = sex;
this->m_department = department;
this->m_monthlySalary = monthlySalary;
this->m_Name = name;
}
//显示所有个人信息
void Manager:: showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工岗位:" << this->getDeptName()
<< "\t职工性名:" << this->m_Name
<< "\t职工性别: " << this->m_sex
<< "\t职工所在部门:"<<this->m_department
<< "\t职工所得工资:"<<this->calculateSalary()
<< "\t岗位职责:制定工作计划、管理和开发团队、沟通和协调" << endl;
}
//获取岗位名称
string Manager:: getDeptName()
{
return string("经理");
}
double Manager::calculateSalary()
{
return m_monthlySalary;
}
void Manager::save(ofstream& ofs)
{
ofs << " Manager " << m_Id << " " << m_DeptId << " " << m_Name << " " << m_sex << " " << m_department << " " << m_monthlySalary << " " << endl;
}
技术人员类:
technicist.h
#pragma once
#include<iostream>
using namespace std;
#include"Worker.h"
#include<string.h>
#include <fstream>
class technicist : public Worker
{
public:
//构造函数
technicist(int id, string name, string sex, string department, double hourlyRate, int hoursWorked, int did);
//显示所有个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
//计算工资
virtual double calculateSalary();
virtual void save(ofstream& ofs);
double hourlyRate;
int hoursWorked;
};
technicist.cpp
#include"technicist.h"
//构造函数
technicist::technicist(int id, string name, string sex, string department, double hourlyRate, int hoursWorked, int did)
{
this->m_Id = id;
this->m_DeptId = did;
this->m_sex = sex;
this->m_department = department;
this->m_hourlyRate = hourlyRate;
this->m_hoursWorked = hoursWorked;
this->m_Name = name;
}
//显示所有个人信息
void technicist::showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工岗位:" << this->getDeptName()
<< "\t职工姓名:" << this->m_Name
<< "\t职工性别: " << this->m_sex
<< "\t职工所在部门:" << this->m_department
<< "\t职工所得工资:" << this->calculateSalary()
<< "\t岗位职责:技术决策和项目管理、资料管理和技术创新" << endl;
}
//获取岗位名称
string technicist::getDeptName()
{
return string("技术人员");
}
double technicist::calculateSalary()
{
return m_hourlyRate * m_hoursWorked;
}
void technicist::save(ofstream& ofs)
{
ofs << " technicist " << m_Id << " " << m_DeptId << " " << m_Name << " " << m_sex << " " << m_department << " " << m_hourlyRate << " " << m_hoursWorked << endl;
}
销售人员类:
saleman.h
#pragma once
#include<iostream>
using namespace std;
#include"Worker.h"
#include<string.h>
#include <fstream>
class salesman : public Worker
{
public:
//构造函数
salesman(int id, string name, string sex, string department, double salesAmount, double commissionRate, int did);
//显示所有个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
//计算工资
virtual double calculateSalary();
virtual void save(ofstream& ofs);
//double monthlySalary; // 固定月薪
double salesAmount; // 销售额
double commissionRate; // 提成率
};
saleman.cpp
#include"salesman.h"
//构造函数
salesman::salesman(int id, string name, string sex, string department, double salesAmount, double commissionRate, int did)
{
this->m_Id = id;
this->m_DeptId = did;
this->m_sex = sex;
this->m_department = department;
this-> m_salesAmount = salesAmount;
this->m_commissionRate = commissionRate;
this->m_Name = name;
}
//显示所有个人信息
void salesman::showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工岗位:" << this->getDeptName()
<< "\t职工姓名:" << this->m_Name
<< "\t职工性别: " << this->m_sex
<< "\t职工所在部门:"<<this->m_department
<< "\t职工所得工资:"<<this->calculateSalary()
<< "\t岗位职责:产品的市场渠道开拓与销售工作、与客户保持良好沟通" << endl;
}
//获取岗位名称
string salesman::getDeptName()
{
return string("销售人员");
}
double salesman::calculateSalary()
{
return m_commissionRate / 100 * m_salesAmount ;
}
void salesman::save(ofstream& ofs)
{
ofs << " salesman " << m_Id << " " << m_DeptId << " " << m_Name << " " << m_sex << " " << m_department << " " << m_salesAmount << " " << m_commissionRate << endl;
}
销售经理类:
salesmanager.h
#pragma once
#include<iostream>
#include"Worker.h"
#include<string.h>
#include <fstream>
using namespace std;
class salesmanager :public Worker
{
public:
//构造函数
salesmanager(int id,string name, string sex, string department, double monthlySalary, double salesAmount, double commissionRate,int did);
//显示所有个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
//计算工资
virtual double calculateSalary();
virtual void save(ofstream& ofs);
double monthlySalary; // 固定月薪
double salesAmount; // 销售额
double commissionRate; // 提成率
};
salesmanager.cpp
#include"salesmanager.h"
//构造函数
salesmanager::salesmanager(int id, string name, string sex, string department, double monthlySalary, double salesAmount, double commissionRate,int did)
{
this->m_Id = id;
this->m_Name = name;
this->m_sex = sex;
this->m_department = department;
this->m_monthlySalary = monthlySalary;
this->m_salesAmount = salesAmount;
this->m_commissionRate = commissionRate;
this->m_DeptId = did;
}
//显示所有个人信息
void salesmanager:: showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工岗位:" << this->getDeptName()
<< "\t职工姓名:" << this->m_Name
<< "\t职工性别: "<< this->m_sex
<< "\t职工所在部门:"<<this->m_department
<< "\t职工所得工资:"<<this->calculateSalary()
<< "\t岗位职责:制定销售计划、销售人员的招募、选择和培训" << endl;
}
//获取岗位名称
string salesmanager:: getDeptName()
{
return string("销售经理");
}
double salesmanager::calculateSalary()
{
return m_monthlySalary+ (m_salesAmount * (m_commissionRate / 100));
}
void salesmanager::save(ofstream& ofs)
{
ofs << " salesmanager " << m_Id << " " << m_DeptId << " " << m_Name << " " << m_sex << " " << m_department << " " << m_monthlySalary << " " << m_salesAmount <<" "<<m_commissionRate<< endl;
}
五、添加职工
5.1 功能分析
分析:用户在批量创建时,可能会创建不同种类的职工
如果想将所有不同种类的员工都放到一个数组中,可以将所有员工的指针维护到一个数组里面
如果想在程序中维护这个不定长度的数组,可以将数组创建到堆区,并利用Worker**的指针维护
5.2 功能实现
在workerManager.h头文件中添加成员属性(上述已经添加)
//记录职工人数
int m_EmpNum;
//职工数组指针
Worker** m_EmpArray;
workerManager.cpp中实现
//添加职工
void WorkManager::Add_Emp() {
cout << "请输入添加职工数量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum <= 0) {
cout << "输入有误!" << endl;
return;
}
int newSize = this->m_EmpNum + addNum;
Worker** newSpace = new Worker * [newSize];
if (this->m_EmpArray != NULL) {
for (int i = 0; i < this->m_EmpNum; i++) {
newSpace[i] = this->m_EmpArray[i];
}
}
int actualAdded = 0;
for (int i = 0; i < addNum; i++) {
int id;
string name, sex, department;
double salesAmount = 0, commissionRate = 0, hourlyRate = 0, monthlySalary = 0;
int hoursWorked = 0, dSelect;
cout << "请输入第" << (actualAdded + 1) << "个职工编号:" << endl;
cin >> id;
if (this->IsExist(id) != -1) {
cout << "编号 " << id << " 已存在,添加取消。" << endl;
continue;
}
cout << "请输入第" << (actualAdded + 1) << "个职工姓名:" << endl;
cin >> name;
cout << "请输入第" << (actualAdded + 1) << "个职工性别:" << endl;
cin >> sex;
cout << "请输入第" << (actualAdded + 1) << "个职工所在部门:" << endl;
cin >> department;
//cout << "请输入第" << (actualAdded + 1) << "个职工的所得工资:" << endl;
//cin >> money;
cout << "请选择该职工岗位:" << endl;
cout << "1.经理" << endl;
cout << "2.技术人员" << endl;
cout << "3.销售人员" << endl;
cout << "4.销售经理" << endl;
cin >> dSelect;
Worker* worker = NULL;
switch (dSelect) {
case 1:
cout << "请输入固定月薪:" << endl;
cin >> monthlySalary;
worker = new Manager(id, name, sex, department, monthlySalary ,1);
break;
case 2:
cout << "请输入小时工资:" << endl;
cin >> hourlyRate;
cout << "请输入工作小时数:" << endl;
cin >> hoursWorked;
worker = new technicist(id, name, sex, department, hourlyRate, hoursWorked, 2); break;
case 3:
cout << "请输入销售额:" << endl;
cin >> salesAmount;
cout << "请输入提成率:" << endl;
cin >> commissionRate;
worker = new salesman(id, name, sex, department, salesAmount, commissionRate,3); break;
case 4:
cout << "请输入固定月薪:" << endl;
cin >> monthlySalary;
cout << "请输入销售额:" << endl;
cin >> salesAmount;
cout << "请输入提成率:" << endl;
cin >> commissionRate;
worker = new salesmanager(id, name, sex, department, monthlySalary, salesAmount, commissionRate,4); break;
default:
cout << "职位选择无效,添加取消。" << endl;
continue;
}
newSpace[this->m_EmpNum + actualAdded] = worker;
actualAdded++;
}
delete[] this->m_EmpArray; // 释放原有空间
this->m_EmpArray = newSpace; // 更新新空间指向
this->m_EmpNum += actualAdded; // 更新新的职工人数
cout << "成功添加 " << actualAdded << " 名新职工!" << endl;
this->save(); // 保存数据
this->displayEmployeeCount(); // 显示总人数
system("pause");
system("cls");
}
六、显示职工
功能:显示当前所有职工信息
6.1 显示职工函数声明
在workerManager.h中添加成员函数
//显示职工
void Show_Emp();
6.2 显示职工函数实现
//显示职工
void WorkManager::Show_Emp()
{
//判断文件是否为空
if (this->m_FileIsEmpty)
{
cout << "文件不存在或者记录为空!" << endl;
}
else
{
for (int i = 0; i < m_EmpNum; i++)
{
//利用多态来调用程序接口
this->m_EmpArray[i]->showInfo();
}
}
//按任意键清屏
system("pause");
system("cls");
}
七、删除职工
功能:进行删除操作
7.1 删除职工函数声明
在workerManager.h中添加成员函数void Del_Emp();
//删除职工
void Del_Emp();
7.2 职工是否存在函数声明
在workerManager.h中添加成员函数int IsExist(int id);
//判断职工是否存在
int IsExist(int id);
7.3 职工是否存在函数实现
//判断职工是否存在
int WorkManager::IsExist(int id)
{
//int index = -1;
for (int i = 0; i < m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Id == id)
{
//找到职工
return i;
}
}
return -1;
}
7.4 职工删除实现
//删除职工
void WorkManager::Del_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
//按照职工编号来删除职工
cout << "请输入想要删除的职工编号:" << endl;
int id = 0;
cin >> id;
int index = this->IsExist(id);
if (index != -1)//说明职工存在,需要删除index位置的职工
{
//数据前移
for (int i = index; i < this->m_EmpNum-1; i++)
{
this->m_EmpArray[i] = this->m_EmpArray[i + 1];
}
this->m_EmpNum--;//更新数组中记录人数个数
//数据同步更新到文件中
this->save();
cout << "删除成功!" << endl;
}
else
{
cout << "删除失败,未找到该职工!" << endl;
}
}
//按任意键清屏
system("pause");
system("cls");
}
八、修改职工
功能:对职工信息进行修改
8.1修改职工函数声明
在workerManager.h中添加成员函数void Mod_Emp();
//修改职工
void Mod_Emp();
8.2 修改职工函数实现
在workerManager.cpp中实现成员函数void Mod_Emp();
//修改职工
void WorkManager::Mod_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
cout << "请输入修改职工的编号:" << endl;
int id;
cin >> id;
int ret = this->IsExist(id);
if (ret != -1)
{
//查找到编号的职工
//delete this->m_EmpArray[ret];
Worker* workerToModify = this->m_EmpArray[ret];
if (workerToModify == nullptr)
{
cout << "职工信息为空,无法修改!" << endl;
return;
}
int newid = 0;
string newName;
string newsex;
string newdepartment;
double newhourlyRate, newsalesAmount = 0, newcommissionRate = 0;
int newhoursWorked = 0;
double newmonthlySalary=0;
double monthlySalary = 0;
int dSelect = 0;
cout << "查到:" << id << "号职工,请输入新职工号:" << endl;
cin >> newid;
cout << "请输入新姓名:" << endl;
cin >> newName;
cout << "请输入新性别:" << endl;
cin >> newsex;
cout << "请输入新所在部门" << endl;
cin >> newdepartment;
cout << "请输入新小时工资:" << endl;
cin >> newhourlyRate;
cout << "请输入新工作小时数" << endl;
cin >> newhoursWorked;
cout << "请输入新岗位:" << endl;
cout << "1.经理" << endl;
cout << "2.技术人员" << endl;
cout << "3.销售人员" << endl;
cout << "4.销售经理" << endl;
cin >> dSelect;
Worker* worker = NULL;
switch (dSelect)
{
case 1:
worker = new Manager(newid, newName, newsex, newdepartment,newmonthlySalary,dSelect);
break;
case 2:
worker = new technicist(newid, newName, newsex,newdepartment,newhourlyRate,newhoursWorked,dSelect);
break;
case 3:
worker = new salesman(newid, newName, newsex,newdepartment,newsalesAmount, newcommissionRate,dSelect);
break;
case 4:
worker = new salesmanager(newid, newName, newsex, newdepartment, newmonthlySalary,newsalesAmount, newcommissionRate,dSelect);
break;
default:
break;
}
//更新数据到数组中
//this->m_EmpArray[ret] = worker;
delete workerToModify; // 释放原有职工对象
this->m_EmpArray[ret] = worker; // 更新为新对象
cout << "修改成功!" << endl;
//保存到文件中
this->save();
}
else
{
cout << "查无此人!" << endl;
}
}
//清屏
system("pause");
system("cls");
}
九、查找职工
功能:提供两种查找职工方式:一种按照职工编号,一种按照职工姓名
9.1 查找职工函数声明
在workerManager.h中添加成员函数void Find_Emp();
//查找职工
void Find_Emp();
9.2 查找职工函数实现
在workerManager.cpp中实现成员函数void Find_Emp();
//查找职工
void WorkManager::Find_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
cout << "请输入查找方式:" << endl;
cout << "1.按照职工编号查找:" << endl;
cout << "2.按照职工姓名查找:" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//按照编号查
int id;
cout << "请输入用户编号:" << endl;
cin >> id;
int ret = IsExist(id);
if (ret != -1)
{
//找到职工
cout << "查找成功!该职工信息如下:" << endl;
this->m_EmpArray[ret]->showInfo();
}
else
{
cout << "查无此人!" << endl;
}
}
else if (select == 2)
{
//按照姓名查
string name;
cout << "请输入要查找的姓名:" << endl;
cin >> name;
//加入判断是否查到的标志
bool flag = false;//默认未找到
for (int i = 0; i < m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Name == name)
{
cout << "查找成功,职工编号为:" << this->m_EmpArray[i]->m_Id << "号职工信息如下:" << endl;
flag = true;
//调用显示信息接口
this->m_EmpArray[i]->showInfo();
}
}
if (flag == false)
{
cout << "查找失败,查无此人!" << endl;
}
}
else
{
cout << "输入错误!" << endl;
}
}
//按任意键清屏
system("pause");
system("cls");
}
//按照编号进行排序
void WorkManager::Sort_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
system("pause");
system("cls");
}
else
{
cout << "请选择排序方式:" << endl;
cout << "1.按照工号进行升序排列:" << endl;
cout << "2.按照工号进行降序排列:" << endl;
int select = 0;
cin >> select;
for (int i = 0; i < 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();
}
}
十、清空文件
功能:将文件中记录数据清空
10.1 清空函数声明
在workerManager.h中添加成员函数void Clean_File();
//清空文件
void Clean_File();
10.2 清空函数实现
在workerManager.cpp中实现成员函数void Clean_File();
//清空文件
void WorkManager::Clean_File()
{
cout << "确定清空吗?" << endl;
cout << "1.确定" << endl;
cout << "2.返回" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//清空文件
ofstream ofs(FILENAME, ios::trunc);//删除文件后重新创建
ofs.close();
if (this->m_EmpArray != NULL)
{
//删除堆区的每个职工
for (int i = 0; i < this->m_EmpNum; i++)
{
delete this->m_EmpArray[i];
this->m_EmpArray[i] = NULL;
}
//删除堆区数组指针
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
this->m_EmpNum = 0;
this->m_FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
WorkManager::~WorkManager()
{
if (this->m_EmpArray != NULL)
{
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
总代码:
Worker.h
#pragma once
#include<iostream>
using namespace std;
#include<string.h>
//职工抽象类
class Worker
{
public:
//显示所有个人信息
virtual void showInfo() = 0;
//获取岗位名称
virtual string getDeptName() = 0;
//职工编号
int m_Id;
//职工姓名
string m_Name;
//部门编号
int m_DeptId;
//职工性别
string m_sex;
//职工所在部门
string m_department;
//职工所得工资
double m_hourlyRate;
int m_hoursWorked;
double m_monthlySalary;
double m_salesAmount;
double m_commissionRate;
virtual double calculateSalary() = 0;
virtual void save(ofstream& ofs) = 0;
};
Manager.h
#pragma once
#include<iostream>
using namespace std;
#include"Worker.h"
#include<string.h>
#include <fstream>
class Manager : public Worker
{
public:
//构造函数
Manager(int id, string name,string sex, string department, double monthlySalary, int did);
//显示所有个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
//计算工资
virtual double calculateSalary();
virtual void save(ofstream& ofs);
double monthlySalary;
};
salesman.h
#pragma once
#include<iostream>
using namespace std;
#include"Worker.h"
#include<string.h>
#include <fstream>
class salesman : public Worker
{
public:
//构造函数
salesman(int id, string name, string sex, string department, double salesAmount, double commissionRate, int did);
//显示所有个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
//计算工资
virtual double calculateSalary();
virtual void save(ofstream& ofs);
//double monthlySalary; // 固定月薪
double salesAmount; // 销售额
double commissionRate; // 提成率
};
salesmanager.h
#pragma once
#include<iostream>
#include"Worker.h"
#include<string.h>
#include <fstream>
using namespace std;
class salesmanager :public Worker
{
public:
//构造函数
salesmanager(int id,string name, string sex, string department, double monthlySalary, double salesAmount, double commissionRate,int did);
//显示所有个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
//计算工资
virtual double calculateSalary();
virtual void save(ofstream& ofs);
double monthlySalary; // 固定月薪
double salesAmount; // 销售额
double commissionRate; // 提成率
};
technicist,h
#pragma once
#include<iostream>
using namespace std;
#include"Worker.h"
#include<string.h>
#include <fstream>
class technicist : public Worker
{
public:
//构造函数
technicist(int id, string name, string sex, string department, double hourlyRate, int hoursWorked, int did);
//显示所有个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
//计算工资
virtual double calculateSalary();
virtual void save(ofstream& ofs);
double hourlyRate;
int hoursWorked;
};
workerManager.h
#pragma once//防止头文件重复包含
#include<iostream>
#include<string.h>
#include"Worker.h"
#include<fstream>
#define FILENAME "empFile.txt"
using namespace std;
class WorkManager
{
public:
//构造函数
WorkManager();
//展示菜单函数
void Show_Menu();
//退出系统
void ExitSystem();
//记录职工人数
int m_EmpNum;
//职工数组指针
Worker** m_EmpArray;
//添加职工
void Add_Emp();
//保存文件
void save();
//判断文件是否为空
bool m_FileIsEmpty;
//统计文件中的人数
int get_EmpNum();
//初始化职工
void init_Emp();
//显示职工
void Show_Emp();
//删除职工
void Del_Emp();
//判断职工是否存在
int IsExist(int id);
//修改职工
void Mod_Emp();
//查找职工
void Find_Emp();
//按照编号进行排序
void Sort_Emp();
//清空文件
void Clean_File();
//统计人数
void displayEmployeeCount();
//析构函数
~WorkManager();
};
cp121.cpp
#include<iostream>
#include<fstream>
#include<string.h>
#include"workManager.h"
#include"Worker.h"
#include"technicist.h"
#include"Manager.h"
#include"salesman.h"
#include"salesmanager.h"
using namespace std;
int main()
{
//实例化管理者对象
WorkManager wm;
int choice = 0;//用来存储用户选项
while (true)
{
//调用展示菜单成员函数
wm.Show_Menu();
cout << "请输入您的选择:" << endl;
cin >> choice;
switch (choice)
{
case 0://退出系统
wm.ExitSystem();
break;
case 1://增加职工
wm.Add_Emp();
break;
case 2://显示职工
wm.Show_Emp();
break;
case 3://删除职工
wm.Del_Emp();
break;
case 4://修改职工
wm.Mod_Emp();
break;
case 5://查找职工
wm.Find_Emp();
break;
case 6://排序职工
wm.Sort_Emp();
break;
case 7://淸空文档
wm.Clean_File();
break;
default:
system("cls");//清屏
break;
}
}
system("pause");
return 0;
}
Manager.cpp
#include"Manager.h"
//构造函数
Manager::Manager(int id, string name, string sex, string department, double monthlySalary,int did)
{
this->m_Id = id;
this->m_DeptId = did;
this->m_sex = sex;
this->m_department = department;
this->m_monthlySalary = monthlySalary;
this->m_Name = name;
}
//显示所有个人信息
void Manager:: showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工岗位:" << this->getDeptName()
<< "\t职工性名:" << this->m_Name
<< "\t职工性别: " << this->m_sex
<< "\t职工所在部门:"<<this->m_department
<< "\t职工所得工资:"<<this->calculateSalary()
<< "\t岗位职责:制定工作计划、管理和开发团队、沟通和协调" << endl;
}
//获取岗位名称
string Manager:: getDeptName()
{
return string("经理");
}
double Manager::calculateSalary()
{
return m_monthlySalary;
}
void Manager::save(ofstream& ofs)
{
ofs << " Manager " << m_Id << " " << m_DeptId << " " << m_Name << " " << m_sex << " " << m_department << " " << m_monthlySalary << " " << endl;
}
salesman.cpp
#include"salesman.h"
//构造函数
salesman::salesman(int id, string name, string sex, string department, double salesAmount, double commissionRate, int did)
{
this->m_Id = id;
this->m_DeptId = did;
this->m_sex = sex;
this->m_department = department;
this-> m_salesAmount = salesAmount;
this->m_commissionRate = commissionRate;
this->m_Name = name;
}
//显示所有个人信息
void salesman::showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工岗位:" << this->getDeptName()
<< "\t职工姓名:" << this->m_Name
<< "\t职工性别: " << this->m_sex
<< "\t职工所在部门:"<<this->m_department
<< "\t职工所得工资:"<<this->calculateSalary()
<< "\t岗位职责:产品的市场渠道开拓与销售工作、与客户保持良好沟通" << endl;
}
//获取岗位名称
string salesman::getDeptName()
{
return string("销售人员");
}
double salesman::calculateSalary()
{
return m_commissionRate / 100 * m_salesAmount ;
}
void salesman::save(ofstream& ofs)
{
ofs << " salesman " << m_Id << " " << m_DeptId << " " << m_Name << " " << m_sex << " " << m_department << " " << m_salesAmount << " " << m_commissionRate << endl;
}
salesmanager.cpp
#include"salesmanager.h"
//构造函数
salesmanager::salesmanager(int id, string name, string sex, string department, double monthlySalary, double salesAmount, double commissionRate,int did)
{
this->m_Id = id;
this->m_Name = name;
this->m_sex = sex;
this->m_department = department;
this->m_monthlySalary = monthlySalary;
this->m_salesAmount = salesAmount;
this->m_commissionRate = commissionRate;
this->m_DeptId = did;
}
//显示所有个人信息
void salesmanager:: showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工岗位:" << this->getDeptName()
<< "\t职工姓名:" << this->m_Name
<< "\t职工性别: "<< this->m_sex
<< "\t职工所在部门:"<<this->m_department
<< "\t职工所得工资:"<<this->calculateSalary()
<< "\t岗位职责:制定销售计划、销售人员的招募、选择和培训" << endl;
}
//获取岗位名称
string salesmanager:: getDeptName()
{
return string("销售经理");
}
double salesmanager::calculateSalary()
{
return m_monthlySalary+ (m_salesAmount * (m_commissionRate / 100));
}
void salesmanager::save(ofstream& ofs)
{
ofs << " salesmanager " << m_Id << " " << m_DeptId << " " << m_Name << " " << m_sex << " " << m_department << " " << m_monthlySalary << " " << m_salesAmount <<" "<<m_commissionRate<< endl;
}
technicist.cpp
#include"technicist.h"
//构造函数
technicist::technicist(int id, string name, string sex, string department, double hourlyRate, int hoursWorked, int did)
{
this->m_Id = id;
this->m_DeptId = did;
this->m_sex = sex;
this->m_department = department;
this->m_hourlyRate = hourlyRate;
this->m_hoursWorked = hoursWorked;
this->m_Name = name;
}
//显示所有个人信息
void technicist::showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工岗位:" << this->getDeptName()
<< "\t职工姓名:" << this->m_Name
<< "\t职工性别: " << this->m_sex
<< "\t职工所在部门:" << this->m_department
<< "\t职工所得工资:" << this->calculateSalary()
<< "\t岗位职责:技术决策和项目管理、资料管理和技术创新" << endl;
}
//获取岗位名称
string technicist::getDeptName()
{
return string("技术人员");
}
double technicist::calculateSalary()
{
return m_hourlyRate * m_hoursWorked;
}
void technicist::save(ofstream& ofs)
{
ofs << " technicist " << m_Id << " " << m_DeptId << " " << m_Name << " " << m_sex << " " << m_department << " " << m_hourlyRate << " " << m_hoursWorked << endl;
}
workManager.cpp
#include"workManager.h"
#include"technicist.h"
#include"Manager.h"
#include"salesman.h"
#include"salesmanager.h"
#include <vector>
WorkManager::WorkManager()
{
//1.文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);//读文件
if (!ifs.is_open())
{
//cout << "文件不存在!" << endl;
//初始化属性
//初始化记录人数
this->m_EmpNum = 0;
//初始化数组指针
this->m_EmpArray = NULL;
//初始化文件是否为空
this->m_FileIsEmpty = true;
ifs.close();
return;
}
//2.文件存在,数据为空
char ch;
ifs >> ch;
if (ifs.eof())
{
//文件为空
//cout << "文件为空!" << endl;
//初始化属性
//初始化记录人数
this->m_EmpNum = 0;
//初始化数组指针
this->m_EmpArray = NULL;
//初始化文件是否为空
this->m_FileIsEmpty = true;
ifs.close();
return;
}
//3.当文件存在,并且记录数据
int num = this->get_EmpNum();
//cout << "职工人数为:" << num << endl;
this->m_EmpNum = num;
//开辟空间
this->m_EmpArray = new Worker * [this->m_EmpNum];
//将文件中的数据存到数组中
this->init_Emp();
this->displayEmployeeCount();
}
void WorkManager::displayEmployeeCount()
{
cout << "当前职工总人数为:" << this->m_EmpNum << endl;
}
//展示菜单
void WorkManager::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 WorkManager::ExitSystem()
{
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);//退出程序
}
//添加职工
void WorkManager::Add_Emp() {
cout << "请输入添加职工数量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum <= 0) {
cout << "输入有误!" << endl;
return;
}
int newSize = this->m_EmpNum + addNum;
Worker** newSpace = new Worker * [newSize];
if (this->m_EmpArray != NULL) {
for (int i = 0; i < this->m_EmpNum; i++) {
newSpace[i] = this->m_EmpArray[i];
}
}
int actualAdded = 0;
for (int i = 0; i < addNum; i++) {
int id;
string name, sex, department;
double salesAmount = 0, commissionRate = 0, hourlyRate = 0, monthlySalary = 0;
int hoursWorked = 0, dSelect;
cout << "请输入第" << (actualAdded + 1) << "个职工编号:" << endl;
cin >> id;
if (this->IsExist(id) != -1) {
cout << "编号 " << id << " 已存在,添加取消。" << endl;
continue;
}
cout << "请输入第" << (actualAdded + 1) << "个职工姓名:" << endl;
cin >> name;
cout << "请输入第" << (actualAdded + 1) << "个职工性别:" << endl;
cin >> sex;
cout << "请输入第" << (actualAdded + 1) << "个职工所在部门:" << endl;
cin >> department;
//cout << "请输入第" << (actualAdded + 1) << "个职工的所得工资:" << endl;
//cin >> money;
cout << "请选择该职工岗位:" << endl;
cout << "1.经理" << endl;
cout << "2.技术人员" << endl;
cout << "3.销售人员" << endl;
cout << "4.销售经理" << endl;
cin >> dSelect;
Worker* worker = NULL;
switch (dSelect) {
case 1:
cout << "请输入固定月薪:" << endl;
cin >> monthlySalary;
worker = new Manager(id, name, sex, department, monthlySalary ,1);
break;
case 2:
cout << "请输入小时工资:" << endl;
cin >> hourlyRate;
cout << "请输入工作小时数:" << endl;
cin >> hoursWorked;
worker = new technicist(id, name, sex, department, hourlyRate, hoursWorked, 2); break;
case 3:
cout << "请输入销售额:" << endl;
cin >> salesAmount;
cout << "请输入提成率:" << endl;
cin >> commissionRate;
worker = new salesman(id, name, sex, department, salesAmount, commissionRate,3); break;
case 4:
cout << "请输入固定月薪:" << endl;
cin >> monthlySalary;
cout << "请输入销售额:" << endl;
cin >> salesAmount;
cout << "请输入提成率:" << endl;
cin >> commissionRate;
worker = new salesmanager(id, name, sex, department, monthlySalary, salesAmount, commissionRate,4); break;
default:
cout << "职位选择无效,添加取消。" << endl;
continue;
}
newSpace[this->m_EmpNum + actualAdded] = worker;
actualAdded++;
}
delete[] this->m_EmpArray; // 释放原有空间
this->m_EmpArray = newSpace; // 更新新空间指向
this->m_EmpNum += actualAdded; // 更新新的职工人数
cout << "成功添加 " << actualAdded << " 名新职工!" << endl;
this->save(); // 保存数据
this->displayEmployeeCount(); // 显示总人数
system("pause");
system("cls");
}
/*void WorkManager::Add_Emp() {
cout << "请输入添加职工数量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum <= 0) {
cout << "输入有误,请输入一个正数!" << endl;
return;
}
// 使用vector来动态管理员工数组
std::vector<Worker*> newSpace;
if (this->m_EmpArray != NULL) {
for (int i = 0; i < this->m_EmpNum; i++) {
newSpace.push_back(this->m_EmpArray[i]);
}
}
int addedCount = 0; // 实际添加的员工数量
for (int i = 0; i < addNum; i++) {
int id;
int dSelect;
double money;
string name;
string department;
string sex;
cout << "请输入第 " << (addedCount + 1) << " 个职工编号:" << endl;
cin >> id;
if (this->IsExist(id) != -1)
{
cout << "编号 " << id << " 已存在,添加取消。" << endl;
continue; // 跳过本次循环
}
cout << "请输入第 " << (addedCount + 1) << " 个职工姓名:" << endl;
cin >> name;
cout << "请输入第 " << (addedCount + 1) << " 个职工性别:" << endl;
cin >> sex;
cout << "请输入第 " << (addedCount + 1) << " 个职工所在部门:" << endl;
cin >> department;
cout << "请输入第 " << (addedCount + 1) << " 个职工的工资:" << endl;
cin >> money;
cout << "请选择该职工岗位:" << endl;
cout << "1.经理" << endl;
cout << "2.技术人员" << endl;
cout << "3.销售人员" << endl;
cout << "4.销售经理" << endl;
cin >> dSelect;
Worker* worker = NULL;
switch (dSelect)
{
case 1:
worker = new Manager(id, name, sex, department, money, 1); break;
case 2:
worker = new technicist(id, name, sex, department, money, 2); break;
case 3:
worker = new salesman(id, name, sex, department, money, 3); break;
case 4:
worker = new salesmanager(id, name, sex, department, money, 3); break;
default:
cout << "职位选择无效,添加取消。" << endl;
continue;
}
if (worker != nullptr) {
newSpace.push_back(worker);
addedCount++;
}
else {
cout << "职位选择无效,添加取消。" << endl;
}
}
// 更新原有数组
delete[] this->m_EmpArray; // 释放原有内存
this->m_EmpNum = addedCount;
this->m_EmpArray = new Worker * [this->m_EmpNum]; // 分配新的内存
for (int i = 0; i < this->m_EmpNum; i++)
{
this->m_EmpArray[i] = newSpace[i];
}
cout << "成功添加 " << addedCount << " 名新职工!" << endl;
this->save();
system("pause");
system("cls");
}*/
//保存文件
void WorkManager::save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);//用输出的方式打开文件 -- 写文件
//将每个人的数据写入文件
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i] != nullptr)
{
this->m_EmpArray[i]->save(ofs);
this->m_EmpArray[i]->showInfo();
}
else
{
cout<< "Erro " << i << endl;
}
}
//关闭文件
ofs.close();
}
//统计文件中的人数
int WorkManager::get_EmpNum()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);//打开文件 读
int id;
string name;
//double money = 0;
int did;
string sex;
string department;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> did )
{
num++;
}
return num;
}
//初始化职工
void WorkManager::init_Emp()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int did;
string sex;
string department;
double hourlyRate = 0;
double salesAmount = 0;
double commissionRate = 0;
double monthlySalary = 0;
int hoursWorked=0;
int index = 0;
while (ifs >> id && ifs >> name && ifs >> did)
{
Worker* worker = NULL;
if (did == 1)
{
worker = new Manager(id, name, sex, department, monthlySalary,did);
}
else if (did == 2)
{
worker = new technicist(id, name, sex, department, hourlyRate, hoursWorked,did);
}
else if(did == 3)
{
worker = new salesman(id, name, sex, department,salesAmount, commissionRate, did);
}
else
{
worker = new salesmanager(id, name, sex, department, monthlySalary,salesAmount,commissionRate, did);
}
this->m_EmpArray[index] = worker;
index++;
}
//关闭文件
ifs.close();
}
//显示职工
void WorkManager::Show_Emp()
{
//判断文件是否为空
if (this->m_FileIsEmpty)
{
cout << "文件不存在或者记录为空!" << endl;
}
else
{
for (int i = 0; i < m_EmpNum; i++)
{
//利用多态来调用程序接口
this->m_EmpArray[i]->showInfo();
}
}
//按任意键清屏
system("pause");
system("cls");
}
//删除职工
void WorkManager::Del_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
//按照职工编号来删除职工
cout << "请输入想要删除的职工编号:" << endl;
int id = 0;
cin >> id;
int index = this->IsExist(id);
if (index != -1)//说明职工存在,需要删除index位置的职工
{
//数据前移
for (int i = index; i < this->m_EmpNum-1; i++)
{
this->m_EmpArray[i] = this->m_EmpArray[i + 1];
}
this->m_EmpNum--;//更新数组中记录人数个数
//数据同步更新到文件中
this->save();
cout << "删除成功!" << endl;
}
else
{
cout << "删除失败,未找到该职工!" << endl;
}
}
//按任意键清屏
system("pause");
system("cls");
}
//判断职工是否存在
int WorkManager::IsExist(int id)
{
//int index = -1;
for (int i = 0; i < m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Id == id)
{
//找到职工
return i;
}
}
return -1;
}
//修改职工
void WorkManager::Mod_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
cout << "请输入修改职工的编号:" << endl;
int id;
cin >> id;
int ret = this->IsExist(id);
if (ret != -1)
{
//查找到编号的职工
//delete this->m_EmpArray[ret];
Worker* workerToModify = this->m_EmpArray[ret];
if (workerToModify == nullptr)
{
cout << "职工信息为空,无法修改!" << endl;
return;
}
int newid = 0;
string newName;
string newsex;
string newdepartment;
double newhourlyRate, newsalesAmount = 0, newcommissionRate = 0;
int newhoursWorked = 0;
double newmonthlySalary=0;
double monthlySalary = 0;
int dSelect = 0;
cout << "查到:" << id << "号职工,请输入新职工号:" << endl;
cin >> newid;
cout << "请输入新姓名:" << endl;
cin >> newName;
cout << "请输入新性别:" << endl;
cin >> newsex;
cout << "请输入新所在部门" << endl;
cin >> newdepartment;
cout << "请输入新小时工资:" << endl;
cin >> newhourlyRate;
cout << "请输入新工作小时数" << endl;
cin >> newhoursWorked;
cout << "请输入新岗位:" << endl;
cout << "1.经理" << endl;
cout << "2.技术人员" << endl;
cout << "3.销售人员" << endl;
cout << "4.销售经理" << endl;
cin >> dSelect;
Worker* worker = NULL;
switch (dSelect)
{
case 1:
worker = new Manager(newid, newName, newsex, newdepartment,newmonthlySalary,dSelect);
break;
case 2:
worker = new technicist(newid, newName, newsex,newdepartment,newhourlyRate,newhoursWorked,dSelect);
break;
case 3:
worker = new salesman(newid, newName, newsex,newdepartment,newsalesAmount, newcommissionRate,dSelect);
break;
case 4:
worker = new salesmanager(newid, newName, newsex, newdepartment, newmonthlySalary,newsalesAmount, newcommissionRate,dSelect);
break;
default:
break;
}
//更新数据到数组中
//this->m_EmpArray[ret] = worker;
delete workerToModify; // 释放原有职工对象
this->m_EmpArray[ret] = worker; // 更新为新对象
cout << "修改成功!" << endl;
//保存到文件中
this->save();
}
else
{
cout << "查无此人!" << endl;
}
}
//清屏
system("pause");
system("cls");
}
//查找职工
void WorkManager::Find_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
cout << "请输入查找方式:" << endl;
cout << "1.按照职工编号查找:" << endl;
cout << "2.按照职工姓名查找:" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//按照编号查
int id;
cout << "请输入用户编号:" << endl;
cin >> id;
int ret = IsExist(id);
if (ret != -1)
{
//找到职工
cout << "查找成功!该职工信息如下:" << endl;
this->m_EmpArray[ret]->showInfo();
}
else
{
cout << "查无此人!" << endl;
}
}
else if (select == 2)
{
//按照姓名查
string name;
cout << "请输入要查找的姓名:" << endl;
cin >> name;
//加入判断是否查到的标志
bool flag = false;//默认未找到
for (int i = 0; i < m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Name == name)
{
cout << "查找成功,职工编号为:" << this->m_EmpArray[i]->m_Id << "号职工信息如下:" << endl;
flag = true;
//调用显示信息接口
this->m_EmpArray[i]->showInfo();
}
}
if (flag == false)
{
cout << "查找失败,查无此人!" << endl;
}
}
else
{
cout << "输入错误!" << endl;
}
}
//按任意键清屏
system("pause");
system("cls");
}
//按照编号进行排序
void WorkManager::Sort_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
system("pause");
system("cls");
}
else
{
cout << "请选择排序方式:" << endl;
cout << "1.按照工号进行升序排列:" << endl;
cout << "2.按照工号进行降序排列:" << endl;
int select = 0;
cin >> select;
for (int i = 0; i < 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 WorkManager::Clean_File()
{
cout << "确定清空吗?" << endl;
cout << "1.确定" << endl;
cout << "2.返回" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//清空文件
ofstream ofs(FILENAME, ios::trunc);//删除文件后重新创建
ofs.close();
if (this->m_EmpArray != NULL)
{
//删除堆区的每个职工
for (int i = 0; i < this->m_EmpNum; i++)
{
delete this->m_EmpArray[i];
this->m_EmpArray[i] = NULL;
}
//删除堆区数组指针
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
this->m_EmpNum = 0;
this->m_FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
WorkManager::~WorkManager()
{
if (this->m_EmpArray != NULL)
{
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
标签:小型,公司员工,int,EmpArray,void,信息管理系统,id,string,cout
From: https://blog.csdn.net/2302_78279100/article/details/140507443