#include "Contact.h"
void init(Pcon pcon)
{
pcon->size = 0;
pcon->capacity = DEFAULT;
pcon->data=(PeoInfo *)malloc(MAX_INIT*sizeof(PeoInfo));
if(pcon->data == NULL)
{
printf("out of menory");
exit(EXIT_FAILURE);
}
memset(pcon->data,0,pcon->capacity*sizeof(PeoInfo));
_load(pcon);
}
void _rise(Pcon pcon) //增大容量
{
PeoInfo *temp = (PeoInfo *)realloc(pcon->data,(pcon->capacity+MAX_INIT)*sizeof(PeoInfo));
if(temp == NULL)
{
printf("out of memory!\n");
exit(0);
}
else
pcon->data = temp;
}
void _dela(Pcon pcon) //减小容量
{
PeoInfo *temp = (PeoInfo *)realloc(pcon->data,(pcon->capacity-MAX_INIT)*sizeof(PeoInfo));
if(temp == NULL)
{
printf("电话本已空!\n");
return ;
}
else
pcon->data = temp;
}
void _destory(Pcon pcon)
{
if(pcon->data != NULL)
{
free(pcon->data);
pcon->data = NULL;
}
}
void _add(Pcon pcon)
{
PeoInfo *temp = NULL;
if(pcon->size == pcon->capacity)
{
_rise(pcon);
}
printf("请输入名字:>");
scanf("%s",pcon->data[pcon->size].name);
printf("请输入年龄:>");
scanf("%d",&(pcon->data[pcon->size].age));
printf("请输入性别:>");
scanf("%s",pcon->data[pcon->size].sex);
printf("请输入电话:>");
scanf("%s",pcon->data[pcon->size].tele);
printf("请输入地址:>");
scanf("%s",pcon->data[pcon->size].addr);
pcon->size++;
printf("添加成功\n");
}
void _display(Pcon pcon)
{
int i = 0;
printf("%9s\t%3s\t%4s\t%11s\t%10s\n","name","age","sex","tele","addr");
for(i = 0;i<pcon->size;i++)
{
printf("%9s\t%3d\t%4s\t%11s\t%10s\n",
pcon->data[i].name,
pcon->data[i].age,
pcon->data[i].sex,
pcon->data[i].tele,
pcon->data[i].addr);
}
}
static int find(Pcon pcon, char *name)
{
int i = 0;
for(i = 0;i<pcon->size; i++)
{
if(strcmp(pcon->data[i].name,name) == 0)
{
return i;
}
}
return -1;
}
void _del(Pcon pcon)
{
int pos = 0;
int index = 0;
char name[NAME_MAX];
if(pcon->size == 0)
{
printf("电话本空\n");
return;
}
printf("请输入要删除人的名字:>");
scanf("%s",name);
pos = find(pcon, name);
if(pos == -1)
{
printf("找不到要删除的人\n");
return;
}
//
for(index = pos; index < pcon->size; index++)
{
pcon->data[index] = pcon->data[index+1];
}
pcon->size--;
printf("删除成功\n");
_dela(pcon);
}
void _search(Pcon pcon)
{
char name[NAME_MAX] = {0};
int pos = 0;
printf("请输入要查找人的名字:>");
scanf("%s",name);
pos = find(pcon, name);
if(pos == -1)
{
printf("指定联系人不存在\n");
return;
}
else
{
printf("%9s\t%3s\t%4s\t%11s\t%10s\n","name","age","sex","tele","addr");
printf("%9s\t%3d\t%4s\t%11s\t%10s\n",
pcon->data[pos].name,
pcon->data[pos].age,
pcon->data[pos].sex,
pcon->data[pos].tele,
pcon->data[pos].addr);
}
}
void _modify(Pcon pcon)
{
char name[NAME_MAX] = {0};
int pos = 0;
printf("请输入要修改人的名字:>");
scanf("%s",name);
pos = find(pcon, name);
if(pos == -1)
{
printf("指定联系人不存在\n");
return;
}
else
{
printf("请输入名字:>");
scanf("%s",pcon->data[pos].name);
printf("请输入年龄:>");
scanf("%d",&(pcon->data[pos].age));
printf("请输入性别:>");
scanf("%s",pcon->data[pos].sex);
printf("请输入电话:>");
scanf("%s",pcon->data[pos].tele);
printf("请输入地址:>");
scanf("%s",pcon->data[pos].addr);
}
}
void _sort(Pcon pcon)
{
int i = 0;
int j = 0;
PeoInfo temp = {0};
for(i = 0;i<pcon->size-1; i++)//控制排序趟数
{
for(j = 0; j<pcon->size-1-i; j++)
{
if(strcmp(pcon->data[j].name,pcon->data[j+1].name) > 0)
{
temp = pcon->data[j];
pcon->data[j] = pcon->data[j+1];
pcon->data[j+1] = temp;
}
}
}
}
void _clear(Pcon pcon)
{
pcon->size = 0;
}
void _save(Pcon pcon) //写入文件
{
int i = 0;
FILE *pfWrite = fopen("contact.txt","w"); //打开一个文件
if(pfWrite == NULL)
{
perror("open file for write");
exit(0);
}
else
{
for(i=0; i<pcon->size; i++)
{
fwrite(&(pcon->data[i]),sizeof(PeoInfo),1,pfWrite);
}
fclose(pfWrite);
}
}
void _load(Pcon pcon) //加载文件内容
{
int i = 0;
PeoInfo temp = {0};
FILE *pfRead = fopen("contact.txt","r");
if(pfRead == NULL)
{
perror("open file for read");
exit(0);
}
while(fread(&temp,sizeof(PeoInfo),1,pfRead))
{
_rise(pcon);
pcon->data[i] = temp;
i++;
pcon->size++;
}
}