-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddressbook.c
313 lines (257 loc) · 5.42 KB
/
addressbook.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
/*
* =====================================================================================
*
* Filename: addressbook.c
*
* Description: 通讯录管理系统
*
* Version: 1.0
* Created: 2013年06月15日 13时06分19秒
* Revision: none
* Compiler: gcc
*
* Author: nonoDevil, linux.kakit@gmail.com
* Company: Xiyou Linux Group
*
* =====================================================================================
*/
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "addressbook.h"
int main(int argc, char *argv[])
{
LinkList head = NULL;
char choice = '\0';
init(&head);
load(head);
welcome_menu(&choice, head);
return EXIT_SUCCESS;
}
/*
* Descritpion: 欢迎界面
*/
int welcome_menu(char *choice, LinkList head)
{
menu:
printf("-----------------------------------------------------\n");
printf("\t\t\t通讯录管理系统\t\t\t\n");
printf("\n");
printf("\t\t\t1.查询联系人\n");
printf("\t\t\t2.增加联系人\n");
printf("\t\t\t3.编辑联系人\n");
printf("\t\t\t4.删除联系人\n");
printf("\t\t\t5.退出通讯录系统\n");
printf("-----------------------------------------------------\n");
*choice = getchar();
printf("choice = %c\n", *choice);
setbuf(stdin, NULL);
system("clear");
list(head);
switch (*choice) {
case '1':
query(head);
break;
case '2':
add(head);
save(head);
break;
case '3':
modify(head);
save(head);
break;
case '4':
del(head);
save(head);
break;
case '5':
exit(0);
default :
printf("输入的选项有误,请重新输入!\n");
goto menu;
}
getchar();
goto menu;
return 0;
}
/*
* Description: 初始化
*/
int init(LinkList *head)
{
*head = (LinkList)malloc(sizeof(Node));
#ifdef DEBUG
printf("head->number = %s\n", (*head)->number);
#endif
strcpy((*head)->number,"\0");
#ifdef DEBUG
printf("head->number = %s\n", (*head)->number);
#endif
strcpy((*head)->name,"\0");
(*head)->next = NULL;
return 0;
}
/*
* Description: 增加
*/
int add(LinkList head)
{
Node *newnode = NULL;
if (!(newnode = (Node *)malloc(sizeof(Node)))) {
perror("malloc");
}
printf("请输入联系人姓名:\n");
scanf("%s", newnode->name);
printf("请输入联系人号码:\n");
scanf("%s", &newnode->number);
#ifdef DEBUG
printf("newnode->name = %s\n", newnode->name);
printf("newnode->number = %s\n", newnode->number);
#endif
/*头插法*/
newnode->next = head->next;
head->next = newnode;
return 0;
}
/*
* Description: 删除
*/
int del(LinkList head)
{
char d_name[NAME_LEN];
Node *cur = NULL;
Node *pre = NULL;
list(head);
printf("请输入要删除的联系人姓名:\n");
scanf("%s", d_name);
cur = head->next;
pre = head;
while (cur != NULL) {
if (0 == (strcmp(d_name, cur->name))) {
printf("将要删除联系人:%s 号码:%s\n", cur->name, cur->number);
pre->next = cur->next;
free(cur);
}
pre = pre->next;
cur = cur->next;
}
printf("删除成功!\n");
return 0;
}
/*
* Description: 修改
*/
int modify(LinkList head)
{
Node *cur = NULL;
char m_name[NAME_LEN];
printf("请输入要修改的联系人姓名:\n");
scanf("%s", m_name);
cur = head->next;
while (cur != NULL) {
if (0 == (strcmp(m_name, cur->name))) {
printf("请重新输入姓名:");
scanf("%s", cur->name);
printf("请重新输入电话号码:");
scanf("%s", cur->number);
printf("修改成功!\n");
#ifdef DEBUG
list(head);
#endif
return 0;
}
cur = cur->next;
}
printf("未找到联系人:%s\n", m_name);
return 0;
}
/*
* Description: 查询
*/
int query(LinkList head)
{
Node *cur = NULL;
char q_name[NAME_LEN];
printf("请输入要查询的联系人姓名或号码:\n");
scanf("%s", q_name);
cur = head->next;
while (cur != NULL) {
if ((0 == strcmp(q_name, cur->name)) || (0 == strcmp(q_name, cur->number))) {
printf("查询到的联系人信息如下:\n");
printf("\t姓名\t\t号码\n");
printf("\t%s\t\t%s\n", cur->name, cur->number);
return 0;
}
cur = cur->next;
}
printf("数据库无相关信息!\n");
return 0;
}
/*
* Description: 加载
*/
int load(LinkList head)
{
FILE *r_file = NULL;
Node *newnode = NULL;
r_file = fopen("./data.txt", "rt");
if (!r_file) {
perror("fopen");
r_file = fopen("./data.txt", "wt");
fclose(r_file);
return 0;
}
while (!feof(r_file)) {
if (!(newnode = (Node *)malloc(sizeof(Node)))) {
perror("malloc");
exit(-1);
}
fscanf(r_file, "%s\t%s\n", newnode->name, newnode->number);
printf("读入数据:%s\t%s...\n", newnode->name, newnode->number);
newnode->next = head->next;
head->next = newnode;
}
printf("读入数据完毕!\n");
fclose(r_file);
#ifdef DEBUG
list(head);
#endif
return 0;
}
/*
* Description: 保存
*/
int save(LinkList head)
{
Node *cur = NULL;
FILE *w_file = NULL;
w_file = fopen("./data.txt", "wt");
if (!w_file) {
perror("fopen");
exit(-1);
}
cur = head->next;
while (cur != NULL) {
fprintf(w_file, "%s\t%s\n", cur->name, cur->number);
printf("正在写入数据: %s\t%s...\n", cur->name, cur->number);
cur = cur->next;
}
printf("保存数据成功!\n");
return 0;
}
/*
* Description: 列出信息
*/
int list(LinkList head)
{
Node *cur = NULL;
printf("\t姓名\t\t\t号码\n");
cur = head->next;
while (cur != NULL) {
printf("\t%s\t\t%s\n", cur->name, cur->number);
cur = cur->next;
}
return 0;
}