本文实例为大家分享了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
|
#ifndef CONTACT_H #define CONTACT_H #define NAME_MAX 20 #define SEX_MAX 10 #define TEL_MAX 12 #define ADDR_MAX 20 #define MEMBER_MAX 1 typedef struct info { char name[NAME_MAX]; char sex[SEX_MAX]; int age; char tele[TEL_MAX]; char addr[ADDR_MAX]; }info; typedef struct contact { info *data; int size; int capacity; }contact, *pcontact; //初始化通讯录 void InitContact(pcontact list); //添加联系人 void AddContact(pcontact list); //删除联系人 void DellContact(pcontact list); //查找联系人 void FindContact(pcontact list); //修改联系人 void ModContact(pcontact list); //显示所有联系人 void ShowContact(pcontact list); //联系人排序(选择法) void SortContact(pcontact list); //检查容量 int CheckSize(pcontact list); //查找联系人在数组data第几个位置 int FindContactAddr(pcontact list); //从文件中加载联系人到内存 void LoadContact(pcontact list); //将联系人从内从中保存到文件 void SaveContact(pcontact list); #endif |
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
|
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "contact.h" //初始化通讯录 void InitContact(pcontact list) { list->data = (info *) malloc ( sizeof (info)* MEMBER_MAX); list->size = 0; list->capacity = MEMBER_MAX; //每次初始化是加载一遍文件,防止最新录入的在保存在文件时覆盖上次 //打开程序时保存的信息 LoadContact(list); } //添加联系人 void AddContact(pcontact list) { info tmp_member; if (CheckCapacity(list) == 0) { printf ( "成员已满,添加失败!" ); return ; } printf ( "请输入姓名:" ); scanf ( "%s" , tmp_member.name); printf ( "请输入性别:" ); scanf ( "%s" , tmp_member.sex); printf ( "请输入年龄:" ); scanf ( "%d" , &tmp_member.age); printf ( "请输入电话:" ); scanf ( "%s" , tmp_member.tele); printf ( "请输入地址:" ); scanf ( "%s" , tmp_member.addr); list->data[list->size] = tmp_member; printf ( "添加成功!\n" ); list->size++; } //删除联系人 void DellContact(pcontact list) { char tmp_name[NAME_MAX] = { 0 }; int ret = 0; printf ( "请输入姓名:" ); scanf ( "%s" , tmp_name); ret = FindContactAddr(list, tmp_name); if (ret == -1) { printf ( "无此联系人!\n" ); return ; } for (ret; ret < list->size - 1; ret++) { list->data[ret] = list->data[ret + 1]; } printf ( "删除成功!\n" ); list->size--; } //查找联系人 void FindContact(pcontact list) { char tmp_name[NAME_MAX] = { 0 }; printf ( "请输入姓名:" ); scanf ( "%s" , tmp_name); for ( int i = 0; i < list->size; i++) { if ( strcmp (tmp_name, list->data[i].name) == 0) { printf ( "===============================\n" ); printf ( "姓名:%s\n" , list->data[i].name); printf ( "性别:%s\n" , list->data[i].sex); printf ( "年龄:%d\n" , list->data[i].age); printf ( "电话:%s\n" , list->data[i].tele); printf ( "地址:%s\n" , list->data[i].addr); printf ( "===============================\n" ); return ; } } printf ( "无此联系人!\n" ); } //查找联系人在数组data第几个位置 int FindContactAddr(pcontact list, char *tmp_name) { for ( int i = 0; i < list->size; i++) { if ( strcmp (tmp_name, list->data[i].name) == 0) return i; } return -1; } //修改联系人 void ModContact(pcontact list) { info tmp_member; int ret = 0; printf ( "请输入姓名:" ); scanf ( "%s" , tmp_member.name); ret = FindContactAddr(list, tmp_member.name); if (ret == -1) { printf ( "无此联系人!\n" ); return ; } printf ( "请输入性别:" ); scanf ( "%s" , tmp_member.sex); printf ( "请输入年龄:" ); scanf ( "%s" , &tmp_member.age); printf ( "请输入电话:" ); scanf ( "%s" , tmp_member.tele); printf ( "请输入地址:" ); scanf ( "%s" , tmp_member.addr); list->data[ret] = tmp_member; printf ( "修改成功!\n" ); } //联系人排序(选择法) void SortContact(pcontact list) { info tmp_member; for ( int i = 0; i < list->size - 2; i++) { for ( int j = 1; j < list->size - 1; j++) { if ( strcmp (list->data[i].name, list->data[j].name) > 0) { tmp_member = list->data[i]; list->data[i] = list->data[j]; list->data[j] = tmp_member; } } } } //检查上次动态开辟的空间是否够用,不够时继续开辟 int CheckCapacity(pcontact list) { if (list->size == list->capacity) { info *tmp = (info *) malloc ( sizeof (info)* (list->capacity + 10)); if (tmp == NULL) return 0; memcpy (tmp, list->data, sizeof (info) * list->size); free (list->data); list->data = tmp; list->capacity += 10; } return 1; } //显示所有联系人 void ShowContact(pcontact list) { for ( int i = 0; i < list->size; i++) { printf ( "============第%d位==============\n" , i + 1); printf ( "姓名:%s\n" , list->data[i].name); printf ( "性别:%s\n" , list->data[i].sex); printf ( "年龄:%d\n" , list->data[i].age); printf ( "电话:%s\n" , list->data[i].tele); printf ( "地址:%s\n" , list->data[i].addr); printf ( "===============================\n" ); } } //从文件中加载联系人到内存 void LoadContact(pcontact list) { info tmp_member; FILE *pf = NULL; pf = fopen ( "./contatc.dat" , "rb" ); if (pf == NULL) perror ( "fopen error" ); //按照内存块读取,一次读1块,1块大小是sizeof(info) while ( fread (&tmp_member, sizeof (info), 1, pf)) { if (CheckCapacity(list)) { list->data[list->size] = tmp_member; list->size++; } } if (pf != NULL) { fclose (pf); pf = NULL; printf ( "加载成功!\n" ); } } //将联系人从内从中保存到文件 void SaveContact(pcontact list) { FILE *pf = NULL; pf = fopen ( "./contatc.dat" , "wb" ); if (pf == NULL) perror ( "fopen error" ); for ( int i = 0; i < list->size; i++) { //按照内存块写入,一次写1块,1块大小是sizeof(info) fwrite (list->data + i, sizeof (info), 1, pf); } if (pf != NULL) { fclose (pf); pf = NULL; printf ( "保存成功!\n" ); } } |
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
73
74
75
76
77
78
79
80
81
82
83
|
#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include "contact.h" enum opt { Exit, Add, Dell, Find, Mod, Show, Load, Save, Sort, }; void menu( void ) { printf ( "/*========================通讯录======================*/\n" ); printf ( "/*= 0.退出通讯录 1.添加联系人 =*/\n" ); printf ( "/*= 2.删除联系人 3.查找联系人 =*/\n" ); printf ( "/*= 4.修改联系人 5.显示所有联系人 =*/\n" ); printf ( "/*= 6.加载联系人 7.保存联系人 =*/\n" ); printf ( "/*= 8.联系人排序 =*/\n" ); printf ( "/*====================================================*/\n" ); } void TestContact( void ) { contact pc; InitContact(&pc); int opt = 0; do { menu(); printf ( "请选择:>" ); scanf ( "%d" , &opt); switch (opt) { case Exit: printf ( "退出系统...\n" ); break ; case Add: AddContact(&pc); break ; case Dell: DellContact(&pc); break ; case Find: FindContact(&pc); break ; case Mod: ModContact(&pc); break ; case Show: ShowContact(&pc); break ; case Load: LoadContact(&pc); break ; case Save: SaveContact(&pc); break ; case Sort: SortContact(&pc); break ; default : printf ( "输入选项有误,重新输入!\n" ); break ; } } while (opt); } int main( void ) { TestContact(); printf ( "\n" ); system ( "pause" ); return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/yangke369/article/details/106132437