-
Notifications
You must be signed in to change notification settings - Fork 0
/
通讯录管理系统.cpp
399 lines (368 loc) · 8.1 KB
/
通讯录管理系统.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
#include <iostream>
#include <string>
#define MAX 1000
using namespace std;
//封装函数显示菜单界面
void showMenu()
{
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;
}
//联系人结构体
struct people
{
string name;
string gender;
string phone;
string job;
string address;
};
//通讯录结构体
struct addressbook
{
struct people addressbookArray[MAX];//通讯录中保存的联系人数组
int size = 0;//通讯录中当前记录联系人人数
};
//函数:增加联系人
void add(struct addressbook* a )
{
if (a->size == MAX)
{
cout << "抱歉,该通讯录已满" << endl;
return;
}
else
{
cout << "请输入联系人的 姓名 性别 手机号 职业 家庭住址" << endl;
//添加具体联系人
//姓名
string name;
cout << "请输入姓名" << endl;
cin >> name;
a->addressbookArray[a->size].name = name;
//性别
int b = 0;
string gender;
cout << "请输入性别,1为男性,2为女性" << endl;
cin >> b;
if (b == 1)
{
gender = "男";
}
else if (b == 2)
{
gender = "女";
}
a->addressbookArray[a->size].gender = gender;
//职业
string job;
cout << "请输入职业" << endl;
cin >> job;
a->addressbookArray[a->size].job = job;
//电话
string phoneNumber;
cout << "请输入电话号码" << endl;
cin >> phoneNumber;
a->addressbookArray[a->size].phone = phoneNumber;
//住址
string address;
cout << "请输入住址" << endl;
cin >> address;
a->addressbookArray[a->size].address = address;
(a->size)++;
cout << "联系人详细信息如下" << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "职业:" << job << endl;
cout << "电话:" << phoneNumber << endl;
cout << "地址:" << address << endl;
}
system("pause");
system("cls");
showMenu();
}
//重复函数1
void infoOne(struct addressbook* a, int i)
{
cout << "性别:" << a->addressbookArray[i].gender << endl;
cout << "职业:" << a->addressbookArray[i].job << endl;
cout << "电话:" << a->addressbookArray[i].phone << endl;
cout << "地址:" << a->addressbookArray[i].address << endl;
}
//函数:显示全部联系人
void showAll(struct addressbook* a)
{
if (a->size == 0)
{
cout << "抱歉,目前通讯录中暂无联系人" << endl;
}
else
{
cout << "以下为全部联系人:" << endl;
for (int i = 0; i < (a->size); i++)
{
cout << "姓名:" << a->addressbookArray[i].name << endl;
infoOne(a, i);
cout << " " << endl;
}
}
system("pause");
system("cls");
showMenu();
}
//函数:删除联系人
void deleteOne(struct addressbook* a)
{
if (a->size == 0)
{
cout << "抱歉,目前通讯录中暂无联系人" << endl;
}
else
{
string name;
string YesorNo;
cout << "请输入您想删除的联系人的姓名" << endl;
cin >> name;
for (int i = 0; i <= (a->size);i++)
{
if (name==a->addressbookArray[i].name)
{
FLAG:
cout << "您确认删除联系人 " << name << " 吗?(Y/N)" << endl;
cin >> YesorNo;
if (YesorNo=="Y")
{
for (int j = i; j <= (a->size); j++)
{
a->addressbookArray[j].name = a->addressbookArray[j + 1].name;
a->addressbookArray[j].address = a->addressbookArray[j + 1].address;
a->addressbookArray[j].phone = a->addressbookArray[j + 1].phone;
a->addressbookArray[j].job = a->addressbookArray[j + 1].job;
a->addressbookArray[j].gender = a->addressbookArray[j + 1].gender;
}
cout << "联系人 " << name << " 已删除" << endl;
(a->size)--;
break;
system("pause");
}
else if (YesorNo == "N")
{
cout << "联系人 " << name << " 未删除" << endl;
break;
system("pause");
}
else
{
cout << "您的输入有误,请重新输入" << endl;
goto FLAG;
}
}
else if (i == (a->size))
{
cout << "抱歉,查无此人" << endl;
}
}
}
system("pause");
system("cls");
showMenu();
}
//函数:查找联系人
void findOne(struct addressbook* a)
{
if (a->size == 0)
{
cout << "抱歉,目前通讯录中暂无联系人" << endl;
}
else
{
string name;
cout << "请输入您想查找的联系人的姓名" << endl;
cin >> name;
for (int i = 0; i <= (a->size); i++)
{
if (name == a->addressbookArray[i].name)
{
cout << "您查找的联系人 " << name << " 的详细信息如下:" << endl;
infoOne(a, i);
break;
}
else if (i == (a->size))
{
cout << "抱歉,查无此人" << endl;
}
}
}
system("pause");
system("cls");
showMenu();
}
//函数:修改联系人
void changeOne(struct addressbook* a)
{
if (a->size == 0)
{
cout << "抱歉,目前通讯录中暂无联系人" << endl;
}
else
{
string name;
string gender;
string job;
string address;
string phoneNumber;
int select = 0;
int b = 0;
cout << "请输入您想修改的联系人的姓名" << endl;
cin >> name;
for (int i = 0; i <= (a->size); i++)
{
if (name == a->addressbookArray[i].name)
{
cout << "您查找的联系人 " << name << " 的详细信息如下:" << endl;
infoOne(a, i);
cout << " " << endl;
FLAG2:
cout << "请选择您想修改的信息" << endl;
cout << "按1:修改姓名" << endl;
cout << "按2:修改性别" << endl;
cout << "按3:修改职业" << endl;
cout << "按4:修改电话" << endl;
cout << "按5:修改地址" << endl;
cout << "按0:返回上级菜单" << endl;
cin >> select;
switch (select)
{
case 0:
cout << "已返回上级菜单" << endl;
system("pause");
system("cls");
showMenu();
return;
case 1:
cout << "请输入联系人的姓名" << endl;
cin >> name;
a->addressbookArray[i].name = name;
break;
case 2:
cout << "请输入联系人的性别,1为男性,2为女性" << endl;
cin >> b;
if (b == 1)
{
gender = "男";
}
else if (b == 2)
{
gender = "女";
}
a->addressbookArray[i].gender = gender;
break;
case 3:
cout << "请输入联系人的职业" << endl;
cin >> job;
a->addressbookArray[i].job = job;
break;
case 4:
cout << "请输入联系人的电话号码" << endl;
cin >> phoneNumber;
a->addressbookArray[i].phone = phoneNumber;
break;
case 5:
cout << "请输入联系人的住址" << endl;
cin >> address;
a->addressbookArray[i].address = address;
break;
default:
cout << "您的输入有误,请重新选择" << endl;
goto FLAG2;
}
cout << "修改后的联系人详细信息如下" << endl;
cout << "姓名:" << a->addressbookArray[i].name << endl;
infoOne(a, i);
system("pause");
system("cls");
showMenu();
return;
}
else if (i == (a->size))
{
cout << "抱歉,查无此人" << endl;
}
}
}
system("pause");
system("cls");
showMenu();
}
//函数:删除全部联系人
void deleteAll(struct addressbook* a)
{
string b;
FLAG3:
cout << "警告:您确认要删除全部联系人吗?(Y/N)" << endl;
cin >> b;
if (b == "Y")
{
a->size = 0;
cout << "通讯录已清空" << endl;
}
else if (b == "N")
{
cout << "通讯录未清空,已返回上层菜单" << endl;
}
else
{
cout << "您的输入有误,请重新输入" << endl;
goto FLAG3;
}
system("pause");
system("cls");
showMenu();
}
int main()
{
int selection = 0;
showMenu();
//创建通讯录结构体变量
struct addressbook one;
//初始化通讯录中当前人员个数
one.size = 0;
while (true)
{
cin >> selection;
switch (selection)
{
case 0:
cout << "感谢您使用本系统" << endl;
return 0;
case 1:
add(&one);//利用地址传递,可以修饰实参
break;
case 2:
showAll(&one);
break;
case 3:
deleteOne(&one);
break;
case 4:
findOne(&one);
break;
case 5:
changeOne(&one);
break;
case 6:
deleteAll(&one);
break;
}
}
system("pause");
return 0;
}