本文实例为大家分享了C++使用链表存储实现通讯录功能管理的具体代码,供大家参考,具体内容如下
简介
这是第二周老师给的一个小项目要求实现基本通讯录功能,有数据的增删改查,包含插入时间的能力。
代码详情
头文件
1
2
3
4
5
|
#include <iostream> #include <string> #include<malloc.h> //system功能调用 #include <windows.h> //使用本地系统API获取插入时间 #include <sstream> |
基本存储结构体
1
2
3
4
5
6
7
8
9
10
11
|
typedef struct info{ string number; string date; string name; string adress; string birthday; }A; typedef struct LNode{ A data; struct LNode *next; }LNode,*LinkList; |
链表数据初始化
用前插法插入数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
int InitList(LinkList &L){ //初始化链表 L = new LNode; L->next = NULL; return OK; } int ListInsert(LinkList &L,string name,string adress,string birthday,string date,string number){ //考虑使用数组来赋值 LinkList p; p= new LNode; p->data.name = name; p->data.adress = adress; p->data.date = date; p->data.birthday = birthday; p->data.number = number; p->next = L->next; L->next = p; return OK; } |
本地WindowsAPI调用插入时间
1
2
3
4
5
6
|
SYSTEMTIME sys; GetLocalTime( &sys ); string y = doubleToString(sys.wYear); string m = doubleToString(sys.wMonth); string d = doubleToString(sys.wDay); string ymd = y+ "-" +m+ "-" +d; |
因为获取的是一个double值,您得对其时间强制类型转换
1
2
3
4
5
6
7
8
|
string doubleToString( double num) { //强制类型转换 double强制转换为string类型 stringstream ss; string str; ss << num; ss >> str; return str; } |
数据查询功能
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
|
LinkList SearchElemChar(LinkList L, int i,string e){ //思路,传递参数1,2,3,4,eg 1代表name,再分不同的方法在链表内循环查找操作 if (i == 1){ //查名字 while (L!= NULL){ if (L->data.name == e){ return L; } else { L = L->next; } } if (L = NULL){ cout << "未查到数据!" <<endl; } } else if (i == 2){ //查生日 while (L!= NULL){ if (L->data.birthday == e){ return L; } else { L = L->next; } } if (L = NULL){ cout << "未查到数据!" <<endl; } } else if (i == 3){ //查地址 while (L!= NULL){ if (L->data.adress == e){ return L; } else { L = L->next; } } if (L = NULL){ cout << "未查到数据!" <<endl; } } else if (i == 4){ //查时间 while (L!= NULL){ if (L->data.date == e){ return L; } else { L = L->next; } } if (L = NULL){ cout << "未查到数据!" <<endl; } } else if (i == 5){ //查电话 while (L!= NULL){ if (L->data.number == e){ return L; } else { L = L->next; } } if (L = NULL){ cout << "未查到数据!" <<endl; } } } |
完整案例
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
|
//乐公第二周项目 实现基本通讯录存储结构 //基础链表存储数据 #include <iostream> #include <string> #include<malloc.h> //system功能调用 #include <windows.h> //使用本地系统API获取插入时间 #include <sstream> #define ERROR 0 #define OK 1 using namespace std; typedef int ElemType; /*定义表元素的类型*/ //结构体文件 typedef struct info{ string number; string date; string name; string adress; string birthday; }A; typedef struct LNode{ A data; struct LNode *next; }LNode,*LinkList; int InitList(LinkList &L){ //初始化链表 L = new LNode; L->next = NULL; return OK; } int ListInsert(LinkList &L,string name,string adress,string birthday,string date,string number){ //考虑使用数组来赋值 LinkList p; p= new LNode; p->data.name = name; p->data.adress = adress; p->data.date = date; p->data.birthday = birthday; p->data.number = number; p->next = L->next; L->next = p; return OK; } //查找元素 (难题需要解决) LinkList SearchElemChar(LinkList L, int i,string e){ //思路,传递参数1,2,3,4,eg 1代表name,再分不同的方法在链表内循环查找操作 if (i == 1){ //查名字 while (L!= NULL){ if (L->data.name == e){ return L; } else { L = L->next; } } if (L = NULL){ cout << "未查到数据!" <<endl; } } else if (i == 2){ //查生日 while (L!= NULL){ if (L->data.birthday == e){ return L; } else { L = L->next; } } if (L = NULL){ cout << "未查到数据!" <<endl; } } else if (i == 3){ //查地址 while (L!= NULL){ if (L->data.adress == e){ return L; } else { L = L->next; } } if (L = NULL){ cout << "未查到数据!" <<endl; } } else if (i == 4){ //查时间 while (L!= NULL){ if (L->data.date == e){ return L; } else { L = L->next; } } if (L = NULL){ cout << "未查到数据!" <<endl; } } else if (i == 5){ //查电话 while (L!= NULL){ if (L->data.number == e){ return L; } else { L = L->next; } } if (L = NULL){ cout << "未查到数据!" <<endl; } } } LinkList SearchElemBefore(LinkList L,string e){ //删除结点必须返回前个结点的地址,这里查找前个结点 LinkList P; while (L!= NULL){ if (L->data.name == e){ return P; } else { P = L; L = L->next; } } if (L = NULL){ return L; } } int ShowMenu(){ //主菜单 int a; cout<< "------欢迎您使用乐公通讯录系统!------" << endl; cout << "-----请根据功能输入对应的序号--------" <<endl; cout << "-----------1.新建联系人--------------" <<endl; cout << "--------2.查看所有联系人-------------" <<endl; cout << "--------3.修改选定联系人-------------" <<endl; cout << "--------4.查询选定联系人-------------" <<endl; cout << "--------5.删除选定联系人-------------" <<endl; cout << "------------6.退出系统---------------" <<endl; cout << "请您输入序号并按回车进入:" ; cin >> a; return a; } string doubleToString( double num) { //强制类型转换 double强制转换为string类型 stringstream ss; string str; ss << num; ss >> str; return str; } void foreachelem(LinkList L){ if (L->next == NULL){ cout<< "通讯录里还没有联系人,快去新建一下吧~" <<endl; } else { while (L->next!=NULL){ L=L->next; cout<< "联系人姓名:" << L->data.name <<endl; cout<< "联系人电话:" << L->data.number<<endl; cout<< "联系人地址:" << L->data.adress <<endl; cout<< "联系人生日:" << L->data.birthday <<endl; cout<< "录入时间:" << L->data.date <<endl; } cout<< "\n" ; } //system("pause"); } int serachsamename(LinkList L,string name){ //查找是否存在姓名相同的联系人 while (L->next!=NULL){ L=L->next; if (L->data.name==name) return 0; } return 1; } int main(){ int i; LinkList L; InitList(L); while (i!=6){ i = ShowMenu(); if (i ==1){ cout << "您选择了:新建联系人" <<endl; string number; string date; string time ; string name; string adress; string birthday; cout << "请输入联系人姓名:" ; cin >> name; SYSTEMTIME sys; GetLocalTime( &sys ); string y = doubleToString(sys.wYear); string m = doubleToString(sys.wMonth); string d = doubleToString(sys.wDay); string ymd = y+ "-" +m+ "-" +d; int repeat = serachsamename(L,name); if (repeat == 0){ cout << "联系人姓名重复,请删除旧联系人或更改姓名!" << endl; } else { cout << "请输入联系人电话:" ; cin >> number; if (number.size()!=11){ cout << "手机号输入有误,请大于11位!" << endl; } else { cout << "请输入联系人生日:" ; cin >> birthday; cout << "请输入联系人地址:" ; cin >> adress; cout << "联系人于" << ymd; int ok; ok = ListInsert(L,name,adress,birthday,ymd,number); if (ok == 1){ cout << "日新建成功!" << endl; } else { cout << "新建失败!" << endl; } } } system ( "pause" ); system ( "cls" ); } else if (i==2){ cout << "您选择了:遍历联系人" <<endl; foreachelem(L); system ( "pause" ); system ( "cls" ); } else if (i==3){ cout << "您选择了:修改选定联系人" <<endl; cout << "请输入要修改的联系人姓名:" ; string name; cin >> name; LinkList B; B = SearchElemChar(L,1,name); if (B){ system ( "cls" ); cout << "联系人查找成功!姓名:" << B->data.name << endl; int select; cout << "---------修改姓名请输入1---------" << endl; cout << "---------修改电话请输入2---------" << endl; cout << "---------修改生日请输入3---------" << endl; cout << "---------修改地址请输入4---------" << endl; cout << "请根据序号输入对象的选项修改:" ; cin >> select; switch (select){ case 1: { string name; cout << "请输入新姓名:" ; cin >> name; B->data.name = name; cout << "修改完成!" << endl; break ; } case 2: { string number; cout << "请输入新电话:" ; cin >> number; if (number.size()!=11){ cout << "手机号输入有误,请大于11位!" << endl; } else { B->data.number = number; cout << "修改完成!" << endl; } break ; } case 3:{ string birthday; cout << "请输入新生日:" ; cin >> birthday; B->data.birthday = birthday; cout << "修改完成!" << endl; break ; } case 4:{ string adress; cout << "请输入新地址:" ; cin >> adress; B->data.adress = adress; cout << "修改完成!" << endl; break ; } default :cout << "序号输入错误,请重新输入!" <<endl; } } else { cout << "未查找到联系人!请重新输入!" << endl; } system ( "pause" ); system ( "cls" ); } else if (i==4){ system ( "cls" ); cout << "您选择了:查询选定联系人" <<endl; cout << "---------根据姓名查询请输入1---------" << endl; cout << "---------根据电话查询请输入2---------" << endl; cout << "---------根据生日查询请输入3---------" << endl; cout << "---------根据地址查询请输入4---------" << endl; int select; cout << "请根据序号输入对象的进行查询:" ; cin >> select; switch (select){ case 1:{ cout << "请输入要查询的联系人姓名:" ; string name; cin >> name; LinkList B; B = SearchElemChar(L,1,name); if (B){ cout<< "查询成功!" << endl; cout<< "联系人姓名:" << B->data.name << endl; cout<< "联系人电话:" << B->data.number << endl; cout<< "联系人生日:" << B->data.birthday << endl; cout<< "联系人地址:" << B->data.adress << endl; cout<< "插入日期:" << B->data.date << endl; } else { cout<< "查询失败!请重新输入!" << endl; } break ; } case 2: { cout << "请输入要查询的联系人电话:" ; string number; cin >> number; LinkList B; B = SearchElemChar(L,5,number); if (B){ cout<< "查询成功!" << endl; cout<< "联系人姓名:" << B->data.name << endl; cout<< "联系人电话:" << B->data.number << endl; cout<< "联系人生日:" << B->data.birthday << endl; cout<< "联系人地址:" << B->data.adress << endl; cout<< "插入日期:" << B->data.date << endl; } else { cout<< "查询失败!请重新输入!" << endl; } break ; } case 3:{ cout << "请输入要查询的联系人生日:" ; string bd; cin >> bd; LinkList B; B = SearchElemChar(L,2,bd); if (B){ cout<< "查询成功!" << endl; cout<< "联系人姓名:" << B->data.name << endl; cout<< "联系人电话:" << B->data.number << endl; cout<< "联系人生日:" << B->data.birthday << endl; cout<< "联系人地址:" << B->data.adress << endl; cout<< "插入日期:" << B->data.date << endl; } else { cout<< "查询失败!请重新输入!" << endl; } break ; } case 4:{ cout << "请输入要查询的联系人地址:" ; string ad; cin >> ad; LinkList B; B = SearchElemChar(L,3,ad); if (B){ cout<< "查询成功!" << endl; cout<< "联系人姓名:" << B->data.name << endl; cout<< "联系人电话:" << B->data.number << endl; cout<< "联系人生日:" << B->data.birthday << endl; cout<< "联系人地址:" << B->data.adress << endl; cout<< "插入日期:" << B->data.date << endl; } else { cout<< "查询失败!请重新输入!" << endl; } break ; break ; } default :cout << "序号输入错误,请重新输入!" <<endl; } system ( "pause" ); system ( "cls" ); } else if (i==5){ cout << "您选择了:删除联系人" <<endl; string name; cout << "请输入联系人姓名:" <<endl; cin >> name; LinkList D,P; P = SearchElemBefore(L,name); if (P){ D = P->next; P->next = D->next; delete D; //释放结点数据 cout << "删除成功!" <<endl; } else { cout<< "查询失败!未找到指定联系人!" << endl; } system ( "pause" ); system ( "cls" ); } else if (i==6){ cout << "系统已退出,欢迎下次使用!" <<endl; } else { cout << "输入异常,请重新选择输入!" <<endl; system ( "pause" ); system ( "cls" ); } } return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/jzfx12211/article/details/109741291