题目:实现一个文件版本的通讯录,通讯录可以用来存储1000个人的信息,每个人的信息包括:姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人
Contact.h(头文件)
#ifndef __CONTACT_H__
#define __CONTACT_H__
#include
#include
#include
#include
#define DEFAULT_SZ 3
#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 12
#define ADDR_MAX 30
#define MAX 1000
typedef struct PeoInfo
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX ];
char tele[TELE_MAX];
char addr[ADDR_MAX];
}PeoInfo;
//静态的版本
//typedef struct Contact
//{
// PeoInfo data[MAX];//存放数据
// int sz;//当前已有信息个数
//}Contact ,*pContact;
//动态的版本
typedef struct Contact
{
PeoInfo* data;//存放数据
int sz;//当前已有信息个数
int capacity;//当前最大的容量
}Contact, *pContact;
void InitContact(pContact pc);
void DestroyContact(pContact pc);
void AddContact(pContact pc);
void ShowContact(pContact pc);
void DelContact(pContact pc);
void SearchContact(pContact pc);
void ModifyContact(pContact pc);
void SortContact(pContact pc);
void EmptyContact(pContact pc);
void SaveContact(pContact pc);
void LoadContact(pContact pc);
#endif //__CONTACT_H__
Contact.c(实现模块)
#define _CRT_SECURE_NO_WARNINGS 1
#include "Contact.h"
void* check_capacity(pContact pc)
{
assert(pc);
if (pc->sz == pc->capacity)//判断是否增容
{
PeoInfo *tmp = realloc(pc->data, (pc->capacity + 2)*sizeof(PeoInfo));
if (tmp != NULL)
{
pc->data = tmp;
pc->capacity += 2;
printf("增容成功\n");
return pc->data;
}
else
{
return NULL;
}
}
}
void LoadContact(pContact pc)
{
PeoInfo tmp = { 0 };
FILE* pf = fopen("contact.dat", "rb");
int i = 0;
if (pf == NULL)
{
perror("open file for read");
exit(EXIT_FAILURE);
}
//读取--加载内存
while (fread(&tmp, sizeof(PeoInfo), 1, pf))
{
if (check_capacity(pc) != NULL)
{
pc->data[pc->sz] = tmp;
pc->sz++;
}
}
fclose(pf);
pf = NULL;
}
void InitContact(pContact pc)
{
assert(pc);
pc->sz = 0;
pc->data = (PeoInfo*)malloc(3 * sizeof(PeoInfo));
pc->capacity = DEFAULT_SZ;
//加载文件信息
LoadContact(pc);
}
void DestroyContact(pContact pc)
{
assert(pc);
free(pc->data);
pc->data = NULL;
pc->capacity = 0;
pc->sz = 0;
}
void AddContact(pContact pc)
{
assert(pc);
if (check_capacity(pc) == NULL)
{
return;
}
//添加信息
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++;
}
//显示信息
void ShowContact(pContact pc)
{
assert(pc);
int i = 0;
printf("%10s\t%4s\t%5s\t%12s\t%20s\n", "name","age","sex","tele","addr");
for (i = 0; i < pc->sz; i++)
{
printf("%10s\t%4d\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 FindEntry(pContact pc, char name[])
{
int i = 0;
assert(pc);
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}
}
return -1;//没找到
}
void DelContact(pContact pc)
{
assert(pc);
char name[NAME_MAX] = { 0 };
int i = 0;
int j = 0;
int pos = 0;
if (pc->sz == 0)
{
printf("通讯录已空,无法删除\n");
return;
}
//1.查找
printf("请输入要删除人的名字>:");
scanf("%s", name);
pos=FindEntry(pc, name);
//2.删除
if (pos!= -1)
{
for (j = pos; j < pc->sz - 1; j++)
{
pc->data[j] = pc->data[j + 1];
}
pc->sz--;
printf("删除成功\n");
}
else
{
printf("要删除的人不存在\n");
}
}
void SearchContact(pContact pc)
{
assert(pc);
char name[NAME_MAX] = { 0 };
int pos = 0;
printf("请输入要查找的名字>:");
scanf("%s", name);
pos=FindEntry(pc, name);
if (pos == -1)
{
printf("要查找的人不存在\n");
}
else
{
printf("%10s\t%4s\t%5s\t%12s\t%20s\n", "name", "age", "sex", "tele", "addr");
printf("%10s\t%4d\t%5s\t%12s\t%20s\n",
pc->data[pos].name,
pc->data[pos].age,
pc->data[pos].sex,
pc->data[pos].tele,
pc->data[pos].addr);
}
}
void ModifyContact(pContact pc)
{
assert(pc);
char name[NAME_MAX] = { 0 };
int pos = 0;
printf("请输入要修改人的名字>:");
scanf("%s", name);
pos = FindEntry(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);
}
}
void SortContact(pContact pc)
{
assert(pc);
int i = 0;
int j = 0;
for (i = 0; i < pc->sz; i++)
{
int flag = 0;
for (j = 0; j < pc->sz - 1 - i; j++)
{
if(strcmp(pc->data[j].name, pc->data[j + 1].name)>0)
{
PeoInfo tmp = pc->data[j];
pc->data[j] = pc->data[j + 1];
pc->data[j+1] = tmp;
flag = 1;
}
}
if (flag == 0)
{
break;
}
}
}
void EmptyContact(pContact pc)
{
pc->sz = 0;
printf("通讯录已清空\n");
}
void SaveContact(pContact pc)
{
FILE* pf = fopen("contact.dat", "wb");
int i = 0;
if (pf == NULL)
{
perror("open file for write");
exit(EXIT_FAILURE);
}
//写文件
for (i = 0; i < pc->sz; i++)
{
fwrite(pc->data + i, sizeof(PeoInfo), 1, pf);
}
fclose(pf);
pf = NULL;
}
test.c(测试模块)
#define _CRT_SECURE_NO_WARNINGS 1
#include "Contact.h"
void menu()
{
printf("*************************************\n");
printf("**** 1.add 2.del ****\n");
printf("**** 3.search 4.modify ****\n");
printf("**** 5.show 6.sort ****\n");
printf("**** 0.exit 7.empty ****\n");
printf("*************************************\n");
}
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT,
EMPTY
};
void test()
{
Contact con;//通讯录
InitContact(&con);//初始化通讯录
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case ADD://增加
AddContact(&con);
break;
case DEL://删除
DelContact(&con);
break;
case SEARCH://查找
SearchContact(&con);
break;
case MODIFY://修改
ModifyContact(&con);
break;
case SHOW://显示
ShowContact(&con);
break;
case SORT://排序
SortContact(&con);
break;
case EXIT:
SaveContact(&con);
DestroyContact(&con);
printf("退出通讯录\n");
break;
case EMPTY://清空
EmptyContact(&con);
break;
default:
printf("选择错误\n");
}
} while (input);
}
int main()
{
test();
system("pause");
return 0;
}