本文实例为大家分享了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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
/*大二实践周所作,
时间:2017.9,11
电话簿管理系统
*/
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL 0
typedef struct lianxiren{
char job[30];
char number[20];
char name[10];
char email[30];
struct lianxiren*next;
}lianxiren;
#define LEN sizeof(lianxiren)
lianxiren* creat_list()
{
lianxiren *head,*p1,*p2;
char name[10]; int n=0;
head=NULL;
p1=(lianxiren *) malloc (LEN);
p2=p1;
printf ( "请输入姓名(姓名为0时停止创建):" );
gets (name);
if ( strcmp (name, "0" )==0) return 0;
else {
strcpy (p1->name,name);
printf ( "请输入电话号码:" ); gets (p1->number);
printf ( "请输入工作单位:" ); gets (p1->job);
printf ( "请输入E-mail:" ); gets (p1->email);
}
while (1)
{
n++;
if (n==1)
head=p1;
else
p2->next=p1;
p2=p1;
printf ( "请输入姓名(姓名为0时停止创建):" );
gets (name);
if ( strcmp (name, "0" )==0) break ;
else {
p1=(lianxiren*) malloc (LEN);
strcpy (p1->name,name);
printf ( "请输入电话号码:" ); gets (p1->number);
printf ( "请输入工作单位:" ); gets (p1->job);
printf ( "请输入E-mail:" ); gets (p1->email);}
}p2->next=NULL;
return head;
} //链表创建函数
lianxiren* shifang_list(lianxiren*head)
{
lianxiren*p1;
for (;head!=NULL;)
{
p1=head;
head=head->next;
free (p1);
}
return head;
} //释放链表函数
void print_list(lianxiren*head)
{
void caidan();
int n=0;
printf ( "现在通讯录中有如下成员:" );
while (head!=NULL)
{
printf ( "\n名字:" ); puts (head->name);
printf ( "\n电话号码:" ); puts (head->number);
printf ( "\n工作单位:" ); puts (head->job);
printf ( "\nE-mail:" ); puts (head->email);
putchar ( '\n' );
head=head->next;n++;
if (n%8==0){
printf ( "按回车键显示下一页" );
getchar ();
system ( "cls" );
caidan();
}
}
printf ( "总共%d个联系人\n" ,n);
} //打印链表函数
int length(lianxiren*head)
{
int n = 0;
lianxiren *p;
p = head;
while (p != NULL)
{
p = p->next;
n++;
}
return n;
}
void paixu1(lianxiren*head)
{
void caidan();
int n=length(head);
int i,j;
lianxiren temp,*p;
p=head;
lianxiren a[100];
for (i=1;i<=n;i++)
{
strcpy (a[i].name,p->name);
strcpy (a[i].number,p->number);
strcpy (a[i].job,p->job);
strcpy (a[i].email,p->email);
p=p->next;
}
for (i=1;i<=n-1;i++)
{
for (j=1;j<=n-i;j++)
{ if ( strcmp (a[j].number,a[j+1].number)>0)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf ( "现在通讯录中有如下成员:" );
for (i=1;i<=n;i++)
{
printf ( "\n名字:" ); puts (a[i].name);
printf ( "\n电话号码:" ); puts (a[i].number);
printf ( "\n工作单位:" ); puts (a[i].job);
printf ( "\nE-mail:" ); puts (a[i].email);
putchar ( '\n' );
if (i%8==0){
printf ( "按回车键显示下一页" );
getchar ();
system ( "cls" );
caidan();
}
}
printf ( "总共%d个联系人\n" ,n);
}
void paixu2(lianxiren*head)
{
void caidan();
int n=length(head); int i,j;
lianxiren temp,*p;
p=head;
lianxiren a[100];
for (i=1;i<=n;i++)
{
strcpy (a[i].name,p->name);
strcpy (a[i].number,p->number);
strcpy (a[i].job,p->job);
strcpy (a[i].email,p->email);
p=p->next;
}
for (i=1;i<=n-1;i++)
{
for (j=1;j<=n-i;j++)
{ if ( strcmp (a[j].name,a[j+1].name)>0)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf ( "现在通讯录中有如下成员:" );
for (i=1;i<=n;i++)
{
printf ( "\n名字:" ); puts (a[i].name);
printf ( "\n电话号码:" ); puts (a[i].number);
printf ( "\n工作单位:" ); puts (a[i].job);
printf ( "\nE-mail:" ); puts (a[i].email);
putchar ( '\n' );
if (i%8==0){
printf ( "按回车键显示下一页" );
getchar ();
system ( "cls" );
caidan();
}
}
printf ( "总共%d个联系人\n" ,n);
}
void paixu_list(lianxiren*head)
{
void caidan();
if (head==NULL)
{ printf ( "电话簿为空,请重新创建!" );
return ;
}
char a;
printf ( "1,按电话号码升序排序\n2,按姓名字母升序排序\n" );
printf ( "请选择一种排序方法:" );
a= getchar ();
getchar ();
switch (a){
case '1' :paixu1(head); break ;
case '2' :paixu2(head); break ;
default : printf ( "输入有误!\n" ); break ;
}
}
void chazhao1(lianxiren*head)
{ if (head==NULL)
{
printf ( "电话簿为空,请重新创建" );
return ;
}
char name[10];
printf ( "请输入名字:" ); gets (name);
while ( strcmp (name,head->name)!=0)
{head=head->next;
if (head==NULL){ printf ( "电话簿未有此联系人\n" );
return ;}
}
printf ( "%s的电话号码为:" ,name);
puts (head->number);
printf ( "\n工作单位:" ); puts (head->job);
printf ( "\nE-mail:" ); puts (head->email);
} //通过名字查找
void chazhao2(lianxiren*head)
{ if (head==NULL)
{
printf ( "电话簿为空,请重新创建" );
return ;
}
char number[20];
printf ( "请输入电话号码:" ); gets (number);
while ( strcmp (number,head->number)!=0)
{head=head->next;
if (head==NULL){ printf ( "电话簿未有此联系人\n" );
return ;}
}
printf ( "%s的主人为:" ,number);
puts (head->name);
printf ( "\n工作单位:" ); puts (head->job);
printf ( "\nE-mail:" ); puts (head->email);
} //通过电话号码查找
void chazhao(lianxiren*head)
{
int n;
printf ( " 1,通过名字查找\n 2,通过电话号码查找\n 3,退出\n请选择你需要的服务:" );
scanf ( "%d" ,&n);
getchar ();
while (1)
{
switch (n){
case 1:{chazhao1(head); printf ( "请选择服务项:" ); scanf ( "%d" ,&n); getchar ();} break ;
case 2:{chazhao2(head); printf ( "请选择服务项:" ); scanf ( "%d" ,&n); getchar ();} break ;
case 3: return ; break ;
default :{ printf ( "输入不正确!" ); printf ( "请选择服务项:" ); scanf ( "%d" ,&n); getchar ();} break ;
}}
} //查找联系人
void add_list(lianxiren*head)
{
lianxiren*p1,*p2,*h;
char name[10];
printf ( "请输入名字(名字为0时停止)" );
gets (name);
if ( strcmp (name, "0" )!=0)
{
p1=(lianxiren*) malloc (LEN);
strcpy (p1->name,name);
printf ( "请输入电话号码:" ); gets (p1->number);
printf ( "请输入工作单位:" ); gets (p1->job);
printf ( "请输入E-mail:" ); gets (p1->email);
}
else return ;
h=p1;
while (1)
{
p2=p1;
printf ( "请输入名字(名字为0时停止)" );
gets (name);
if ( strcmp (name, "0" )==0) break ;
else {
p1=(lianxiren*) malloc (LEN);
strcpy (p1->name,name);
printf ( "请输入电话号码:" ); gets (p1->number);
printf ( "请输入工作单位:" ); gets (p1->job);
printf ( "请输入E-mail:" ); gets (p1->email);
p2->next=p1;
}
}
p1=head->next;
head->next=h;
p2->next=p1;
} //添加链表函数
lianxiren*xiugai_list(lianxiren*head)
{
char a[20];
printf ( "请输入联系人名字或电话:" );
gets (a);
if (head==NULL)
{
printf ( "电话簿为空,请重新创建" );
return head;
}
lianxiren*p=head;
while ( strcmp (a,p->name)!=0&& strcmp (a,p->number)!=0)
{
p=p->next;
if (p==NULL){ printf ( "电话簿未有此联系人\n" );
return head;}
}
printf ( "查找到了!" );
printf ( "\n名字:" ); puts (p->name);
printf ( "\n电话号码:" ); puts (p->number);
printf ( "\n工作单位:" ); puts (p->job);
printf ( "\nE-mail:" ); puts (p->email);
putchar ( '\n' );
int n;
while (1)
{ printf ( "请选择操作项(1,名字2,电话号码3,工作单位4,E-mail 5,退出):" );
scanf ( "%d" ,&n);
getchar ();
switch (n)
{
case 1:{ printf ( "请输入名字:" ); gets (p->name);} break ;
case 2:{ printf ( "请输入电话号码:" ); gets (p->number);} break ;
case 3:{ printf ( "请输入工作单位:" ); gets (p->job);} break ;
case 4:{ printf ( "请输入E-mail:" ); gets (p->email);} break ;
case 5: return head; break ;
default : printf ( "输入错误!" ); break ;
}
}
} //修改链表函数
lianxiren*delete_list(lianxiren*head)
{
char a[20];
printf ( "请输入联系人名字或电话:" );
gets (a);
if (head==NULL)
{
printf ( "电话簿为空,请重新创建" );
return head;
}
lianxiren*p=head,*p1;
while ( strcmp (a,p->name)!=0&& strcmp (a,p->number)!=0)
{
p1=p;
p=p->next;
if (p==NULL){ printf ( "电话簿未有此联系人\n" );
return head;}
}
printf ( "查找到了!" );
printf ( "\n名字:" ); puts (p->name);
printf ( "\n电话号码:" ); puts (p->number);
printf ( "\n工作单位:" ); puts (p->job);
printf ( "\nE-mail:" ); puts (p->email);
putchar ( '\n' );
char n;
printf ( "是否删除该联系人(Y/N)" );
n= getchar ();
switch (n)
{
case 'y' :
case 'Y' :{ if (p==head){head=p->next; free (p);} else {p1->next=p->next; free (p);} printf ( "成功删除!" );} break ;
case 'N' :
case 'n' : printf ( "取消删除!" ); break ;
default : printf ( "输入错误!" ); break ;
}
return head;
} //删除链表函数
void save_list(lianxiren*head)
{ FILE *fp;
if ((fp= fopen ( "dianhuabu.dat" , "wb" ))==NULL) {
printf ( "File cannot be opened\n" );
exit (0);}
if (head==NULL)
{
printf ( "通讯录为空\n" );
return ;
}
lianxiren*p1=head;
while (p1!=NULL)
{
if ( fwrite (p1,LEN,1,fp)!=1){
printf ( "cannot open file\n" );
return ;}
p1=p1->next;
}
printf ( "保存完毕!\n" );
fclose (fp);
} //文件写入函数
lianxiren*load_list(lianxiren*head)
{ FILE *fp;
if ((fp= fopen ( "dianhuabu.dat" , "rb" ))==NULL) {
printf ( "电话簿为空,请重新创建\n" );
exit (0);}
lianxiren*p1,*p2;
p1=(lianxiren*) malloc (LEN);
if ( fread (p1,LEN,1,fp)==0)
{ printf ( "电话簿为空,请重新创建" );
return head;
}
head=p1;
p2=p1;
p1=(lianxiren*) malloc (LEN);
while ( fread (p1,LEN,1,fp))
{
p2->next=p1;
p2=p1;
p1=(lianxiren*) malloc (LEN);
}
p2->next=NULL;
free (p1);
return (head);
fclose (fp);
} //文件读取函数
int kouling()
{
char s[20];
printf ( "请输入口令:" );
int n=0;
while (n<3)
{ scanf ( "%s" ,s);
getchar ();
if ( strcmp (s, "20160390527" )==0)
return 0;
else {n++;
if (n==3){
printf ( "抱歉,输入错误\n" );
return 1;}
printf ( "口令错误,请重新输入\n" );
}
} return 1;
} //口令验证,密码为:20160390527
void caidan()
{
printf ( " 欢迎进入电话簿系统 \n" );
printf ( "********************************************\n" );
printf ( " 1、创建电话簿(会将原有电话簿覆盖)\n" );
printf ( " 2、查找联系人 \n" );
printf ( " 3、添加联系人 \n" );
printf ( " 4、修改联系人 \n" );
printf ( " 5、删除联系人 \n" );
printf ( " 6、查看电话簿(排序) \n" );
printf ( " 7、退出系统 \n" );
printf ( "********************************************\n" );
} //菜单界面
void main()
{
if (kouling()) return ; //口令验证
system ( "cls" );
caidan(); //进入菜单界面
printf ( " 请选择你所需要的服务:" );
int n; scanf ( "%d" ,&n);
getchar ();
lianxiren*head;
while (1){
system ( "cls" );
caidan();
switch (n){
case 1:{
head=creat_list();
system ( "cls" );
caidan();
print_list(head);
save_list(head);
shifang_list(head);
printf ( "********************************************\n" );
printf ( "\n如需要其他服务,请重新输入:" );
scanf ( "%d" ,&n);
getchar ();} break ; //创建电话簿(创建链表、写入文件,释放链表)
case 2:{
head=load_list(head);
chazhao(head);
shifang_list(head);
printf ( "********************************************\n" );
printf ( "\n如需要其他服务,请重新输入:" );
scanf ( "%d" ,&n);
getchar ();} break ; //查找联系人(读入文件、查找函数、释放链表)
case 3:{
head=load_list(head);
add_list(head);
system ( "cls" );
caidan();
print_list(head);
save_list(head);
shifang_list(head);
printf ( "********************************************\n" );
printf ( "\n如需要其他服务,请重新输入:" );
scanf ( "%d" ,&n);
getchar ();} break ; //添加联系人(读入文件、添加链表、写入文件、释放链表)
case 4:{
head=load_list(head);
head=xiugai_list(head);
save_list(head);
shifang_list(head);
printf ( "********************************************\n" );
printf ( "\n如需要其他服务,请重新输入:" );
scanf ( "%d" ,&n);
getchar ();} break ; //修改联系人(读入文件,修改链表、写入文件、释放链表)
case 5:{
head=load_list(head);
head=delete_list(head);
save_list(head);
shifang_list(head);
printf ( "********************************************\n" );
printf ( "\n如需要其他服务,请重新输入:" );
scanf ( "%d" ,&n);
getchar ();} break ; //删除联系人(读入文件,删除链表、写入文件、释放链表)
case 6:{
head=load_list(head);
paixu_list(head);
shifang_list(head);
printf ( "********************************************\n" );
printf ( "\n如需要其他服务,请重新输入:" );
scanf ( "%d" ,&n);
getchar ();} break ; //查看电话簿(读入文件,排序链表、释放链表)
case 7:{
system ( "cls" );
return ;} break ; //退出
default :{
printf ( "\n输入有误,请重新输入:" );
scanf ( "%d" ,&n);
getchar ();} break ;
}}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_39513443/article/details/77932838