C语言:通讯录
题目要求:
实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
添加联系人信息
删除指定联系人信息
查找指定联系人信息
修改指定联系人信息
显示所有联系人信息
清空所有联系人
以名字排序所有联系人
保存联系人到文件
加载联系人
contact.h
#pragma warning(disable:4996)//消除scanf的警告;
#ifndef __CONTACT_H__//防止重定义;
#define __CONTACT_H__
#include
#include
#include
#define NAME_MAX 20
#define SEX_MAX 4
#define ADD_MAX 10
#define TEL_MAX 13
#define INIT 2//刚开始电话本的容量
//#define CONTACT_FILE "contant.txt"
//姓名、性别、年龄、电话、住址
typedef struct person
{
char name[NAME_MAX ];
char sex[SEX_MAX];
int age;
char tel[TEL_MAX ];
char address[ADD_MAX ];
}person;
//通讯录成员结构体;
typedef struct cotact
{
person *people;
int size;//确定通讯录当前人数;
int capacity ; //电话本容量;
}contact_t,*contact_p;//指针用来在函数内部接受结构体指针;
contact_p InitContact(const char *file);
void AddPeople(contact_p Con);//新增成员;
void DeletePeople(contact_p Con);//删除成员;
void SearchPeople(contact_p Con);//查找成员;
void UpdatePeople(contact_p Con);//修改成员;
void ListPeople(contact_p Con);//显示所有成员;
void EmptyContact(contact_p Con);//清空通讯录;
void SortPeople(contact_p Con);//排序所有成员;
void menu();//打印菜单;
void save(contact_p Con,const char *file);
#endif //__CONTACT_H__
contact.c
#include"contact.h"
void menu()
{
printf("*****************************\n");
printf("**1、add 2、delete**\n");
printf("**3、update 4、search**\n");
printf("**5、list 6、empty **\n");
printf("**7、sort 0、exit **\n");
printf("*****************************\n");
printf("Please Enter:");
}
static int find(contact_p Con, char* arr)//公用查找函数;
{
int i =0;
for(;isize;i++)
{
if(strcmp(Con->people [i].name,arr) == 0)
return i;
}
return -1;
}
void save(contact_p Con,const char *file)
{
FILE *fp = fopen(file,"wb");
if(!fp)
{
printf("File fopen is error!\n");
return;
}
fwrite(Con,sizeof(contact_t)+Con->size * sizeof(person),1,fp);
fclose(fp);
}
void AddPeople(contact_p Con)//增加成员;
{
if(Con->size == Con->capacity )
{
/*
当前人数已经达到最大通讯录容量,则进行扩容,用realloc函数;
*/
person *tmp = (person *)realloc (Con->people,(Con->capacity +3)*sizeof(person));
if(tmp == NULL)
{
perror("realloc");
return;
}
else
{
Con->people = tmp;
(Con->capacity) += 3;
}
}//判断通讯录是否溢出;
printf("Start adding!\n");
printf("Please enter name:");
scanf("%s",&(Con->people[Con->size].name ));
printf("Please enter sex:");
scanf("%s",&(Con->people[Con->size].sex ));
printf("Please enter age:");
scanf("%s",&(Con->people[Con->size].age));
printf("Please enter telephone:");
scanf("%s",&(Con->people[Con->size].tel));
printf("Please enter address:");
scanf(" %s",&(Con->people[Con->size].address));
printf("Add a contact success!\n");
Con->size++;//注意:每增加一个成员,通讯录总人数size都得+1;
}
void DeletePeople(contact_p Con)//删除成员;
{
int ret = 0;
char name[20] = {0};
if(Con->size == 0)
{
printf("Honey,the contacts is empty!\n");
return ;
}
printf("PLease enter the name of the contact you want to delete:\n");
scanf("%s",&name);
ret = find(Con,name);
/*
删除的方法是将最后一个成员放到删除的这个成员位置上;
注意: 不能忘记size--;
*/
if(ret>=0)
{
Con->people[ret] = Con->people[Con->size-1];
(Con->size)--;
printf("Dlete success!\n");
}
else
printf("The contact you want to delete was not found!\n");
}
void SearchPeople(contact_p Con)//查找成员;直接调用find函数;
{
int ret = 0;
char name[20] = {0};
printf("Please enter the name of the contact you want to modify:\n");
scanf("%s",&name);
ret = find(Con,name);
if(ret>=0)
{
printf("%10s\t%4s\t%3s\t%13s\t%10s\n","name","sex","age","tel","address");
printf("%10s\t%4s\t%3d\t%13s\t%10s\n",Con->people [ret].name ,
Con->people [ret].sex ,Con->people [ret].age,
Con->people [ret].tel ,Con->people [ret].address );
}
else
{
printf("The contact was not found!\n");
}
}
void UpdatePeople(contact_p Con)//改变成员;
{
int i = 0;
int ret = 0;
char name[20] = {0};
char *ptr[] = {"name","sex","age","tel","address"};
//每次改变成员的单个内容;
printf("Please enter the name of the contact you want to modify:\n");
scanf("%s",&name);
ret = find(Con,name);//调用查找函数;
if(ret>=0)
{
int ages = 0;
char mod[20] = {0};
int sel = 0;
printf("Please enter the options you want to modify:\n");
printf("1.name\n2.sex\n3.age\n4.tel\n5.adress\n");
printf("Please enter:");
scanf("%d",&sel);
printf("Please enter:");
//这块选用switch语句;比较容易看懂;其实可以进行封装;
switch(sel)
{
case 1:
scanf("%s",&mod);
strcpy(Con->people [ret].name,mod);
printf("Modify success!\n");
break;
case 2:
scanf("%s",&mod);
strcpy(Con->people [ret].sex ,mod);
printf("Modify success!\n");
break;
case 3:
scanf("%d",&ages);
Con->people [ret].age = ages;
printf("Modify success!\n");
break;
case 4:
scanf("%s",&mod);
strcpy(Con->people [ret].tel ,mod);
printf("Modify success!\n");
break;
case 5:
scanf("%s",&mod);
strcpy(Con->people [ret].address ,mod);
printf("Modify success!\n");
break;
default:
printf("Modify failed!\n");
break;
}
}
else
printf("Without this contact!\n");
}
void ListPeople(contact_p Con)//显示所有成员;
{
int i = 0;
printf("%10s\t%4s\t%3s\t%13s\t%10s\n","name","sex","age","tel","address");
//循环打印每个通讯录成员;
for(; i < Con->size ; i++)
{
printf("%10s\t%4s\t%3d\t%13s\t%10s\n",Con->people [i].name ,
Con->people [i].sex ,Con->people [i].age,
Con->people [i].tel ,Con->people [i].address );
}
}
void EmptyContact(contact_p Con)//清空联系人;
{
/*
清空通讯录只需要将通讯录总成员数size改为 0;
*/
Con->size = 0;
printf("Contact is emoty!\n");
}
void SortPeople(contact_p Con)//排序所有联系人;
{
/*
冒泡排序所有成员的名字;
*/
int flag = 0;
int i = 0;
int j = 0;
for(i = 0; i < Con->size-1 ;i++)
{
flag = 1;
for(j = 0;j <(Con->size) - i -1;j++)
{
if((strcmp(Con->people[j].name , Con->people [j+1].name))> 0)
{
person tmp = Con->people[j];
Con->people[j] = Con->people[j+1];
Con->people[j+1] = tmp;
flag = 0;
}
}
if(flag==1)
{
break;
}
}
}
main.c
#include"contact.h"
int main()
{
contact_t Con = {0};
//size一定要初始化为0;
Con.size = 0;
Con.capacity = INIT ;
Con.people = (person *)malloc(INIT *sizeof(person));
/*
注意与malloc对应的free();在退出功能进行free。
*/
if(Con.people == NULL)//是否开辟成功的判断;
{
perror("malloc");
return 0;
}
else
{
int quit = 0;
int input = -1;
while (!quit)
{
menu();
/*fread(&Con.people ,sizeof(Con.people),1,fp);*/
/*fclose(fp);*/
scanf("%d", &input);
switch (input)
{
case 0:
quit = 1;
free(Con.people);
Con.people = NULL;
break;
case 1:
AddPeople(&Con);
break;
case 2:
DeletePeople(&Con);
break;
case 3:
UpdatePeople(&Con);
break;
case 4:
SearchPeople(&Con);
break;
case 5:
ListPeople(&Con);
break;
case 6:
EmptyContact(&Con);
break;
case 7:
SortPeople(&Con);
break;
default:
printf("Input error , Try again!\n");
break;
}
}
}
printf("Bye Bye!\n");
system("pause");
return 0;
}