服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 基于C++实现职工管理系统

基于C++实现职工管理系统

2022-12-15 14:18鎃龘䰢 C/C++

这篇文章主要为大家详细介绍了基于C++实现职工管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现职工管理系统的具体代码,供大家参考,具体内容如下

1、管理系统需求

职工管理系统可以用来管理公司内所有员工的信息

利用C++来实现一个基于多态的职工管理系统

公司中职工分为三类:普通员工、经理、老板,显示信息时,需要显示职工编号、职工姓名、职工岗位、以及职责

普通员工职责:完成经理交给的任务

经理职责:完成老板交给的任务,并下发任务给员工

老板职责:管理公司所有事务

管理系统中需要实现的功能如下:

  • 退出管理程序:退出当前管理系统
  • 增加职工信息:实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号
  • 显示职工信息:显示公司内部所有职工的信息
  • 删除离职职工:按照编号删除指定的职工
  • 修改职工信息:按照编号修改职工个人信息
  • 查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息
  • 按照编号排序:按照职工编号,进行排序,排序规则由用户指定
  • 清空所有文档:清空文件中记录的所有职工信息 (清空前需要再次确认,防止误删)

我们创建以下多个头文件和与其对应的 .cpp文件:

基于C++实现职工管理系统

boss.h头文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include <iostream>
using namespace std;
#include <string>
#include "Worker.h"
 
//老板类
class Boss :public Worker
{
public:
 
    Boss(int id, string name, int post);
 
    //显示个人信息
    virtual void ShowInfo();
 
    //获取岗位名称
    virtual string getPost();
 
};

manager.h 头文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
class Manager :public Worker
{
public:
 
    Manager(int id, string name, int post);
 
    //显示个人信息
    virtual void ShowInfo();
 
    //获取岗位名称
    virtual string getPost();
 
};

employee.h头文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
class Employee:public Worker
{
public:
 
    Employee(int id,string name,int post);
 
    //显示个人信息
    virtual void ShowInfo();
 
    //获取岗位名称
    virtual string getPost();
 
};

worker.h头文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//职工抽象类
class Worker
{
public:
    //显示个人信息
    virtual void ShowInfo() = 0;
 
    //获取岗位名称
    virtual string getPost() = 0;
 
    //职员编号
    int m_Id;
    //职工姓名
    string m_Name;
    //部门编号
    int m_Post;
};

worker_Manager.h头文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once
#include <iostream>
using namespace std;
#include "boss.h"
#include "manager.h"
#include "Worker.h"
#include "employee.h"
#include <fstream>
#define TXT "empfine.txt"
 
//各程序执行的类
class Worker_Manager
{
public:
    Worker_Manager();
 
    //显示菜单
    void ShowMenu();
 
    //记录职工人数
    int m_EmpNum;
 
    //职工指针数组
    Worker** m_EmpArray;
 
    //添加职工
    void Add_Emp();
 
    //保存文件
    void save();
 
    //判断文件是否为空
    bool m_File;
 
    //统计文件中的人数
    int get_EmpNum();
 
    //初始化员工
    void In_Emp();
 
    //显示员工
    void Show_Emp();
 
    //判断职工是否存在
    int Ison_Emp(int id);
 
    //删除员工
    void Del_Emp();
 
    //修改员工
    void Mod_Emp();
 
    //查找员工
    void Find_Emp();
 
    //排序
    void Sort_Emp();
 
    //清空文件
    void Clean_File();
 
 
    ~Worker_Manager();
 
};

boss.cpp文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "boss.h"
 
Boss::Boss(int id, string name, int post)
{
    this->m_Id = id;
    this->m_Name = name;
    this->m_Post = post;
}
 
//显示个人信息
void Boss::ShowInfo()
{
    cout << "职工编号:" << this->m_Id
        << "\t职工姓名:" << this->m_Name
        << "\t职工部门:" << this->getPost()
        << "\t岗位职责:管理公司所有事务" << endl;
}
 
//获取岗位名称
string Boss::getPost()
{
    return string("老板");
}

manager.cpp文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "manager.h"
 
Manager::Manager(int id, string name, int post)
{
    this->m_Id = id;
    this->m_Name = name;
    this->m_Post = post;
}
 
//显示个人信息
void Manager::ShowInfo()
{
    cout << "职工编号:" << this->m_Id
        << "\t职工姓名:" << this->m_Name
        << "\t职工部门:" << this->getPost()
        << "\t岗位职责:完成老板布置的任务,并下发任务给员工" << endl;
}
 
//获取岗位名称
string Manager::getPost()
{
    return string("经理");
}

employee.cpp文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "employee.h"
 
Employee::Employee(int id, string name, int post)
{
    this->m_Id = id;
    this->m_Name = name;
    this->m_Post = post;
}
 
//显示个人信息
void Employee::ShowInfo()
{
    cout << "职工编号:" << this->m_Id
        << "\t职工姓名:" << this->m_Name
        << "\t职工部门:" << this->getPost()
        << "\t岗位职责:完成经理布置的任务" << endl;
}
 
//获取岗位名称
string Employee::getPost()
{
    return string("员工");
}

worker_Manager.cpp文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include "worker_Manager.h"
 
Worker_Manager::Worker_Manager()
{
    //1、文件不存在
    ifstream ifs;
    ifs.open(TXT, ios::in);
    if (!ifs.is_open())
    {
        //cout << "文件不存在" << endl;
        //初始化属性
        //初始化记录人数
        this->m_EmpNum = 0;
        //初始化指针数组
        this->m_EmpArray = NULL;
        //初始化文件是否为空
        this->m_File = true;
        ifs.close();
        return;
    }
    
    //2、文件存在,但为空
    char ch;
    ifs >> ch;
    if (ifs.eof())
    {
        //cout << "文件为空" << endl;
        //初始化属性
        //初始化记录人数
        this->m_EmpNum = 0;
        //初始化指针数组
        this->m_EmpArray = NULL;
        //初始化文件是否为空
        this->m_File = true;
        ifs.close();
        return;
    }
 
    //3.文件存在且数据不为空
    int num = this->get_EmpNum();
    this->m_EmpNum = num;
 
    //开辟空间
    this->m_EmpArray = new Worker * [this->m_EmpNum];
    //将文件中的数据,存到数组中
    this->In_Emp();
}
 
void Worker_Manager::ShowMenu()
{
    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;
}
 
 
void Worker_Manager::Add_Emp()
{
    cout << "请输入要添加职工数量:" << endl;
 
    //保存用户的输入数量
    int addNum = 0;
    cin >> addNum;
 
    if (addNum > 0)
    {
        //计算添加新空间的大小
        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];
            }
        }
 
        //添加新数据
        for (int i = 0; i < addNum; i++)
        {
            int id;
            string name;
            int post;
            cout << "请输入第 " << i + 1 << "个新职工的编号: " << endl;
            cin >> id;
 
            cout << "请输入第 " << i + 1 << "个新职工的姓名: " << endl;
            cin >> name;
 
            cout << "请选择职工岗位: " << endl;
            cout << "1、普通员工" << endl;
            cout << "2、经理" << endl;
            cout << "3、老板" << endl;
            cin >> post;
 
            Worker* worker = NULL;
            switch (post)
            {
            case 1:
                worker = new Employee(id, name, post);
                break;
            case 2:
                worker = new Manager(id, name, post);
                break;
            case 3:
                worker = new Boss(id, name, post);
                break;
            default:
                cout << "错误输入" << endl;
                break;
            }
            //将创建的职工指针,保存到数组中
            newSpace[this->m_EmpNum + i] = worker;
        }
        //释放原指针
        delete[] this->m_EmpArray;
 
        //更改新空间的指向
        this->m_EmpArray = newSpace;
 
        //更新职工人数
        this->m_EmpNum = newSize;
 
        cout << "成功添加" << addNum << "名新职工" << endl;
 
        this->m_File = false;
 
        //保存数据到文件中
        this->save();
    }
    else
    {
        cout << "输入有误" << endl;
    }
    system("pause");
    system("cls");
}
 
void Worker_Manager::save()
{
    ofstream ofs;
    ofs.open(TXT, ios::out);
    for (int i = 0; i < this->m_EmpNum; i++)
    {
        ofs << this->m_EmpArray[i]->m_Id<<" "
            << this->m_EmpArray[i]->m_Name<<" "
            << this->m_EmpArray[i]->m_Post<<" "
            << endl;
    }
    ofs.close();
}
 
 
int Worker_Manager::get_EmpNum()
{
    ifstream ifs;
    ifs.open(TXT, ios::in);
    int id;
    string name;
    int post;
 
    int num = 0;
    while (ifs >> id && ifs >> name && ifs >> post)
    {
        num++;
    }
    return num;
}
 
void Worker_Manager::In_Emp()
{
    ifstream ifs;
    ifs.open(TXT, ios::in);
 
    int id;
    string name;
    int post;
 
    int index = 0;
    while (ifs>>id && ifs>>name && ifs>>post)
    {
        Worker* worker = NULL;
        if (post == 1)
        {
            worker = new Employee(id, name, post);//普通员工
        }
        else if (post == 2)
        {
            worker = new Manager(id, name, post);//经理
        }
        else
        {
            worker = new Boss(id, name, post);//老板
        }
        this->m_EmpArray[index] = worker;
        index++;
    }
    ifs.close();
}
 
void Worker_Manager::Show_Emp()
{
    if (this->m_File)
    {
        cout << "该文件不存在或为空" << endl;
    }
    else
    {
        for (int i = 0; i < this->m_EmpNum; i++)
        {
            //利用多态调用
            this->m_EmpArray[i]->ShowInfo();
        }
    }
    //清屏
    system("pause");
    system("cls");
}
 
int Worker_Manager::Ison_Emp(int id)
{
    int index = -1;
    for (int i = 0; i < this->m_EmpNum; i++)
    {
        if (this->m_EmpArray[i]->m_Id == id)
        {
            index = i;
            break;
        }
    }
    return index;
}
 
void Worker_Manager::Del_Emp()
{
    if (this->m_File)
    {
        cout << "该文件不存在或为空" << endl;
    }
    else
    {
        cout << "请输入你要删除员工的编号:" << endl;
        int id;
        cin >> id;
        int ret = this->Ison_Emp(id);
        //职工存在,且在index位置上
        if (ret != -1)
        {
            for (int i = ret; i < this->m_EmpNum - 1; i++)
            {
                //数据前移
                this->m_EmpArray[i] = this->m_EmpArray[i + 1];
            }
            //更新人数
            this->m_EmpNum--;
            cout << "删除成功" << endl;
 
            //同步到文件中
            this->save();
        }
        else
        {
            cout << "删除失败" << endl;
        }
    }
    
    system("pause");
    system("cls");
}
 
void Worker_Manager::Mod_Emp()
{
    if (this->m_File)
    {
        cout << "该文件不存在或为空" << endl;
    }
    else
    {
        int id = 0;
        cout << "输入你要修改的员工编号:" << endl;
        cin >> id;
        int ret = this->Ison_Emp(id);
        if (ret != -1)
        {
            delete this->m_EmpArray[ret];
            int Newid = 0;
            string name="";
            int post = 0;
            cout << "查到: " << id << "号员工,请输入新的员工编号: " << endl;
            cin >> Newid;
 
            cout << "查到: " << id << "号员工,请输入新的员工姓名: " << endl;
            cin >> name;
 
            cout << "请输入新岗位" << endl;
            cout << "1、普通员工" << endl;
            cout << "2、经理" << endl;
            cout << "3、老板" << endl;
            cin >> post;
 
            Worker* worker = NULL;
            switch (post)
            {
            case 1:
                worker = new Employee(Newid, name, post);
                break;
            case 2:
                worker = new Manager(Newid, name, post);
                break;
            case 3:
                worker = new Boss(Newid, name, post);
                break;
            default:
                break;
            }
            //将修改的数据放回原来位置
            this->m_EmpArray[ret] = worker;
            cout << "修改成功" << endl;
 
            this->save();
 
        }
        else
        {
            cout << "修改失败" << endl;
        }
    }
 
    system("pause");
    system("cls");
}
 
 
void Worker_Manager::Find_Emp()
{
    if (this->m_File)
    {
        cout << "该文件不存在或为空" << endl;
    }
    else
    {
        cout << "请选择查找的方式" << endl;
        cout << "1、按编号查找" << endl;
        cout << "2、按姓名查找" << endl;
        int input = 0;
        cin >> input;
        if (input == 1)
        {
            //按编号查找
            int id = 0;
            cout << "请输入你要查找的编号为:" << endl;
            cin >> id;
            int ret = this->Ison_Emp(id);
            if (ret != -1)
            {
                cout << "查找到此人信息  如下:" << endl;
                this->m_EmpArray[ret]->ShowInfo();
            }
            else
            {
                cout << "查无此人" << endl;
            }
        }
        else if(input==2)
        {
            //按姓名查找
            string name;
            cout << "请输入你要查找人的姓名: " << endl;
            cin >> name;
 
            bool flag = false;
            for (int i = 0; i < this->m_EmpNum; i++)
            {
                if (this->m_EmpArray[i]->m_Name == name)
                {
                    cout << "成功找到员工,员工编号为:" << this->m_EmpArray[i]->m_Id
                        << "此员工信息如下" << endl;
                    
                    this->m_EmpArray[i]->ShowInfo();
                    flag = true;
                }
                if (flag == false)
                {
                    cout << "查无此人" << endl;
                }
            
        }
        else
        {
            cout << "输入选项有误" << endl;
        }
    }
    system("pause");
    system("cls");
}
 
void Worker_Manager::Sort_Emp()
{
    if (this->m_File)
    {
        cout << "该文件不存在或为空" << endl;
        system("pause");
        system("cls");
    }
    else
    {
        cout << "请选择排序方式" << endl;
        cout << "1、按职工号升序" << endl;
        cout << "2、按职工号降序" << endl;
        int input = 0;
        cin >> input;
        for (int i = 0; i < this->m_EmpNum; i++)
        {
            int minOrmax = i;
            for (int j = i + 1; j < this->m_EmpNum; j++)
            {
                if (input == 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 (minOrmax != i)
            {
                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 Worker_Manager::Clean_File()
{
    cout << "确定清空?" << endl;
    cout << "1、确定" << endl;
    cout << "2、取消" << endl;
 
    int input = 0;
    cout << "请输入: " << endl;
    cin >> input;
 
    if (input == 1)
    {
        ofstream ofs;
        ofs.open(TXT, 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_File = true;
            this->m_EmpNum = 0;
        }
        cout << "清空成功" << endl;
    }
    system("pause");
    system("cls");
}
 
 
Worker_Manager::~Worker_Manager()
{
    if (this->m_EmpArray != NULL)
    {
        for (int i = 0; i < this->m_EmpNum; i++)
        {
            if (this->m_EmpArray[i] != NULL)
            {
                delete this->m_EmpArray[i];
            }
        }
        delete[]this->m_EmpArray;
        this->m_EmpArray = NULL;
    }
}

职工管理系统.cpp文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;
#include <string>
#include "worker_Manager.h"
#include "Worker.h"
#include "employee.h"
#include "boss.h"
#include "manager.h"
 
int main()
{
    //测试代码
    //Worker* worker = NULL;
    //worker = new Employee(1, "张三", 1);
    //worker->ShowInfo();
    //delete worker;
 
    // worker = new Manager(2, "张三", 2);
    //worker->ShowInfo();
    //delete worker;
 
    //worker = new Boss(3, "张三", 3);
    //worker->ShowInfo();
    //delete worker;
 
    //实例化对象
    Worker_Manager wm;
    
    int input = 0;
    do
    {
        wm.ShowMenu();
        cout << "请输入你的选择:" << endl;
        cin >> input;
        switch (input)
        {
        case 0:
            cout << "欢迎下次使用!" << endl;
            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;
        }
 
    } while (input);
 
    system("pause");
 
    return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_52237916/article/details/113881918

延伸 · 阅读

精彩推荐