要求:实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:姓名、性别、年龄、电话、住址
contact.c
#include<stdio.h>
#include"contact.h"
#include<string.h>
#include<stdlib.h>
void menu()
{
printf("********************************\n");
printf("********1.add 2.dele***********\n");
printf("********3.find 4.mod ***********\n");
printf("********5.show 6.empty**********\n");
printf("********7.sork 0.exit***********\n");
printf("********************************\n");
}
void add_pro(pContact pcon) //添加联系人信息
{
if(pcon->sz==MAX)
{
printf("数据已满,不能存入\n");
}
else
{
printf("请输入姓名》");
scanf("%s",&pcon->data[pcon->sz].name);
printf("请输入性别》");
scanf("%s",&pcon->data[pcon->sz].sex);
printf("请输入年龄》");
scanf("%d",&pcon->data[pcon->sz].age);
printf("请输入电话》");
scanf("%d",&pcon->data[pcon->sz].tel);
printf("请输入住址》");
scanf("%s",&pcon->data[pcon->sz].address);
printf("信息录入成功\n");
pcon->sz++;
}
}
int find_peo(pContact pcon,char* name)
{
int i=0;
for(i=0; i<pcon->sz; i++)
{
if(strcmp(pcon->data[i].name,name)==0)
{
return i;
}
}
if(i=pcon->sz)
{
return -1;
}
}
void dele_pro(pContact pcon) //删除指定联系人信息
{
int i=0;
int ret=0;
char name[MAX_NAME];
if(pcon->sz==0)
{
printf("通讯录里人数为零,无需删除\n");
}
else
{
printf("请输入要删除人的名字\n");
scanf("%s",&name);
ret=find_peo(pcon,name);
if(ret==-1)
{
printf("未找到所查找人的信息\n");
}
else
{
for(i=ret; i<=pcon->sz-1; i++)
{
pcon->data[i]=pcon->data[i+1];
}
pcon->sz--;
}
}
}
void find_pro(pContact pcon)// 查找指定联系人信息
{
int i=0;
int ret=0;
char name[MAX_NAME];
if(pcon->sz==0)
{
printf("通讯录里人数为零,无需查找\n");
}
else
{
printf("请输入要查找人的名字\n");
scanf("%s",name);
ret=find_peo(pcon,name);
if(ret==-1)
{
printf("未找到所查找人的信息\n");
}
else
{
printf("%10s%4s%13s%13s%20s\n","name","sex","age","telephone","address");
printf("%10s%4s%13d%13d%20s\n",
pcon->data[ret].name,
pcon->data[ret].sex,
pcon->data[ret].age,
pcon->data[ret].tel,
pcon->data[ret].address
);
}
}
}
void show_pro(pContact pcon)// 显示所有联系人信息
{
int i=0;
printf("%10s%4s%13s%13s%20s\n","name","sex","age","telephone","address");
for(i=0; i<pcon->sz; i++)
{
printf("%10s%4s%13d%13d%20s\n",
pcon->data[i].name,
pcon->data[i].sex,
pcon->data[i].age,
pcon->data[i].tel,
pcon->data[i].address
);
}
}
void init_my_con(pContact pcon)
{
pcon->sz=0;
memset(pcon->data,0,sizeof(Peoinfor)*MAX);
}
void mod_pro(pContact pcon) // 修改指定联系人信息
{
char name[MAX_NAME];
char sex[MAX_SEX];
char address[MAX_ADDRESS];
int age;
int tel;
int ret=0;
printf("请输入要修改人的姓名》");
scanf("%s",name);
ret=find_peo(pcon,name);
if(pcon->sz==0)
{
printf("通讯录里没有元素,无需修改\n");
}
else
{
printf("请输入修改后的姓名》");
scanf("%s",name);
strcpy(pcon->data[ret].name,name);
printf("请输入修改后的性别》");
scanf("%s",sex);
strcpy(pcon->data[ret].sex,sex);
printf("请输入修改后年龄》");
scanf("%d",&age);
pcon->data[ret].age=age;
printf("请输入修改后电话》");
scanf("%d",&tel);
pcon->data[ret].tel=tel;
printf("请输入修改后住址》");
scanf("%s",address);
strcpy(pcon->data[ret].address,address);
}
}
void empty_pro(pContact pcon) //清空所有联系人
{
pcon->sz=0;
memset(pcon->data,0,sizeof(Peoinfor)*MAX);
}
void sork_pro(pContact pcon)// 以名字排序所有联系人
{
int sz=pcon->sz;
int i=0;
int j=0;
int a=0;
for(i=0; i<sz; i++)
{
for(j=0; j<sz-i-1; j++)
{
if(strcmp(pcon->data[j].name,pcon->data[j+1].name)>0)
{
Peoinfor tmp=pcon->data[j];
pcon->data[j]=pcon->data[j+1];
pcon->data[j+1]=tmp;
}
}
}
}
text.c
#include"contact.h"
#include<stdio.h>
enum Pu
{
exit,
add ,
dele,
find,
mod,
show,
empty,
sork
};
int main()
{
int input=0;
Contact my_con;
init_my_con(&my_con);
do
{
menu();
printf("请选择》");
scanf("%d",&input);
switch(input)
{
case add:
{
add_pro(&my_con);
break;
}
case dele:
{
dele_pro(&my_con);
break;
}
case find:
{
find_pro(&my_con);
break;
}
case mod:
{
mod_pro(&my_con);
break;
}
case show:
{
show_pro(&my_con);
break;
}
case empty:
{
empty_pro(&my_con);
break;
}
case sork:
{
sork_pro(&my_con);
break;
}
default:
{
printf("输入错误,请重新输入");
break;
}
}
}while(input);
system("pause");
return 0;
}
contact.h
#ifdef CONTACT
void add_pro(pContact my_con);
void dele_pro(pContact my_con);
void find_pro(pContact my_con);
void mod_pro(pContact my_con);
void show_pro(pContact my_con);
void empty_pro(pContact my_con);
void sork_pro(pContact my_con);
#endif//结束 _CONTACT_H__