#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define fname "txl.db"
#define FALSE 0
#define fname "txl.db"
typedef struct LINKMAN //建立结构体存放联系人信息
{
char name[12];
char sex[10];
unsigned char age;
char tel[12];
char addr[40];
}LINKMAN;
{
char name[12];
char sex[10];
unsigned char age;
char tel[12];
char addr[40];
}LINKMAN;
typedef struct node
{
LINKMAN data;
struct node *next;
}Node;
typedef Node *llist;
{
LINKMAN data;
struct node *next;
}Node;
typedef Node *llist;
char *menu_1[] =
{
"**********主菜单*********\n",
"********1.增加好友*******\n",
"********2.删除好友*******\n",
"********3.修改***********\n",
"********4.查询***********\n",
"********0.退出程序*******\n",
"*************************\n"
};
{
"**********主菜单*********\n",
"********1.增加好友*******\n",
"********2.删除好友*******\n",
"********3.修改***********\n",
"********4.查询***********\n",
"********0.退出程序*******\n",
"*************************\n"
};
char *menu_find[] =
{
"**********查询菜单********\n",
"1.按姓名查询\n",
"2.查看所有好友\n",
"0.返回主菜单\n",
"**************************\n"
};
{
"**********查询菜单********\n",
"1.按姓名查询\n",
"2.查看所有好友\n",
"0.返回主菜单\n",
"**************************\n"
};
char *menu_delete[] =
{
"********删除菜单*******\n",
"1.按姓名删除\n",
"2.全部删除\n",
"0.返回主菜单\n",
"***********************\n"
};
{
"********删除菜单*******\n",
"1.按姓名删除\n",
"2.全部删除\n",
"0.返回主菜单\n",
"***********************\n"
};
char *menu_modify[] =
{
"********修改菜单*******\n",
"1.按姓名查找修改\n",
"0.返回主菜单\n",
"***********************\n"
};
{
"********修改菜单*******\n",
"1.按姓名查找修改\n",
"0.返回主菜单\n",
"***********************\n"
};
char menu(char *str[],int len)
{
char sel;
int i;
for (i=0 ; i<len; i++)
{
printf("%s",str[i]); //将菜单输出到屏幕
}
printf("请输入您的选择:");
scanf(" %c",&sel);
getchar();
return sel;
}
{
char sel;
int i;
for (i=0 ; i<len; i++)
{
printf("%s",str[i]); //将菜单输出到屏幕
}
printf("请输入您的选择:");
scanf(" %c",&sel);
getchar();
return sel;
}
int InitList(llist *L) //初始化,建立一个空的线性表
{
*L = (llist)malloc(sizeof(Node));
if (NULL == *L)
{
return FALSE;
}
else
{
(*L)->next = NULL;
return TRUE;
}
}
{
*L = (llist)malloc(sizeof(Node));
if (NULL == *L)
{
return FALSE;
}
else
{
(*L)->next = NULL;
return TRUE;
}
}
int ListLength(llist L) //返回线性表的元素个数
{
int i=0;
llist p;//p指向头结点
p = L->next;
while (p != NULL)
{
i++;
p = p->next;
}
return i;
}
{
int i=0;
llist p;//p指向头结点
p = L->next;
while (p != NULL)
{
i++;
p = p->next;
}
return i;
}
int ListInsert(llist *L, int i, LINKMAN e) //在第i个位置插入新元素e
{
int j=1;
llist p,s;
p = (*L);
if (i < 1 || i > ListLength(*L)+1) //第i个元素是否存在
{
return FALSE;
}
while (p != NULL && j < i) //寻找第i个结点
{
p = p->next;
j++;
}
s = (llist)malloc(sizeof(Node)); //生成新结点
if (s == NULL)
{
printf("内存分配失败,按任意键推出\n");
getchar();
exit(0);
}
s->data = e;
s->next = p->next; //将p的后续结点赋值给s的后继
p->next = s; //将s赋值给p的后继
return TRUE;
}
int LocateList(llist L, char *str, int flag)
{
int i;
llist p;
p = L->next;
if (NULL == L->next)
{
return -1;
}
switch (flag)
{
case 0:
{
i = 1;
while(NULL != p)
{
if (0 == strcmp(str,p->data.name)) //比较输入的名字和要查询的名字相同
{
return i;
}
else
{
p = p->next;
i++; //计数是第几个好友
}
}
return -1; //没有找到要找的好友
}
break;
default:return -1;
}
}
{
int i;
llist p;
p = L->next;
if (NULL == L->next)
{
return -1;
}
switch (flag)
{
case 0:
{
i = 1;
while(NULL != p)
{
if (0 == strcmp(str,p->data.name)) //比较输入的名字和要查询的名字相同
{
return i;
}
else
{
p = p->next;
i++; //计数是第几个好友
}
}
return -1; //没有找到要找的好友
}
break;
default:return -1;
}
}
int GetElem(llist L, int i, LINKMAN *e)
{
int j=1; //j为计数器
llist p; //声明一结点p
p = L->next; //让p指向链表L的第一个结点
if (i < 1 || i > ListLength(L)) //第i个元素不存在
{
return FALSE;
}
while (p != NULL && j < i) //p不为空或者计数器j还没有等于i时,循环继续
{
p = p->next; //让p指向下个结点
j++;
}
*e = p->data; //取第i个元素的数据
return TRUE;
}
{
int j=1; //j为计数器
llist p; //声明一结点p
p = L->next; //让p指向链表L的第一个结点
if (i < 1 || i > ListLength(L)) //第i个元素不存在
{
return FALSE;
}
while (p != NULL && j < i) //p不为空或者计数器j还没有等于i时,循环继续
{
p = p->next; //让p指向下个结点
j++;
}
*e = p->data; //取第i个元素的数据
return TRUE;
}
int ListDelete(llist *L,int i,LINKMAN *e)
{
int j = 1;
llist p,q;
if ( i<1 || i>ListLength(*L))
{
return FALSE;
}
p = (*L);
while (NULL != p && j<i)
{
p = p->next;
j++;
}
q = p->next;
p->next = q->next;
*e = q->data;
free(q);
return TRUE;
}
{
int j = 1;
llist p,q;
if ( i<1 || i>ListLength(*L))
{
return FALSE;
}
p = (*L);
while (NULL != p && j<i)
{
p = p->next;
j++;
}
q = p->next;
p->next = q->next;
*e = q->data;
free(q);
return TRUE;
}
int ClearList(llist *L)
{
int i = 0;
llist p,q;
if (NULL == (*L)->next)
{
return TRUE;
}
p = q = (*L)->next;
do
{
p = p->next;
free(q);
i++;
q = p;
}while(NULL != p);
(*L)->next = NULL;
printf("删除了%d个好友\n",i);
return TRUE;
}
{
int i = 0;
llist p,q;
if (NULL == (*L)->next)
{
return TRUE;
}
p = q = (*L)->next;
do
{
p = p->next;
free(q);
i++;
q = p;
}while(NULL != p);
(*L)->next = NULL;
printf("删除了%d个好友\n",i);
return TRUE;
}
char *fgets_wrapper(char *buffer, size_t buflen, FILE * fp)
{
if (fgets(buffer, buflen, fp) != 0)
{
size_t len = strlen(buffer);
if (len > 0 && buffer[len-1] == '\n')
{
buffer[len-1] = '\0';
}
//清空剩余的数据
else
{
int ch;
while ((ch = getc(fp)) != EOF && ch != '\n');
}
return buffer;
}
return 0;
}
{
if (fgets(buffer, buflen, fp) != 0)
{
size_t len = strlen(buffer);
if (len > 0 && buffer[len-1] == '\n')
{
buffer[len-1] = '\0';
}
//清空剩余的数据
else
{
int ch;
while ((ch = getc(fp)) != EOF && ch != '\n');
}
return buffer;
}
return 0;
}
int input(LINKMAN *e)
{
int age; //把age强制转化为int型
printf("姓名:");
fgets_wrapper(e->name, 12, stdin);
printf("地址:");
fgets_wrapper(e->addr, 40, stdin);
printf("手机号:");
fgets_wrapper(e->tel, 12, stdin);
printf("性别:");
fgets_wrapper(e->sex, 10, stdin);
printf("年龄:");
scanf("%d",&age);
getchar();
e->age = (unsigned char)age;
return TRUE;
}
{
int age; //把age强制转化为int型
printf("姓名:");
fgets_wrapper(e->name, 12, stdin);
printf("地址:");
fgets_wrapper(e->addr, 40, stdin);
printf("手机号:");
fgets_wrapper(e->tel, 12, stdin);
printf("性别:");
fgets_wrapper(e->sex, 10, stdin);
printf("年龄:");
scanf("%d",&age);
getchar();
e->age = (unsigned char)age;
return TRUE;
}
int output(LINKMAN e)
{
printf("姓名:%s\n",e.name);
printf("地址:%s\n",e.addr);
printf("手机号:%s\n",e.tel);
printf("性别:%s\n",e.sex);
printf("年龄:%d\n",(int)e.age);
return TRUE;
}
{
printf("姓名:%s\n",e.name);
printf("地址:%s\n",e.addr);
printf("手机号:%s\n",e.tel);
printf("性别:%s\n",e.sex);
printf("年龄:%d\n",(int)e.age);
return TRUE;
}
int find(llist L)
{
char sel=1,name[12];
int i,j;
LINKMAN e;
llist p;
while('0' != sel)
{
sel = menu(menu_find,5);
switch (sel)
{
case '1':
{
printf("请输入姓名:");
fgets_wrapper(name,12,stdin);
j = LocateList(L,name,0);
if (-1 != j)
{
printf("查询到记录,显示如下:\n");
GetElem(L,j,&e);
output(e);
printf("按任意键继续...\n");
getchar();
}
else
{
printf("查无此人,按任意键继续...\n");
getchar();
}
} break;
case '2':
{
if ( NULL == L->next)
{
printf("当前还没有任何记录\n");
printf("按任意键继续...\n");
getchar();
}
else
{
p = L->next;
i=1;
//for(i=0; i<L.length; i++)
while( NULL != p)
{
printf("第%d条记录\n",i);
output(p->data);
printf("\n");
p = p->next;
i++;
}
printf("按任意键继续...\n");
getchar();
}
} break;
case '0':break;
default:printf("错误输入\n");break;
}
}
return TRUE;
}
{
char sel=1,name[12];
int i,j;
LINKMAN e;
llist p;
while('0' != sel)
{
sel = menu(menu_find,5);
switch (sel)
{
case '1':
{
printf("请输入姓名:");
fgets_wrapper(name,12,stdin);
j = LocateList(L,name,0);
if (-1 != j)
{
printf("查询到记录,显示如下:\n");
GetElem(L,j,&e);
output(e);
printf("按任意键继续...\n");
getchar();
}
else
{
printf("查无此人,按任意键继续...\n");
getchar();
}
} break;
case '2':
{
if ( NULL == L->next)
{
printf("当前还没有任何记录\n");
printf("按任意键继续...\n");
getchar();
}
else
{
p = L->next;
i=1;
//for(i=0; i<L.length; i++)
while( NULL != p)
{
printf("第%d条记录\n",i);
output(p->data);
printf("\n");
p = p->next;
i++;
}
printf("按任意键继续...\n");
getchar();
}
} break;
case '0':break;
default:printf("错误输入\n");break;
}
}
return TRUE;
}
int delete(llist *L)
{
char sel=1,yorno,name[12];
int j;
LINKMAN e;
while('0' != sel)
{
sel = menu(menu_delete,5); //在屏幕上输出删除菜单
switch (sel)
{
case '1':
{
printf("请输入姓名:");
fgets_wrapper(name,12,stdin);
j = LocateList(*L,name,0);
if (-1 != j)
{
GetElem(*L,j,&e);
output(e);
ListDelete(L,j,&e);
printf("已删除\n");
printf("按任意键继续...\n");
getchar();
}
else
{
printf("查无此人,按任意键继续...\n");
getchar();
}
} break;
case '2':
{
printf("你确定全部删除吗?(y/n)");
scanf(" %c",&yorno);
if ('y' == yorno || 'Y' == yorno)
{
ClearList(L);
printf("全部好友已经删除\n");
}
}break;
case '0':break;
default:printf("错误输入\n");break;
}
}
return TRUE;
}
{
char sel=1,yorno,name[12];
int j;
LINKMAN e;
while('0' != sel)
{
sel = menu(menu_delete,5); //在屏幕上输出删除菜单
switch (sel)
{
case '1':
{
printf("请输入姓名:");
fgets_wrapper(name,12,stdin);
j = LocateList(*L,name,0);
if (-1 != j)
{
GetElem(*L,j,&e);
output(e);
ListDelete(L,j,&e);
printf("已删除\n");
printf("按任意键继续...\n");
getchar();
}
else
{
printf("查无此人,按任意键继续...\n");
getchar();
}
} break;
case '2':
{
printf("你确定全部删除吗?(y/n)");
scanf(" %c",&yorno);
if ('y' == yorno || 'Y' == yorno)
{
ClearList(L);
printf("全部好友已经删除\n");
}
}break;
case '0':break;
default:printf("错误输入\n");break;
}
}
return TRUE;
}
int modify(llist *L)
{
char sel=1,name[12];
int j;
LINKMAN e;
while('0' != sel)
{
sel = menu(menu_modify,4);
switch (sel)
{
case '1':
{
printf("请输入姓名:");
fgets_wrapper(name,12,stdin);
j = LocateList(*L,name,0);
if (-1 != j)
{
GetElem(*L,j,&e);
output(e);
ListDelete(L,j,&e);
input(&e);
ListInsert(L,j,e);
printf("已修改,按任意键继续...\n");
getchar();
}
else
{
printf("查无此人,按任意键继续...\n");
getchar();
}
} break;
case '0':break;
default:printf("错误输入\n");break;
}
}
return TRUE;
}
{
char sel=1,name[12];
int j;
LINKMAN e;
while('0' != sel)
{
sel = menu(menu_modify,4);
switch (sel)
{
case '1':
{
printf("请输入姓名:");
fgets_wrapper(name,12,stdin);
j = LocateList(*L,name,0);
if (-1 != j)
{
GetElem(*L,j,&e);
output(e);
ListDelete(L,j,&e);
input(&e);
ListInsert(L,j,e);
printf("已修改,按任意键继续...\n");
getchar();
}
else
{
printf("查无此人,按任意键继续...\n");
getchar();
}
} break;
case '0':break;
default:printf("错误输入\n");break;
}
}
return TRUE;
}
int Rfile(llist *L){
FILE *fp;
LINKMAN e;
int i,size;
if((fp=fopen(fname,"r"))==NULL)
{
perror("打开文件失败");
return FALSE;
}
fseek(fp,0,SEEK_END);
size=ftell(fp)/sizeof(LINKMAN);
rewind(fp);
for(i=0;i<size;i++){
fread(&e,sizeof(LINKMAN),1,fp);
ListInsert(L,1,e);
}
fclose(fp);
return TRUE;
}
FILE *fp;
LINKMAN e;
int i,size;
if((fp=fopen(fname,"r"))==NULL)
{
perror("打开文件失败");
return FALSE;
}
fseek(fp,0,SEEK_END);
size=ftell(fp)/sizeof(LINKMAN);
rewind(fp);
for(i=0;i<size;i++){
fread(&e,sizeof(LINKMAN),1,fp);
ListInsert(L,1,e);
}
fclose(fp);
return TRUE;
}
int Wfile(llist *L){
FILE *fp;
LINKMAN e;
llist p=(*L)->next;
if((fp=fopen(fname,"w"))==NULL)
{
perror("打开文件失败");
return FALSE;
}
while(p!=NULL)
{
e=p->data;
fwrite(&e,sizeof(LINKMAN),1,fp);
p=p->next;
}
fclose(fp);
return TRUE;
}
FILE *fp;
LINKMAN e;
llist p=(*L)->next;
if((fp=fopen(fname,"w"))==NULL)
{
perror("打开文件失败");
return FALSE;
}
while(p!=NULL)
{
e=p->data;
fwrite(&e,sizeof(LINKMAN),1,fp);
p=p->next;
}
fclose(fp);
return TRUE;
}
int main(int argc,char *argv[])
{
llist txl;
LINKMAN e;
char sel;
InitList(&txl);
if(FALSE == Rfile(&txl))
{
printf("文件打开失败\n");
exit(1);
}
while (1)
{
sel = menu(menu_1,7);
switch (sel)
{
case '1':memset(&e,0,sizeof(LINKMAN));input(&e);ListInsert(&txl,1,e);break; //头插
case '2':delete(&txl);break;
case '3':modify(&txl);break;
case '4':find(txl);break;
case '0':printf("谢谢使用本软件,按任意键退出...\n");getchar();exit(0);break;
default:printf("错误输入\n");break;
}
}
return 0;
}
{
sel = menu(menu_1,7);
switch (sel)
{
case '1':memset(&e,0,sizeof(LINKMAN));input(&e);ListInsert(&txl,1,e);break; //头插
case '2':delete(&txl);break;
case '3':modify(&txl);break;
case '4':find(txl);break;
case '0':printf("谢谢使用本软件,按任意键退出...\n");getchar();exit(0);break;
default:printf("错误输入\n");break;
}
}
return 0;
}