#include
#include
#include
#define OK 1
#define ERROR 0
#define MAX 20
typedef int Status;
typedef struct Node
{
char name[MAX];
char number[MAX];
char email[MAX];
struct Node *next;
}Node;
typedef Node *Linklist;
Status Initlist(Linklist *Lp)
{
Node *p=(Node *)malloc(sizeof(Node));
if(p==NULL)
{
printf("初始化失败!\n");
return ERROR;
}
p->next=NULL;
*Lp=p;
}
Status addlinkman(Linklist *Lp)
{
Linklist L=*Lp;
char name1[MAX],number1[MAX],email1[MAX],adress1[MAX];
Node *p=(Node *)malloc(sizeof(Node));
if(p==NULL)
{
printf("malloc failed!\n"); //申请内存失败,抛出错误
return ERROR;
}
printf("请输入要存储人的姓名:\n");
gets(name1);
printf("请输入要存储人的手机号:\n");
gets(number1);
printf("请输入要存储人的电子邮箱:\n");
gets(email1);
strcpy(p->name,name1);
strcpy(p->number,number1);
strcpy(p->email,email1);
while(L->next != NULL)
{
L=L->next;
}
L->next=p;
//printf("%s %s %s \n",L->name,L->number,L->email);
return OK ;
}
void Findman(Linklist Lp)
{
Linklist q=Lp;
int sel;
int flag=1;
char name2[MAX];
char number2[MAX];
char email2[MAX];
printf("请输入要查询的方式:\n\t1姓名\n\t2手机号\n\t3电子邮箱\n\t4返回\n");
scanf("%d",&sel);
getchar ();
switch(sel)
{
case 1:
printf("请输入要查询的人的名字:\n");
gets(name2);
while (q->next != NULL)
{
if (strcmp(q->next->name,name2) == 0)
{
printf("%s %s %s\n",q->next->name,q->next->number,q->next->email);
flag=0;
}
q=q->next;
}
if (flag != 0)
{
printf("查询失败!\n");
}
break;
case 2:
printf("请输入要查询的人的手机号:\n");
gets(number2);
while (q->next != NULL)
{
if (strcmp(q->next->number,number2) == 0)
{
printf("%s %s %s\n",q->next->name,q->next->number,q->next->email);
flag = 0;
}
q=q->next;
if (flag == 0)
{
break;
}
}
if ( flag != 0)
{
printf("查询失败!\n");
}
break;
case 3:
printf("请输入要查询的人的电子邮箱:\n");
gets(email2);
while (q->next != NULL)
{
if (strcmp(q->next->email,email2) == 0)
{
printf("%s %s %s\n",q->next->name,q->next->number,q->next->email);
flag = 0;
}
q=q->next;
}
if (flag != 0)
{
printf("查询失败!\n");
}
case 4:
return;
default:
printf("无此查询方式!\n");
}
}
void DeleteMan(Linklist *Lp)
{
Linklist q=*Lp;
int sel;
int flag=1;
char name3[MAX];
char number3[MAX];
char email3[MAX];
printf("请输入要删除的方式:\n\t1姓名\n\t2电话\n\t3电子邮箱\n\t4返回\n");
scanf("%d",&sel);
getchar ();
switch(sel)
{
case 1:
printf("请输入要删除的人的名字:\n");
gets(name3);
while (q->next != NULL)
{
if (strcmp(q->next->name,name3) == 0)
{
flag=0;
Linklist p=q->next;
q->next=p->next;
free(p);
printf("删除成功!\n");
}
else
q=q->next;
}
if (flag !=0)
{
printf("查无此人,无法删除\n");
}
break;
case 2:
printf("请输入要查询的人的电话:\n");
gets(number3);
while (q->next != NULL)
{
if (strcmp(q->next->number,number3) == 0)
{
flag=0;
Linklist p=q->next;
q->next=p->next;
free(p);
printf("删除成功!\n");
}
else
q=q->next;
}
if (flag !=0)
{
printf("查无此人,无法删除\n");
}
break;
case 3:
printf("请输入要查询的人的电子邮箱:\n");
gets(email3);
while (q->next != NULL)
{
if (strcmp(q->next->email,email3) == 0)
{
flag=0;
Linklist p=q->next;
q->next=p->next;
free(p);
printf("删除成功!\n");
}
else
q=q->next;
}
if (flag !=0)
{
printf("查无此人,无法删除\n");
}
case 4:
return;
default:
printf("无此查询方式!\n");
break;
}
}
void update(Linklist *Lp)
{
Linklist q=*Lp;
int sel;
int flag=1;
char name3[MAX];
char number3[MAX];
char email3[MAX];
printf("请输入要修改联系人的姓名:\n");
gets(name3);
while (q->next != NULL)
{
if (strcmp(q->next->name,name3) == 0)
{
flag=0;
}
q=q->next;
if (flag ==0)
{
break;
}
}
if (flag == 0)
{
printf("请输入要修改的内容:\n\t1姓名\n\t2电话\n\t3电子邮箱\n\t4返回\n");
scanf("%d",&sel);
getchar();
switch(sel)
{
case 1:
printf("请输入新名字:\n");
gets(name3);
strcpy(q->name,name3);
break;
case 2:
printf("请输入新电话:\n");
gets(number3);
strcpy(q->number,number3);
break;
case 3:
printf("请输入新电子邮箱:\n");
gets(email3);
strcpy(q->email,email3);
}
}
printf("%s %s %s\n",q->name,q->number,q->email);
}
Status Length(Linklist Lp)
{
int len = 0;
Linklist q=Lp;
while(q->next != NULL)
{
q=q->next;
len++;
}
return len;
}
void BubbleName (Linklist Lp)
{
int i,j,m;
for(i = 1;i < Length(Lp);i++)
{
for(j = Length(Lp);j > i;j--)
{
Linklist temp1 = Lp;
for(m = 0;m < j - 2;m++)
{
temp1 = temp1->next;
}
Linklist temp2 = temp1->next;
if(strcmp(temp2->next->name,temp2->name) < 0)
{
temp1->next = temp2->next;
temp2->next = temp1->next->next;
temp1->next->next = temp2;
}
}
}
}
Status printlist(Linklist L) //打印通讯录
{
if (L==NULL)
{
printf("nothing in the list \n");
return ERROR;
}
Linklist p=L;
while(p->next !=NULL)
{
p=p->next;
printf("姓名:%s\n",p->name);
printf("电话:%s\n",p->number);
printf("电子邮箱:%s\n",p->email);
}
printf("\n");
return OK;
}
int tell_length(FILE *f)/*文件字符个数*/
{
int length;
fseek(f,0,2);
length = ftell(f);
rewind(f);
return length;
}
void savelinkman(Linklist Lp)/*保存链表中各结点信息到文件*/
{
FILE *file = fopen("./linktext","w+");
if(file == NULL)
{
perror("fopen");
exit(1);
}
char name[MAX];
char number[MAX];
char email[MAX];
int i;
for(i=0; i<20; i++)
{
name[i]=' ';
number[i] = ' ';
email[i] = ' ';
}
Linklist temp = Lp;
while(temp->next != NULL)
{
strcpy(name,temp->next->name);
strcpy(number,temp->next->number);
strcpy(email,temp->next->email);
if(fwrite(name,sizeof(char),20,file) == EOF)
{
perror("fwrite1");
fclose(file);
exit(2);
}
if(fwrite(number,sizeof(char),20,file) == EOF)
{
perror("fwrite2");
fclose(file);
exit(3);
}
if(fwrite(email,sizeof(char),20,file) == EOF)
{
perror("fwrite3");
fclose(file);
exit(4);
}
if(fwrite("\n\r",sizeof(char),2,file) == EOF)
{
perror("fwrite5");
fclose(file);
exit(5);
}
memset(name,0,20);
memset(number,0,20);
memset(email,0,20);
temp = temp->next;
}
fclose(file);
}
void loadlinkman(Linklist Lp)/*加载文件中的联系人到链表中*/
{
FILE *file = fopen("./linktext","a+");
if(file == NULL)
{
perror("fopen5");
exit(10);
}
char buffer1[20] = {0};
char buffer2[20] = {0};
char buffer3[20] = {0};
int len = tell_length(file);
int i;
for(i = 0;i < (len / 62);i++)
{
if(fread(buffer1,sizeof(char),20,file) == EOF)
{
perror("fread6");
exit(11);
}
if(fread(buffer2,sizeof(char),20,file) == EOF)
{
perror("fread7");
exit(12);
}
if(fread(buffer3,sizeof(char),20,file) == EOF)
{
perror("fread8");
exit(13);
}
fseek(file,2,1);
Linklist p = (Linklist)malloc(sizeof(Node));
if(p == NULL)
{
return;
}
strcpy(p->name,buffer1);
strcpy(p->number,buffer2);
strcpy(p->email,buffer3);
memset(buffer1,0,20);
memset(buffer2,0,20);
memset(buffer3,0,20);
Linklist temp = Lp;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = p;
p->next = NULL;
}
}
int main()
{
int sel;
Linklist L1;
Initlist(&L1);
loadlinkman(L1);
while(1)
{
printf("请输入您想进行的操作:\n\t\t\t1添加联系人\n\t\t\t2删除联系人\n\t\t\t3排序联系人\n\t\t\t4查询联系人\n\t\t\t5修改联系人\n\t\t\t6打印通讯录\n\t\t\t7保存退出\n");
scanf("%d",&sel);
getchar();
switch(sel)
{
case 1:
addlinkman(&L1);
break;
case 2:
DeleteMan(&L1);
break;
case 3:
BubbleName(L1);
break;
case 4:
Findman(L1);
break;
case 5:
update(&L1);
break;
case 6:
printlist(L1);
break;
case 7:
savelinkman(L1);
system("clear");
printf("成功退出!\n联系人已保存!\n");
exit(14);
// return;
default:
break;
}
}
return 0;
}