通过这一段时间的学习,这两天花了不少时间巩固理解,终于完成了在Linux下开发简易通讯录,由于时间仓促,该程序有着不少bug,对于用户的输入容错率也不高,只做了开始菜单的输入容错,功能也只实现了基本的增删改查,而且没有实现内容的脱机保存。
下面附上代码:
头文件 cwtxl.h
#pragma once
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;/* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef struct node
{
char name[50];
char tel[50];
char sex[50];
char addr[50];
struct node *next;
}Node;
typedef Node *LinkList;
静态库文件cw_txl:
#include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include "cwtxl.h"
16
17
18 Status InitList(LinkList *L)//初始化
19 {
20 (*L) = (LinkList)malloc(sizeof(Node));
21 if((*L)==NULL)
22 {
23 printf("Initial Error!\n");
24 return ERROR;
25 }
26 (*L)->next=NULL;
27 return OK;
28 }
29
30 Status ListEmpty(LinkList L)//查询是否为空
31 {
32 if(L->next==NULL)
33 return TRUE;
34 else
35 return FALSE;
36 }
37
38 int ListLength(LinkList L)//求长度
39 {
40 int i=0;
41 LinkList p = L->next;
42 while(p!=NULL)
43 {
44 p=p->next;
45 i++;
46 }
47 return i;
48 }
49
50 Status ListInsert(LinkList *L,char *name,char *tel,char *sex,char *addr)//插入
51 {
52 LinkList p,s;
53 if((*L)==NULL)
54 {
55 printf("Linklist is not inited\n");
56 return ERROR;
57 }
58 p = *L;
59 while(p != NULL && p->next != NULL)
60 {
61 p=p->next;
62 }
63 s = (LinkList)malloc(sizeof(Node));
64 if(s == NULL)
65 {
66 printf("Initial Error!");
67 return ERROR;
68 }
69 //s->name = (char *)malloc(strlen(name)+1);
70 //s->tel = (char *)malloc(strlen(tel)+1);
71 //s->sex = (char *)malloc(strlen(sex)+1);
72 //s->addr = (char *)malloc(strlen(addr)+1);
73 strcpy(s->name,name);
74 strcpy(s->tel,tel);
75 strcpy(s->sex,sex);
76 strcpy(s->addr,addr);
77 s->next = p->next;
78 p->next = s;
79 return OK;
80 }
81
82 Status ListDelete(LinkList *L,char *name)//删除
83 {
84 LinkList p,q;
85 if((*L) == NULL)
86 {
87 printf("LinkList is not inited!\n");
88 return ERROR;
89 }
90 if(TRUE == ListEmpty(L))
91 {
92 printf("LinkList is empty!\n");
93 return ERROR;
94 }
95 p=(*L)->next;
96 while(p != NULL)
97 {
98 if(strcmp(p->name,name) == 0)
99 {
100 if(p == (*L)->next)
101 {
102 (*L)->next=p->next;
103 free(p);
104 return OK;
105 }
106 else
107 {
108 q->next=p->next;
109 free(p);
110 return OK;
111 }
112 }
113 q=p;
114 p=p->next;
115 }
116 return ERROR;
117 }
118
119
120 Status ListTraverse(LinkList L)//输出链表
121 {
122 LinkList p;
123 if(NULL == L)
124 {
125 printf("LinkList is not inited!\n");
126 return ERROR;
127 }
128 if(ListEmpty(L))
129 {
130 printf("LinkList is empty!\n");
131 return ERROR;
132 }
133 p=L->next;
134 while(p != NULL)
135 {
136 printf("%s\t%s\t%s\t%s\n",p->name,p->tel,p->sex,p->addr);
137 p=p->next;
138 }
139 return OK;
140 }
141
142 Status ListQuery(LinkList L,char *data,int type)//查询
143 {
144 LinkList p;
145 if(NULL == L)
146 {
147 printf("LinkList is not inited!\n");
148 return ERROR;
149 }
150 if(ListEmpty(L))
151 {
152 printf("LinkList is empty!\n");
153 return ERROR;
154 }
155 p=L->next;
156 if(type==1)//姓名精确查询
157 {
158 while(p != NULL)
159 {
160 if(strcmp(p->name,data) == 0)
161 {
162 printf("%s\t%s\t%s\t%s\n",p->name,p->tel,p->sex,p->addr);
163 return OK;
164 }
165 p=p->next;
166 }
167 }
168 if(type==2)//姓名模糊查询
169 {
170 while(p != NULL)
171 {
172 if(strncmp(p->name,data,strlen(data)) == 0)
173 {
174 printf("%s\t%s\t%s\t%s\n",p->name,p->tel,p->sex,p->addr);
175 }
176 p=p->next;
177 }
178 return OK;
179 }
180 if(type==3)//号码查询
181 {
182 while(p != NULL)
183 {
184 if(strcmp(p->tel,data) == 0)
185 {
186 printf("%s\t%s\t%s\t%s\n",p->name,p->tel,p->sex,p->addr);
187 return OK;
188 }
189 p=p->next;
190 }
191 }
192 if(type==4)//号码模糊查询
193 {
194 while(p != NULL)
195 {
196 if(strncmp(p->tel,data,strlen(data)) == 0)
197 {
200 p=p->next;
201 }
202 return OK;
203 }
204 if(type==5)//性别查询
205 {
206 while(p != NULL)
207 {
208 if(strcmp(p->sex,data) == 0)
209 {
210 printf("%s\t%s\t%s\t%s\n",p->name,p->tel,p->sex,p->addr);
211 }
212 p=p->next;
213 }
214 return OK;
215 }
216 return ERROR;
217 }
218
220 {
221 LinkList p;
222 if((*L)==NULL)
223 {
224 printf("Linklist is not inited\n");
225 return ERROR;
226 }
227 if(ListEmpty(L))
228 {
229 printf("LinkList is empty!\n");
230 return ERROR;
231 }
232 p = (*L)->next;
233 if(type == 1)//根据姓名修改信息
234 {
235 while(p!=NULL)
236 {
237 if(strcmp(p->name,name)==0)
238 {
239 strcpy(p->tel,tel);
240 strcpy(p->sex,sex);
241 strcpy(p->addr,addr);
242 return OK;
243 }
244 p=p->next;
245 }
246 }
247 if(type == 2)//根据电话修改信息
248 {
249 while(p!=NULL)
250 {
251 if(strcmp(p->tel,tel)==0)
252 {
253 strcpy(p->name,name);
254 strcpy(p->sex,sex);
255 strcpy(p->addr,addr);
256 return OK;
257 }
258 p=p->next;
259 }
260 }
261 return ERROR;
262 }
263
通讯录主程序:
/*****************************************************************
2 * Copyright (C) 2018 SU_QIAN Ltd. All rights reserved.
3 *
4 * 文件名称:cwtxlmain.c
5 * 创 建 者:Cwwww
6 * 创建日期:2018年07月23日
7 * 描 述:Good Job!
8 *
9 *****************************************************************/
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include "cwtxl.h"
15
16 char Menu()
17 {
18 char x,t;
19 int n=0;
20 printf("\n\t\t*****************************************\n");
21 printf("\t\t==\t 1.添加信息 ==\n\t\t+\t\t\t\t\t+\n");
22 printf("\t\t==\t 2.显示信息 ==\n\t\t+\t\t\t\t\t+\n");
23 printf("\t\t==\t 3.查询信息 ==\n\t\t+\t\t\t\t\t+\n");
24 printf("\t\t==\t 4.修改信息 ==\n\t\t+\t\t\t\t\t+\n");
25 printf("\t\t==\t 5.删除信息 ==\n\t\t+\t\t\t\t\t+\n");
26 printf("\t\t==\t 6.退出系统 ==\n\t\t+\t\t\t\t\t+\n");
27 printf("\t\t*****************************************\n");
28 printf("\n请根据需要选择(1-6):\t");
29 scanf(" %c",&x);
30 while(scanf("%c",&t)!=EOF)
31 {
32 if(t == '\n')
33 break;
34 else
35 n++;
36 }
37 if (n == 0 && x >'0' && x < '7')
38 {
39 return x;
40 }
41 else
42 {
43 printf("input error\n");
44 Menu();
45 }
46 }
47
48 void insert(LinkList *L)
49 {
50 LinkList p=*L;
51 char name[50],tel[50],sex[50],addr[50];
52 int n=1;
53 while(n==1)
54 {
55 printf("please input:\n姓名:");
56 scanf("%s",name);
57 printf("电话:");
58 scanf("%s",tel);
59 printf("性别:");
60 scanf("%s",sex);
61 printf("地址:");
62 scanf("%s",addr);
63 ListInsert(&p,name,tel,sex,addr);
64 printf("\n继续添加--1\t退出--0: ");
65 scanf("%d",&n);
66 }
67 }
68
69 void query(LinkList L)
70 {
71 LinkList p=L;
72 char data[50];
73 int n=1;
74 while(n!=6)
75 {
76 printf("\n\t\t*****************************************\n");
77 printf("\t\t==\t 1.姓名精确查询 ==\n\t\t+\t\t\t\t\t+\n");
78 printf("\t\t==\t 2.姓名模糊查询 ==\n\t\t+\t\t\t\t\t+\n");
79 printf("\t\t==\t 3.号码精确查询 ==\n\t\t+\t\t\t\t\t+\n");
80 printf("\t\t==\t 4.号码模糊查询 ==\n\t\t+\t\t\t\t\t+\n");
81 printf("\t\t==\t 5.性别查询 ==\n\t\t+\t\t\t\t\t+\n");
82 printf("\t\t==\t 6.退出查询 ==\n\t\t+\t\t\t\t\t+\n");
83 printf("\t\t*****************************************\n");
84 printf("please input your choice:");
85 scanf("%d",&n);
86 if(n==6)
87 break;
88 else
89 {
90 printf("please input :");
91 scanf("%s",data);
92 printf("姓名\t电话 \t性别\t地址\n");
93 ListQuery(p,data,n);
94 }
95 printf("\n继续查询--1\t退出--0: ");
96 scanf("%d",&n);
97 if(n==0)
98 n=6;
99 }
100
101 }
102
103 void modify(LinkList *L)
104 {
105 LinkList p=*L;
106 char name[50],tel[50],sex[50],addr[50];
107 int n=1;
108 while(n!=3)
109 {
110 n=0;
111 printf("\n\t\t*****************************************\n");
112 printf("\t\t==\t 1.根据姓名修改 ==\n\t\t+\t\t\t\t\t+\n");
113 printf("\t\t==\t 2.根据号码修改 ==\n\t\t+\t\t\t\t\t+\n");
114 printf("\t\t==\t 3.退出查询 ==\n\t\t+\t\t\t\t\t+\n");
115 printf("\t\t*****************************************\n");
116 printf("please input your choice:");
117 scanf("%d",&n);
118 if(n==3)
119 break;
120 else if(n==1)
121 {
122 printf("input old name:");
123 scanf("%s",name);
124 printf("input tel:");
125 scanf("%s",tel);
126 printf("input sex:");
127 scanf("%s",sex);
128 printf("input addr:");
129 scanf("%s",addr);
130 ListModify(&p,n,name,tel,sex,addr);
131 }
132 else if(n==2)
133 {
134 printf("input old tel:");
135 scanf("%s",tel);
136 printf("input name");
137 scanf("%s",name);
138 printf("input sex:");
139 scanf("%s",sex);
140 printf("input addr:");
141 scanf("%s",addr);
142 ListModify(&p,n,name,tel,sex,addr);
143 }
144 printf("\n继续修改--1\t退出--0: ");
145 scanf("%d",&n);
146 if(n==0)
147 n=3;
148
149 }
150 }
151
152 void delete_(LinkList *L)
153 {
154 LinkList p=*L;
155 char name[50];
156 int n=1;
157 while(1)
158 {
159 printf("please input old name:");
160 scanf("%s",name);
161 if(ERROR == ListDelete(&p,name))
162 printf("No query found!\n");
163 else
164 {
165 printf("Deleting successful!\n");
166 printf("\n继续删除--1\t退出--0: ");
167 scanf("%d",&n);
168 if(n==0)
169 break;
170 }
171 }
172 }
173
174
175 int main(int argc,char *argv[])
176 {
177 LinkList L;
178 if(ERROR == InitList(&L))
179 exit(1);
180 ListInsert(&L,"张三","15755591234","女","合肥市");
181 ListInsert(&L,"赵六","15755591222","男","合肥市");
182 ListInsert(&L,"李四","13855551111","男","六安市");
183 ListInsert(&L,"王五","18956667777","女","马鞍山市");
184 ListInsert(&L,"王梓","18956667777","女","马鞍山市");
185 char x;
186 x=Menu();
187 while(x!='6')
188 {
189 switch(x)
190 {
191 case '1':insert(&L);break;
192 case '2':ListTraverse(L);break;
193 case '3':query(L);break;
194 case '4':modify(&L);break;
195 case '5':delete_(&L);break;
196 case '6':printf("谢谢使用!\n");break;
197 default:break;
198 }
199 if(x!='6')
200 x=Menu();
201 }
202 return 0;
203 }
运行界面:
由于无法脱机保存,所以在main函数中提前插入了几条信息
添加信息:
查询信息:
其余部分暂不演示
ps:
静态库文件、动态库文件的生成及使用
举例:自编库函数文件名为mylib.c,主程序为main.c,主程序中调用库函数
1)静态库文件制作与使用
/****************
gcc -c mylib.c
ar rcsv libmylib.a mylib.o
gcc -o main main.c -L. –lmylib
********************/
2)动态库文件制作与使用
/******************
gcc -fPIC -Wall -c mylib.c
gcc -shared -o libmylib.so mylib.o
gcc -o main main.c -L. –lmylib.c
******************/
在运行可执行程序之前,需要注册动态库的路径名。
其方法有几种:修改/etc/ld.so.conf文件,或者修改LD_LIBRARY_PATH环境变量,或者将库文件直接拷贝到/lib或者/usr/lib目录下(这两个目录为系统的默认的库路径名)。