#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Person //单链表节点
{
char name[128];
char phonenum[128];
struct Person *next;
};
int sum = 0; //联系人总数
void star(); //程序开场白
void addPerson(struct Person **head); //1.头插法插入联系人
void add1Person(struct Person **head); //1.尾插法插入联系人,头、尾插法在main函数中更换即可
void getPerson(struct Person *person); //1.1录入联系人
int displayContacts(struct Person *head,int count); //5.显示当前通讯录
void release(struct Person *head); //6.退出通讯录程序
void findPerson(struct Person *head); //2.查找已有联系人
void changePerson(struct Person *head); //3.更改已有联系人
void delPerson(struct Person **head); //4.删除已有联系人
int main(void)
{
struct Person *head = NULL; // 单3链表 头指针
int num;
star();
do
{
do
{
num = 0;
printf("\n*请输入功能选项:");
scanf("%d",&num);
fflush(stdin);
if(num >= 1 && num <= 6)
{
break;
}
printf("无效指令,请重新输入!\n");
}while(1);
switch(num)
{
case 1: add1Person(&head);break;
case 2: findPerson(head);break;
case 3: changePerson(head);break;
case 4: delPerson(&head);break;
case 5: displayContacts(head,0);break;
case 6: release(head);break;
}
}while(num != 6);
return 0;
}
void delPerson(struct Person **head) //4.删除已有联系人
{
struct Person *now;
struct Person *before = NULL;
int num,count = 1,num1;
now = *head;
num1 = displayContacts(*head,4);
if(num1 != 1)
{
do
{
printf("请输入要删除的联系人序号(1~%d):",sum);
scanf("%d",&num);
fflush(stdin);
if(num <= sum && num >= 1)
{
break;
}
printf("序号输出错误,请重新输入!\n");
}while(1);
while(count < num && now != NULL)
{
before = now;
now = now->next;
count++;
}
if(before == NULL)
{
*head = now->next;
}
else
{
before->next = now->next;
}
free(now);
sum--;
printf("\n*联系人信息删除完成!\n");
}
}
void changePerson(struct Person *head) //3.更改已有联系人
{
struct Person *person;
int num1 = 0,num,count = 1;
char ch; //判读是否更改姓名、电话
person = head;
num1 = displayContacts(head,3);
if(num1 != 1)
{
do
{
printf("请输入要更改的联系人序号(1~%d):",sum);
scanf("%d",&num);
fflush(stdin);
if(num <= sum && num >= 1)
{
break;
}
printf("序号输出错误,请重新输入!\n");
}while(1);
while(person != NULL && count < num)
{
person = person->next;
count++;
}
printf("\n*更改第%d位联系人信息:\n",num);
printf("是否更改姓名“%s ”(Y/N)?",person->name);
scanf("%c",&ch);
fflush(stdin);
if(ch == 'Y')
{
printf("更改为:");
scanf("%s",person->name);
fflush(stdin);
}
printf("是否更改电话“%s ”(Y/N)?",person->phonenum);
scanf("%c",&ch);
fflush(stdin);
if(ch == 'Y')
{
printf("更改为:");
scanf("%s",person->phonenum);
fflush(stdin);
}
printf("\n*联系人信息更改完成!\n");
}
else
{
return;
}
}
void findPerson(struct Person *head) //2.查找已有联系人
{
char find[128];
int num = 0;
struct Person *person;
printf("请输入查找信息(姓名/电话):");
scanf("%s",find);
getchar();
person = head;
while(person != NULL)
{
if(!strcmp(person->name,find) || !strcmp(person->phonenum,find))
{
if(num == 0)
{
printf("\n-------已查找到以下联系信息-------\n");
}
printf("\n第%d位联系人:\n",num + 1);
printf("姓名:%s\n",person->name);
printf("电话:%s\n",person->phonenum);
num++;
}
person = person->next;
}
if(num == 0)
{
printf("\n--------未查到相关联系信息--------\n");
}
printf("\n----------------------------------\n");
}
void release(struct Person *head) //6.退出通讯录程序
{
printf("\n*此通讯录小程序已退出!\n");
free(head);
exit(1);
}
int displayContacts(struct Person *head,int count) //5.显示当前通讯录
{
int num = 1;
struct Person *person;
person = head;
printf("------------通讯录如下------------\n");
while(person != NULL)
{
printf("\n第%d位联系人信息如下:\n",num);
printf("姓名:%s\n",person->name);
printf("电话:%s\n",person->phonenum);
num++;
person = person->next;
}
if(num == 1)
{
if(count == 0)
{
printf("\n当前通讯录暂无联系人,请添加后再显示!\n");
}
else if(count == 3)
{
printf("\n当前通讯录暂无联系人,请添加后再进行更改!\n");
}
else if(count == 4)
{
printf("\n当前通讯录暂无联系人,请添加后再进行删除!\n");
}
printf("\n----------------------------------\n");
return 1;
}
printf("\n----------------------------------\n");
num = 1;
}
void getPerson(struct Person *person) //1.1录入联系人
{
printf("\n请输入联系人姓名:");
scanf("%s",person->name);
getchar();
printf("请输入联系人电话:");
scanf("%s",person->phonenum);
getchar();
}
void addPerson(struct Person **head) //1.头插法插入联系人
{
struct Person *person;
struct Person *temp;
person = (struct Person *)malloc(sizeof(struct Person));
if(person == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
getPerson(person); //录入联系人
if(*head == NULL)
{
*head = person;
person->next = NULL;
}
else
{
temp = *head;
*head = person;
person->next = temp;
}
printf("第%d位联系人已录入完成!\n",++sum);
}
void add1Person(struct Person **head) //1.尾插法插入联系人
{
struct Person *person;
static struct Person *tail;
person = (struct Person *)malloc(sizeof(struct Person));
if(person == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
getPerson(person); //录入联系人
if(*head == NULL)
{
*head = person;
person->next = NULL;
}
else
{
tail->next = person;
person->next = NULL;
}
tail = person;
printf("第%d位联系人已录入完成!\n",++sum);
}
void star()
{
printf("********************************\n");
printf("** 夏日de通讯录小程序 **\n");
printf("** 功能选项: **\n");
printf("** 1.添加新的联系人 **\n");
printf("** 2.查找已有联系人 **\n");
printf("** 3.更改已有联系人 **\n");
printf("** 4.删除已有联系人 **\n");
printf("** 5.显示当前通讯录 **\n");
printf("** 6.退出通讯录程序 **\n");
printf("** **\n");
printf("********************************\n");
}
|