通讯录信息管理系统
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 /*
2 * ContractManager.cc
3 *
4 * Created on: 2010-7-29
5 * Author: LiuFeng
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #define INPUT_LINK_LENGTH 40
13 #define BUFSIZE 16
14 #define DBSIZE 100
15
16 typedef struct
17 {
18 int ID;
19 char name[BUFSIZE];
20 char workPlace[BUFSIZE];
21 char teleOff[BUFSIZE];
22 char teleHome[BUFSIZE];
23 }*ADDRP, ADDRESS;
24
25 ADDRESS addrTmp[DBSIZE], adTmp;
26
27 int cntAddr = 0;
28 const char * strFmt = "\n%d\t%s\t%s\t%s\t%s\n";
29 const char * fstrFmt = "%d %s %s %s %s \n";
30
31 // ------------------------------------------------------------------------------------------
32 // Utilities
33 void inputItem(ADDRP itp)
34 {
35 printf("请输入ID:\n"); scanf("%d", &(itp->ID));
36 printf("请输入姓名:\n"); scanf("%s", itp->name);
37 printf("请输入工作单位:\n"); scanf("%s", itp->workPlace);
38 printf("请输入办公电话:\n"); scanf("%s", itp->teleOff);
39 printf("请输入家庭电话:\n"); scanf("%s", itp->teleHome);
40 };
41
42 void outputItem(ADDRP itemp)
43 {
44 printf(strFmt, itemp->ID, itemp->name, itemp->workPlace, itemp->teleOff,itemp->teleHome);
45 };
46
47 void list(ADDRESS addr[], int length)
48 {
49 for (int i = 0; i < length; ++i)
50 outputItem(&addr[i]);
51 }
52
53 // ------------------------------------------------------------------------------------------
54 // Sorted Display
55 void idsort(ADDRESS addr[])
56 {
57 for (int i = 0; i < cntAddr; ++i)
58 addrTmp[i] = addr[i];
59
60 for (int i = 0; i < cntAddr - 1; ++i)
61 for (int j = i + 1; j < cntAddr; ++j)
62 if (addrTmp[i].ID > addrTmp[j].ID)
63 {
64 adTmp = addrTmp[i];
65 addrTmp[i] = addrTmp[j];
66 addrTmp[j] = adTmp;
67 }
68 list(addrTmp, cntAddr);
69 };
70
71 void namesort(ADDRESS addr[], int length)
72 {
73 for (int i = 0; i < length; ++i)
74 addrTmp[i] = addr[i];
75
76 for (int i = 0; i < length - 1; ++i)
77 for (int j = i + 1; j < length; ++j)
78 if (strcmp(addrTmp[i].name, addrTmp[j].name) > 0)
79 {
80 adTmp = addrTmp[i];
81 addrTmp[i] = addrTmp[j];
82 addrTmp[j] = adTmp;
83 }
84 list(addrTmp, length);
85 };
86
87 void unitssort(ADDRESS addr[], int length)
88 {
89 for (int i = 0; i < length; ++i)
90 addrTmp[i] = addr[i];
91
92 for (int i = 0; i < length - 1; ++i)
93 for (int j = i + 1; j < length; ++j)
94 if (strcmp(addrTmp[i].workPlace, addrTmp[j].workPlace) > 0)
95 {
96 adTmp = addrTmp[i];
97 addrTmp[i] = addrTmp[j];
98 addrTmp[j] = adTmp;
99 }
100 list(addrTmp, length);
101 };
102
103 // ------------------------------------------------------------------------------------------
104 // Search
105 void namesearch(ADDRESS adr[], int length)
106 {
107 char in[BUFSIZE];
108 printf("请输入姓名:\n");
109 scanf("%s", in);
110
111 for (int i = 0; i < length; ++i)
112 if (strcmp(adr[i].name, in) == 0)
113 outputItem(&adr[i]);
114 };
115
116 void unitssearch(ADDRESS adr[], int length)
117 {
118 char in[BUFSIZE];
119 printf("请输入工作单位:\n");
120 scanf("%s", in);
121
122 for (int i = 0; i < length; ++i)
123 if (strcmp(adr[i].workPlace, in) == 0)
124 outputItem(&adr[i]);
125 };
126
127 void officenumbersearch(ADDRESS adr[], int length)
128 {
129 char in[BUFSIZE];
130 printf("请输入办公电话:\n");
131 scanf("%s", in);
132
133 for (int i = 0; i < length; ++i)
134 if (strcmp(adr[i].teleOff, in) == 0)
135 outputItem(&adr[i]);
136 };
137
138 void homenumbersearch(ADDRESS adr[], int length)
139 {
140 char in[BUFSIZE];
141 printf("请输入家庭电话:\n");
142 scanf("%s", in);
143
144 for (int i = 0; i < length; ++i)
145 if (strcmp(adr[i].teleHome, in) == 0)
146 outputItem(&adr[i]);
147 };
148
149 // ------------------------------------------------------------------------------------------
150 // Delete
151 int delet(ADDRESS adr[], int length)
152 {
153 int j;
154 printf("要修改第几条记录:\n");
155 scanf("%d", &j); --j;
156
157 for (int i = j; i < length - 1; ++i)
158 adr[i] = adr[i + 1];
159 return --cntAddr;
160 };
161
162 // ------------------------------------------------------------------------------------------
163 // Modify
164 void rewrite(ADDRESS adr[])
165 {
166 int j;
167 printf("要修改第几条记录:\n");
168 scanf("%d", &j);
169
170 inputItem(&adr[j]);
171 };
172
173 // ------------------------------------------------------------------------------------------
174 // Add
175 int add(ADDRESS adr[], int last)
176 {
177 inputItem(&adr[++cntAddr]);
178 return cntAddr;
179 };
180
181 int enter(ADDRESS adr[])
182 {
183 while (1)
184 {
185 inputItem(&adr[cntAddr++]);
186
187 printf("\n输入下一条?Y/N:");
188 fflush( stdin);
189 char quitChar = getchar();
190 if ('N' == quitChar || 'n' == quitChar) break;
191 }
192 return cntAddr;
193 };
194
195 // ------------------------------------------------------------------------------------------
196 // file I/O
197 int load(ADDRESS adr[])
198 {
199 FILE *fp;
200 if ((fp = fopen("D:\\ConData.dat", "r+")) != NULL)
201 {
202 cntAddr = 0;
203 while (!feof(fp))
204 {
205 fscanf(fp, fstrFmt, &(adr[cntAddr].ID), adr[cntAddr].name,adr[cntAddr].workPlace, adr[cntAddr].teleOff,adr[cntAddr].teleHome);
206 ++cntAddr;
207 }
208 }
209 fclose(fp);
210 return cntAddr;
211 };
212
213 void display(ADDRESS* addr)
214 {
215 list(addr, cntAddr);
216 };
217
218 void save(ADDRESS adr[], int length)
219 {
220 FILE *fp = fopen("D:\\ConData.dat", "w+");
221 for (int i = 0; i < cntAddr; ++i)
222 fprintf(fp, fstrFmt, adr[i].ID, adr[i].name, adr[i].workPlace,adr[i].teleOff, adr[i].teleHome);
223 fclose(fp);
224 };
225
226 void copy(void)
227 {
228 char in[BUFSIZE];
229 FILE *fp, *fc;
230 printf("请输入新文件名(路径中的反斜杠用\\表示)\n");
231 scanf("%s", in);
232
233 if ((fp = fopen("D:\\ConData.dat", "r+")) != NULL)
234 {
235 fc = fopen(in, "w");
236 while (!feof(fp))
237 {
238 fscanf(fp, fstrFmt, &(adTmp.ID), adTmp.name, adTmp.workPlace,adTmp.teleOff, adTmp.teleHome);
239 fprintf(fc, fstrFmt, adTmp.ID, adTmp.name, adTmp.workPlace,adTmp.teleOff, adTmp.teleHome);
240 }
241 fclose(fc);
242 }
243 fclose(fp);
244 };
245
246 // ------------------------------------------------------------------------------------------
247 // Menu
248 void showpaixu(ADDRESS adr[], int length)
249 {
250 int choice;
251 menu1:
252 system("cls");
253 printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n");
254 printf(" 1. 全部显示通讯录信息\n\n");
255 printf(" 2. 按序号显示通讯录信息\n\n");
256 printf(" 3. 按姓名排序\n\n");
257 printf(" 4. 按工作单位排序\n\n");
258 printf(" 0. 返回上一级菜单\n\n");
259 printf(" ********************************************************************\n");
260 printf(" 请选择(0~4)\n ");
261
262 scanf("%d", &choice);
263
264 switch (choice)
265 {
266 case 1:
267 display(adr);
268 system("pause");
269 break;
270 case 2:
271 idsort(adr);
272 system("pause");
273 break;
274 case 3:
275 namesort(adr, length);
276 system("pause");
277 break;
278 case 4:
279 unitssort(adr, length);
280 system("pause");
281 break;
282 case 0:
283 return;
284 default:
285 printf("输入选项有误,请重新输入(0-4):");
286 system("pause");
287 break;
288 }
289 goto menu1;
290 };
291
292 void chazhao(ADDRESS adr[], int length)
293 {
294 int choice;
295 menu2:
296 system("cls");
297 printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n");
298 printf(" 1. 通过姓名查找\n\n");
299 printf(" 2. 通过工作单位查找\n\n");
300 printf(" 3. 通过办公电话查找\n\n");
301 printf(" 4. 通过家庭电话查找\n\n");
302 printf(" 0. 返回上一级菜单\n\n");
303 printf(" ********************************************************************\n");
304 printf(" 请选择(0~4)\n ");
305 scanf("%d", &choice);
306 switch (choice)
307 {
308 case 1:
309 namesearch(adr, length);
310 system("pause");
311 break;
312 case 2:
313 unitssearch(adr, length);
314 system("pause");
315 break;
316 case 3:
317 officenumbersearch(adr, length);
318 system("pause");
319 break;
320 case 4:
321 homenumbersearch(adr, length);
322 system("pause");
323 break;
324 case 0:
325 return;
326 default:
327 printf("输入选项有误,请重新输入(0-4):");
328 system("pause");
329 break;
330 }
331 goto menu2;
332 };
333
334 void biangeng(ADDRESS adr[], int length)
335 {
336 int choice;
337 menu3:
338 system("cls");
339 printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n");
340 printf(" 1. 删除记录\n\n");
341 printf(" 2. 修改记录\n\n");
342 printf(" 3. 添加记录\n\n");
343 printf(" 0. 返回上一级菜单\n\n");
344 printf(" ********************************************************************\n");
345 printf(" 请选择(0~3)\n ");
346 scanf("%d", &choice);
347 switch (choice)
348 {
349 case 1:
350 length = delet(adr, length);
351 save(adr, length);
352 getchar();
353 break;
354 case 2:
355 rewrite(adr);
356 save(adr, length);
357 break;
358 case 3:
359 length = add(adr, length);
360 save(adr, length);
361 getchar();
362 break;
363 case 0:
364 return;
365 default:
366 printf("输入选项有误,请重新输入(0-3):");
367 system("pause");
368 break;
369 }
370 goto menu3;
371 };
372
373 int wenjian(ADDRESS adr[], int length)
374 {
375 int choice;
376 menu4:
377 system("cls");
378 printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n");
379 printf(" 1. 导入通讯录到内存\n\n");
380 printf(" 2. 把当前文件复制到新文件上\n\n");
381 printf(" 0. 返回上一级菜单\n\n");
382 printf(" ********************************************************************\n");
383 printf(" 请选择(0~2)\n ");
384 scanf("%d", &choice);
385 switch (choice)
386 {
387 case 1:
388 length = load(adr);
389 printf("您已经成功的导入通讯录文件到内存!\n");
390 system("pause");
391 break;
392 case 2:
393 copy();
394 printf("复制成功!\n");
395 system("pause");
396 break;
397 case 0:
398 return length;
399 default:
400 printf("输入选项有误,请重新输入(0-2):");
401 system("pause");
402 break;
403 }
404 goto menu4;
405 };
406
407 void about()
408 {
409 system("color 87");
410 system("cls");
411 printf("\n\n\n\n 《通讯录信息管理系统》\n\n\n");
412 printf(" *****************************************\n\n\n");
413 printf(" 版权所有(C) 2010-2011 \n\n\n");
414 printf(" 制作人:\n\n\n");
415 printf(" *****************************************\n\n\n");
416 system("pause");
417 };
418
419 int main(void)
420 {
421 int choice;
422 int length = 0;
423 ADDRESS adr[DBSIZE];
424
425 length = load(adr);
426
427 menu:
428 system("color f9");
429 system("cls");
430 printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n");
431 printf(" 请选择以下通讯录管理功能:\n\n");
432 printf(" 1. 通讯录信息输入\n\n");
433 printf(" 2. 通讯录信息浏览及排序\n\n");
434 printf(" 3. 通讯录信息查询\n\n");
435 printf(" 4. 通讯录信息更改\n\n");
436 printf(" 5. 通讯录及文件操作\n\n");
437 printf(" 6. 关于通讯录信息管理系统\n\n");
438 printf(" 0. 退出通讯录信息管理系统\n\n");
439 printf(" ********************************************************************\n");
440 printf(" 请选择(0~6)\n ");
441 scanf("%d", &choice);
442 switch (choice)
443 {
444 case 1:
445 length += enter(adr);
446 save(adr, length);
447 system("pause");
448 break;
449 case 2:
450 showpaixu(adr, length);
451 break;
452 case 3:
453 chazhao(adr, length);
454 break;
455 case 4:
456 biangeng(adr, length);
457 break;
458 case 5:
459 length = wenjian(adr, length);
460 break;
461 case 6:
462 about();
463 break;
464 case 0:
465 return 0;
466 default:
467 printf("输入选项有误,请重新输入(0-6):");
468 system("pause");
469 break;
470 }
471 goto menu;
472
473 return 0;
474 };
475
posted on 2010-07-29 19:58 sohu2000000 阅读(863) 评论(0) 编辑 收藏 举报