C语言实现制作通讯录(新手推荐)
#include"contact.h"
//打印项目
void pri()
{
printf("%-15s %-10s %-10s %-10s %-20s
", "姓名", "性别", "年龄", "电话", "住址");
}
//查找联系人的位置
int FindPOS(const struct contact* pc,char name[])
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (0==strcmp(pc->data[i].name, name) )
{
return i;
}
}
return -1;
}
//初始化
void InitContact(struct contact* pc)
{
pc->sz = 0;
memset(pc->data, 0, MAX * sizeof(struct PeoIfo));
}
//添加联系人
void add(struct contact* pc)
{
if (pc->sz >= MAX)
{
printf("通讯录已满。
");
return;
}
printf("请输入姓名
");
scanf("%s", pc->data[pc->sz].name);
printf("请输入性别
");
scanf("%s", pc->data[pc->sz].sex);
printf("请输入年龄
");
scanf("%d", &(pc->data[pc->sz].age));
printf("请输入电话
");
scanf("%s", pc->data[pc->sz].tel);
printf("请输入住址
");
scanf("%s", pc->data[pc->sz].adr);
pc->sz++;
}
//删除联系人
void del(struct contact* pc)
{
printf("请输入要删除的联系人姓名:
");
char delname[nameMAX];
scanf("%s", &delname);
int ret=FindPOS(pc, delname);
if (-1 == ret)
{
printf("要删除的联系人不存在
");
}
else
{
int i = 0;
for (i = ret; i < pc->sz - 1; i++)
{
pc->data[ret] = pc->data[ret + 1];
}
pc->sz--;
printf("删除成功
");
}
}
//查找联系人
void search(struct contact* pc)
{
char sechname[nameMAX];
printf("请输入要找的联系人:
");
scanf("%s", &sechname);
int ret = FindPOS(pc, sechname);
if (-1 == ret)
{
printf("找不到联系人
");
return;
}
pri();
printf("%-15s %-10s %-10d %-10s %-20s
",
pc->data[ret].name,
pc->data[ret].sex,
pc->data[ret].age,
pc->data[ret].tel,
pc->data[ret].adr);
}
//修改联系人
void modify(struct contact* pc)
{
printf("请输入要修改的联系人姓名:
");
char moname[nameMAX];
scanf("%s", &moname);
int ret = FindPOS(pc, moname);
if (-1 == ret)
{
printf("没有找到联系人
");
}
else
{
printf("请输入姓名
");
scanf("%s", pc->data[ret].name);
printf("请输入性别
");
scanf("%s", pc->data[ret].sex);
printf("请输入年龄
");
scanf("%d", &(pc->data[ret].age));
printf("请输入电话
");
scanf("%s", pc->data[ret].tel);
printf("请输入住址
");
scanf("%s", pc->data[ret].adr);
}
}
//展示通讯录
void show(struct contact* pc)
{
pri();
int i = 0;
for (i = 0; i < pc->sz; i++)
{
printf("%-15s %-10s %-10d %-10s %-20s
",
pc->data[i].name,
pc->data[i].sex,
pc->data[i].age,
pc->data[i].tel,
pc->data[i].adr);
}
}
//按姓名排序
int compare(void* s1, void* s2)
{
return strcmp(((struct PeoIfo*)s1)->name, ((struct PeoIfo*)s2)->name);
}
void Sort(struct contact* pc)
{
qsort(pc->data, pc->sz, sizeof(struct PeoIfo), compare);
}