#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
//从文件中加载数据
void LoadContact(Contact* pc)
{
FILE* pf = fopen("ContactData.text", "rb");
if (pf == NULL)
{
perror("LoadContact");
return;
}
PeoInfo temp = { 0 };
while (fread(&temp, sizeof(PeoInfo), 1, pf))
{
if (CheckCapacity(pc) == 0)
return;
pc->data[pc->sz] = temp;
pc->sz++;
}
//关闭
fclose(pf);
pf = NULL;
}
//初始化联系人
void InitContact(Contact* pc)
{
assert(pc);
pc->data = (PeoInfo*)malloc(INT_SZ*sizeof(PeoInfo));
if (pc->data == NULL)
{
perror("InitContact");
return;
}
pc->sz = 0;
pc->capacity = INT_SZ;
//加载联系人
LoadContact(pc);
}
//扩容
int CheckCapacity(Contact* pc)
{
if (pc->sz == pc->capacity)
{
PeoInfo* ptr = (PeoInfo*)realloc(pc->data, (pc->capacity + INT_SZ) * sizeof(PeoInfo));
if (ptr == NULL)
{
printf("扩容失败!");
perror("CheckCapacity");
return 0;
}
else
{
pc->data = ptr;
pc->capacity += INT_SZ;
printf("扩容成功!");
return 1;
}
}
return 1;
}
//添加联系人+
void AddContact(Contact* pc)
{
assert(pc);
//判断
if (CheckCapacity(pc) == 0)
{
printf("添加失败,空间不足!");
return;
}
if (CheckCapacity(pc) == 1)
{
printf("扩容成功\n");
//添加
printf("请输入姓名:");
scanf("%s", pc->data[pc->sz].name);
printf("请输入年龄:");
scanf("%d", &pc->data[pc->sz].age);
printf("请输入性别:");
scanf("%s", pc->data[pc->sz].sex);
printf("请输入电话:");
scanf("%s", pc->data[pc->sz].tele);
printf("请输入住址:");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
printf("添加成功\n");
}
}
//显示联系人
void ShowContact(const Contact* pc)//const修饰,不让其通过pc指针改变其指向内容。
{
assert(pc);
int i = 0;
//打印表头
printf("%-20s\t %2s\t %-5s\t %-12s\t %-20s\n", "名字", "年龄", "性别", "电话", "住址");
//打印内容
for (i = 0; i < pc->sz; i++)
{
printf("%-20s\t %2d\t %-5s\t %-12s\t %-20s\n",
pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
//查找
int FindByName(Contact* pc, char name[])
{
int i = 0;
//找到要删除的下标
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
//记下找到的下标值
return i;
}
}
return -1;
}
//删除指定联系人(name)
void DelContact(Contact* pc)
{
char name[MAX_NAME];
assert(pc);
printf("请输入要删除的名字\n");
scanf("%s", name);
int i = 0;
//如果为空,不删
if (pc->sz == 0) {
printf("通讯录为空\n");
return;
}
int del = FindByName(pc, name);
if (del == -1) {
printf("没找到\n");
return;
}
for (i = del; i < pc->sz - 1; i++) {
pc->data[i] = pc->data[i + 1];
}
pc->sz--;
printf("删除成功!\n");
}
//查找一个人
void SearchContact(const Contact* pc)
{
assert(pc);
char name[MAX_NAME];
printf("请输入要删除的名字\n");
scanf("%s", name);
int find = FindByName(pc, name);
if (find == -1)
{
printf("没有这个人\n");
return;
}
//打印找到人的信息
printf("%-20s\t %-2s\t %-5s\t %-12s\t %-20s\n", "名字", "年龄", "性别", "电话", "住址");
//打印内容
printf("%-20s\t %-2d\t %-5s\t %-12s\t %-20s\n",
pc->data[find].name,
pc->data[find].age,
pc->data[find].sex,
pc->data[find].tele,
pc->data[find].addr);
}
//修改指定联系人信息
void ModifyContact(Contact* pc)
{
assert(pc);
printf("请输入要修改联系人的姓名:\n");
char name[MAX_NAME];
scanf("%s", name);
int find = FindByName(pc, name);
if (find == -1)
{
printf("没有这个人\n");
return;
}
printf("请输入要修改的内容:\n");
printf("请输入姓名:");
scanf("%s", pc->data[find].name);
printf("请输入年龄:");
scanf("%d", &pc->data[find].age);
printf("请输入性别:");
scanf("%s", pc->data[find].sex);
printf("请输入电话:");
scanf("%s", pc->data[find].tele);
printf("请输入住址:");
scanf("%s", pc->data[find].addr);
printf("修改成功!!!!\n");
}
//排序联系人信息
void SortContact(struct Contact* pc)
{
if (pc->sz == 0)
{
printf("通讯录为空,无法排序!!!\n");
return;
}
int i = 0;
int j = 0;
struct PeoInfo tmp;
for (i = 0; i < pc->sz - 1; i++)
{
int flag = 1;
for (j = 0; j < pc->sz - i - 1; j++)
{
if (strcmp(pc->data[j].name, pc->data[j + 1].name) < 0)
{
tmp = pc->data[j];
pc->data[j] = pc->data[j + 1];
pc->data[j + 1] = tmp;
flag = 0;
}
}
if (flag == 1)
{
return;
}
}
printf("排序成功\n");
}
//释放
void DestroyContact(Contact* pc)
{
free(pc->data);
pc->data = NULL;
pc->capacity = 0;
pc->sz = 0;
}
//保存联系人
void SaveContact(Contact* pc)
{
FILE* pf = fopen("ContactData.text", "wb");
if (pf == NULL)
{
perror("SaveContact");
return;
}
//写文件
int i = 0;
for (i = 0; i < pc->sz; i++)
{
fwrite(pc->data,sizeof(PeoInfo),1,pf);
}
//关闭文件
fclose(pf);
pf == NULL;
}