C++员工管理系统(封装+多态+继承+分类化+函数调用+读写文件+指针+升序降序算法等一系列知识结合)
阅读原文时间:2021年08月12日阅读:1

1 C++职工管理系统
2 该项目实现 八个 功能
3 1-增加功能 2-显示功能 3-删除功能 4-修改功能
4 5-查找功能 6-排序功能 7-清空功能 8-退出功能
5 实现多个功能使用了多个C++知识
增加功能:可以按着提示 增加员工的信息 并保存到初始化文件中,增加信息(数量 编号 姓名 岗位)
显示功能:没有存员工信息 提示 没有 有的话可以显示出来
删除功能:没有 提供没有的提示 如果有按着编号来删除
修改功能:提供修改已有的职工编号 来修改职工信息
查找功能:2种查找方法:按编号查找 按姓名查找 找到将信息显示 找不到提示信息
排序功能:按职工的编号来升序 降序 排列
清空功能:将所有保存的信息清空
退出功能:退出整个程序

1 EmployeeManagementSystem.cpp
2 /**
3 *项目名称:基于多态的职工管理系统
4 *时 间:2021.08
5 *作 者:ByteZero!-zhenglei
6 */
7
8 #include
9 #include "workerManger.h"
10 using namespace std;
11
12 #include "worker.h"
13 #include "employee.h"
14 #include"manager.h"
15 #include"boss.h"
16
17 int main()
18 {
19
20 ////测试代码
21 ////员工
22 //Worker* worker = NULL;
23 //worker = new Employee(1, "张三",1);
24 //worker->showInfo();
25 //delete worker;
26
27 ////经理
28 //worker = new Manager(2, "李四", 2);
29 //worker->showInfo();
30 //delete worker;
31
32 ////老板
33 //worker = new Boss(3, "王五", 3);
34 //worker->showInfo();
35 //delete worker;
36
37
38 //实例化管理者的对象
39 WorkerManger wm;
40
41 int choice = 0; //用来存储用户的选项
42
43
44 while (true)
45 {
46
47 //调用展示菜单的成员函数
48 wm.Show_Menu();
49
50 cout << "请输入您的选择: " << endl; 51 cin >> choice; //接收用户的选项
52
53 //选择菜单
54 switch (choice)
55 {
56 case 0: //退出系统
57 wm.ExitSystem();
58 break;
59 case 1: //增加职工
60 wm.Add_Emp();
61 break;
62 case 2: //显示职工
63 wm.Show_Emp();
64 break;
65 case 3: //删除职工
66 //{
67 // //测试
68 // int ret = wm.IsExist(5);
69 // if (ret != -1)
70 // {
71 // cout << "职工存在" << endl;
72 // }
73 // else
74 // {
75 // cout << "职工不存在" << endl;
76 // }
77 // break;
78 //}
79 wm.Del_Emp();
80 break;
81 case 4: //修改职工
82 wm.Mod_Emp();
83 break;
84 case 5: //查找职工
85 wm.Find_Emp();
86 break;
87 case 6: //排序职工
88 wm.Sort_Emp();
89 break;
90 case 7: //清空文档
91 wm.Clean_File();
92 break;
93 default:
94 system("cls"); //清屏操作
95 break;
96 }
97
98 }
99
100
101
102 system("pause");
103
104 return 0;
105
106 }

1 workerManger.cpp
2 /**
3 *项目名称:基于多态的职工管理系统
4 *时 间:2021.08
5 *作 者:ByteZero!-zhenglei
6 */
7 #include "workerManger.h"
8
9 //作用域下的构造函数(初始化)
10 WorkerManger::WorkerManger()
11 {
12 //1.文件不存在
13 ifstream ifs;
14 ifs.open(FILENAME, ios::in); //读文件
15
16 if(!ifs.is_open())
17 {
18 cout << "文件不存在!" << endl; 19 20 //初始化属性 21 //初始化记录人数 22 this->m_EmpNum = 0;
23 //初始化数组指针
24 this->m_EmpArray = NULL;
25 //初始化文件是否为空
26 this->m_FileIsEmpty = true;
27
28 ifs.close();
29 return;
30 }
31
32 //2.文件存在 数据为空
33 char ch;
34 ifs >> ch;
35 if (ifs.eof())
36 {
37 //文件为空
38 cout << "文件为空!" << endl; 39 //初始化记录人数 40 this->m_EmpNum = 0;
41 //初始化数组指针
42 this->m_EmpArray = NULL;
43 //初始化文件是否为空
44 this->m_FileIsEmpty = true;
45
46 ifs.close();
47 return;
48 }
49
50 //3.文件存在 ,并且记录数据
51 int num = this->get_EmpNum();
52 cout << "职工人数为: " << num << endl; 53 this->m_EmpNum = num;
54 //将文件中的数据 存到数组中
55 this->m_EmpArray = new Worker * [this->m_EmpNum];
56 this->init_Emp();
57
58
59 //测试代码
60 for (int i = 0; i < this->m_EmpNum; i++)
61 {
62 cout << "职工编号: " << this->m_EmpArray[i]->m_Id
63 << " 姓名: " << this->m_EmpArray[i]->m_Name
64 << " 部门编号: " << this->m_EmpArray[i]->m_DeptId << endl; 65 } 66 67 } 68 69 //展示菜单 70 void WorkerManger::Show_Menu() 71 { 72 cout << "********************************************" << endl; 73 cout << "********** 欢迎使用职工管理系统 **********" << endl; 74 cout << "************* 0.退出管理系统 *************" << endl; 75 cout << "************* 1.增加职工信息 *************" << endl; 76 cout << "************* 2.显示职工信息 *************" << endl; 77 cout << "************* 3.删除离职职工 *************" << endl; 78 cout << "************* 4.修改职工信息 *************" << endl; 79 cout << "************* 5.查找职工信息 *************" << endl; 80 cout << "************* 6.按照编号排序 *************" << endl; 81 cout << "************* 7.清空所有文档 *************" << endl; 82 cout << "********************************************" << endl; 83 cout << endl; 84 85 86 } 87 88 89 //退出系统 90 void WorkerManger::ExitSystem() 91 { 92 cout << "欢迎下次使用 See You ! " << endl; 93 system("pause"); 94 exit(0); //退出程序 95 } 96 97 98 //添加职工 99 void WorkerManger:: Add_Emp() 100 { 101 cout << "请输入添加职工的数量:" << endl; 102 103 int addNum = 0; //保存用户的输入数量 104 cin >> addNum;
105
106
107 if (addNum > 0)
108 {
109 //添加
110 //计算添加空间大小
111 //新空间 = 原来记录人数 + 新增人数
112 int newSize = this->m_EmpNum + addNum;
113
114 //开辟新空间
115 Worker** newSpace = new Worker * [newSize];
116
117
118 //将原来的空间下数据,拷贝到新空间下
119 if (this->m_EmpArray != NULL)
120 {
121 for (int i = 0; i < this->m_EmpNum; i++)
122 {
123 newSpace[i] = this->m_EmpArray[i];
124 }
125 }
126
127 //批量添加新数据
128 for (int i = 0; i < addNum; i++) 129 { 130 int id; //职工编号 131 string name; //职工姓名 132 int dSelect; //部门选择 133 134 cout << "请输入第 " << i + 1 << " 个新职工编号: " << endl; 135 cin >> id;
136 /*int ret= IsExist(id);
137 if (ret == -1)
138 {
139 cout << "添加成功!" << endl; 140 } 141 else 142 { 143 cout << "已有Id,添加失败!!\n请重新添加:" << endl; 144 return; 145 146 }*/ 147 148 cout << "请输入第 " << i + 1 << " 个新职工姓名: " << endl; 149 cin >> name;
150
151 cout << "请选择该职工岗位: " << endl; 152 cout << "1,普通职工" << endl; 153 cout << "2,经理" << endl; 154 cout << "3,老板" << endl; 155 cin >> dSelect;
156
157 Worker* worker = NULL;
158 switch (dSelect)
159 {
160 case 1:
161 worker = new Employee(id, name, 1);
162 break;
163 case 2:
164 worker = new Manager(id, name, 2);
165 break;
166 case 3:
167 worker = new Boss(id, name, 3);
168 break;
169
170 default:
171 break;
172 }
173 //将创建职工指针 保存到数组中
174 newSpace[this->m_EmpNum + i] = worker;
175
176
177 }
178
179 //释放原有的空间
180 delete[] this->m_EmpArray;
181
182 //更改新空间的指向
183 this->m_EmpArray = newSpace;
184
185 //更新新的职工人数
186 this->m_EmpNum = newSize;
187
188 //更新职工不为空标志
189 this->m_FileIsEmpty = false;
190
191 //成功添加后 保存文件中
192
193 //提示添加成功
194 cout << "成功添加" << addNum << "名新职工!" << endl; 195 196 //保存数据到文件中 197 this->save();
198
199 }
200 else
201 {
202 cout << "输入数据有误" << endl; 203 } 204 //按任意键后 清屏 回到上级目录 205 system("pause"); 206 system("cls"); 207 } 208 209 //保存 210 void WorkerManger::save() 211 { 212 ofstream ofs; 213 //用输出的方式打开文件 ----写文件 214 ofs.open(FILENAME, ios::out); 215 216 //将每个人的数据写入到文件中 217 for (int i = 0; i < this->m_EmpNum; i++)
218 {
219 ofs << this->m_EmpArray[i]->m_Id << " " 220 << this->m_EmpArray[i]->m_Name << " " 221 << this->m_EmpArray[i]->m_DeptId << endl; 222 } 223 224 //关闭文件 225 ofs.close(); 226 227 228 229 } 230 231 //统计文件中人数 232 int WorkerManger:: get_EmpNum() 233 { 234 ifstream ifs; 235 ifs.open(FILENAME, ios::in); //打开文件 读 236 237 int id; 238 string name; 239 int dId; 240 int num = 0; 241 while (ifs >> id && ifs >> name&& ifs >>dId)
242 {
243 //统计人数
244 num++;
245 }
246 return num;
247
248
249
250 }
251 //初始化员工
252 void WorkerManger::init_Emp()
253 {
254 ifstream ifs;
255 ifs.open(FILENAME, ios::in);
256
257 int id;
258 string name;
259 int dId;
260
261 int index = 0;
262
263 while (ifs >> id && ifs >> name && ifs >> dId)
264 {
265 Worker* worker = NULL;
266
267 if (dId == 1) ///普通职工
268 {
269 worker = new Employee(id, name, dId);
270 }
271 else if (dId == 2) //经理
272 {
273 worker = new Manager(id, name, dId);
274 }
275 else //老板
276 {
277 worker = new Boss(id, name, dId);
278 }
279 this->m_EmpArray[index] = worker;
280 index++;
281 }
282
283
284 //关闭文件
285 ifs.close();
286 }
287
288 //显示职工
289 void WorkerManger::Show_Emp()
290 {
291 //判断文件是否为空
292 if (this->m_FileIsEmpty)
293 {
294 cout << "文件不存在或者记录为空!" << endl; 295 } 296 else 297 { 298 for (int i = 0; i < m_EmpNum; i++) 299 { 300 //利用多态调用程序接口 301 this->m_EmpArray[i]->showInfo();
302
303
304 }
305 }
306 //按任意键清屏
307 system("pause");
308 system("cls");
309 }
310
311 //删除职工
312 void WorkerManger::Del_Emp()
313 {
314 if (this->m_FileIsEmpty)
315 {
316 cout << "文件不存在或者记录为空!" << endl; 317 } 318 else 319 { 320 //按照职工编号删除 321 cout << "请输入删除的职工编号!: " << endl; 322 int id = 0; 323 cin >> id;
324
325 int index = this->IsExist(id);
326 if (index != -1) //说明职工存在,并且要删掉index位置上的职工
327 {
328
329 for (int i = index; i < this->m_EmpNum - 1; i++)
330 {
331 //数据前移
332 this->m_EmpArray[i] = this->m_EmpArray[i + 1];
333
334 }
335
336 //更新数组中记录人员个数
337 this->m_EmpNum--;
338
339 //数据同步更新到文件中
340 this->save();
341
342 cout << "删除成功!" << endl; 343 344 } 345 else 346 { 347 cout << "删除失败,未找到该职工!" << endl; 348 } 349 } 350 351 //按任意键 清屏 352 system("pause"); 353 system("cls"); 354 } 355 356 //判断职工是否存在 如果存在返回数组中的位置 不存在返回-1 357 int WorkerManger::IsExist(int id) 358 { 359 int index = -1; 360 for (int i = 0; i < this->m_EmpNum; i++)
361 {
362 if (this->m_EmpArray[i]->m_Id == id)
363 {
364 //找到职工
365 index = i;
366
367 break;
368 }
369 }
370 return index;
371 }
372
373
374 //修改职工
375 void WorkerManger::Mod_Emp()
376 {
377 if (this->m_FileIsEmpty)
378 {
379 cout << "文件不存在或记录为空!" << endl; 380 } 381 else 382 { 383 cout << "请输入修改职工的id编号:" << endl; 384 385 int id; 386 cin >> id;
387
388 int ret = this->IsExist(id);
389
390 if (ret != -1)
391 {
392 //查找到编号的职工
393 delete this->m_EmpArray[ret];
394
395 int newId = 0;
396 string newName = "";
397 int dSelect = 0;
398
399 cout << "已经查到: " << id << "号职工,\n请输入新职工号: " << endl; 400 cin >> newId;
401
402 cout << "请输入新姓名: " << endl; 403 cin >> newName;
404
405 cout << "请输入岗位:" << endl; 406 cout << "1,普通职工" << endl; 407 cout << "2,经理" << endl; 408 cout << "3,老板" << endl; 409 410 cin >> dSelect;
411
412 Worker* worker = NULL;
413
414 switch (dSelect)
415 {
416 case 1:
417 worker = new Employee(newId, newName, dSelect);
418 break;
419 case 2:
420 worker = new Manager(newId, newName, dSelect);
421 break;
422 case 3:
423 worker = new Boss(newId, newName, dSelect);
424 break;
425
426
427 default:
428 break;
429 }
430 //更新数据 到数组中
431 this->m_EmpArray[ret] = worker;
432
433 cout << "修改成功!" << endl; 434 435 436 //保存到文件中 437 this->save();
438
439 }
440 else
441 {
442 cout << "修改失败,查无此人!!" << endl; 443 } 444 } 445 446 //按任意键继续 清屏 447 system("pause"); 448 system("cls"); 449 450 } 451 452 453 //查找职工 454 void WorkerManger::Find_Emp() 455 { 456 if (this->m_FileIsEmpty)
457 {
458 cout << "文件不存在或者记录为空!" << endl; 459 } 460 else 461 { 462 cout << "请输入查找的方式: " << endl; 463 cout << "1,按照职工的编号查找 " << endl; 464 cout << "2,按照职工的姓名查找 " << endl; 465 466 int select = 0; 467 cin >> select;
468
469 if (select == 1)
470 {
471 //按照编号查
472 int id;
473 cout << "请输入查找的职工编号: " << endl; 474 cin >> id;
475
476 int ret = IsExist(id);
477 if (ret != -1)
478 {
479 //找到了这个职工
480 cout << "查找成功!\n该职工信息如下: " << endl; 481 this->m_EmpArray[ret]->showInfo();
482 }
483 else
484 {
485 cout << "查找失败,查无此人!" << endl; 486 } 487 488 489 490 } 491 else if (select == 2) 492 { 493 //按照姓名查 494 string name; 495 cout << "请输入查找的姓名:" << endl; 496 cin >> name;
497
498 //加入判断是否查到的标志
499 bool flag = false; //默认未找到
500
501
502 for (int i = 0; i < m_EmpNum; i++) 503 { 504 if (this->m_EmpArray[i]->m_Name == name)
505 {
506 cout << "查找工作,职工编号为: " 507 << this->m_EmpArray[i]->m_Id
508 << " 号\n职工信息如下:" << endl; 509 510 flag = true; 511 512 //调用显示信息接口 513 this->m_EmpArray[i]->showInfo();
514 }
515 }
516 if (flag == false)
517 {
518 cout << "查找失败,查无此人!" << endl; 519 } 520 521 } 522 else 523 { 524 //输入错误 525 cout << "输入选项有误,重新输入!" << endl; 526 } 527 } 528 529 //按任意键清屏 530 system("pause"); 531 system("cls"); 532 } 533 534 535 //排序函数 升序或降序 536 void WorkerManger::Sort_Emp() 537 { 538 if (this->m_FileIsEmpty)
539 {
540 cout << "文件不存在或记录为空!" << endl; 541 542 system("pause"); 543 system("cls"); 544 } 545 else 546 { 547 cout << "请选择排序方式:" << endl; 548 cout << "1,按照职工号进行升序!" << endl; 549 cout << "2,按照职工号进行降序!" << endl; 550 551 552 int select = 0; 553 cin >> select;
554 for (int i = 0; i < m_EmpNum; i++) 555 { 556 int minOrMax = i; //声名 最小值 或 最大值下标 557 for (int j = i + 1; j < this->m_EmpNum; j++)
558 {
559 if (select == 1) //升序
560 {
561 if (this->m_EmpArray[minOrMax]->m_Id > this->m_EmpArray[j]->m_Id)
562 {
563 minOrMax = j;
564 }
565
566 }
567 else //降序
568 {
569 if (this->m_EmpArray[minOrMax]->m_Id < this->m_EmpArray[j]->m_Id)
570 {
571 minOrMax = j;
572 }
573 }
574
575 }
576 //判断一开始认定的最小值 或 最大值 是不计算的最小值或最大值
577 //如果不是 交换数据
578 if (i != minOrMax)
579 {
580 Worker* temp = this->m_EmpArray[i];
581 this->m_EmpArray[i] = this->m_EmpArray[minOrMax];
582 this->m_EmpArray[minOrMax] = temp;
583 }
584 }
585 cout << "排序成功!排序后的结果为: " << endl; 586 this->save(); //排序后结果保存到文件中
587 this->Show_Emp(); //展示所有职工
588 }
589 }
590
591
592 //清空文件
593 void WorkerManger::Clean_File()
594 {
595 cout << "确定清空吗?" << endl; 596 cout << "1,确定" << endl; 597 cout << "2,返回" << endl; 598 599 int select = 0; 600 cin >> select;
601
602 if (select == 1)
603 {
604 //清空文件
605 ofstream ofs(FILENAME, ios::trunc);//删除文件后重新弄创建
606 ofs.close();
607
608 if (this->m_EmpArray != NULL)
609 {
610 //删除堆区的每个职工对象
611 for (int i = 0; i < this->m_EmpNum; i++)
612 {
613 delete this->m_EmpArray[i];
614 this->m_EmpArray[i] = NULL;
615 }
616
617 //删除堆区数组指针
618 delete[] this->m_EmpArray;
619 this->m_EmpArray = NULL;
620 this->m_EmpNum = 0;
621 this->m_FileIsEmpty = true;
622 }
623
624 cout << "清空成功!" << endl; 625 } 626 system("pause"); 627 system("cls"); 628 629 630 631 } 632 633 634 //作用域下的析构函数(清理的) 635 WorkerManger::~WorkerManger() 636 { 637 if (this->m_EmpArray != NULL)
638 {
639 for (int i =0;this->m_EmpArray[i]; i++)
640 {
641 if (this->m_EmpArray[i] != NULL)
642 {
643 delete this->m_EmpArray[i];
644 }
645 }
646 delete[] this->m_EmpArray;
647 this->m_EmpArray = NULL;
648 }
649 }

1 manager.cpp
2 /**
3 *项目名称:基于多态的职工管理系统
4 *时 间:2021.08
5 *作 者:ByteZero!-zhenglei
6 */
7 #include "manager.h"
8
9 //构造函数
10 Manager::Manager(int id, string name, int dId)
11 {
12 this->m_Id = id;
13 this->m_Name = name;
14 this->m_DeptId = dId;
15 }
16
17 //显示个人信息
18 void Manager:: showInfo()
19 {
20 cout << "职工编号: " << this->m_Id
21 << "\t职工的姓名: " << this->m_Name
22 << "\t岗位: " << this->getDeptName()
23 << "\t岗位职责:完成老板交给的任务,并且给普通员工下发任务!" << endl;
24 }
25
26 //获取岗位名称
27 string Manager:: getDeptName()
28 {
29 return string("经理");
30 }

1 employee.cpp
2 /**
3 *项目名称:基于多态的职工管理系统
4 *时 间:2021.08
5 *作 者:ByteZero!-zhenglei
6 */
7 #include "employee.h"
8
9
10 //构造函数
11 Employee::Employee(int id, string name, int dId)
12 {
13 this->m_Id = id;
14 this->m_Name = name;
15 this->m_DeptId = dId;
16 }
17
18
19
20 //显示个人信息
21 void Employee::showInfo()
22 {
23 cout << "职工编号: " << this->m_Id
24 << "\t职工的姓名: " << this->m_Name
25 << "\t岗位: " << this->getDeptName()
26 << "\t岗位职责:完成经理交给的任务!" << endl;
27 }
28
29 //获取岗位名称
30 string Employee::getDeptName()
31 {
32 return string("员工");
33 }

booss.cpp
/**
*项目名称:基于多态的职工管理系统
*时 间:2021.08
*作 者:ByteZero!-zhenglei
*/
#include "boss.h"

//构造函数
Boss::Boss(int id, string name, int dId)
{
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = dId;
}

//显示个人信息
void Boss::showInfo()
{
cout << "职工编号: " << this->m_Id
<< "\t职工的姓名: " << this->m_Name
<< "\t岗位: " << this->getDeptName()
<< "\t岗位职责:管理公司所有的事务。" << endl;
}

//获取岗位名称
string Boss::getDeptName()
{
return string("老板");
}

1.添加员工功能

2.显示功能

3.删除功能

4.修改功能

5查找

6.排序

添加5个员工

7.清空文档

//0退出