#include
#include
#include
typedef struct Telephone
{
char name[50];
char sex[30];
char tel[30];
char city[30];
char eip[30];
struct Telephone *next;
}TEL;
TEL *head;
void Initiate();
void Menu();
void Create();//的功能是:创建新的通讯录。
void Add();// 在通讯录的末尾,写入新的信息,并返回选单
void Find();//查找记录
void Alter();//修改记录如果未找到要修改的人,则提示通讯录中没有此人的信息,并返回选单。
void Delete();//删除某人的信息,如果未找到要删的人,提示通讯录中没有此人的信息,并返回选单。
void List();//的功能是:显示通讯录中的所有记录。
int main()
{
int choice;
char yes_no;
Initiate();
do
{
system("cls");
Menu();
printf("请选择0-6的数字
");
scanf("%d",&choice);
printf("
");
switch(choice)
{
case 1:Create();
break;
case 2:Add();
break;
case 3:Find();
break;
case 4:Alter();
break;
case 5:Delete();
break;
case 6:List();
break;
case 0:
printf("************感谢您的使用************
");
exit(0);
break;
default:
printf("输入有误!请重新输入
");
break;
}
printf("是否继续 Y or N?
");
do
{
scanf("%c",&yes_no);
}while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}while(yes_no=='Y'||yes_no=='y');
}
//初始化
void Initiate()
{
if((head=(TEL *)malloc(sizeof(TEL)))==NULL) exit(1);
head->next=NULL;
}
//显示菜单
void Menu()
{
printf("**************欢迎使用通讯录系统**************");
printf("
");
printf("
");
printf(" 1。
创建通讯录。
");
printf(" 2。插入信息。
");
printf(" 3。查询记录
");
printf(" 4。修改记录
");
printf(" 5。
删除记录!
");
printf(" 6。显示所有记录
");
printf(" 0。
退出通讯录
");
printf(" 请输入0~6
");
}
//创建通讯录
void Create()
{
TEL *p1[100],*p2;
int m,i;
printf("请输入创建个数:");
scanf("%d",&m);
for(i=1;iname);
printf("性别:
");
scanf("%s",&p1[i]->sex);
printf("电话:
");
scanf("%s",&p1[i]->tel);
printf("城市:
");
scanf("%s",&p1[i]->city);
printf("邮编:
");
scanf("%s",&p1[i]->eip);
p1[i]->next=NULL;
if(head->next==NULL)
head->next=p1[i];
else
{
for(p2=head;p2->next!=NULL;p2=p2->next); //找到结点尾
p2->next=p1[i];
}
}
printf("信息已添加!
");
return; //保存到链表
}
//添加通讯录信息
void Add()
{
TEL *p,*q;
if((q=(TEL *)malloc(sizeof(TEL)))==NULL) exit(1);
printf("请输入要添加的信息!
");
printf("姓名:
"); //添加信息
scanf("%s",&q->name);
printf("性别:
");
scanf("%s",q->sex);
printf("电话:
");
scanf("%s",q->tel);
printf("城市:
");
scanf("%s",q->city);
printf("邮编:
");
scanf("%s",q->eip);
for(p=head;p->next!=NULL;p=p->next) ;
p->next=q;
q->next=NULL;
printf("此信息已添加!");
return;
}
//查找通讯录信息
void Find()
{
TEL *p;
char name[50];
if(head->next==NULL)
{
printf("此通讯录为空!
");
return;
}
printf("请输入要查找的姓名:
");
scanf("%s",&name);
for(p=head->next;p!=NULL;p=p->next)
{
if(strcmp(p->name,name)==0)
{
printf("姓名性别城市邮编电话
");
printf("%s%s%s%s%s
",p->name,p->sex,p->city,p->eip,p->tel);
}
else if(p->next==NULL)
printf("无此信息!
");
}
}
//修改通讯录信息
void Alter()
{
char name[50]; //先查找 后删除
TEL *p, *p1;
if (head->next == NULL)
{
printf("此通讯录为空!
");
return;
}
printf("请输入要修改的姓名:
");
scanf("%s",name);
for(p=head->next;p!=NULL;p=p->next)
{
if(strcmp(p->name,name)==0)
break;
else if(p->next==NULL)
{
printf("无此信息!
");
return;
}
}
p1=(TEL *)malloc(sizeof(TEL));
printf("姓名:
"); //添加信息
scanf("%s",p1->name);
strcpy(p->name,p1->name);
printf("性别:
");
scanf("%s",p1->sex);
strcpy(p->sex,p1->sex);
printf("电话:
");
scanf("%s",p1->tel);
strcpy(p->tel,p1->tel);
printf("城市:
");
scanf("%s",p1->city);
strcpy(p->city,p1->city);
printf("邮编:
");
scanf("%s",p1->eip);
strcpy(p->eip,p1->eip);
printf("此信息已修改!
");
//显示修改的信息
printf("姓名性别城市邮编电话
");
printf("%s%s%s%s%s
",p->name,p->sex,p->city,p->eip,p->tel);
free(p1);
}
//删除通讯录信息
void Delete()
{
char name[50]; //先查找 后删除
TEL *p = head->next, *p1 =head->next,*p2;
if (head->next == NULL)
{
printf("此通讯录为空!
");
return;
}
printf("请输入要删除的姓名:
");
scanf(" %s",name);
while ((strcmp(p->name,name)!=0 )&& p->next != NULL)
{
p1=p;
p =p->next;
}
if (strcmp(name, p->name)==0) //输出删除信息
{
if (p == head->next&&p->next!=NULL)
head->next = p->next;
else if(p==head->next&&p->next==NULL)
{
head->next=p->next;
printf("信息已删除,先此通讯录为空!!
");
return;
}
else
p1->next = p->next;
}
else
{
printf("此信息不存在!!!
");
return;
}
printf("此信息已删除!");
printf("姓名性别城市邮编电话
");
for(p2=head->next;p2!=NULL;p2=p2->next)
printf("%s%s%s%s%s
",p2->name,p2->sex,p2->city,p2->eip,p2->tel);
}
//显示所有记录
void List()
{
TEL *p;
if(head->next==NULL)
{
printf("此通讯录中无记录!
");
return;
}
printf("姓名性别城市邮编电话
");
for(p=head->next;p!=NULL;p=p->next)
printf("%s%s%s%s%s
",p->name,p->sex,p->city,p->tel,p->eip);
}。
全部