通讯录管理系统
学习目标:
对C++的基础进行复习,为后续深入学习打好基础
案例描述:
通讯录是一个可以记录亲人、好友信息的工具。
本教程主要利用C++来实现一个通讯录管理系统
系统中需要实现的功能如下:
- 添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
- 显示联系人:显示通讯录中所有联系人信息
- 删除联系人:按照姓名进行删除指定联系人
- 查找联系人:按照姓名查看指定联系人信息
- 修改联系人:按照姓名重新修改指定联系人
- 清空联系人:清空通讯录中所有信息
- 退出通讯录:退出当前使用的通讯录
实现代码:
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
|
#include <iostream> #include <string> using namespace std; #define MAX 1000 struct person { string name; int sex {}; int age {}; string phonenumber; string address; }; struct addressbook { struct person personArr[MAX]; int person_size {}; }; void showMenu() //打印通讯录首菜单 { cout << "*****************************" << endl; cout << "******* 1、添加联系人 *******" << endl; cout << "******* 2、显示联系人 *******" << endl; cout << "******* 3、删除联系人 *******" << endl; cout << "******* 4、查找联系人 *******" << endl; cout << "******* 5、修改联系人 *******" << endl; cout << "******* 6、清空联系人 *******" << endl; cout << "******* 0、退出通讯录 *******" << endl; } void addPerson(addressbook* aaa) //添加联系人 { if (aaa->person_size < MAX) { string name; cout << "请输入姓名:" << endl; cin >> name; aaa->personArr[aaa->person_size].name = name; int sex; cout << "请输入性别对应序号:(1--男 2--女)" << endl; while ( true ) { cin >> sex; if ((sex == 1) || (sex == 2)) { aaa->personArr[aaa->person_size].sex = sex; break ; } else { cout << "您输入的有误,请检查后重新输入!" << endl; } } int age = 0; cout << "请输入年龄:" << endl; cin >> age; aaa->personArr[aaa->person_size].age = age; string phonenumber; cout << "请输入电话:" << endl; cin >> phonenumber; aaa->personArr[aaa->person_size].phonenumber = phonenumber; string address; cout << "请输入地址:" << endl; cin >> address; aaa->personArr[aaa->person_size].address = address; aaa->person_size++; cout << "添加联系人成功!" << endl; } else { cout << "联系人已满,请删除部分联系人再添加!" << endl; } system ( "pause" ); system ( "cls" ); } void showPerson(addressbook person) { if (person.person_size == 0) { cout << "联系人列表为空!" << endl; } for ( int i = 0; i < person.person_size; i++) { cout << i + 1 << ". " << "姓名:" << person.personArr[i].name << " " << "性别:" << (person.personArr[i].sex == 1 ? "男" : "女" ) << " " << "年龄:" << person.personArr[i].age << " " << "电话:" << person.personArr[i].phonenumber << " " << "住址:" << person.personArr[i].address << " " << endl; } system ( "pause" ); system ( "cls" ); } int isExist(addressbook* person,string name) //根据姓名判断是否存在 { for ( int i = 0; i < person->person_size; i++) { if (person->personArr[i].name == name) { return i; } } return -1; } void deletePerson( addressbook* person) //删除联系人 { string name; cout << "请输入您要删除的联系人姓名!" << endl; cin >> name; int exist = isExist(person, name); if (exist != -1) { for ( int i = exist; i < person->person_size; i++) { { person->personArr[i] = person->personArr[i + 1]; } } (person->person_size)--; cout << "删除成功!" << endl; } else { cout << "没有这个人!" << endl; } system ( "pause" ); system ( "cls" ); } void findPerson(addressbook* person) //查找联系人 { string name; cout << "请输入您要查找的联系人姓名:" << endl; cin >> name; int exist = isExist(person, name); if (exist != -1) { cout << "该联系人信息如下:" << endl; cout << "姓名:" << person->personArr[exist].name << " " << "性别:" << (person->personArr[exist].sex == 1 ? "男" : "女" ) << " " << "年龄:" << person->personArr[exist].age << " " << "电话:" << person->personArr[exist].phonenumber << " " << "住址:" << person->personArr[exist].address << " " << endl; } else { cout << "没有查到这个人哦!" << endl; } system ( "pause" ); system ( "cls" ); } void modifyPerson(addressbook* person) { string name; cout << "请输入要修改联系人的姓名 :" << endl; cin >> name; int exist = isExist(person,name); if (exist != -1) { string modifyName; cout << "请输入修改后的名字:" ; cin >> modifyName; person->personArr[exist].name = modifyName; while ( true ) { int modifySex; cout << "请输入修改后的性别(1、男 2、女):" ; cin >> modifySex; if (modifySex == 1 || modifySex == 2) { person->personArr[exist].sex = modifySex; break ; } else { cout << "您应当输入1或2,请重新输入!" << endl; } } int modifyAge; cout << "请输入修改后的年龄:" ; cin >> modifyAge; person->personArr[exist].age = modifyAge; string modifyPhone; cout << "请输入修改后的电话:" ; cin >> modifyPhone; person->personArr[exist].phonenumber = modifyPhone; string modifyAddress; cout << "请输入修改后的住址:" ; cin >> modifyAddress; person->personArr[exist].address = modifyAddress; cout << "修改成功" << endl; } else { cout << "没有查到这个名字的人,故无法修改" << endl; } system ( "pause" ); system ( "cls" ); } void emptyPerson(addressbook* person) { string ensure; cout << "您确定要清空所有联系人吗,此操作不可逆,如需清空,请输入\"我同意\"这三个字: " << endl; cin >> ensure; if (ensure == "我同意" ) { person->person_size = 0; cout << "清空联系人成功" << endl; } else { cout << "撤销了清空联系人操作!" << endl; } system ( "pause" ); system ( "cls" ); } int main() { int userselect = 0; struct addressbook aaa; aaa.person_size = 0; while ( true ) { showMenu(); cout << "请在下方输入您想选择的功能(输入前面的数字即可): " << endl; cin >> userselect; switch (userselect) { case 1: addPerson(&aaa); break ; case 2: showPerson(aaa); break ; case 3: deletePerson(&aaa); break ; case 4: findPerson(&aaa); break ; case 5: modifyPerson(&aaa); break ; case 6: emptyPerson(&aaa); break ; case 0: cout << "退出系统成功,欢迎您下次使用!" << endl; system ( "pause" ); return 0; default : cout << "输入有误,请重新输入!" << endl; break ; } } } |
运行结果:
这个系统里用到了system(“cls”),这个是清屏的意思。
“system("cls")”是在C语言程序中,调用系统命令cls完成清屏操作。
system函数是C语言提供的与操作系统衔接的函数,函数原型如下:
1
2
|
#include <stdlib.h> //所在头文件 int system ( const char *command); //参数为操作系统命令 |
函数功能:execute a shell command 执行一个操作系统命令
如:
1
2
|
system ( "time /t" ) ;显示时间 system ( "dir" ); //列目录 |
示例:
1
2
3
4
5
|
#include<stdlib.h> main() { system ( "cls" ); /*清屏*/ system ( "dirc://" ); /*列C盘根目录*/ } |
总结
到此这篇关于利用C++实现通讯录管理系统的文章就介绍到这了,更多相关C++通讯录管理系统内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_44721910/article/details/122590205