-
Notifications
You must be signed in to change notification settings - Fork 0
/
workmanege.cpp
496 lines (426 loc) · 8.35 KB
/
workmanege.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
#include"workmanege.h"
workmanege::workmanege()
{
ifstream ifs;
ifs.open(FILENAME, ios::in); //读文件
//文件不存在
if (!ifs.is_open())
{
cout << "文件不存在。" << endl;
m_EmpNum = 0;
m_EmpArray = NULL;
m_FileIsEmpty = true;
ifs.close();
return;
}
char ch;
ifs >> ch;
//文件为空
if (ifs.eof())
{
cout << "文件为空。" << endl;
m_EmpNum = 0;
m_EmpArray = NULL;
m_FileIsEmpty = true;
ifs.close();
return;
}
//文件存在
int num = getEmpNum();
cout << "职工人数为:" << num << endl;
this->m_EmpNum = num;
this->m_EmpArray = new worker * [this->m_EmpNum]; //开辟空间
this->init_Emp(); //将文件读取到开辟的堆区空间
}
workmanege::~workmanege()
{
if (m_EmpArray != NULL)
{
for (int i = 0; i < m_EmpNum; i++)
{
if (m_EmpArray[i] != NULL)
{
delete m_EmpArray[i];
m_EmpArray[i] = NULL;
}
}
delete[] m_EmpArray;
m_EmpArray = NULL;
}
}
void workmanege::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;
cout << "********************************************" << endl;
cout << endl;
}
void workmanege::exitsystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
void workmanege:: addEmp()
{
cout << "请输入要添加的职工人数:"<<endl;
int addnum = 0; //保存用户要添加的职工的人数
cin >> addnum;
if (addnum > 0)
{
//计算添加新空间大小
int newSize = this->m_EmpNum + addnum; //新空间大小等于原来的人数加上新添加的人数。
//开辟新空间
worker** newspace = new worker* [newSize];
//将原来的数据拷贝到新空间
if (m_EmpArray != NULL)
{
for (int i = 0; i < m_EmpNum; i++)
{
newspace[i] = m_EmpArray[i];
}
}
//批量添加新数据
for (int i = 0; i < addnum; i++)
{
int id; //职工ID
string name; //姓名
int dselect; //部门编号
cout << "请输入第" << i + 1 << "个职工的编号:" << endl;
cin >> id;
cout << "请输入第" << i + 1 << "个职工的姓名:" << endl;
cin >> name;
cout << "请选择该职工的岗位:" << endl;
cout << "1、普通职工" << endl;
cout << "2、经理" << endl;
cout << "3、老板" << endl;
cin >> dselect;
worker* worker = NULL;
switch (dselect)
{
case 1:
worker = new Employee(id, name, dselect);
break;
case 2:
worker = new Manager(id, name, dselect);
break;
case 3:
worker = new Boss(id, name, dselect);
break;
default:
break;
}
//将创建的职工信息保存到数组中
newspace[this->m_EmpNum + i] = worker;
}
//释放原有空间
delete[] m_EmpArray;
//记录新空间地址
m_EmpArray = newspace;
//更新员工信息
m_EmpNum = newSize;
//更新文件不为空标志
m_FileIsEmpty = false;
cout << "成功添加"<<addnum<< "名员工。"<< endl;
this->save();
}
else
{
cout << "输入有误" << endl;
}
system("pause");
system("cls");
}
void workmanege::save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);
for (int i = 0; i < m_EmpNum; i++)
{
ofs << m_EmpArray[i]->m_ID << " " << m_EmpArray[i]->m_name << " " << m_EmpArray[i]->m_DeptID << endl;
}
ofs.close();
}
int workmanege::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 workmanege::init_Emp()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int Did;
int index = 0;
while (ifs >> id && ifs >> name && ifs >> Did)
{
worker* work = NULL;
if (Did == 1)
{
work = new Employee(id, name, Did);
}
else if (Did == 2)
{
work = new Manager(id, name, Did);
}
else if (Did == 3)
{
work = new Boss(id, name, Did);
}
this->m_EmpArray[index] = work;
index++;
}
ifs.close();
}
void workmanege::showEmp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或者为空。" << endl;
}
else
{
for (int i = 0; i < this->m_EmpNum; i++)
{
this->m_EmpArray[i]->showInfo();
}
}
system("pause");
system("cls");
}
int workmanege::isExist(int id)
{
int index = -1;
for (int i = 0; i < m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_ID == id)
{
index = i;
break;
}
}
return index;
}
void workmanege::delEmp()
{
if (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");
}
void workmanege::changeEmp()
{
if (m_FileIsEmpty)
{
cout << "文件不存在或记录为空。" << endl;
}
else
{
cout << "请输入要修改的员工的编号:" << endl;
int id = 0;
string name = "";
int Did = 0;
int dselect = 0;
cin >> id;
int index = this->isExist(id);
if (index != -1)
{
delete this->m_EmpArray[index];
cout << "请输入新的编号:" << endl;
cin >> id;
cout << "请输入新的姓名:" << endl;
cin >> name;
cout << "请选择该职工请输入新的岗位:" << endl;
cout << "1、普通职工" << endl;
cout << "2、经理" << endl;
cout << "3、老板" << endl;
cin >> dselect;
worker* worker = NULL;
switch (dselect)
{
case 1:
worker = new Employee(id, name, dselect);
break;
case 2:
worker = new Manager(id, name, dselect);
break;
case 3:
worker = new Boss(id, name, dselect);
break;
default:
break;
}
this->m_EmpArray[index] = worker;
this->save();
cout << "修改成功。" << endl;
}
else
{
cout << "查无此人。" << endl;
}
}
system("pause");
system("cls");
}
void workmanege::findEmp()
{
bool flag = false;
if (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;
for (int i = 0; i < m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_name == name)
{
cout << "查找成功,该职工信息如下:" << endl;
flag = true;
this->m_EmpArray[i]->showInfo();
}
}
if (flag == false)
{
cout << "查找失败,查无此人。" << endl;
}
}
else
{
cout << "输入有误。" << endl;
}
}
system("pause");
system("cls");
}
void workmanege::sortEmp()
{
worker* temp = NULL;
for (int i = 0; i < m_EmpNum; i++)
{
for (int j = 0; j < m_EmpNum - i - 1; j++)
{
if (this->m_EmpArray[j]->m_ID > this->m_EmpArray[j + 1]->m_ID)
{
temp = m_EmpArray[j + 1];
m_EmpArray[j+1] = m_EmpArray[j];
m_EmpArray[j] = temp;
}
}
}
cout << "排序成功。" << endl;
this->save();
cout << "是否显示排序后结果[Y/N]" << endl;
string yn;
cin >> yn;
if (yn == "y" || yn == "Y")
{
showEmp();
}
else
{
system("pause");
system("cls");
}
}
void workmanege::cleanEmp()
{
cout << "是否确定清空?[Y/N]" << endl;
string yn;
cin >> yn;
if (yn == "y" || yn == "Y")
{
ofstream ofs(FILENAME, ios::trunc); //复写文件,完成清空
ofs.close();
if (m_EmpArray != NULL)
{
for (int i = 0; i < m_EmpNum; i++)
{
if (m_EmpArray[i] != NULL)
{
delete m_EmpArray[i];
m_EmpArray[i] = NULL;
}
}
//删除堆区数组指针
delete[] m_EmpArray;
m_EmpArray = NULL;
m_EmpNum = 0;
m_FileIsEmpty = true;
}
cout << "成功清除文件。" << endl;
}
system("pause");
system("cls");
}