在今天,我将为大家来来一个模拟实现通讯录的功能。
文章目录
通讯录需要的文档
contact.h ---------- 头文件的引用和结构体的声明
contact.c ---------- 函数的实现
test.c ---------- 通讯录的检验
菜单的实现和函数的调用
#include "contact.h"
void Menu()
{
printf("****************************************\n");
printf("****** 1.add 2.del ******\n");
printf("****** 3.search 4.modify ******\n");
printf("****** 5.show 6.sort ******\n");
printf("****** 0.exit ******\n");
printf("****************************************\n");
}
int main()
{
int input = 0;
struct contact con;
InitContact(&con);
do
{
Menu();
printf("请选择:>");
scanf("%d",&input);
switch(input)
{
case 1:
AddContact(&con);
break;
case 2:
DelContact(&con);
break;
case 3:
SearchContact(&con);
break;
case 4:
ModifyContact(&con);
break;
case 5:
ShowContact(&con);
break;
case 6:
SortContact(&con);
break;
case 0:
printf("退出通讯录\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
}while(input);
}
头文件的引用、参数的定义和函数的声明
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<stdlib.h>
#define Max 100
#define Max_name 20
#define Max_tele 12
#define Max_sex 5
#define Max_addr 30
//人的信息
struct people
{
char name[Max_name];
char tele[Max_tele];
char sex[Max_sex];
char addr[Max_addr];
int age;
};
struct contact
{
struct people data[Max];
int sz;
};
//初始化通讯录
void InitContact(struct contact* pc);
//添加联系人到通讯录
void AddContact(struct contact* pc);
//删除通讯录的指定联系人
void DelContact(struct contact* pc);
//显示通讯录
void ShowContact(struct contact* pc);
//查找联系人
void SearchContact(struct contact* pc);
//修改联系人信息
void ModifyContact(struct contact* pc);
//排序联系人
void SortContact(struct contact* pc);
初始化结构体函数
void InitContact(struct contact* pc)
{
pc->sz = 0;
assert(pc != NULL);
memset(pc->data,0,Max*sizeof(struct people));
}
增加联系人函数
void AddContact(struct contact* pc)
{
assert(pc != NULL);
if(pc->sz == Max)
{
printf("通讯录已经满了\n");
return;
}
printf("请输入联系人的姓名:>\n");
scanf("%s",pc->data[pc->sz].name);
printf("请输入联系人的电话:>\n");
scanf("%s",pc->data[pc->sz].tele);
printf("请输入联系人的地址:>\n");
scanf("%s",pc->data[pc->sz].addr);
printf("请输入联系人的性别:>\n");
scanf("%s",pc->data[pc->sz].sex);
printf("请输入联系人的年龄:>\n");
scanf("%d",&(pc->data[pc->sz].age));
pc->sz++;
printf("成功添加新的联系人\n");
}
显示所有联系人函数
void ShowContact(struct contact* pc)
{
int i = 0;
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n","姓名","性别","年龄","号码","地址");
for(i=0;i<pc->sz;i++)
{
printf("%-20s\t%-5s\t%-5d\t%-12s\t%-30s\n",pc->data[i].name,
pc->data[i].sex,
pc->data[i].age,
pc->data[i].tele,
pc->data[i].addr);
}
}
删除联系人函数
int Findpeople(struct contact* pc,char name[])
{
int i = 0;
for(i=0;i<pc->sz;i++)
{
if(0 == strcmp(pc->data[i].name,name))
{
return i;
}
}
return -1;
}
void DelContact(struct contact* pc)
{
char name[Max_name];
int ret = 0;
int i = 0;
printf("请输入你要删除的联系人的名字\n");
scanf("%s",name);
//查找该联系人
ret = Findpeople(pc,name);
if(ret == -1)
{
printf("找不到该联系人\n");
}
else
{
//删除
for(i = ret;i<pc->sz-1;i++)
{
pc->data[i] = pc->data[i+1];
}
pc->sz--;
printf("指定联系人已经删除\n");
}
}
查找联系人函数
void SearchContact(struct contact* pc)
{
char name[Max_name];
int ret = 0;
printf("输入想要被查找的联系人\n");
scanf("%s",&name);
ret = Findpeople(pc,name);
if(ret == -1)
{
printf("找不到\n");
}
else
{
printf("%-20s\t%-5s\t%-5s\t%-12s\t%--30s\t\n","姓名","性别","年龄","电话","地址");
printf("%-20s\t%-5s\t%-5d\t%-12s\t%-30s\t\n",pc->data[ret].name,
pc->data[ret].sex,
pc->data[ret].age,
pc->data[ret].tele,
pc->data[ret].addr);
}
}
修改联系人函数
void ModifyContact(struct contact* pc)
{
char name[Max_name];
int ret = 0;
printf("请输入要修改的联系人的名字\n");
scanf("%s",&name);
ret = Findpeople(pc,name);
if(ret == -1)
{
printf("不存在该联系人\n");
}
else
{
printf("找到该联系人,请依次输入信息进行修改\n");
printf("请输入该联系人的姓名:>\n");
scanf("%s",pc->data[ret].name);
printf("请输入该联系人的电话:>\n");
scanf("%s",pc->data[ret].tele);
printf("请输入该联系人的地址:>\n");
scanf("%s",pc->data[ret].addr);
printf("请输入该联系人的性别:>\n");
scanf("%s",pc->data[ret].sex);
printf("请输入该联系人的年龄:>\n");
scanf("%d",&(pc->data[ret].age));
printf("修改联系人成功\n");
}
}
排序联系人函数
void menu()
{
printf("*****************************************\n");
printf("****** 1.name 2.sex ******\n");
printf("****** 3.age 4.addr ******\n");
printf("****** 5.tele 0.exit ******\n");
printf("*****************************************\n");
}
//以名字顺序排序
int com_by_name(const void* str1,const void* str2)
{
return strcmp(((struct people*)str1)->name,((struct people*)str2)->name);
}
//以性别顺序排序
int com_by_sex(const void* str1,const void* str2)
{
return strcmp(((struct people*)str1)->sex,((struct people*)str2)->sex);
}
//以电话顺序排序
int com_by_tele(const void* str1,const void* str2)
{
return strcmp(((struct people*)str1)->tele,((struct people*)str2)->tele);
}
//以地址顺序排序
int com_by_addr(const void* str1,const void* str2)
{
return strcmp(((struct people*)str1)->addr,((struct people*)str2)->addr);
}
//以年龄顺序排序
int com_by_age(const void* str1,const void* str2)
{
return ((struct people*)str1)->age - ((struct people*)str2)->age;
}
void SortContact(struct contact* pc)
{
int input = 0;
again:
menu();
printf("请选择以什么方式排序\n");
scanf("%d",&input);
switch(input)
{
case 1:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_name);
break;
case 2:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_sex);
break;
case 3:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_age);
break;
case 4:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_addr);
break;
case 5:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_tele);
break;
case 0:
break;
default:
printf("选择错误,请重新选择\n");
goto again;
break;
}
if(input != 0)
{
printf("排序成功\n");
}
else
{
printf("退出排序成功\n");
}
}
contact.h文档的全部代码
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<stdlib.h>
#define Max 100
#define Max_name 20
#define Max_tele 12
#define Max_sex 5
#define Max_addr 30
//人的信息
struct people
{
char name[Max_name];
char tele[Max_tele];
char sex[Max_sex];
char addr[Max_addr];
int age;
};
struct contact
{
struct people data[Max];
int sz;
};
//初始化通讯录
void InitContact(struct contact* pc);
//添加联系人到通讯录
void AddContact(struct contact* pc);
//删除通讯录的指定联系人
void DelContact(struct contact* pc);
//显示通讯录
void ShowContact(struct contact* pc);
//查找联系人
void SearchContact(struct contact* pc);
//修改联系人信息
void ModifyContact(struct contact* pc);
//排序联系人
void SortContact(struct contact* pc);
contact.c文档的全部代码
#include "contact.h"
void InitContact(struct contact* pc)
{
pc->sz = 0;
assert(pc != NULL);
memset(pc->data,0,Max*sizeof(struct people));
}
void AddContact(struct contact* pc)
{
assert(pc != NULL);
if(pc->sz == Max)
{
printf("通讯录已经满了\n");
return;
}
printf("请输入联系人的姓名:>\n");
scanf("%s",pc->data[pc->sz].name);
printf("请输入联系人的电话:>\n");
scanf("%s",pc->data[pc->sz].tele);
printf("请输入联系人的地址:>\n");
scanf("%s",pc->data[pc->sz].addr);
printf("请输入联系人的性别:>\n");
scanf("%s",pc->data[pc->sz].sex);
printf("请输入联系人的年龄:>\n");
scanf("%d",&(pc->data[pc->sz].age));
pc->sz++;
printf("成功添加新的联系人\n");
}
void ShowContact(struct contact* pc)
{
int i = 0;
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n","姓名","性别","年龄","号码","地址");
for(i=0;i<pc->sz;i++)
{
printf("%-20s\t%-5s\t%-5d\t%-12s\t%-30s\n",pc->data[i].name,
pc->data[i].sex,
pc->data[i].age,
pc->data[i].tele,
pc->data[i].addr);
}
}
int Findpeople(struct contact* pc,char name[])
{
int i = 0;
for(i=0;i<pc->sz;i++)
{
if(0 == strcmp(pc->data[i].name,name))
{
return i;
}
}
return -1;
}
void DelContact(struct contact* pc)
{
char name[Max_name];
int ret = 0;
int i = 0;
printf("请输入你要删除的联系人的名字\n");
scanf("%s",name);
//查找该联系人
ret = Findpeople(pc,name);
if(ret == -1)
{
printf("找不到该联系人\n");
}
else
{
//删除
for(i = ret;i<pc->sz-1;i++)
{
pc->data[i] = pc->data[i+1];
}
pc->sz--;
printf("指定联系人已经删除\n");
}
}
void SearchContact(struct contact* pc)
{
char name[Max_name];
int ret = 0;
printf("输入想要被查找的联系人\n");
scanf("%s",&name);
ret = Findpeople(pc,name);
if(ret == -1)
{
printf("找不到\n");
}
else
{
printf("%-20s\t%-5s\t%-5s\t%-12s\t%--30s\t\n","姓名","性别","年龄","电话","地址");
printf("%-20s\t%-5s\t%-5d\t%-12s\t%-30s\t\n",pc->data[ret].name,
pc->data[ret].sex,
pc->data[ret].age,
pc->data[ret].tele,
pc->data[ret].addr);
}
}
void ModifyContact(struct contact* pc)
{
char name[Max_name];
int ret = 0;
printf("请输入要修改的联系人的名字\n");
scanf("%s",&name);
ret = Findpeople(pc,name);
if(ret == -1)
{
printf("不存在该联系人\n");
}
else
{
printf("找到该联系人,请依次输入信息进行修改\n");
printf("请输入该联系人的姓名:>\n");
scanf("%s",pc->data[ret].name);
printf("请输入该联系人的电话:>\n");
scanf("%s",pc->data[ret].tele);
printf("请输入该联系人的地址:>\n");
scanf("%s",pc->data[ret].addr);
printf("请输入该联系人的性别:>\n");
scanf("%s",pc->data[ret].sex);
printf("请输入该联系人的年龄:>\n");
scanf("%d",&(pc->data[ret].age));
printf("修改联系人成功\n");
}
}
void menu()
{
printf("*****************************************\n");
printf("****** 1.name 2.sex ******\n");
printf("****** 3.age 4.addr ******\n");
printf("****** 5.tele 0.exit ******\n");
printf("*****************************************\n");
}
//以名字顺序排序
int com_by_name(const void* str1,const void* str2)
{
return strcmp(((struct people*)str1)->name,((struct people*)str2)->name);
}
//以性别顺序排序
int com_by_sex(const void* str1,const void* str2)
{
return strcmp(((struct people*)str1)->sex,((struct people*)str2)->sex);
}
//以电话顺序排序
int com_by_tele(const void* str1,const void* str2)
{
return strcmp(((struct people*)str1)->tele,((struct people*)str2)->tele);
}
//以地址顺序排序
int com_by_addr(const void* str1,const void* str2)
{
return strcmp(((struct people*)str1)->addr,((struct people*)str2)->addr);
}
//以年龄顺序排序
int com_by_age(const void* str1,const void* str2)
{
return ((struct people*)str1)->age - ((struct people*)str2)->age;
}
void SortContact(struct contact* pc)
{
int input = 0;
again:
menu();
printf("请选择以什么方式排序\n");
scanf("%d",&input);
switch(input)
{
case 1:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_name);
break;
case 2:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_sex);
break;
case 3:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_age);
break;
case 4:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_addr);
break;
case 5:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_tele);
break;
case 0:
break;
default:
printf("选择错误,请重新选择\n");
goto again;
break;
}
if(input != 0)
{
printf("排序成功\n");
}
else
{
printf("退出排序成功\n");
}
}
test.c文档的全部代码
#include "contact.h"
void Menu()
{
printf("****************************************\n");
printf("****** 1.add 2.del ******\n");
printf("****** 3.search 4.modify ******\n");
printf("****** 5.show 6.sort ******\n");
printf("****** 0.exit ******\n");
printf("****************************************\n");
}
int main()
{
int input = 0;
struct contact con;
InitContact(&con);
do
{
Menu();
printf("请选择:>");
scanf("%d",&input);
switch(input)
{
case 1:
AddContact(&con);
break;
case 2:
DelContact(&con);
break;
case 3:
SearchContact(&con);
break;
case 4:
ModifyContact(&con);
break;
case 5:
ShowContact(&con);
break;
case 6:
SortContact(&con);
break;
case 0:
printf("退出通讯录\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
}while(input);
}
今天,通讯录的代码实现就分享到这里,关注点一点,防止丢失。