C语言实现电话本 动态开辟 信息存储于文件

下面是我用C写的一个电话本小项目,实现的功能有:添加 删除 修改 查找 排序 清空 显示,功能还是比较全的,内存也是动态开辟的。能存储于本地,能从本地读出并显示

 

头文件部分代码,contact.h:

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#pragma warning(disable:4996) 

#define MAX_NAME 10
#define MAX_TEL 12
#define MAX_ADDR 20
#define MAX_SEX 5
#define MAX 1000
#define CAPACITY  3
#define FILENAME "contact.txt"


typedef struct P
{
	char name[MAX_NAME];
	char sex[MAX_SEX];
	int  age;
	char tel[MAX_TEL];
	char addr[MAX_ADDR];

}P;

typedef struct Con
{
	P* contact;  //联系人信息
	int len;//联系人数量
	int capacity;  //容量

}Con, *pCon;


void init_mycon();
void add_mycon();
void del_mycon();
void search_mycon();
void mod_mycon();
void dis_mycon();
void clear_mycon();
void sort_mycon();
void exit_mycon();

 

程序部分,contact.c 部分代码:

  1 #include"contact.h"
  2 
  3 void load(pCon _con)
  4 {
  5     int i = 0;
  6     FILE* fp = fopen(FILENAME, "r");
  7     if (fp == NULL)
  8     {
  9         perror("open the FILENAME for read\n");
 10         exit(EXIT_FAILURE);
 11     }
 12 
 13       fscanf(fp, "%d", &(_con->len));
 14     while (_con->len >= _con->capacity)
 15     {
 16         P* cp = (P*)realloc(_con->contact, (_con->capacity + CAPACITY)*sizeof(P));
 17         if ((cp != _con->contact) && (cp != NULL))
 18         {
 19             _con->contact = cp;
 20         }
 21         _con->capacity += CAPACITY;
 22     }
 23 
 24     for (i = 0; i < _con->len; i++)
 25     {
 26         fscanf(fp, "%s%s%d%s%s", _con->contact[i].name, _con->contact[i].sex, &(_con->contact[i].age),
 27             _con->contact[i].tel, _con->contact[i].addr);
 28     }
 29     fclose(fp);
 30 }
 31 
 32 void init_mycon(pCon _con)
 33 {
 34     _con->len = 0;
 35     _con->contact = (P*)malloc(CAPACITY * sizeof(P));
 36     if (_con->contact == NULL)
 37     {
 38         perror("创建空间");
 39         exit(EXIT_FAILURE);
 40     }
 41     memset(_con->contact, 0, CAPACITY);
 42     _con->capacity = CAPACITY;
 43     load(_con);
 44 }
 45 
 46 int findpeo(pCon _con, char* name)
 47 {
 48     int i = 0;
 49     for (i = 0; i < _con->len; i++)
 50     {
 51         if (strcmp(name, _con->contact[i].name) == 0)
 52         {
 53             return i;
 54         }
 55     }
 56     return -1;
 57 }
 58 
 59 void add_mycon(pCon _con)
 60 {
 61     load(_con);
 62     while(_con->len >= _con->capacity)
 63     {
 64         P* cp = realloc(_con->contact,(_con->capacity + CAPACITY)*sizeof(P));
 65         if ((cp != _con->contact) && (cp != NULL))
 66         {
 67             _con->contact = cp;
 68         }
 69         _con->capacity += CAPACITY;
 70     }
 71     
 72 
 73     printf("请输入姓名:");
 74     scanf("%s",_con->contact[_con->len].name);
 75     printf("请输入性别:");
 76     scanf("%s",_con->contact[_con->len].sex);
 77     printf("请输入年龄:");
 78     scanf("%d",&(_con->contact[_con->len].age));
 79     printf("请输入电话号码:");
 80     scanf("%s",_con->contact[_con->len].tel);
 81     printf("请输入地址:");
 82     scanf("%s",_con->contact[_con->len].addr);
 83     
 84     _con->len++;
 85     printf("添加成功\n");
 86 }
 87 
 88 void dis_mycon(pCon _con)
 89 {
 90     int i = 0;
 91     if (_con->len == 0)
 92     {
 93         printf("无联系人\n");
 94         return;
 95     }
 96     printf("%10s\t%3s\t%3s\t%12s\t%10s\n", "name", "sex", "age", "tel", "addr");
 97     for (i = 0; i < _con->len; i++)
 98     {
 99         printf("%10s\t%3s\t%3d\t%12s\t%10s\n", _con->contact[i].name,
100             _con->contact[i].sex, _con->contact[i].age,
101             _con->contact[i].tel, _con->contact[i].addr);
102     }
103 }
104 
105 void del_mycon(pCon _con)
106 {
107     char name[MAX_NAME] = { 0 };
108     int set = 0,i = 0;
109     if (_con->len == 0)
110     {
111         printf("电话薄为空\n");
112         return;
113     }
114     printf("输入想删掉人的姓名:");
115     scanf("%s", name);
116     set=findpeo(_con,name);
117     if (set == -1)
118     {
119         printf("联系人不存在\n");
120         return;
121     }
122     for (i = set; i < _con->len-1; i++)
123     {
124         _con->contact[i] = _con->contact[i + 1];
125     }
126     _con->len--;
127     printf("删除成功\n");
128 }
129 
130 void search_mycon(pCon _con)
131 {
132     char name[MAX_NAME] = { 0 };
133     int i = 0;
134     int set = 0;
135     printf("请输入要查找人的姓名:");
136     scanf("%s", name);
137     set = findpeo(_con, name);
138     if (set == -1)
139     {
140         printf("联系人不存在\n");
141         return;
142     }
143     printf("%10s\t%3s\t%3s\t%12s\t%10s\n", "name", "sex", "age", "tel", "addr");
144     printf("%10s\t%3s\t%3d\t%12s\t%10s\n", _con->contact[i].name,
145             _con->contact[i].sex, _con->contact[i].age,
146             _con->contact[i].tel, _con->contact[i].addr);
147 }
148 
149 void mod_mycon(pCon _con)
150 {
151     char name[MAX_NAME] = { 0 };
152     char nname[MAX_NAME] = { 0 };
153     char nsex[MAX_SEX] = {0};
154     char ntel[MAX_TEL] = {0};
155     char naddr[MAX_ADDR] = {0};
156 
157     int input = 1, choice = 0;
158     int set = 0;
159     if (_con->len == 0)
160     {
161         printf("没有可修改的信息\n");
162         return;
163     }
164     printf("请输入要修改人的姓名:");
165     scanf("%s", name);
166     set = findpeo(_con, name);
167     if (set == -1)
168     {
169         printf("联系人不存在\n");
170         return;
171     }
172     else
173     {
174         printf("1.修改姓名   2.修改性别   3.修改年龄\n");
175         printf("4.修改电话   5.修改地址   0.保存并退出\n");
176         while (input)
177         {
178             printf("请选择:");
179             scanf("%d", &choice);
180             switch(choice)
181             {
182             case 1:
183                 {
184                     printf("请输入新的姓名:");
185                     scanf("%s", nname);
186                     strcpy(_con->contact[set].name, nname);
187                     break;
188                 }
189             case 2:
190                 {
191                     printf("请输入新的性别:");
192                     scanf("%s", nsex);
193                     strcpy(_con->contact[set].sex , nsex);
194                     break;
195                 }
196             case 3:
197                {
198                       printf("请输入新的年龄:");
199                       scanf("%d", &_con->contact[set].age);
200                       break;
201                }
202             case 4:
203               {
204                       printf("请输入新的电话:");
205                       scanf("%s", ntel);
206                       strcpy(_con->contact[set].tel, ntel);
207                       break;
208               }
209             case 5:
210                {
211                       printf("请输入新的地址:");
212                       scanf("%s", naddr);
213                       strcpy(_con->contact[set].addr, naddr);
214                       break;
215                }
216             case 0:
217                {
218                       input = 0;
219                       break;
220                }
221             default:
222                 break;
223             }
224         }
225     }
226 }
227 int _compare(const void* cp1, const void* cp2)
228 {
229     P* p1= (P*)(cp1);
230     P* p2 = (P*)(cp2);
231     if (strcmp(p1->name, p2->name) > 0)
232         return 1;
233     else
234         return -1;
235 }
236 
237 
238 void sort_mycon(pCon _con)
239 {
240     P* cp = _con->contact;
241     int i = 0;
242 
243     if (_con->len == 0)
244     {
245         printf("电话本为空\n");
246         return;
247     }
248     qsort(_con->contact, _con->len, sizeof(P),_compare);
249     printf("排序成功:\n");
250 }
251 
252 void clear_mycon(pCon _con)
253 {
254     _con->len = 0;
255     printf("清除成功\n");
256 }
257 
258 void exit_mycon(pCon _con)
259 {
260     int i = 0;
261     FILE* fp = fopen(FILENAME, "w");
262     _con->capacity = _con->len;
263     if (NULL == fp)
264     {
265         perror("open the FILENAME for write");
266         exit(EXIT_FAILURE);
267     }
268     fprintf(fp, "%d ", _con->len);
269     for (i = 0; i < _con->len; i++)
270     {
271         fprintf(fp, "%s %s %d %s %s ", _con->contact[i].name, _con->contact[i].sex, _con->contact[i].age,
272             _con->contact[i].tel, _con->contact[i].addr);
273     }
274     
275     fclose(fp);
276 }

运用了fprintf和fscanf两个函数,向本地文件中以文本形式写入和从本地文件中读出。

测试部分代码,test.c:

 1 #include"contact.h"
 2 
 3 enum OP
 4 {
 5     EXIT,
 6     ADD,
 7     DEL,
 8     SERCH,
 9     MOD,
10     SORT,
11     DIS,
12     CLEAR
13 };
14 
15 void menu()
16 {
17     printf("*******************************************\n");
18     printf("**********     电话本        *************\n");
19     printf("********  1.add      2.del      ***********\n");
20     printf("********  3.serch    4.mod      ***********\n");
21     printf("********  5.sort     6.dis     ***********\n");
22     printf("********  7.clear    0.exit     ***********\n");
23     printf("*******************************************\n");
24 }
25 
26 int main()
27 {
28     int input = 1;
29     Con mycon;
30     init_mycon(&mycon);
31     while (input)
32     {
33         menu();
34         printf("选择操作:");
35         
36         scanf("%d",&input);
37         switch (input)
38         {
39         case ADD:
40             add_mycon(&mycon);
41             break;
42         case DEL:
43             del_mycon(&mycon);
44             break;
45         case SERCH:
46             search_mycon(&mycon);
47             break;
48         case MOD:
49             mod_mycon(&mycon);
50             break;
51         case SORT:
52             sort_mycon(&mycon);
53             break;
54         case DIS:
55             dis_mycon(&mycon);
56             break;
57         case CLEAR:
58             clear_mycon(&mycon);
59             break;
60         case EXIT:
61             exit_mycon(&mycon);
62             break;
63         default:
64             break;
65         }
66     }
67 
68     system("pause");
69     return 0;
70 }

 

posted @ 2016-05-17 18:11  Mr_listening  阅读(409)  评论(0编辑  收藏  举报
橙.文森特博客