本文实例为大家分享了C语言通讯录系统(增删改查),供大家参考,具体内容如下
全部代码如下所示:
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
|
#include <iostream>
#include <string>
using namespace std;
const int MAX = 1000;
//联系人结构体
typedef struct person
{
string m_name; //姓名
int m_sex; //性别 1 男,0 女
int m_age; //年龄
string m_phone; //电话
string m_addr; //住址
}person_t;
//通讯录结构体
typedef struct addressbooks
{
person_t personArray[MAX]; //通讯录中保存的联系人数组
int m_size; //通讯录中人员个数
}addressbooks_t;
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;
cout << "************************" << endl;
}
void addPerson(addressbooks_t * abs )
{
//判断通讯录是否满了,如果满了就不再添加
if ( abs ->m_size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
return ;
}
else
{
//添加具体联系人
cout << "请输入姓名: " ;
cin >> abs ->personArray[ abs ->m_size].m_name;
int sex;
while ( true )
{
cout << "请输入性别(1男,0女): " ;
cin >> sex;
if (sex == 1 || sex == 0)
{
abs ->personArray[ abs ->m_size].m_sex = sex;
break ;
}
else
{
cout << "输入有误,请重新输入!" << endl;
}
}
cout << "请输入年龄: " ;
cin >> abs ->personArray[ abs ->m_size].m_age;
cout << "请输入电话: " ;
cin >> abs ->personArray[ abs ->m_size].m_phone;
cout << "请输入地址: " ;
cin >> abs ->personArray[ abs ->m_size].m_addr;
//更新通讯录人数
++ abs ->m_size;
cout << "添加成功!" << endl;
system ( "pause" );
}
}
void displayPerson(addressbooks_t * abs )
{
if ( abs ->m_size == 0)
{
cout << "记录为空!" << endl;
}
else
{
cout << "姓名\t" << "年龄\t" << "性别\t" << "电话\t\t" << "地址\t\t" << endl;
for ( int i = 0; i < abs ->m_size; ++i)
{
cout << abs ->personArray[i].m_name << "\t" ;
cout << abs ->personArray[i].m_age << "\t" ;
cout << ( abs ->personArray[i].m_sex == 1 ? "男" : "女" ) << "\t" ;
cout << abs ->personArray[i].m_phone << "\t" ;
cout << abs ->personArray[i].m_addr << "\t" ;
cout << endl;
}
}
system ( "pause" );
}
//按姓名查找用户,如果查找到返回联系人在数组中的下标,未查找到返回-1
int findByName(addressbooks_t * abs ,string name)
{
for ( int i = 0; i < abs ->m_size; ++i)
{
if (name == abs ->personArray[i].m_name)
{
return i;
}
}
return -1;
}
void deletePerson(addressbooks_t * abs )
{
string name;
cout << "请输入要删除联的系人姓名:" ;
cin >> name;
int index = findByName( abs ,name);
if (index == -1)
{
cout << "通讯录中未查到" << name << endl;
}
else
{
for ( int i = index; i < abs ->m_size; ++i)
{
abs ->personArray[i] = abs ->personArray[i+1];
}
-- abs ->m_size;
cout << "删除成功" << endl;
}
system ( "pause" );
}
void findPerson(addressbooks_t * abs )
{
string name;
cout << "请输入要查找的联系人姓名:" ;
cin >> name;
int index = findByName( abs ,name);
if (index == -1)
{
cout << "通讯录中未查到" << name << endl;
}
else
{
cout << "姓名\t" << "年龄\t" << "性别\t" << "电话\t\t" << "地址\t\t" << endl;
cout << abs ->personArray[index].m_name << "\t" ;
cout << abs ->personArray[index].m_age << "\t" ;
cout << ( abs ->personArray[index].m_sex == 1 ? "男" : "女" ) << "\t" ;
cout << abs ->personArray[index].m_phone << "\t" ;
cout << abs ->personArray[index].m_addr << "\t" ;
cout << endl;
}
system ( "pause" );
}
void updatePerson(addressbooks_t * abs )
{
string name;
cout << "请输入要修改的联系人姓名:" ;
cin >> name;
int index = findByName( abs ,name);
if (index == -1)
{
cout << "通讯录中未查到" << name << endl;
}
else
{
//添加具体联系人
cout << "请输入姓名: " ;
cin >> abs ->personArray[index].m_name;
int sex;
while ( true )
{
cout << "请输入性别(1男,0女): " ;
cin >> sex;
if (sex == 1 || sex == 0)
{
abs ->personArray[index].m_sex = sex;
break ;
}
else
{
cout << "输入有误,请重新输入!" << endl;
}
}
cout << "请输入年龄: " ;
cin >> abs ->personArray[index].m_age;
cout << "请输入电话: " ;
cin >> abs ->personArray[index].m_phone;
cout << "请输入地址: " ;
cin >> abs ->personArray[index].m_addr;
cout << "修改成功!" << endl;
}
system ( "pause" );
}
void clearPerson(addressbooks_t * abs )
{
abs ->m_size = 0;
cout << "清空成功!" << endl;
system ( "pause" );
}
int main()
{
//创建一个结构体变量
addressbooks_t abs ;
//初始化通讯录中人员个数
abs .m_size = 0;
int select = 0; //创建用户选择输入的变量
while ( true )
{
system ( "cls" ); //清空屏幕
showMenu(); //菜单调用
cout << "请输入您的选择: " ;
cin >> select;
switch (select)
{
case 0: //0.退出通讯录
cout << "欢迎下次使用" << endl;
return 0;
break ;
case 1: //1.添加联系人
addPerson(& abs );
break ;
case 2: //2.显示联系人
displayPerson(& abs );
break ;
case 3: //3.删除联系人
deletePerson(& abs );
break ;
case 4: //4.查找联系人
findPerson(& abs );
break ;
case 5: //5.修改联系人
updatePerson(& abs );
break ;
case 6: //6.清空联系人
clearPerson(& abs );
break ;
default :
break ;
}
}
return 0;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_43474701/article/details/119106344