/* 实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人 */
#include<stdio.h>
#include"phone_book.h"
#include<string.h>
#include<assert.h>
typedef struct phone_book
{
char name[10];
char sex[5];
char addr[20];
int year;
int phone_num;
struct phone_book *pNext;
}Book,*pBook;
pBook add_linkman(pBook head)
{
int i = 0;
char name[10];
char sex[5];
char addr[20];
int year;
long phone_num;
pBook pBase = head;
assert(head);
flag:
printf("你要添加多少个联系人:");
scanf("%d",&i);
if (i <= 0)
{
printf("输入数量错误!\n");
goto flag;
}
if (head->pNext == NULL)
{
while (i--)
{
pBook pNew = (pBook)malloc(sizeof(Book));
assert(pNew);
printf("请输入联系人信息:(姓名、性别、地址、年龄、电话号码)\n");
scanf("%s", name);
scanf("%s", sex);
scanf("%s", addr);
strcpy(pNew->name, name);
strcpy(pNew->sex, sex);
strcpy(pNew->addr, addr);
scanf("%d", &pNew->year);
scanf("%d", &pNew->phone_num);
pBase->pNext = pNew;
pNew->pNext = NULL;
pBase = pNew;
}
}
else
{
while (pBase->pNext != NULL)
{
pBase = pBase->pNext;
}
while (i--)
{
pBook pNew = (pBook)malloc(sizeof(Book));
assert(pNew);
printf("请输入联系人信息:(姓名、性别、地址、年龄、电话号码)\n");
scanf("%s", name);
scanf("%s", sex);
scanf("%s", addr);
strcpy(pNew->name, name);
strcpy(pNew->sex, sex);
strcpy(pNew->addr, addr);
scanf("%d", &pNew->year);
scanf("%d", &pNew->phone_num);
pBase->pNext = pNew;
pNew->pNext = NULL;
}
}
return head;
}
pBook remove_linkman(pBook head)
{
pBook link_man = head;
char name[10];
if (!head)
{
printf("没有联系人,不能删除!\n");
return NULL;
}
printf("请问你要删除哪位联系人? ");
scanf("%s",name);
while (link_man->pNext!=NULL)
{
if (strcmp(link_man->pNext->name, name) == 0)
{
if (link_man->pNext->pNext == NULL)
{
free(link_man->pNext);
link_man->pNext = NULL;
printf("删除完成!\n");
}
else
{
pBook ptmp = NULL;
ptmp = link_man->pNext;
link_man->pNext = link_man->pNext->pNext;
free(ptmp);
printf("删除完成!\n");
}
return head;
}
else
{
link_man = link_man->pNext;
}
}
printf("没有找到该联系人!\n");
return head;
}
void traver_linkman(pBook head)
{
pBook link_man = head;
if (head->pNext == NULL)
{
printf("电话簿为空!\n");
}
while (link_man->pNext!=NULL)
{
printf("%s\n", link_man->pNext->name);
printf("%s\n", link_man->pNext->sex);
printf("%s\n", link_man->pNext->addr);
printf("%d\n", link_man->pNext->year);
printf("%ld\n", link_man->pNext->phone_num);
printf("***********************************\n");
link_man = link_man->pNext;
}
}
void empty_linkman(pBook head)
{
pBook ptmp = NULL;
pBook link_man = head;
if (head->pNext == NULL)
{
printf("电话簿为空!不用清空哦!(ˇ0ˇ) \n");
}
while (link_man->pNext != NULL)
{
if (link_man->pNext->pNext == NULL)
{
free(link_man->pNext);
link_man->pNext = NULL;
}
else
{
ptmp = link_man->pNext;
link_man->pNext = link_man->pNext->pNext;
free(ptmp);
}
}
printf("清空完成!\n");
}
pBook amend_linkman(pBook head)
{
char name[10];
char sex[5];
char addr[20];
pBook link_man = head;
if (!head)
{
printf("没有联系人,不能修改!\n");
return NULL;
}
printf("请问你要修改哪位联系人? ");
scanf("%s", name);
while (link_man->pNext != NULL)
{
if (strcmp(link_man->pNext->name, name) == 0)
{
printf("请输入联系人信息:(姓名、性别、地址、年龄、电话号码)\n");
scanf("%s", name);
scanf("%s", sex);
scanf("%s", addr);
strcpy(link_man->pNext->name, name);
strcpy(link_man->pNext->sex, sex);
strcpy(link_man->pNext->addr, addr);
scanf("%d", &link_man->pNext->year);
scanf("%d", &link_man->pNext->phone_num);
return head;
}
else
link_man = link_man->pNext;
}
return head;
}
void search_linkman(pBook head)
{
char name[10];
pBook link_man = head;
if (!head)
{
printf("没有联系人,查找失败!\n");
return NULL;
}
printf("请问你要查找哪位联系人? ");
scanf("%s", name);
while (link_man->pNext != NULL)
{
if (strcmp(link_man->pNext->name, name) == 0)
{
printf("%s\n", link_man->pNext->name);
printf("%s\n", link_man->pNext->sex);
printf("%s\n", link_man->pNext->addr);
printf("%d\n", link_man->pNext->year);
printf("%ld\n", link_man->pNext->phone_num);
printf("***********************************\n");
return 0;
}
else
link_man = link_man->pNext;
}
printf("没有找到!\n");
}
void start(pBook *phead)
{
int i = -1;
assert(phead);
while (1)
{
printf("欢迎使用电话薄系统!选择您的操作:\n\n");
printf("*********************************************\n"
"*1.添加联系人信息 2.删除指定联系人信息 *\n"
"*3.查找指定联系人信息 4.修改指定联系人信息 *\n"
"*5.显示所有联系人信息 6.清空所有联系人 *\n"
"*0.结束系统! ————中情局专用———— *\n"
"*********************************************\n");
scanf("%d",&i);
switch (i)
{
case 0:
printf("*****感谢使用本系统!*****\n");
exit(0);
case 1:
*phead=add_linkman( phead);
break;
case 2:
*phead = remove_linkman(phead);
break;
case 3:
search_linkman(phead);
break;
case 4:
*phead = amend_linkman(phead);
break;
case 5:
traver_linkman(phead);
break;
case 6:
empty_linkman( phead);
break;
default:
printf("选择有误!请重新选择\n");
break;
}
}
}
#include<stdio.h>
#include"phone_book.h"
int main()
{
pBook phead = NULL;
start(&phead);
return 0;
}
|