本文实例为大家分享了C语言实现通讯录功能的具体代码,供大家参考,具体内容如下
头文件contact.h
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
|
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 //#define max 1000 #define fault 3 #define max_name 20 #define max_telephone 12 #define max_sex 5 #define max_address 30 #include<stdio.h> #include<string.h> #include<assert.h> #include<stdlib.h> #include<errno.h> enum option { EXIT, ADD, DEL, SEARCH, MODIFY, SHOW, SORT, SAVE }; struct people_information { char name[max_name]; char telephone[max_telephone]; char sex[max_sex]; char address[max_address]; int age; }; struct contact { //struct people_information data[max]; struct people_information *data; //动态空间 int size; //记录当前有多少信息 int capacity; //当前最大容量 }; void initcontact( struct contact* ps); //初始化函数 void add( struct contact* ps); void my_delete( struct contact* ps); void search( const struct contact* ps); void modify( struct contact* ps); //修改指定联系人 void show( const struct contact* ps); //展示全部联系人 void sort( struct contact* ps); void destroy( struct contact* ps); void save( struct contact* ps); |
分装函数contact.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
|
#define _CRT_SECURE_NO_WARNINGS 1 #include"contact.h" void initcontact( struct contact* ps) { //memset(ps->data, 0, sizeof(ps->data)); //ps->size = 0;//初始化通讯录中只有0个信息 ps->data = ( struct people_information*) malloc (fault * sizeof ( struct people_information)); if (ps->data == NULL) { return ; } ps->size = 0; ps->capacity = fault; } void checkcapacity( struct contact* ps) { if (ps->size == ps->capacity) { struct people_information * ptr= realloc (ps->data, (ps->capacity + 2)* sizeof ( struct people_information)); if (ptr != NULL) { ps->data = ptr; ps->capacity += 2; printf ( "增容成功\n" ); } else { printf ( "增容失败\n" ); } } } void add( struct contact* ps) { /*if (ps->size == max) { printf("通讯录已满,无法增加\n"); } else { printf("请输入姓名:>"); scanf("%s", ps->data[ps->size].name); printf("请输入电话:>"); scanf("%s", ps->data[ps->size].telephone); printf("请输入性别:>"); scanf("%s", ps->data[ps->size].sex); printf("请输入住址:>"); scanf("%s", ps->data[ps->size].address); printf("请输入年龄:>"); scanf("%d", &(ps->data[ps->size].age)); ps->size++; printf("添加成功!\n"); }*/ //检测当前通讯录的容量 checkcapacity(ps); printf ( "请输入姓名:>" ); scanf ( "%s" , ps->data[ps->size].name); printf ( "请输入电话:>" ); scanf ( "%s" , ps->data[ps->size].telephone); printf ( "请输入性别:>" ); scanf ( "%s" , ps->data[ps->size].sex); printf ( "请输入住址:>" ); scanf ( "%s" , ps->data[ps->size].address); printf ( "请输入年龄:>" ); scanf ( "%d" , &(ps->data[ps->size].age)); ps->size++; printf ( "添加成功!\n" ); } void show( const struct contact* ps) { if (ps->size == 0) { printf ( "通讯录为空\n" ); } else { int i = 0; for (i = 0; i < ps->size; i++) { printf ( "%-20s\t%-12s\t%-5s\t%-30s\t%-4s\n" , "姓名" , "电话" , "性别" , "住址" , "年龄" ); printf ( "%-20s\t%-12s\t%-5s\t%-30s\t%-4d\n" , ps->data[i].name, ps->data[i].telephone, ps->data[i].sex, ps->data[i].address, ps->data[i].age); } } } static int FindByName( const struct contact* ps, char name[max_name]) //冗余函数、查找函数 { //找到了返回名字所在元素的下标,找不到返回-1 int i = 0; for (i = 0; i < ps->size; i++) { if (0 == strcmp (ps->data[i].name, name)) { return i; } } return -1; } void my_delete( struct contact* ps) { printf ( "请输入您要删除联系人的姓名:" ); char name[max_name] ; scanf ( "%s" , name); int pos = FindByName(ps, name); if (pos==-1) { printf ( "您要删除的人不存在\n" ); } else { //删除数据 int j = 0; for (j = pos; j < ps->size - 1; j++) { ps->data[j] = ps->data[j + 1]; } ps->size--; printf ( "删除成功!\n" ); } } void search( const struct contact* ps) { printf ( "请输入您要查找联系人的姓名:" ); char name[max_name]; scanf ( "%s" , name); int pos = FindByName(ps, name); if (pos == -1) { printf ( "您要查找的联系人不存在\n" ); } else { printf ( "%-20s\t%-12s\t%-5s\t%-30s\t%-4s\n" , "姓名" , "电话" , "性别" , "住址" , "年龄" ); printf ( "%-20s\t%-12s\t%-5s\t%-30s\t%-4d\n" , ps->data[pos].name, ps->data[pos].telephone, ps->data[pos].sex, ps->data[pos].address, ps->data[pos].age); } } //完成 void modify( struct contact* ps) { printf ( "请输入您要修改联系人的姓名:" ); char name[max_name]; scanf ( "%s" , name); int pos = FindByName(ps, name); if (pos == -1) { printf ( "您要修改的联系人不存在\n" ); } else { printf ( "新的姓名:" ); scanf ( "%s" , ps->data[pos].name); printf ( "新的电话:" ); scanf ( "%s" , ps->data[pos].telephone); printf ( "新的性别:" ); scanf ( "%s" , ps->data[pos].sex); printf ( "新的住址:" ); scanf ( "%s" , ps->data[pos].address); printf ( "新的年龄:" ); scanf ( "%d" , &(ps->data[pos].age)); printf ( "修改完成!\n" ); } } void sort( struct contact* ps) { assert (ps); if (ps->size == 0) { printf ( "通讯录为空,无需排序\n" ); } else if (ps->size == 1) { printf ( "您的通讯录中只有一个人,无需排序\n" ); printf ( "%-20s\t%-12s\t%-5s\t%-30s\t%-4s\n" , "姓名" , "电话" , "性别" , "住址" , "年龄" ); printf ( "%-20s\t%-12s\t%-5s\t%-30s\t%-4d\n" , ps->data[0].name, ps->data[0].telephone, ps->data[0].sex, ps->data[0].address, ps->data[0].age); } else { int i = 0; for (i = 0; i < ps->size; i++) { int j = 0; for (j = 0; j < ps->size - i - 1; j++) { int ret = strcmp (ps->data[j].name, ps->data[j + 1].name); if (ret > 0) { char tmp1[max_name] = { 0 }; char tmp2[max_telephone] = { 0 }; char tmp3[max_sex] = { 0 }; char tmp4[max_address] = { 0 }; int tmp5 = 0; strcpy (tmp1, ps->data[j].name); strcpy (tmp2, ps->data[j].telephone); strcpy (tmp3, ps->data[j].sex); strcpy (tmp4, ps->data[j].address); tmp5 = ps->data[j].age; //交给临时变量 strcpy (ps->data[j].name, ps->data[j+1].name); strcpy (ps->data[j].telephone, ps->data[j+1].telephone); strcpy (ps->data[j].sex, ps->data[j+1].sex); strcpy (ps->data[j].address, ps->data[j+1].address); ps->data[j].age= ps->data[j+1].age; strcpy ( ps->data[j + 1].name,tmp1); strcpy ( ps->data[j + 1].telephone,tmp2); strcpy ( ps->data[j + 1].sex,tmp3); strcpy ( ps->data[j + 1].address,tmp4); ps->data[j + 1].age=tmp5; } } } } printf ( "排序完成!\n" ); } void destroy( struct contact* ps) { free (ps->data); ps->data = NULL; } void save( struct contact* ps) { FILE * pfwrite = fopen ( "contact.dat" , "wb" ); if (pfwrite == NULL) { printf ( "%s\n" , strerror ( errno )); return ; } //写通讯录中的数据到文件中 int i = 0; for (i = 0; i < ps->size; i++) { fwrite (&(ps->data[i]), sizeof ( struct people_information), 1, pfwrite); } fclose (pfwrite); pfwrite = NULL; } |
源文件test.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
|
#define _CRT_SECURE_NO_WARNINGS 1 //功能: //一、存放1000个系人信息,信息包括:姓名、电话、性别、住址、年龄 //二、增加联系人信息 //三、删除指定联系人信息 //四、查找联系人信息 //五、修改联系人信息 //六、显示全部联系人信息 //七、排序 #include<stdio.h> #include"contact.h" void menu() { printf ( "******************************\n" ); printf ( "****1.add 2.delete****\n" ); printf ( "****3.search 4.modify****\n" ); printf ( "****5.show 6.sort ****\n" ); printf ( "****7.save 0.exit ****\n" ); printf ( "******************************\n" ); } int main() { int input = 0; //创建通讯录 struct contact con; //con就是通讯录,存放1000个信息 //初始化通讯录 initcontact(&con); do { menu(); printf ( "请选择:>" ); scanf ( "%d" , &input); switch (input) { case ADD: add(&con); //增加联系人信息 break ; case DEL: my_delete(&con); //删除指定联系人信息 break ; case SEARCH: search(&con); //查找联系人信息 break ; case MODIFY: modify(&con); //修改联系人信息 break ; case SHOW: show(&con); //显示全部联系人信息 break ; case SORT: sort(&con); //排序 break ; case EXIT: printf ( "退出通讯录\n" ); //销毁通讯录 destroy(&con); break ; case SAVE: save(&con); default : printf ( "选择错误\n" ); break ; } } while (input); return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_48690739/article/details/120277034