通讯录可以用来存储联系人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人
# define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct person
{
char name[10];//姓名
char sex[3];//性别
int age;//年龄
char phone[12];//手机号码
char address[20];//地址
struct person* pnext;//存放下一个人信息的地址
};
int menu()//打印菜单
{
int choose = 0;
printf ("+--------------------------------------------+\n");
printf ("+---------- 欢迎来到通讯录系统 ----------+\n");
printf ("+------ 1、添加联系人 2、删除联系人------+\n");
printf ("+------ 3、修改信息 4、查找联系人------+\n");
printf ("+------ 5、显示联系人信息 6、清空联系人------+\n");
printf ("+------ 7、按名字排序联系人 0、退出------+\n");
printf ("+--------------------------------------------+\n");
printf ("请选择>\n");
scanf ("%d", &choose);
return choose;
}
void insert(struct person **phead)//插入信息
{
struct person *p = (struct person *) malloc(sizeof(struct person));
struct person *cur = NULL;
printf ("name:");
scanf ("%s", p->name);
printf ("sex:");
scanf ("%s", p->sex);
printf ("phone:");
scanf ("%s", p->phone);
printf ("age:");
scanf ("%d", &(p->age));
printf ("address:");
scanf ("%s", p->address);
p->pnext = NULL;
if(*phead == NULL)
{
*phead = p;
}
else
{
cur = *phead;
while(cur->pnext != NULL)
{
cur = cur->pnext;
}
cur->pnext= p;
}
}
void show(struct person* phead)//打印通讯录人员信息
{
printf ("姓名\t性别\t年龄\t手机号\t\t地址\n");
while (phead != NULL)
{
printf ("%s\t%s\t%d\t%s\t%s\n", phead->name, phead->sex, phead->age, phead->phone, phead->address);
phead = phead ->pnext;
}
}
void delete_person(struct person **phead)
{
char arr[32] = {0};
struct person *pre = NULL;//指向要删除的节点的前一个节点
struct person *cur = *phead;//指向要删除的节点
printf ("请输入你要删除的人:");
scanf ("%s", arr);
while (*phead != NULL)
{
if (pre == NULL && strcmp (arr, cur->name) == 0)//要删除的是第一个人
{
*phead = cur->pnext;
free(cur);
return;
}
else if(strcmp (arr, cur->name) == 0)//要删除的不是第一个人
{
pre->pnext = cur ->pnext;
free(cur);
return;
}
pre = cur;
cur = cur->pnext;
}
show (*phead);
}
void find (struct person *head)//查找联系人信息
{
char arr[32] = {0};
printf ("请输入你要查找的人名字:");
scanf ("%s", arr);
while (head != NULL)
{
if (strcmp (arr, head->name) == 0)
{
printf ("%s\t%s\t%d\t%s\t%s\n", head->name, head->sex, head->age, head->phone, head->address);
}
head = head->pnext;
}
}
int modify_menu()
{
int choose = 0;
printf ("-----------------------------------\n");
printf ("* 1、姓名 2、性别 *\n");
printf ("* 3、年龄 4、手机号 *\n");
printf ("* 5、地址 0、取消修改 *\n");
printf ("-----------------------------------\n");
scanf ("%d", &choose);
return choose;
}
void modify(struct person **phead)//修改联系人信息
{
char arr[32] = {0};
int ret = 0;
struct person *cur = *phead;
printf ("请输入你要修改的联系人的姓名:");
scanf ("%s", arr);
while (*phead != NULL)
{
if (strcmp (arr, cur->name) == 0)
{
printf ("请选择你要修改的信息:\n");
ret = modify_menu();
switch (ret)
{
case 1:
printf("name> ");
scanf ("%s", cur->name);
break;
case 2:
printf ("sex> ");
scanf ("%s", cur->sex);
break;
case 3:
printf ("age> ");
scanf ("%d", &cur->age);
break;
case 4:
printf ("phone> ");
scanf ("%s", cur->phone);
break;
case 5:
printf ("address> ");
scanf ("%s", cur->address);
break;
case 0:
break;
default :
printf ("选择错误!!!\n");
break;
}
show(*phead);
return ;
}
cur = cur->pnext;
}
printf ("没有找到该联系人!!!\n");
}
void empty_people(struct person* phead)
{
struct person* cur = phead;
while (phead != NULL)
{
cur = phead;
phead = phead ->pnext;
free (cur);
cur = NULL;
}
if (phead == NULL)
{
printf ("清空成功!!!\n");
}
}
void sort(struct person** phead)
{
char tmp[32] = "0";//临时变量
int age;//临时变量
struct person* cur = *phead;
struct person* str = *phead;
while (cur->pnext != NULL)
{
while (str!= NULL)
{
if (strcmp (cur->name , str->name)>0)
{
strcpy (tmp, cur->name);
strcpy(cur->name, str->name);
strcpy(str->name, tmp);
strcpy (tmp, cur->sex);
strcpy(cur->sex, str->sex);
strcpy(str->sex, tmp);
strcpy (tmp, cur->phone);
strcpy(cur->phone, str->phone);
strcpy(str->phone, tmp);
strcpy (tmp, cur->address);
strcpy(cur->address, str->address);
strcpy(str->address, tmp);
age = cur->age;
cur->age = str->age;
str->age = age;
}
str=str->pnext;
}
cur = cur ->pnext;
str = cur;
}
show (*phead);
}
int main()
{
struct person* phead = NULL;
int ret = 1;
do
{
ret = menu();
switch (ret)
{
case 0:
break;
case 1: insert(&phead);
break;
case 2:
delete_person(&phead);
break;
case 3:
modify(&phead);
break;
case 4: find(phead);
break;
case 5:
printf ("联系人信息:\n");
show (phead);
break;
case 6:
empty_people (phead);
break;
case 7:
sort(&phead);
break;
default :printf ("输入有误,请重新输入!!!\n");
break;
}
}while(ret);
system ("pause");
return 0;
}