C++实现职工管理系统(下)
C++实现职工管理系统(下)
大家好,今天是在博客园的第十五天,博主今天给大家带来的是职工管理系统(C++)(下)
这次的随笔记录是实现(中)结语处说的几个功能,另外新增一个
修改功能
此次要实现的功能
- 修改职工信息:按照编号修改职工个人信息
- 查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息
- 按照编号排序:按照职工编号,进行排序,排序规则由用户指定
- 清空所有文档:清空文件中记录的所有职工信息 (清空前需要再次确认,防止误删)
1.实现修改功能
思路:首先需要判断文件是否为空,接着寻找需要更正的人,接着释放掉该信息的内存,新建一块内存,将修改后的信息存入原来的位置,保存。
void WorkerInformation::Mod_Person()
{
if (this->m_FileIsEmpty)
{
cout << "该文件为空或记录为空" << endl;
}
else
{
cout << "请输入需要修改员工的员工编号:";
int a;
cin >> a;
int ret = this->IsExist(a);
if (ret != -1)
{
delete this->m_PersonArr[ret];
int newid = 0;
string newname = " ";
int newdeptid = 0;
cout << "请输入修改后的职工编号:";
cin >> newid;
cout << "请输入修改后的职工名字:";
cin >> newname;
cout << "请输入修改后职工的岗位:" << endl;
cout << "1、普通员工" << endl;
cout << "2、经理" << endl;
cout << "3、老板" << endl;
cin >> newdeptid;
Worker* worker = NULL;
switch (newdeptid)
{
case 1:
worker = new GeneralWorker(newid, newname, newdeptid);
break;
case 2:
worker = new Manage(newid, newname, newdeptid);
break;
case 3:
worker = new Boss(newid, newname, newdeptid);
break;
default:
break;
}
this->m_PersonArr[ret] = worker;
cout << "修改成功!" << endl;
this->save();
}
else
{
cout << "修改失败,查无此人" << endl;
}
}
cout << endl;
cout << "稍等请按任意键进行清屏操作!" << endl;
system("pause");
system("cls");
}
2.实现查找功能
思路:首先需要判断文件是否为空,①通过id查找,返回对应的数组下标。②通过名字查找,遍历数组,一一匹配,查找成功则显示信息。
void WorkerInformation::Find_Persom()
{
if (this->m_FileIsEmpty)
{
cout << "该文件为空或记录为空" << endl;
}
else
{
int select = 0;
cout << "1.通过职工编号查找" << endl;
cout << "2.通过职工名字查找" << endl;
cout << "请输入您要查找的方式:";
cin >> select;
if (select == 1)
{
int id = 0;
cout << "请输入需要查找的职工编号:";
cin >> id;
int ret = IsExist(id);
if (ret != -1)
{
cout << "查找成功,信息如下:" << endl;
this->m_PersonArr[ret]->Show_WI();
}
else
{
cout << "查找失败,无此人";
}
}
else if (select == 2)
{
string name = " ";
cout << "请输入需要查找的职工名字:";
cin >> name;
bool flag = false;
for (int i = 0; i < this->PersonNum; i++)
{
if (this->m_PersonArr[i]->m_Name == name)
{
cout << "查找成功,信息如下:" << endl;
this->m_PersonArr[i]->Show_WI();
flag = true;
}
}
if (flag == false)
{
cout << "查找失败,无此人!" << endl;
}
}
else
{
cout << "输入选项有误" << endl;
}
}
cout << endl;
cout << "稍等请按任意键进行清屏操作!" << endl;
system("pause");
system("cls");
}
3.实现排序功能
思路:首先需要判断文件是否为空,利用选择排序法实现升序降序。
void WorkerInformation::Sort_Person()
{
if (this->m_FileIsEmpty)
{
cout << "该文件为空或记录为空" << endl;
}
else
{
int select = 0;
cout << "1.按职工编号进行排序(升序)" << endl;
cout << "2.按职工编号进行排序(降序)" << endl;
cout << "请输入您要排序的方式:";
cin >> select;
for (int i = 0; i < this->PersonNum; i++)
{
int minOrMax = i;
for (int j = i + 1; j < this->PersonNum; j++) //升序
{
if (select == 1)
{
if (this->m_PersonArr[minOrMax]->m_Id > this->m_PersonArr[j]->m_Id)
{
minOrMax = j;
}
}
else
{
if (this->m_PersonArr[minOrMax]->m_Id < this->m_PersonArr[j]->m_Id)
{
minOrMax = j;
}
}
}
if (i != minOrMax)
{
Worker* temp = this->m_PersonArr[i];
this->m_PersonArr[i] = this->m_PersonArr[minOrMax];
this->m_PersonArr[minOrMax] = temp;
}
}
cout << "排序成功!" << endl;
this->save();
cout << "排序结果如下:" << endl;
this->show_Person();
cout << endl;
cout << "显示成功!" << endl;
}
cout << endl;
cout << "稍等请按任意键进行清屏操作!" << endl;
system("pause");
system("cls");
}
4.实现清空功能
思路:首先判断文件是否为空,再次确认是否要执行清空,如确定,遍历数组一次释放内存,将相关的标记进行置空,清零。
void WorkerInformation::Clean_Person()
{
if (this->m_FileIsEmpty)
{
cout << "该文件为空或记录为空" << endl;
}
else
{
int select = 0;
cout << "1.确认" << endl;
cout << "2.返回" << endl;
cout << "请问是否决定清空?";
cin >> select;
if (select == 1)
{
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
if (this->m_PersonArr != NULL)
{
for (int i = 0; i < this->PersonNum; i++)
{
if (this->m_PersonArr[i] != NULL)
{
delete this->m_PersonArr[i];
}
}
delete[]this->m_PersonArr;
this->PersonNum = 0;
this->m_PersonArr = NULL;
m_FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
}
cout << endl;
cout << "稍等请按任意键进行清屏操作!" << endl;
system("pause");
system("cls");
}
结语
职工管理系统到这就先告一段落了,如果之后有什么新想法,再进行修改分享出来,继续加油,小伙伴们一起进步吧


浙公网安备 33010602011771号