C语言课设——通讯录操作系统

  该微型系统实现了联系人的增删改查,以及排序清空等操作。

  一个联系人有3 个以下的号码,本来想用邻接表实现对号码的创建及其他操作,但我没法对链表的邻接表进行文件的读取操作,只有对号码数目固定。

  下面是源代码

 1 #pragma once
 2 #ifndef  _LIST_H
 3 #define  _LIST_H
 4 
 5 typedef struct
 6 {
 7     char num1[20];
 8     char num2[20];
 9     char num3[20];
10     int n;
11     char name[20];
12 }DATATYPE_T;
13 
14 typedef struct Node
15 {
16     DATATYPE_T  data;
17     struct Node *next;
18 }ListType;
19 
20 /* 添加节点到链表末尾 */
21 ListType *list_add_end(ListType *,DATATYPE_T);
22 
23 /* 添加节点到链表首部 */
24 ListType *list_add_first(ListType *,DATATYPE_T);
25 
26 /*添加节点到链表中间*/ 
27 ListType *list_insert(ListType *,char *,DATATYPE_T);
28 
29 /* 按关键字在链表中查找内容 */
30 ListType *list_find_name(ListType *,char *);
31 
32 /* 按关键字在链表中查找内容 */
33 ListType *list_find(ListType *,char *);
34 
35 /* 删除指定关键字的节点 */
36 ListType *list_delete(ListType *,char *);
37 
38 /*获取链表的节点数量 */
39 int list_length(ListType *);
40 
41 /* 显示链表所有信息*/
42 void list_all(ListType *);
43 
44 /*添加联系人*/
45 ListType *add_contact(ListType *);
46 
47 /*按照关键字查找联系人*/
48 int find_contact(ListType *);
49 
50 /*按照关键字删除联系人*/
51 ListType *delete_contact(ListType *);
52 
53 /*插入联系人信息*/
54 ListType *insert_contact(ListType *);
55 
56 /*显示所有联系人信息*/
57 int show_contact(ListType *);
58 
59 /*读取文件*/ 
60 ListType *readfile(ListType *);
61 
62 /*修改链表*/ 
63 void change_contact(ListType *);
64 
65 /*保存文件*/ 
66 void save_contact(ListType *);
67 
68 /*排序*/ 
69 void sort_contact(ListType *);
70 
71 /*清空*/ 
72 ListType *free_contact(ListType *);
73 
74 /*界面*/ 
75 int menu();
76 
77 #endif
list.h
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include "list.h"
  5 
  6 ListType *list_add_end(ListType *head,DATATYPE_T data)
  7 {
  8     ListType *node,*phead;
  9 
 10     node = (ListType *)malloc(sizeof(ListType));
 11     if(NULL == node)
 12     {
 13         printf("malloc failed\n");
 14         return NULL;
 15     }
 16     node->data.n = data.n;
 17     strcpy(node->data.name,data.name);
 18     strcpy(node->data.num1,data.num1);
 19     strcpy(node->data.num2,data.num2);
 20     strcpy(node->data.num3,data.num3);
 21     node->next = NULL;
 22     if(head ==NULL)
 23     {
 24         head = node;
 25         return head;
 26     }
 27     phead = head;
 28     
 29     while(phead->next != NULL)
 30     {
 31         phead = phead->next;
 32     }
 33     phead->next = node;
 34 
 35     return head;
 36 }
 37 
 38 ListType *list_add_first(ListType *head,DATATYPE_T data)
 39 {
 40     ListType *node;
 41 
 42     node = (ListType *)malloc(sizeof(ListType));
 43     if(NULL == node)
 44     {
 45         printf("malloc failed\n");
 46         return NULL;
 47     }
 48     node->data = data;
 49     node->next = head;
 50     head = node;
 51 
 52     return head;
 53 }
 54 
 55 ListType *list_find(ListType *head,char *key)
 56 {
 57     ListType *phead;
 58     phead = head;
 59     
 60     while(phead)
 61     {
 62         if(strcmp(phead->data.name,key)==0 || strcmp(phead->data.num1,key)==0 || strcmp(phead->data.num2,key)==0 || strcmp(phead->data.num3,key)==0)
 63         {
 64             return phead;
 65         }
 66         phead = phead->next;
 67     }
 68     return NULL;
 69 }
 70 
 71 ListType *list_insert(ListType *head,char *findkey,DATATYPE_T data)
 72 {
 73     ListType *node,*node1;
 74     
 75     node = (ListType *)malloc(sizeof(ListType));
 76     if(NULL == node)
 77     {
 78         printf("malloc failed\n");
 79         system("pause");
 80         return NULL;
 81     }
 82 
 83     node->data.n = data.n;
 84     strcpy(node->data.name,data.name);
 85     strcpy(node->data.num1,data.num1);
 86     strcpy(node->data.num2,data.num2);
 87     strcpy(node->data.num3,data.num3);
 88     node1 = list_find(head,findkey);    
 89     if(node1)
 90     {
 91         node->next = node1->next;
 92         node1->next = node; 
 93     }
 94     else
 95     {
 96         free(node);
 97         printf("can't find key\n");
 98         system("pause");
 99     }
100     return head;
101 }
102 
103 ListType *list_delete(ListType *head,char *key)
104 {
105     ListType *node,*phead;
106     node = head;
107     phead = head;
108     
109     while(phead)
110     {
111         if(strcmp(phead->data.name,key)==0 || strcmp(phead->data.num1,key)==0 || strcmp(phead->data.num2,key)==0 || strcmp(phead->data.num3,key)==0)
112         {
113             node = head->next;
114             free(head);
115             head = NULL;
116             head = node;
117             if(head!=NULL)
118             {
119                 return head;
120             }
121             else
122             {
123                 return NULL;
124             }
125         }
126         if(strcmp(phead->data.name,key)==0 || strcmp(phead->data.num1,key)==0 || strcmp(phead->data.num2,key)==0 || strcmp(phead->data.num3,key)==0)
127         {
128             node->next = phead->next;
129             free(phead);
130             phead = NULL;
131             return head;
132         }
133         else
134         {
135             node = phead;
136             phead = phead->next;
137         }
138     }
139     return NULL;
140 }
141 
142 ListType *free_contact(ListType *head)
143 {
144     ListType *p;
145     p = head;
146     while(head)
147     {
148         p = head->next;
149         free(head);
150         head = p;    
151     }
152     return head;
153 }
154 
155 int list_length(ListType *head)
156 {
157     int length = 0;
158     ListType *phead;
159 
160     phead = head;
161     while(phead)
162     {
163         phead = phead->next;
164         length++;
165     }
166 
167     return length;
168 }
list.cpp
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "list.h"
 4 
 5 int main()
 6 {
 7     int opt = 0;
 8     ListType *head=NULL;
 9     head = readfile(head);
10     do
11     {
12         menu();
13         scanf("%d",&opt);
14         printf("you select for %d\n",opt);
15         system("pause");
16         switch(opt)
17         {
18             case 1:
19                 head = add_contact(head);        // 添加 
20                 break;
21             case 2:
22                 find_contact(head);                // 查找 
23                 break;        
24             case 3:
25                 head = delete_contact(head);    // 删除 
26                 break;
27             case 4:
28                 head = insert_contact(head);    // 插入 
29                 break;
30             case 5:
31                 show_contact(head);                // 输出 
32                 break;
33             case 6:
34                 change_contact(head);
35                 break;                            // 修改 
36             case 7:
37                    save_contact(head);                // 保存 
38                    break;
39             case 8:
40                 sort_contact(head);                // 排序
41                 break;    
42             case 9:
43                 head = free_contact(head);        // 清空 
44                 break;                     
45             case 0:
46                 return 0;
47             default:
48                 printf("unknow select\n");
49                 break;
50         }
51     }while(opt!=0);
52     
53     return 0;
54 }
main.cpp
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include "list.h"
  5 
  6 /* 显示链表所有信息*/
  7 void list_all(ListType *head)
  8 {
  9     ListType *phead;
 10     DATATYPE_T data;
 11     phead = head;
 12 
 13     while(phead)
 14     {
 15         data = phead->data;
 16         printf("\t\t***************************\n");
 17         printf("\t\tname: %s\n\t\ttelephone number: %d\n",data.name,data.n);
 18         if(data.n >= 1)
 19           {
 20               printf("\t\ttelephone 1: %s\n", data.num1);
 21           }
 22           if(data.n >= 2)
 23           {
 24               printf("\t\ttelephone 2: %s\n", data.num2);
 25           }
 26           if(data.n >= 3)
 27           {
 28               printf("\t\ttelephone 3: %s\n", data.num3);
 29           }
 30             phead = phead->next;
 31             printf("\n");
 32     }
 33     system("pause");
 34 }
 35 
 36 /*添加联系人*/
 37 ListType *add_contact(ListType *head)
 38 {
 39     DATATYPE_T  contactInfo;
 40     int n,flag;
 41     printf("\t\t***************************\n");
 42     printf("\t\tplease input contact number:");
 43     scanf("%d", &n);
 44 
 45     while(n--)
 46     {
 47     printf("\t\t***************************\n");
 48     printf("\t\tplease input contact information\n"); 
 49       printf("\t\tname:");
 50       scanf("%s", contactInfo.name);
 51       printf("\t\tHow many telephone:");
 52       scanf("%d", &contactInfo.n);
 53       if(contactInfo.n >= 1)
 54       {
 55           printf("\t\ttelephone 1:");
 56           scanf("%s", contactInfo.num1);
 57       }
 58       if(contactInfo.n >= 2)
 59       {
 60           printf("\t\ttelephone 2:");
 61           scanf("%s", contactInfo.num2);
 62       }
 63       if(contactInfo.n >= 3)
 64       {
 65           printf("\t\ttelephone 3:");
 66           scanf("%s", contactInfo.num3);
 67       }
 68     head = list_add_end(head,contactInfo);
 69  //     sort_contact(head);
 70    }
 71       
 72     return head;
 73 }
 74 
 75 /*按照关键字查找联系人*/
 76 int find_contact(ListType *head)
 77 {
 78     char key[20];
 79     ListType *node = NULL;
 80     
 81     if(head == NULL)
 82     {
 83         printf("the list is NULL!!!\n");
 84         system("pause");
 85         return 0;
 86     }
 87     
 88        printf("\t\tplease input find key: ");
 89        scanf("%s",key);
 90     node = list_find(head,key);
 91     
 92     if(node!=NULL)
 93     {
 94         printf("\t\t***************************\n");
 95            printf("\t\tname:%s\n\t\ttelephone number:%d\n",node->data.name,node->data.n);
 96         if(node->data.n >= 1)
 97           {
 98               printf("\t\ttelephone 1:%s\n", node->data.num1);
 99           }
100           if(node->data.n >= 2)
101           {
102               printf("\t\ttelephone 2:%s\n", node->data.num2);
103           }
104           if(node->data.n >= 3)
105           {
106               printf("\t\ttelephone 3:%s\n", node->data.num3);
107           }
108           system("pause");
109     }
110     else
111     {
112         printf("\t\tthe key can't find!!!\n");
113     }
114     return 0;
115 }
116 
117 /*按照关键字删除联系人*/
118 ListType *delete_contact(ListType *head)
119 {
120     char key[15];
121     ListType *phead = NULL;
122     if(head == NULL)
123     {
124         printf("the list is NULL!!!\n");
125         system("pause");
126         return head;
127     } 
128     printf("\t\tplease input delete key:  ");
129     scanf("%s",key);
130     phead = list_delete(head,key);
131     if(phead == NULL)
132     {
133         printf("delete after the list is NULL!!!\n");
134         return NULL;
135     }
136     return phead;
137 }
138 
139 /*插入联系人信息*/
140 ListType *insert_contact(ListType *head)
141 {
142     char key[20];
143     DATATYPE_T insertData;
144     ListType *phead = NULL;
145     if(head == NULL)
146     {
147         printf("the list is NULL!!!\n");
148         system("pause");
149         return head;
150     } 
151     printf("\t\tplease input insert key\n");
152     scanf("%s",key);
153     printf("\t\tplease input insert contact information\n"); 
154     printf("\t\tname:");
155       scanf("%s", insertData.name);
156       printf("\t\tHow many telephone: ");
157       scanf("%d", &insertData.n);
158       if(insertData.n >= 1)
159       {
160           printf("\t\ttelephone 1: ");
161           scanf("%s", insertData.num1);
162       }
163       if(insertData.n >= 2)
164       {
165           printf("\t\ttelephone 2: ");
166           scanf("%s", insertData.num2);
167       }
168       if(insertData.n >= 3)
169       {
170           printf("\t\ttelephone 3: ");
171           scanf("%s", insertData.num3);
172       }
173     
174     phead = list_insert(head,key,insertData);
175 //    sort_contact(phead);
176     return phead;
177 }
178 
179 /*显示所有联系人信息*/
180 int show_contact(ListType *head)
181 {
182     
183     if(head==NULL)
184     {
185         printf("the list is NULL\n");
186            system("pause");
187         return -1;
188     }
189     
190     sort_contact(head);
191     return 0;
192 }
193 
194 /*读取文件*/ 
195 ListType * readfile(ListType *head)
196 {
197     FILE *fp;
198     ListType *p;
199     fp=fopen("通讯录.txt","rb");
200     if(fp == NULL)
201     {
202         printf("通讯录不存在");
203         if ((fp=fopen("通讯录.txt","wb"))==NULL) 
204         
205         { 
206             printf("\n\t\t建立失败"); 
207             exit(0); 
208         } 
209         else 
210         { 
211             printf("\n\t-----------------欢迎使用通讯录管理系统------------------");
212             printf("\n\t 通讯录文件已建立 "); 
213             printf("\n\t 按任意键进入主菜单 "); 
214             printf("\n\t---------------------------------------------------------");
215             getchar(); 
216         } 
217     }
218     
219     fseek(fp, 0, SEEK_END);
220     if (ftell(fp) != 0)
221     {
222 /* 
223         float nLen = ftell(fp);
224         nLen = nLen/sizeof(ListType);
225         printf("%f\n", nLen);
226         while(nLen--)
227 */        
228         rewind(fp);
229         while (!feof(fp))
230         {    
231                 ListType *p;
232                 p = (ListType *)malloc(sizeof(ListType));
233         //        fread(p,sizeof(ListType),1,fp);
234                 if(fread(p,sizeof(ListType),1,fp)!=1)//读到末尾或者出错,跳出循环
235                 {
236                     free(p);
237                     break;
238                 }
239                 p->next = head;
240                 head = p;
241                 
242 /*                printf("name:%s\ntelephone number:%d\n",p->data.name,p->data.n);
243                 if(head->data.n >= 1)
244                   {
245                       printf("telephone 1:%s\n", head->data.num1);
246                   }
247                   if(head->data.n >= 2)
248                   {
249                       printf("telephone 2:%s\n", head->data.num2);
250                   }
251                   if(head->data.n >= 3)
252                   {
253                       printf("telephone 3:%s\n", head->data.num3);
254                   }
255                   system("pause");
256 */
257         }
258         
259         
260         printf("\n\t-------------欢迎使用通讯录管理系统-----------------------");
261         printf("\n\t 文件导入成功 "); 
262         printf("\n\t 按任意键返回主菜单 "); 
263         printf("\n\t---------------------------------------------------------");
264         getchar(); 
265 
266     }
267     else
268     {
269         printf("\n\t--------------欢迎使用通讯录管理系统----------------------");
270         printf("\n\t 文件导入成功 "); 
271         printf("\n\t 通讯录文件中无任何纪录 "); 
272         printf("\n\t 按任意键返回主菜单 "); 
273         printf("\n\t----------------------------------------------------------");
274         getchar();  
275     }
276     
277     return head;
278 }
279 
280 void save_contact(ListType *head)
281 {
282     FILE *fp;
283     ListType *p;
284     if(head == NULL)
285     {
286         printf("the list is NULL!!!\n");
287         system("pause");
288     }
289     else
290     {
291         fp=fopen("通讯录.txt","wb");
292         p = head;
293         while(p)
294         {
295             fwrite(p,sizeof(ListType),1,fp);
296             p = p->next;
297         }
298 //    fwrite(head,sizeof(ListType),list_length(head),fp);
299         printf("save success\n");
300         system("pause"); 
301     } 
302     
303 }
304 
305 void change_contact(ListType *head)
306 {
307     char key[20];
308     ListType *node = NULL;
309     
310     if(head == NULL)
311     {
312         printf("the list is NULL!!!\n");
313         system("pause");
314     }
315     else
316     {
317            printf("\t\tplease input find key: ");
318            scanf("%s",key);
319         node = list_find(head,key);
320     
321         if(node!=NULL)
322           {
323                printf("\t\t***************************\n");
324                printf("\t\tname:%s\n\t\ttelephone number:%d\n",node->data.name,node->data.n);
325             if(node->data.n >= 1)
326               {
327                   printf("\t\ttelephone 1:%s\n", node->data.num1);
328               }
329               if(node->data.n >= 2)
330               {
331                   printf("\t\ttelephone 2:%s\n", node->data.num2);
332               }
333               if(node->data.n >= 3)
334               {
335                   printf("\t\ttelephone 3:%s\n", node->data.num3);
336               }
337               system("pause");
338         }
339           else
340         {
341             printf("\t\tthe key can't find!!!\n");
342           }
343         DATATYPE_T  contactInfo;
344 
345         printf("\t\tplease input contact information\n"); 
346           printf("\t\tname:");
347           scanf("%s", contactInfo.name);
348           printf("\t\tHow many telephone:");
349           scanf("%d", &contactInfo.n);
350           if(contactInfo.n >= 1)
351           {
352             printf("\t\ttelephone 1:");
353               scanf("%s", contactInfo.num1);
354           }
355           if(contactInfo.n >= 2)
356           {
357               printf("\t\ttelephone 2:");
358               scanf("%s", contactInfo.num2);
359           }
360           if(contactInfo.n >= 3)
361           {
362               printf("\t\ttelephone 3:");
363               scanf("%s", contactInfo.num3);
364           }
365           node->data.n = contactInfo.n;
366          strcpy(node->data.name,contactInfo.name);
367        strcpy(node->data.num1,contactInfo.num1);
368          strcpy(node->data.num2,contactInfo.num2);
369           strcpy(node->data.num3,contactInfo.num3);        
370     }
371 
372 }
373 
374 void sort_contact(ListType *head)
375 {
376     ListType *p, *t, *p1, *t1, *r;
377     DATATYPE_T q;
378     for(p = head; p!=NULL; p = p->next)
379     {
380         for(t = p->next; t!=NULL;t = t->next)
381         {
382             if(strcmp(p->data.name,t->data.name)>0)
383             {
384                 q.n = t->data.n;
385                 strcpy(q.name,t->data.name);
386                 strcpy(q.num1,t->data.num1);
387                 strcpy(q.num2,t->data.num2);
388                 strcpy(q.num3,t->data.num3);
389                 t->data.n = p->data.n;
390                 strcpy(t->data.name,p->data.name);
391                 strcpy(t->data.num1,p->data.num1);
392                 strcpy(t->data.num2,p->data.num2);
393                 strcpy(t->data.num3,p->data.num3);
394                 p->data.n = q.n;
395                 strcpy(p->data.name,q.name);
396                 strcpy(p->data.num1,q.num1);
397                 strcpy(p->data.num2,q.num2);
398                 strcpy(p->data.num3,q.num3);  
399                               
400             }
401         }
402     }
403     if(head == NULL)
404     {
405         printf("the list is NULL!!!\n");
406         system("pause");
407     }
408     else
409     {
410         list_all(head);
411     }
412     
413 }
414 
415 int menu()
416 {
417     printf("\n");
418     system("cls");
419     system("color 17");
420     printf("\t\t\t****Address Book****");
421     printf("\n\t\t\t** author:******* **\n");
422     printf("\t\t\t**  ID:14045227  **\n");
423     printf("\t\t\t********************\n");
424     printf("\t\t\t 1.add contact\n");
425     printf("\t\t\t 2.find a contact\n");
426     printf("\t\t\t 3.delete a contact\n");
427     printf("\t\t\t 4.insert a contact\n");
428     printf("\t\t\t 5.show a contact\n");
429     printf("\t\t\t 6.change a contact\n");
430     printf("\t\t\t 7.save contact\n");
431     printf("\t\t\t 8.sort contact\n");
432     printf("\t\t\t 9.free all contact\n");
433     printf("\t\t\t 0.quit ");
434     printf("\n");
435     printf("\t\t\t********************\n");
436     printf("please select: ");
437 }
Address.cpp

 

posted @ 2015-06-25 07:11  TensionRidden  阅读(293)  评论(0编辑  收藏  举报