(4)删除好友
(5)退出
#ifndef _ADDRESSBOOK_H_
#define _ADDRESSBOOK_H_
struct Addressbook
{
char name[10];
char id[20];
char address[20];
char tele[20];
char mobile[20];
struct Addressbook *next;
};
typedef struct Addressbook addressbook;
typedef addressbook *LinkList;
void ListInit(LinkList *l);
int InsertInfo(LinkList *l);
int ShowInfo(LinkList l);
int SearchInfo(LinkList l);
int DeleteInfo(LinkList l);
int ChangeInfo(LinkList *l);
#endif
#include
#include
#include"addressbook.h"
void show()
{
system("clear");
printf("************************************\n");
printf("************************************\n");
printf("********Welcome AddressBook!********\n");
printf("************************************\n");
printf("************************************\n");
sleep(1);
system("clear");
}
void PrintInfo()
{
printf("************************************\n");
printf("***1.添加用户信息 2.列出好友信息***\n");
printf("***3.查找好友信息 4.删除好友*******\n");
printf("***5.修改好友信息 6.退出***********\n");
printf("************************************\n\n");
printf("***********请输入你的选择***********\n");
}
int main()
{
LinkList addbook;
int choice;
show();
ListInit(&addbook);
while(1)
{
PrintInfo();
scanf("%d",&choice);
switch(choice)
{
case 1:
InsertInfo(&addbook);
break;
case 2:
ShowInfo(addbook);
break;
case 3:
SearchInfo(addbook);
break;
case 4:
DeleteInfo(addbook);
break;
case 5:
ChangeInfo(&addbook);
break;
case 6:
exit(1);
break;
default:
printf("无效操作!\n");
}
}
return 0;
}
#include
#include
#include
#include"addressbook.h"
void ListInit(LinkList *l)
{
(*l) = (LinkList)malloc(sizeof(addressbook));
(*l)->next = NULL;
}
int InsertInfo(LinkList *l)
{
LinkList p = *l;
LinkList s;
printf("请输入好友信息:\n");
s = (LinkList)malloc(sizeof(addressbook));
scanf("%s%s%s%s%s",s->name,s->id,s->mobile,s->tele,s->address);
if(strlen(s->mobile) != 11)
{
printf("无效手机号码!\n");
return 0;
}
if(strlen(s->tele) != 8)
{
printf("无效家庭电话号码!\n");
return 0;
}
while(p->next)
{
if(strcmp((p->next)->mobile,s->mobile) == 0)
{
printf("已存在相同手机号码!\n");
return 0;
}
p = p->next;
}
p=*l;
if(p->next ==NULL)
{
s->next = p->next;
p->next = s;
}
else
{
while(p->next)
{
if(strcmp((p->next)->name,s->name)>0)
{
s->next = p->next;
p->next = s;
break;
}
p = p->next;
}
if(p->next ==NULL)
{
s->next = p->next;
p->next = s;
}
}
}
int ShowInfo(LinkList l)
{
if(l->next == NULL)
{
printf("通讯录为空!\n");
}
else
{
LinkList p = l->next;
while(p)
{
printf("Name: %s Id: %s Mobile: %s Tele: %s Address: %s\n",p->name,p->id,p->mobile,p->tele,p->address);
p=p->next;
}
}
}
int SearchInfo(LinkList l)
{
LinkList p = l->next;
char n[10];
printf("请输入您想要查找的好友名字:\n");
scanf("%s",n);
while(p)
{
if(strcmp(p->name,n) == 0)
{
printf("ID: %s Tele: %s Mobile: %s Address: %s\n",p->id,p->tele,p->mobile,p->address);
break;
}
else
{
p =p->next;
}
}
if(p == NULL)
{
printf("查无此人!\n");
}
}
int DeleteInfo(LinkList l)
{
LinkList p = l;
LinkList tmp;
char n[10];
printf("请输入你想删除的好友名字:\n");
scanf("%s",n);
while(p->next)
{
if(strcmp((p->next)->name,n) == 0)
{
tmp = p->next;
p->next = tmp->next;
free(tmp);
printf("删除好友信息成功!\n");
return 0;
}
else
{
p = p->next;
}
}
if(p->next == NULL)
{
printf("查无此人!\n");
}
}
int ChangeInfo(LinkList *l)
{
LinkList p = (*l)->next;
LinkList t = *l;
LinkList s ;
char n[10];
printf("请输入您想修改的好友姓名:\n");
scanf("%s",n);
while(p)
{
s = (LinkList)malloc(sizeof(addressbook));
if(strcmp(p->name,n) == 0)
{
printf("请输入您要修改的信息:姓名 编号 电话 家庭电话 住址\n");
scanf("%s%s%s%s%s",s->name,s->id,s->mobile,s->tele,s->address);
if(strlen(s->mobile) != 11)
{
printf("无效手机号码!\n");
return 0;
}
if(strlen(s->tele) != 8)
{
printf("无效家庭电话号码!\n");
return 0;
}
while(t->next)
{
if(strcmp((t->next)->mobile,s->mobile) == 0)
{
printf("已存在相同手机号码!\n");
return 0;
}
t = t->next;
}
strcpy(p->name , s->name);
strcpy(p->id , s->id);
strcpy(p->mobile , s->mobile);
strcpy(p->tele , s->tele);
strcpy(p->address , s->address);
free(s);
printf("修改成功!\n");
break;
}
else
{
p = p->next;
}
}
if(p == NULL)
{
printf("查无此人!\n");
}
}