在学习完c语言的部分之后,运用了最后学到的有关结构体,枚举,内存管理,文件等知识,写了一个基础版的通讯录,能够实现简单的功能如下:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人现代码如下:
#include "addlist.h"
void Init(struct A *p)//初始化通讯录
{
p->people = (struct man*)calloc(MAX_SIZE, sizeof(struct man));
if (p->people == NULL)
{
printf("FAIL");
exit(1);
}
p->size = 0;
p->capacity = SIZE;
}
void Add(struct A *p)//添加联系人
{
printf("请输入你要添加的联系人的姓名:>");
scanf("%s", p->people[p->size].name);
printf("请输入你要添加的联系人的性别:>");
scanf("%s", p->people[p->size].sex);
printf("请输入你要添加的联系人的年龄:>");
scanf("%d",&(p->people[p->size].age));
printf("请输入你要添加的联系人的电话号码:>");
scanf("%s", p->people[p->size].phone);
printf("请输入你要添加的联系人的地址:>");
scanf("%s", p->people[p->size].address);
p->size++;
printf("添加成功!\n");
}
void Remove(struct A *p)//删除联系人
{
char name[20];
int i = 0;
printf("请输入您要删除的联系人: >");
scanf("%s", &name);
int ret = search(p, name);
if (ret != -1)
{
for (i = ret; i < p->size-1; i++)
{
p->people[i] = p->people[i + 1];
}
p->size--;
printf("删除成功!\n");
}
else
{
printf("您输入的联系人不存在!\n");
printf("请重新输入!\n");
}
}
void Find(struct A *p)//查询联系人
{
char name[20];
printf("请输入您要查询的联系人:>");
scanf("%s", &name);
int ret = search(p, name);;
if (ret != -1)
{
printf("姓名:%s\n", p->people[ret].name);
printf("性别:%s\n", p->people[ret].sex);
printf("年龄:%d\n", p->people[ret].age);
printf("电话号码:%d\n", p->people[ret].phone);
printf("地址:%s\n", p->people[ret].address);
}
else
{
printf("您所输入的联系人不存在\n");
}
}
int search(struct A *p,char *name)//搜索某一个联系人
{
int i = 0;
for (i = 0; i < p->size; i++)
{
if (strcmp(p->people[i].name,name) == 0)
{
return i;
}
}
return -1;
}
void Modify(struct A *p)
{
char name[20];
printf("请输入您要修改的联系人:>");
scanf("%s", &name);
int ret = search(p, name);
if (ret != -1)
{
printf("请输入年龄:");
scanf("%d", &(p->people[ret].age));
printf("请输入电话:");
scanf("%s", p->people[ret].phone);
printf("请输入地址:");
scanf("%s", p->people[ret].phone);
printf("修改成功!\n");
}
else
{
printf("未找到指定联系人!\n");
}
}
int Empty(struct A *p)
{
char ch = 0;
printf("您确认要清空所有联系人吗?<Y,N>\n");
fflush(stdin);
scanf("%c", &ch);
if (ch == 'Y')
{
p->people = NULL;
p->size = 0;
printf("删除成功!\n");
}
else if (ch == 'N')
{
}
else
{
printf("您输入的指令无效!\n");
}
return 0;
}
void Show(struct A *p)
{
int i = 0;
printf("|--------|---------|---------|-------------|------------|\n");
printf("| 姓名 | 性别 | 年龄 |电话号码 | 地址 |\n");
printf("|--------|---------|---------|-------------|------------|\n");
for (i = 0; i < p->size; i++)
{
printf("|%s\t |%s\t |%d\t |%s\t |%s |\n", p->people[i].name, p->people[i].sex, p->people[i].age, p->people[i].phone, p->people[i].address);
printf("|--------|---------|---------|-------------|------------|\n");
}
}
void Sort(struct A *p)
{
int i, j = 0;
for (i = 0; i < p->size-1; i++)
{
int flag = 0;
for (j = 0; j < p->size - i - 1; j++)
{
if (strcmp((p->people[j].name),(p->people[j + 1].name))<0)
{
struct man tmp = p->people[j];
p->people[j] = p->people[j + 1];
p->people[j + 1] = tmp;
flag = 1;
}
}
if (flag == 0)
{
break;
}
}
Show(p);
}
void Save(struct A *p)
{
FILE *fpw = fopen("AddressList.txt", "wt");
if (fpw == NULL)
{
perror("fopen");
exit(0);
}
int i = 0;
for (i = 0; i < p->size; i++)
{
fprintf(fpw, "%s %s %d %s %s", p->people[p->size].name, \
p->people[p->size].sex, &(p->people[p->size].age), p->people[p->size].phone, p->people[p->size].address);
}
free(p->people);
fclose(fpw);
printf("保存成功!\n");
}
void Load(struct A *p)
{
FILE *fpr = fopen("AddressList.txt", "rt");
if (fpr == NULL)
{
perror("fopen");
exit(1);
}
while (!feof(fpr))
{
fscanf(fpr, "%s %s %d %s %s", p->people[p->siz
e].name, \
p->people[p->size].sex, &(p->people[p->size].age), p->people[p->size].phone, p->people[p->size].address);
p->size++;
}
fclose(fpr);
}
部分功能显示如下: