#include"contact.h"
//1.InitContact函数的实现
void InitContact(struct Contact* ps)
{
memset(ps->data, 0, sizeof(ps->data));
ps->num = 0;
}
//2.AddContact函数的实现
void AddContact(struct Contact* ps)
{
if (ps->num >= MAX)
{
printf("通讯录已满,请扩大通讯录后,再进行添加");
}
else
{
printf("请输入你所要增添的信息\n");
printf("请输入好友姓名:");
scanf("%s",ps->data[ps->num].name);
printf("请输入好友电话:");
scanf("%s",ps->data[ps->num].tele);
printf("请输入好友id:");
scanf("%s", ps->data[ps->num].id);
printf("请输入好友住址:");
scanf("%s", ps->data[ps->num].add);
printf("请输入好友性别:");
scanf("%s", ps->data[ps->num].sex);
printf("请输入好友年龄:");
scanf("%d", &(ps->data[ps->num].age));
ps->num++;
printf("输入成功\n");
}
}
//3.这个就是删除功能
void DeleContact(struct Contact* ps)
{
if (ps->num == 0)
{
printf("你还没有好友给你删\n");
}
else if (ps->num > 0)
{
int i = 0;
char name[MAX_NAME];
printf("请输入你要删除的好友信息\n");
scanf("%s",&name);
if (0 == strcmp(ps->data[i].name, name))
{
for (i = 0; i < ps->num; i++)
{
ps->data[i] = ps->data[i + 1];
ps->num--;
}
printf("删除成功\n");
}
else if (i == ps->num)
{
printf("找不到该好友信息\n");
}
}
}
//4.替换功能的实现(没有一次写对)
void TihuanContact(struct Contact* ps)
{
if (ps->num == 0)
{
printf("无好友信息可以替换\n");
}
else
{
int i = 0;
char name[MAX_NAME];
printf("请输入你要替换的名字:\n");
scanf("%s", &name);
if (0 == strcmp(ps->data[i].name, name))
{
printf("请替换你的姓名:");
scanf("%s",ps->data[i].name);
printf("请替换你的电话::");
scanf("%s", ps->data[i].tele);
printf("请替换你的id:");
scanf("%s", ps->data[i].id);
printf("请替换你的地址:");
scanf("%s", ps->data[i].add);
printf("请替换你的性别:");
scanf("%s", ps->data[i].sex);
printf("请替换你的年龄:");
scanf("%d",&(ps->data[i].age));
printf("替换成功\n");
}
if (0 != strcmp(ps->data[i].name, name))
{
printf("无可替换的好友信息\n");
}
}
}
//5.展示功能的实现
void ShowContact(struct Contact* ps)
{
if (ps->num == 0)
{
printf("你还没有好友哦!快去交朋友吧!\n");
}
else
{
int i = 0;
printf("%-5s\t %-12s\t %-20s\t %-20s\t %-5s\t %-5s\n", "姓名", "电话", "身份证号", "住址", "性别", "年龄");
for (i = 0; i < ps->num; i++)
{
printf("%-5s\t %-12s\t %-20s\t %-20s\t %-5s\t %-5d\n",
ps->data[i].name,
ps->data[i].tele,
ps->data[i].id,
ps->data[i].add,
ps->data[i].sex,
ps->data[i].age);
}
}
}
//6.查找功能的实现
void SearchContact(struct Contact* ps)
{
if (ps->num == 0)
{
printf("你还没有好友你查个鬼\n");
}
else
{
int i = 0;
char name[MAX_NAME];
printf("请输入你要查找的好友信息\n");
scanf("%s",&name);
if (0 == (strcmp(ps->data[i].name, name)))
{
printf("找到了你的好友信息\n");
printf("%-5s\t %-12s\t %-20s\t %-20s\t %-5s\t %-5s \n","姓名","电话","身份证","地址","性别","年龄");
printf("%-5s\t %-12s\t %-20s\t %-20s\t %-5s\t %-5d \n", ps->data[i].name, ps->data[i].tele, ps->data[i].id, ps->data[i].add, ps->data[i].sex, ps->data[i].age);
printf("找到了哦!,是它是它就是它!\n");
}
}
}
//7.排序功能的实现
void SortContact(struct Contact* ps)
{
int i = 0;
for (i = 0; i < ps->num; i++)
{
int j = 0;
for (j = 0; j < ps->num - i - 1; j++)
{
if (strcmp(ps->data[j].name, ps->data[j + 1].name) > 0)
{
struct Peoinfor tmp = ps->data[j];
ps->data[j] = ps->data[j + 1];
ps->data[j + 1] = tmp;
}
}
}
}
//8.清空功能
void Emptycontact(struct Contact* ps)
{
char ch[10];
printf("真的确认将所有好友信息清空吗?(是/不是)\n");
scanf("%s", &ch);
if (0 == strcmp("是", ch))
{
ps->num = 0;
printf("已全部清空\n");
}
else
{
printf("清空失败\n");
}
}