define _CRT_SECURE_NO_WARNINGS
include"contact.h"
//初始化通讯录
void InitContact(struct Contact* pc)
{
pc->sz = 0;//默认没有信息
//memset(pc->data, 0, PEOPLE_MAX * sizeof(struct PeoInfo));
memset(pc->data, 0, sizeof(pc->data));
}
//增加联系人
void AddContact(struct Contact* pc)
{
if (pc->sz == PEOPLE_MAX)
{
printf("通讯录满了\n");
}
else
{
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);
//提示
printf("添加成功!\n");
pc->sz++;
}
}
//显示所有的联系人
void ShowContact(struct Contact* pc)
{
int i = 0;
//打印标题 (美观)
printf("%15s\t%5s\t%8s\t%15s\t%30s\n\n", "name", "age", "sex", "tele", "addr");
for (i = 0; i < pc->sz; i++)
{
//打印每个数据
printf("%15s\t%5d\t%8s\t%15s\t%30s\n\n",
pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr );
}
}
//删除指定联系人
int FindContactByName(const struct Contact pc, const char name)
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}
}
//找不到
return -1;
}
void DelContact(struct Contact* pc)
{
if (pc->sz == 0)
{
printf("通讯录为空,无法删除!\n");
return;
}
char name[NAME_MAX] = { 0 };
printf("请输入要删除联系人的名字:>");
scanf("%s", name);
//查找
int pos = FindContactByName(pc, name);
if (pos == -1)
{
printf("指定的联系人不存在!\n");
}
//删除
else
{
int j = 0;
for (j = pos; j < pc->sz - 1; j++)
{
pc->data[j] = pc->data[j + 1];
}
pc->sz--;
//提示
printf("删除成功!\n");
}
}
//查找指定联系人
void SearchContact(const struct Contact* pc)
{
char [Skrill下载](https://www.gendan5.com/wallet/Skrill.html)name[NAME_MAX] = { 0 };
printf("输入要查找的联系人的名字:>");
scanf("%s", name);
//查找
int pos = FindContactByName(pc, name);
if (pos == -1)
{
printf("查无此人!\n");
}
else
{
//打印标题 (美观)
printf("%15s\t%5s\t%8s\t%15s\t%30s\n\n", "name", "age", "sex", "tele", "addr");
//打印每个数据
printf("%15s\t%5d\t%8s\t%15s\t%30s\n\n",
pc->data[pos].name,
pc->data[pos].age,
pc->data[pos].sex,
pc->data[pos].tele,
pc->data[pos].addr);
}
}
//修改指定联系人
void ModifyContact(struct Contact* pc)
{
char name[NAME_MAX] = { 0 };
printf("输入要修改的联系人的名字:>");
scanf("%s", name);
//查找
int pos = FindContactByName(pc, name);
if (pos == -1)
{
printf("要修改的联系人数据不存在!\n");
}
else
{
printf("请输入新的名字:>");
scanf("%s", pc->data[pos].name);
printf("请输入新的年龄:>");
scanf("%d", &(pc->data[pos].age));
printf("请输入新的性别:>");
scanf("%s", pc->data[pos].sex);
printf("请输入新的电话:>");
scanf("%s", pc->data[pos].tele);
printf("请输入新的地址:>");
scanf("%s", pc->data[pos].addr);
//提示
printf("修改成功!\n");
}
}