抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

TO DO

简略菜单

1
2
3
4
5
6
7
8
9
10
11
12
13
void MenuUI() {
cout << " ************************** " << endl;
cout << " ** 公司员工工号管理系统 ** " << endl;
cout << " *----- 1. 添加员工 -----* " << endl;
cout << " *----- 2. 显示员工 -----* " << endl;
cout << " *----- 3. 删除员工 -----* " << endl;
cout << " *----- 4. 查找员工 -----* " << endl;
cout << " *----- 5. 修改员工 -----* " << endl;
cout << " *----- 6. 清空员工 -----* " << endl;
cout << " *----- 0. 退出通讯录 -----* " << endl;
cout << " ************************** " << endl;
cout << "请做出您的选择(0-6,6、0退出):" << endl;
}

简略结构体 - 员工和员工簿

1
2
3
4
5
6
7
8
9
10
11
struct Worker
{
string w_name;
int w_num;
};

struct WorkerBook
{
Worker workerArray[MAX];
int w_size;
};

保存数据到本地

将添加的员工信息记录至staticdata.txt文件:
未实装!

1
2
3
4
// 将员工信息保存至txt文件内  -  尚未完成!!!!!
void saveWorkerData( WorkerBook *worker ) {
ofstream myfile("staticdata.txt", ios::app);
}

添加员工:

  • 尚未添加的功能:如此联系人已经存在,则提示不可重复添加。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 添加员工
void addWorker( WorkerBook *worker ) {
if (worker->w_size == MAX) {
cout << "通讯录已满,无法添加!" << endl;
}
else {
// 姓名
string name;
cout << "请输入您的名字:" << endl;
cin >> name;
worker->workerArray[worker->w_size].w_name = name;
// 工号
int num;
cout << "请输入您的工号(不加42前缀):" << endl;
cin >> num;
worker->workerArray[worker->w_size].w_num = num;
}
worker->w_size++;
saveWorkerData(worker);
system("cls");
return;
}

显示员工

1
2
3
4
5
6
7
8
9
10
11
12
13
void showWorker( WorkerBook *worker ) {
if (worker->w_size == 0) {
cout << "当前通讯录为空!" << endl;
}
else {
int i;
for (i=0; i<=worker->w_size ; i++)
cout << worker->workerArray[worker->w_size].w_name << '\t';
cout << 42 << worker->workerArray[worker->w_size].w_num << '\t';
}
system("pause");
system("cls");
}

判断通讯录是否有要搜寻的员工:

1
2
3
4
5
6
7
8
int isExist( WorkerBook *wbs, string name ) {
int i;
for (i=0; i<wbs->w_size; i++) {
if (wbs->workerArray[i].w_name == name)
return i;
}
return -1;
}

删除员工

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void delWorker( WorkerBook *worker ) {
cout << "请输入要查找的员工的姓名:" << endl;
string name;
cin >> name;
int ret = isExist( worker, name );

//? ret != -1 未查到
//? ret == -1 查到了
int i;
if (ret != -1) {
cout << "查到此人,正在删除!" << endl;
for (i=0; i<worker->w_size; i++) {
worker->workerArray[i] = worker->workerArray[i+1];
}
worker->w_size--;
cout << "删除成功!" << endl;
}
else
cout << "查无此人!" << endl;

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

查找员工

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void seaWorker( WorkerBook *worker ) {
string name;
cin >> name;
int ret = isExist( worker, name );

if (ret != -1) {
cout << "已查到此人!" << endl;
cout << worker->workerArray[ret].w_name << endl;
cout << worker->workerArray[ret].w_num << endl;
}
else
cout << "查无此人!" << endl;

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

修改员工

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
void changeWorker( WorkerBook *worker ) {
string name;
cin >> name;
cout << "请输入您要修改的联系人:" << endl;
int ret = isExist( worker, name );

if (ret != -1) {
//? 找到联系人

// 姓名
string name;
cin >> name;
worker->workerArray[ret].w_name = name;

// 工号
int num;
cin >> num;
worker->workerArray[ret].w_num = num;
}
else
cout << "查无此人!" << endl;

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

清空通讯录

1
2
3
4
5
6
7
void clearWorker( WorkerBook *worker ) {
worker->w_size = 0.;
cout << "通讯录已经清空!" << endl;

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

最终代码

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

#define MAX 500

void MenuUI() {
cout << " ************************** " << endl;
cout << " ** 公司员工工号管理系统 ** " << endl;
cout << " *----- 1. 添加员工 -----* " << endl;
cout << " *----- 2. 显示员工 -----* " << endl;
cout << " *----- 3. 删除员工 -----* " << endl;
cout << " *----- 4. 查找员工 -----* " << endl;
cout << " *----- 5. 修改员工 -----* " << endl;
cout << " *----- 6. 清空员工 -----* " << endl;
cout << " *----- 0. 退出通讯录 -----* " << endl;
cout << " ************************** " << endl;
cout << "请做出您的选择(0-6,6、0退出):" << endl;
}

struct Worker
{
string w_name;
int w_num;
};

struct WorkerBook
{
Worker workerArray[MAX];
int w_size;
};

// 将员工信息保存至txt文件内
void saveWorkerData( WorkerBook *worker ) {
ofstream myfile("staticdata.txt", ios::app);
}

// 添加员工
void addWorker( WorkerBook *worker ) {
if (worker->w_size == MAX) {
cout << "通讯录已满,无法添加!" << endl;
}
else {
// 姓名
string name;
cout << "请输入您的名字:" << endl;
cin >> name;
worker->workerArray[worker->w_size].w_name = name;
// 工号
int num;
cout << "请输入您的工号(不加42前缀):" << endl;
cin >> num;
worker->workerArray[worker->w_size].w_num = num;
}
worker->w_size++;
system("cls");
return;
}

// 显示员工
void showWorker( WorkerBook *worker ) {
if (worker->w_size == 0) {
cout << "当前通讯录为空!" << endl;
}
else {
int i;
for (i=0; i<=worker->w_size ; i++)
cout << worker->workerArray[worker->w_size].w_name << '\t';
cout << 42 << worker->workerArray[worker->w_size].w_num << '\t';
}
system("pause");
system("cls");
}

/*
* 判断员工是否在通讯录中,如果是则返回-1。
*/
int isExist( WorkerBook *wbs, string name ) {
int i;
for (i=0; i<wbs->w_size; i++) {
if (wbs->workerArray[i].w_name == name)
return i;
}
return -1;
}

// 删除员工
void delWorker( WorkerBook *worker ) {
cout << "请输入要查找的员工的姓名:" << endl;
string name;
cin >> name;
int ret = isExist( worker, name );
//? ret != -1 未查到
//? ret == -1 查到了
int i;
if (ret != -1) {
cout << "查到此人,正在删除!" << endl;
for (i=0; i<worker->w_size; i++) {
worker->workerArray[i] = worker->workerArray[i+1];
}
worker->w_size--;
cout << "删除成功!" << endl;
}
else
cout << "查无此人!" << endl;
system("cls");
system("pause");
}

// 查找员工
void seaWorker( WorkerBook *worker ) {
string name;
cin >> name;
int ret = isExist( worker, name );
if (ret != -1) {
cout << "已查到此人!" << endl;
cout << worker->workerArray[ret].w_name << endl;
cout << worker->workerArray[ret].w_num << endl;
}
else
cout << "查无此人!" << endl;
system("cls");
system("pause");
}

// 修改员工
void changeWorker( WorkerBook *worker ) {
string name;
cin >> name;
cout << "请输入您要修改的联系人:" << endl;
int ret = isExist( worker, name );
if (ret != -1) {
//? 找到联系人
// 姓名
string name;
cin >> name;
worker->workerArray[ret].w_name = name;
// 工号
int num;
cin >> num;
worker->workerArray[ret].w_num = num;
}
else
cout << "查无此人!" << endl;
system("pause");
system("cls");
}

// 清空通讯录
void clearWorker( WorkerBook *worker ) {
worker->w_size = 0.;
cout << "通讯录已经清空!" << endl;
system("pause");
system("cls");
}

int main()
{
int select = 0;
WorkerBook worker;
worker.w_size = 0;

while ( true )
{
MenuUI();
cin >> select;
switch (select)
{
case 1: addWorker(&worker); break;
case 2: showWorker(&worker); break;
case 3: delWorker(&worker); break;
case 4: seaWorker(&worker); break;
case 5: changeWorker(&worker); break;
case 6: clearWorker(&worker); break;
case 0: cout << "欢迎下次使用,再见!" << endl; system("pause");; return 0; break;
default: break;
}
}

system("pause");
return 0;
}

评论

都看到这里了,不留一句再走?