C++练习项目

基于数组的通讯录管理系统

需要实现以下功能
1.添加联系人 向通讯录中添加新人,信息包括(姓名,性别,年龄,联系电话,家庭住址)最多记录1000人
2.显示联系人 显示通讯录中所有人的信息
3.删除联系人 按照姓名进行删除指定联系人
4.查找联系人 按照姓名查找指定联系人信息
5.修改联系人 按照姓名重新修改指定联系人
6.清空联系人 清空通讯录中所有信息
7.退出通讯录 退出当前使用的通讯录

我的:

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
#include<iostream>
#include<string>
using namespace std;

struct contact;
int DH(contact contactarr[]);
void caidang(int* getfunction);


//菜单
void caidang(int* getfunction)
{
cout << endl<<endl
<< "*******************\n"
<< "********菜单*******\n"
<< "* 1.添加联系人 *\n"
<< "* 2.显示联系人 *\n"
<< "* 3.删除联系人 *\n"
<< "* 4.查找联系人 *\n"
<< "* 5.修改联系人 *\n"
<< "* 6.清空联系人 *\n"
<< "* 7.退出通讯录 *\n"
<< "*******************\n" << endl;
cout << "请输入想要使用的功能" << endl;
cin >> *getfunction;
}


//联系人结构体
struct contact { string name; string sex; int age; int telephone; string homeAddress; };

//添加联系人函数
void AddContact(contact contactarr[], int len)
{
int a=DH(contactarr);
cout << "姓名:" << endl;
string tampName;
cin >> tampName;
contactarr[a].name = tampName;

cout << "性别:" << endl;
string tampSex;
cin >> tampSex;
contactarr[a].sex = tampSex;

cout << "年龄:" << endl;
int tampAge;
cin >> tampAge;
contactarr[a].age = tampAge;

cout << "电话:" << endl;
int tamptelephone;
cin >> tamptelephone;
contactarr[a].telephone= tamptelephone;

cout << "地址:" << endl;
string tampHome;
cin >> tampHome;
contactarr[a].homeAddress= tampHome;

cout << "输入成功" << endl;

cout << "姓名:" << contactarr[a].name
<< "性别:" << contactarr[a].sex
<< "年龄:" << contactarr[a].age
<< "联系电话:" << contactarr[a].telephone
<< "家庭住址:" << contactarr[a].homeAddress << endl;
};


//显示联系人函数
void ShouContact(contact contactarr[], int len)
{
for (int b = 0; b < 1000; b++)
{
if (contactarr[b].telephone != 0)
{
cout << "姓名:" << contactarr[b].name
<< "性别:" << contactarr[b].sex
<< "年龄:" << contactarr[b].age
<< "联系电话:" << contactarr[b].telephone
<< "家庭住址:" << contactarr[b].homeAddress << endl;
} }

}


//删除联系人函数 按照姓名进行删除指定联系人
void DeleteContact(contact contactarr[], int len)
{
int c = 0;

cout << "请输入想要删除的联系人的名字:" << endl;
string name;
cin >> name;
for (int b = 0; b < 1000; b++)
{
if (contactarr[b].name==name)
{
contactarr[b].name = "0";
contactarr[b].sex = "0";
contactarr[b].age = 0;
contactarr[b].telephone = 0;
contactarr[b].homeAddress = "0";
cout << "删除成功" << endl;
c = 1;
}
}


if (c == 0) { cout << "查无此人" << endl; }
}


//查找联系人 按照姓名查找指定联系人信息
void FindContact(contact contactarr[], int len)
{
int c = 0;
cout << "请输入想要查找的联系人的名字:" << endl;
string name;
cin >> name;
for (int b = 0; b < 1000; b++)
{
if (contactarr[b].name == name)
{
cout << "姓名:" << contactarr[b].name
<< "性别:" << contactarr[b].sex
<< "年龄:" << contactarr[b].age
<< "联系电话:" << contactarr[b].telephone
<< "家庭住址:" << contactarr[b].homeAddress << endl;
cout << "查询成功" << endl;
c = 1;
}

}
if (c == 0) { cout << "查无此人" << endl; }

}


//修改联系人 按照姓名重新修改指定联系人
void ModifyContact(contact contactarr[], int len)
{
int c = 0;
cout << "请输入想要修改的联系人的名字:" << endl;
string name1;
cin >> name1;
for (int b = 0; b < 1000; b++)
{
if (contactarr[b].name == name1)
{
cout << "姓名:" << contactarr[b].name
<< "性别:" << contactarr[b].sex
<< "年龄:" << contactarr[b].age
<< "联系电话:" << contactarr[b].telephone
<< "家庭住址:" << contactarr[b].homeAddress << endl;
cout << "查询成功" << endl;

c = 1;

cout << "请输入你想修改的信息" << endl;
cout << "1.姓名" << endl;
cout << "2.性别" << endl;
cout << "3.年龄" << endl;
cout << "4.电话" << endl;
cout << "5.地址" << endl;
int a = 0;
string temp_1;
int temp_2;
cin >> a;
switch (a)
{
case 1:cout << "请输入新的姓名" << endl; cin >> temp_1; contactarr[b].name = temp_1; break;
case 2:cout << "请输入新的性别" << endl; cin >> temp_1; contactarr[b].sex = temp_1; break;
case 3:cout << "请输入新的年龄" << endl; cin >> temp_2; contactarr[b].age = temp_2; break;
case 4:cout << "请输入新的电话" << endl; cin >> temp_2; contactarr[b].telephone = temp_2; break;
case 5:cout << "请输入新的地址" << endl; cin >> temp_1; contactarr[b].homeAddress = temp_1; break;

}

cout << "修改成功" << endl;break;
}

}
if (c == 0) { cout << "查无此人" << endl; }
}

//还有需要一个遍历全部电话的函数
//如果电话=0,就使用该下标
//让下标不再重复
int DH(contact contactarr[]) {
int a = 0;
for (int b = 0; b < 1000; b++) { if (contactarr[b].telephone == 0) { a = b; break; } }
return a;
}

//6.清空联系人 清空通讯录中所有信息
void ClearContact(contact contactarr[], int len)
{
for (int b = 0; b < 1000; b++)
{

contactarr[b].name = "0";
contactarr[b].sex = "0";
contactarr[b].age = 0;
contactarr[b].telephone = 0;
contactarr[b].homeAddress = "0";

}
cout << "清空成功" << endl;
}

int main()
{

//定义一个联系人结构体数组
contact contactarr[1000]{};
int len = sizeof(contactarr) / sizeof(contactarr[0]);

//在添加联系人函数中,++的功能,防止覆盖信息
int a = 0;
int getfunction;
ABC:
caidang(&getfunction);
switch (getfunction)
{
case 1:AddContact(contactarr, len);goto ABC;break;
case 2 :ShouContact(contactarr, len); goto ABC; break;
case 3 :DeleteContact(contactarr, len); goto ABC; break;
case 4 :FindContact(contactarr, len); goto ABC; break;
case 5 :ModifyContact(contactarr, len); goto ABC; break;
case 6 :ClearContact(contactarr, len); goto ABC; break;
case 7 :cout<<"欢迎下次使用"<<endl;system("pause");
default:break;

}
//cout << contactarr[0].name << contactarr[0].sex << contactarr[0].age << contactarr[0].telephone << contactarr[0].homeAddress << endl;

//按任意键退出 system("pause");
return 0;

}

教程的:

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
#include<iostream>
#include<string>
using namespace std;
#define MAX 1000

//联系人结构体
struct person { string name; int sex; int age; string telephone; string homeAddress; };

//设计一个通讯录结构体
struct Addressbooks {
//定义一个联系人结构体数组
person personArray[MAX]{};

//通讯录中记录当前联系人个数
int m_Size = 0;
};

//添加联系人
void addperson(Addressbooks*abs)
{
string name;
//判断通讯录是否已满,如果满了就不再添加
if (abs->m_Size==MAX)
{

cout << "通讯录已满,无法添加" << endl;
return;
}
else
{
cout << "姓名:" << endl;
cin >> name;
abs->personArray[abs->m_Size].name = name; //用通讯录数组中的个数当成下标

cout << "性别:" << endl;
cout << "1.男" << endl;
cout << "2.女" << endl;
int sex = 0;

while (true)
{//输入正确的就退出循环
cin >> sex;
if (sex == 1||sex==2) { abs->personArray[abs->m_Size].sex = sex; break; }
cout << "错误" << endl;

}

cout << "年龄:" << endl;
int agx = 0;
cin >> agx;
abs->personArray[abs->m_Size].age = agx;

cout << "电话:" << endl;
string phone;
cin >> phone;
abs->personArray[abs->m_Size].telephone = phone;

cout << "地址:" << endl;
string address;
cin >> address;
abs->personArray[abs->m_Size].homeAddress = address;

//更新通讯录人数
abs->m_Size++;

system("pause"); // 按任意键继续
system("cls"); // 清屏操作
}
}

//显示联系人函数
void showperson(Addressbooks* abs)
{
//判断人数是否为0,显示为空
if (abs->m_Size == 0) { cout << "当前联系人为空"<<endl; }
else{
for (int i = 0; i<abs->m_Size;i++)
{
cout << "姓名:" << abs->personArray[i].name<<"\t"
<< "性别:" << (abs->personArray[i].sex==1 ?"男":"女") << "\t"
<< "年龄:" << abs->personArray[i].age << "\t"
<< "联系电话:" << abs->personArray[i].telephone << "\t"
<< "家庭住址:" << abs->personArray[i].homeAddress << endl;

}
}
system("pause"); // 按任意键继续
system("cls"); // 清屏操作
}


//判断联系人是否存在 返回i/0
int isExist(Addressbooks* abs, string a)
{

for (int i = 0; i < abs->m_Size; i++)
{
if (abs->personArray[i].name == a)
{
return i;//找到就返回这个人所在的数组下标
}
}
return -1;//没有就返回-1
}
//删除联系人函数 按照姓名进行删除指定联系人
void deletePerson (Addressbooks* abs)
{

cout << "请输入想要删除的联系人的名字:" << endl;
string name;
cin >> name;
int a = isExist(abs, name);


if (a != -1)
{
for (; abs->m_Size >a ; a++)
{
abs->personArray[a + 1] = abs->personArray[a];//用循环把后面的信息代替前面的信息
}
abs->m_Size--; //人数-1
cout << "删除成功" << endl;
}
else cout << "查无此人" << endl;

system("pause"); // 按任意键继续
system("cls"); // 清屏操作
}


//查找联系人 按照姓名查找指定联系人信息
void FindContact(Addressbooks*abs)
{

cout << "请输入想要查找的联系人的名字:" << endl;
string name;
cin >> name;
int a = isExist(abs, name);
if (a!=-1)
{
cout << "姓名:" << abs->personArray[a].name << "\t"
<< "性别:" << (abs->personArray[a].sex == 1 ? "男" : "女") << "\t"
<< "年龄:" << abs->personArray[a].age << "\t"
<< "联系电话:" << abs->personArray[a].telephone << "\t"
<< "家庭住址:" << abs->personArray[a].homeAddress << endl;
}
else { cout << "查无此人" << endl; }

system("pause"); // 按任意键继续
system("cls"); // 清屏操作
}


//修改联系人 按照姓名重新修改指定联系人
void ModifyContact(Addressbooks* abs)
{
cout << "请输入想要修改的联系人的名字:" << endl;
string name1;
cin >> name1;
int a = isExist(abs, name1);


if (a != -1)
{
string temp_1;
int temp_2;
cout << "请输入新的姓名" << endl; cin >> temp_1; abs->personArray[a].name = temp_1;
cout << "请输入新的性别" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2) { abs->personArray[a].sex = sex; break; }
cout << "错误" << endl;

}
cout << "请输入新的年龄" << endl; cin >> temp_2; abs->personArray[a].age = temp_2;
cout << "请输入新的电话" << endl; cin >> temp_2; abs->personArray[a].telephone = temp_2;
cout << "请输入新的地址" << endl; cin >> temp_1; abs->personArray[a].homeAddress = temp_1;
cout << "修改成功" << endl;
}
else
{ cout << "查无此人" << endl; }

system("pause"); // 按任意键继续
system("cls"); // 清屏操作
}

//6.清空联系人 清空通讯录中所有信息
void cleanPerson(Addressbooks* abs)
{
abs->m_Size = 0;//将当前记录的联系人数量置为0,做逻辑清空
cout << "通讯录已经清空" << endl;
system("pause"); // 按任意键继续
system("cls"); // 清屏操作
}

//菜单
void caidang()
{
cout << endl << endl
<< "*******************\n"
<< "********菜单*******\n"
<< "* 1.添加联系人 *\n"
<< "* 2.显示联系人 *\n"
<< "* 3.删除联系人 *\n"
<< "* 4.查找联系人 *\n"
<< "* 5.修改联系人 *\n"
<< "* 6.清空联系人 *\n"
<< "* 7.退出通讯录 *\n"
<< "*******************\n" << endl;
}
int main()
{
//创建一个通讯录结构体变量
Addressbooks abs;
int select = 0;//用户选择输入的变量



while (true) {

//菜单调用
caidang();

cin >> select;

switch (select)
{
case 1:addperson(&abs); break;
case 2:showperson(&abs); break;
case 3:deletePerson(&abs); break;
case 4:FindContact(&abs); break;
case 5:ModifyContact(&abs); break;
case 6:cleanPerson(&abs); break;
case 7:cout << "欢迎下次使用" << endl; system("pause"); return 0; break;
default:break;
}

}

}

不同之处:

教程

  • 教程宏定义了一个MAX 1000方便后面修改通讯录大小

  • 教程设计一个通讯录数组记录,以指针访问内容

​ 定义一个联系人结构体数组

​ 通讯录中记录当前联系人个数

  • 使用if设计了通讯录已满的情况

  • 使用数字代替输入的性别,输出时用三目运算符替换

  • 用while循环判断男女是否输入正确

  • 用if判断联系人是否为0

  • 函数判断联系人是否存在,返回下标

  • 删除功能使用冒泡的方式替换

  • 使用以下方式清屏

    system("pause"); // 按任意键继续 system("cls"); // 清屏操作

  • 删除联系人的方式为逻辑清空

  • 在退出功能上:

​ 我用goto语句

​ 教程用while(true)循环

基于类和对象的职工管理系统

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

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

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

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

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

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

头文件

职工.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>
#include<string>
using namespace std;

class Worker //职工类
{
public:

//显示个人信息
virtual void showinfo() = 0;

//获取岗位名称
virtual string getgwnamae() = 0;

int m_ON;//编号
string m_name;//姓名
int m_department;//部门编号
};

普通员工.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include<iostream>
#include<string>
#include"职工类.h"
using namespace std;

class Employee :public Worker
{
public:
Employee(int ON, string name, int did);

//显示个人信息
void showinfo();

//获取岗位名称
string getgwnamae();

};

老板.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include<iostream>
#include<string>
#include"职工类.h"
using namespace std;

class boss :public Worker
{
public:
boss(int ON, string name, int did);

//显示个人信息
void showinfo();

//获取岗位名称
string getgwnamae();

};

经理.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include<iostream>
#include<string>
#include"职工类.h"
using namespace std;

class manager :public Worker
{
public:
manager(int ON, string name, int did);

//显示个人信息
void showinfo();

//获取岗位名称
string getgwnamae();

};

  • 管理.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
66
67
68
69
#pragma once
#include<iostream>
#include"职工类.h"
#include"普通员工.h"
#include"经理.h"
#include"老板.h"
#include<fstream>
#define FIL "emp.txt"

using namespace std;

class WorderManager //管理类 1.提供菜单 2.实现各种功能 3.文件读写
{
public:
//菜单
void menu();

//退出功能
void out();

//记录职工人数
int m_worker_mun;

//职工数组指针
Worker** m_arr;


//添加员工功能
void add();

//文件写入
void save();

//记录文件中的人数
int SSD();

//判断文件是否为空
bool PD;

//初始化数据
void init_Emp();

//显示职工
void see();

//删除员工
void XXD();

//判断员工是否存在
int CPDD(int id);

//修改员工
void mod();

//查找员工信息
void XXDD();

//排序
void paixu();

//清空文件
void BB();

WorderManager();

~WorderManager();
};


源文件

普通员工

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include"普通员工.h"

Employee:: Employee(int ON, string name, int did)
{
m_ON = ON;
m_name = name;
m_department = did;
}
;
//显示个人信息
void Employee::showinfo()
{
cout << "编号:" << m_ON
<< "\t\t姓名:" << m_name
<< "\t\t岗位:" << getgwnamae()
<< "\t\t岗位职责:完成经理交给的任务" << endl;
};

//获取岗位名称
string Employee::getgwnamae() { return "员工"; };

老板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include"老板.h"

boss::boss(int ON, string name, int did)
{
m_ON = ON;
m_name = name;
m_department = did;
}
;
//显示个人信息
void boss::showinfo()
{
cout << "编号:" << m_ON
<< "\t\t姓名:" << m_name
<< "\t\t岗位:" << getgwnamae()
<< "\t\t岗位职责:管理公司所有事物" << endl;
};

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

经理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include"经理.h"

manager::manager(int ON, string name, int did)
{
m_ON = ON;
m_name = name;
m_department = did;
}
;
//显示个人信息
void manager::showinfo()
{
cout << "编号:" << m_ON
<< "\t\t姓名:" << m_name
<< "\t\t岗位:" << getgwnamae()
<< "\t\t岗位职责:完成老板交给的任务并并下发任务给员工" << endl;
};

//获取岗位名称
string manager::getgwnamae() { return "经理"; };
  • 管理
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#include"管理.h"

WorderManager::WorderManager()
{

ifstream ifs;
ifs.open(FIL, ios::in);

if (!ifs.is_open())//文件不存在
{
//初始化职工人数
m_worker_mun = 0;

//初始化职工数量指针
m_arr = NULL;

//判断文件是否存在初始化为真
this->PD = true;

ifs.close();
return;
}

char A;
ifs >> A;
//文件存在,数据为空
if (ifs.eof())//eof是检查文件流是否达到文件末尾的函数,原理:读取文件数据时,如果达到了文件末尾,则eof就会判断为turn
{

//初始化职工人数
m_worker_mun = 0;

//初始化职工数量指针
m_arr = NULL;

//判断文件是否存在初始化为真
this->PD = true;

ifs.close();
return;
}

//文件存在并且有数据
int num;

//更新职工人数
num = SSD();
this->m_worker_mun = num;

//记得先开辟m_arr的空间
m_arr = new Worker * [m_worker_mun];
init_Emp();

//判断文件是否存在初始化为真
this->PD = false;

ifs.close();
return;

};

WorderManager::~WorderManager()
{
if (this->m_arr != NULL)
{
delete[] this->m_arr;
this->m_arr = NULL;
}
};

//菜单
void WorderManager::menu()
{
cout << "**********************" << endl;
cout << "*欢迎使用职工管理系统*" << endl;
cout << "*1.增加职工信息 *" << endl;
cout << "*2.显示职工信息 *" << endl;
cout << "*3.删除离职职工 *" << endl;
cout << "*4.修改职工信息 *" << endl;
cout << "*5.查找职工信息 *" << endl;
cout << "*6.排序职工信息 *" << endl;
cout << "*7.清空所有文档 *" << endl;
cout << "*8.退出管理程序 *" << endl;
cout << "**********************" << endl;
};

//退出功能
void WorderManager::out()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);//无论在哪一段代码中,只要调用就会退出

};




//添加员工功能
void WorderManager::add()
{
cout << "请输入人数:" << endl;
int number;//记录输入人数
cin >> number;
int wordernum = m_worker_mun + number;//更新职工人数

if (number > 0)
{
Worker** m_wor1 = new Worker * [wordernum];//开辟新空间为储存职工指针数组

if (m_worker_mun != 0)//将原来空间的数据拷贝到新空间
{
for (int a = 0; a < m_worker_mun; a++)
{
m_wor1[a] = m_arr[a];
}
}

//添加新数据
for (int a = 0; a < number; a++)
{
cout << "请输入编号:" << endl;
int on;
cin >> on;

cout << "请输入姓名:" << endl;
string name;
cin >> name;

cout << "请输入部门:" << endl;
cout << "1.老板:" << endl;
cout << "2.经理:" << endl;
cout << "3.普通员工:" << endl;
int department;//用于添加职工时判断部门
Worker* worker = NULL;
while (true)
{
cin >> department;
switch (department)
{
case 1: { worker = new boss(on, name, department); break; }
case 2: { worker = new manager(on, name, department); break; }
case 3: { worker = new Employee(on, name, department); break; }
default: {cout << "没有这个岗位,请重新输入" << endl; }
}
if (department == 1 || department == 2 || department == 3)
{
cout << "添加成功" << endl;
m_wor1[this->m_worker_mun + a] = worker;//将数据放入数组
break;

}

}


system("pause");
system("cls");
}
m_worker_mun = wordernum;//更新最终人数

//文件是否为空判断为假
this->PD = false;

//释放原有空间
delete[]this->m_arr;

//更新新空间的指向
this->m_arr = m_wor1;

save();//将数组中的数据写入文本

}
else
{
cout << "错误" << endl;
}
}

//写入文件
void WorderManager::save()
{
ofstream ofs;
ofs.open(FIL, ios::out);
for (int a = 0; a < this->m_worker_mun; a++)
{
ofs << this->m_arr[a]->m_ON<< " "
<< this->m_arr[a]->m_name << " "
<< this->m_arr[a]->m_department << " ";
}

ofs.close();
}

//记录文件中的人数
int WorderManager::SSD()
{
ifstream ifs;
ifs.open(FIL, ios::in);
int on, id;
string name;

int RS = 0;//人数
while (ifs >> on && ifs >> name && ifs >> id)
{
RS++;
}
ifs.close();
return RS;
}

//初始化数据
void WorderManager::init_Emp()
{
ifstream ifs;
ifs.open(FIL, ios::in);

int on, id;
string name;
int a = 0;//作为数组元素


while (ifs >> on && ifs >> name && ifs >> id)
{
Worker* worker1 = NULL;
if (id == 1)
{
worker1 = new boss(on, name, id);
}
else if (id == 2)
{
worker1 = new manager(on, name, id);
}
else
{
worker1 = new Employee(on, name, id);
}
m_arr[a] = worker1;
a++;
}
ifs.close();
}

//显示职工
void WorderManager::see()
{
if (this->PD)
{
cout << "文件不存在或者记录为空" << endl;
}
else
{
for (int a = 0; a < this->m_worker_mun; a++)
{
m_arr[a]->showinfo();
}
}
system("pause");
system("cls");

}

//判断员工是否存在
int WorderManager::CPDD(int id)
{
int X = -1;//用于记录员工元素编号

for (int i = 0; i < this->m_worker_mun; i++)
{
if (id == this->m_arr[i]->m_ON)
{
X = i;
break;
}
}
return X;//返回元素编号
}

//删除员工
void WorderManager::XXD()
{


int a;
cout << "请输入想删除的员工编号" << endl;
cin >> a;
int s = CPDD(a);

if (s != -1)
{
for (int i = s; i < this->m_worker_mun - 1; i++)//总人数- 元素编号-1 =剩下的元素个数 = 迁移数
{
this->m_arr[i] = this->m_arr[i + 1];//数据迁移
}

//delete this->m_arr[m_worker_mun - 1];//释放最后一位的内存
//this->m_arr[m_worker_mun - 1] = NULL;

this->m_worker_mun--;//人数-1 要放在循环外面,防止i++,人数--同时发生,造成少循环

this->save();//删除最后一位时,没有覆盖,直接人数-1,写入的时候就没有写入,程序结束后被释放

cout << "删除成功" << endl;

if (this->m_worker_mun == 0)//如果员工为0,判断文件为空
{
this->PD = true;
}

system("pause");
system("cls");

}
else
{
cout << "员工不存在" << endl;
system("pause");
system("cls");
}

}

//修改员工
void WorderManager::mod()
{
int a;
cout << "请输入想修改的员工编号" << endl;
cin >> a;
int s = CPDD(a);
if (s != -1)
{
cout << "请输入编号:" << endl;
int on;
cin >> on;

cout << "请输入姓名:" << endl;
string name;
cin >> name;

cout << "请输入部门:" << endl;
cout << "1.老板:" << endl;
cout << "2.经理:" << endl;
cout << "3.普通员工:" << endl;
int department;//用于添加职工时判断部门
Worker* worker = NULL;
while (true)
{
cin >> department;
switch (department)
{
case 1: { worker = new boss(on, name, department); break; }
case 2: { worker = new manager(on, name, department); break; }
case 3: { worker = new Employee(on, name, department); break; }
default: {cout << "没有这个岗位,请重新输入" << endl; }
}
if (department == 1 || department == 2 || department == 3)
{
delete this->m_arr[s];//释放用来空间
this->m_arr[s] = worker;//将数据放入数组
break;
}

}

this->save();

cout << "修改成功" << endl;

system("pause");
system("cls");

}
else
{
cout << "员工不存在" << endl;
}
}



//查找员工信息
void WorderManager::XXDD()
{
cout << "查找方式:" << endl;
cout << "1.编号查找" << endl;
cout << "2.姓名查找" << endl;

while (true)
{
int c;
cin >> c;

if (c == 1)
{
int a;
cout << "请输入想查找的员工编号" << endl;
cin >> a;
int s = CPDD(a);

if (s != -1)
{
this->m_arr[s]->showinfo();
}
else
{
cout << "员工不存在" << endl;
}
break;

}
else if (c == 2)
{
bool ccc = false;//判断释放有此人
cout << "请输入想查找的员工姓名" << endl;
string d;
cin >> d;

for (int i = 0; i < this->m_worker_mun; i++)//遍历所以姓名,打印匹配的信息
{
if (this->m_arr[i]->m_name == d)
{
this->m_arr[i]->showinfo();
ccc = true;
}
}
if (ccc == true)
{
cout << "查询成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
break;
}
else
{
cout << "请重新输入" << endl;
}
}

system("pause");
system("cls");

}


//排序
void WorderManager::paixu()
{
cout << "请选择排序类型" << endl;
cout << "1.升序" << endl;
cout << "2.降序" << endl;
int a;//判断排序类型

while (true)
{

Worker* w1 = NULL;//临时空间
cin >> a;
if (a == 1)//升序
{
for (int i = 0; i < this->m_worker_mun - 1; i++)
{
for (int b = 0; b < this->m_worker_mun - i - 1; b++)
{
if (this->m_arr[b]->m_ON > this->m_arr[b + 1]->m_ON)
{
w1 = this->m_arr[b];
this->m_arr[b] = this->m_arr[b + 1];
this->m_arr[b + 1] = w1;
}
}
}
this->save();
//delete w1;//释放临时空间
//w1 = NULL;//临时空间指向空
break;
}
else if (a == 2)//降序
{
for (int i = 0; i < this->m_worker_mun - 1; i++)
{
for (int b = 0; b < this->m_worker_mun - i - 1; b++)
{
if (this->m_arr[b]->m_ON < this->m_arr[b + 1]->m_ON)
{
w1 = this->m_arr[b];
this->m_arr[b] = this->m_arr[b + 1];
this->m_arr[b + 1] = w1;
}
}
}
this->save();
//delete w1;//释放临时空间
//w1 = NULL;//临时空间指向空
break;
}
else
{
cout << "错误,请重新输入" << endl;
}

}
}

//清空文件
void WorderManager::BB()
{
cout << "此选项将会清空数据,请输入 1 再次确认" << endl;
int a = 0;
cin >> a;

if (a == 1)
{
ofstream ofs;
ofs.open(FIL, ios::trunc);// trunc :如果文件存在,删除文件并重新创造

if (this->m_arr != NULL)//释放所以堆区数据
{
for (int a = 0; a < this->m_worker_mun; a++)
{
if (this->m_arr[a] != NULL)
{
delete this->m_arr[a];
this->m_arr[a] = NULL;
}
}
}
this->m_arr = NULL;//二级指针指向空
this->m_worker_mun = 0;//人数归零
this->PD = true;//判断为空文件
}
else
{
cout << "清空操作已取消" << endl;
}
system("pause");
system("cls");

}

职工管理系统(启动项

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
#include<iostream>
#include"管理.h"
using namespace std;

#include"职工类.h"
#include"普通员工.h"
#include"经理.h"
#include"老板.h"

int main()
{

WorderManager mn;//创建一个管理类
int a;
while (true)
{
mn.menu();//调用管理类菜单
cout << "请选择想要的功能" << endl;
cin >> a;
switch (a)
{
case 1:mn.add(); break;
case 2:mn.see(); break;
case 3:mn.XXD(); break;
case 4:mn.mod(); break;
case 5:mn.XXDD(); break;
case 6:mn.paixu(); break;
case 7:mn.BB(); break;
case 8: mn.out(); break;
default: system("cls"); break;

}
system("cls");
}

system("pause");
return 0;

}