在之前我们已经写过静态通讯录和动态通讯录了,但是也可以发现它们两个的通病,就是每次写完联系人信息不能保存,退出程序就消失了,下次再打开也没有了。而我们这次主要就是解决这个问题。
文件版本通讯录
一、test.c(通讯录主干)
1.通讯录菜单的实现
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 ************************\n");
printf("**********************************************\n");
printf("**********************************************\n");
}
2.创建通讯录,初始化通讯录
struct Contact con;
InitContact(&con);
3.通讯录功能的调用
int main()
{
int input = 0;
struct Contact con;
InitContact(&con);
do
{
menu();
printf("请选择:");
scanf("%d",&input);
switch (input)
{
case 1:
AddContact(&con);
break;
case 2:
DelContact(&con);
break;
case 3:
SearchContact(&con);
break;
case 4:
ModifyContact(&con);
break;
case 5:
ShowContact(&con);
break;
case 6:
SortContact(&con);
break;
case 0:
SaveContact(&con);
DestroyContact(&con);
printf("退出程序\n");
break;
default:
printf("输入错误请从新输入:\n");
break;
}
} while (input);
return 0;
}
二、contact.c(函数的实现)
1.通讯录初始化
static int check_capacity(struct Contact* pc);
void LoadContact(struct Contact* pc)
{
FILE* pfr = fopen("test.txt","rb");
if (pfr == NULL)
{
perror("LoadContact:fopen");
return;
}
struct PeoInfo tmp = {0};
while (fread(&tmp, sizeof(struct PeoInfo), 1, pfr))
{
check_capacity(pc);
pc->data[pc->sz] = tmp;
pc->sz++;
}
fclose(pfr);
pfr = NULL;
}
void InitContact(struct Contact* pc)
{
assert(pc);
pc->sz = 0;
pc->Capacity = DEFAULT_SZ;
pc->data = (struct PeoInfo*)malloc(3 * sizeof(struct PeoInfo));
if (pc->data == NULL)
{
perror("InitContact()");
return;
}
LoadContact(pc);
}
static int check_capacity(struct Contact* pc)
{
if (pc->sz == pc->Capacity)
{
struct PeoInfo* ptr=(struct PeoInfo*)realloc(pc->data, (pc->Capacity + INC_SZ) * sizeof(struct PeoInfo));
if (ptr != NULL)
{
pc->data = ptr;
pc->Capacity += INC_SZ;
printf("增容成功\n");
return 1;
}
}
else
{
return 1;
}
}
2.查看联系人是否存在函数实现
static int FindByName(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;
}
3.单个修改联系人各项的信息函数实现
static void name2(struct Contact* pc, int ret)
{
printf("请写入新的信息\n");
scanf("%s",pc->data[ret].name);
}
static void sex2(struct Contact* pc, int ret)
{
printf("请写入新的信息\n");
scanf("%s", pc->data[ret].sex);
}
static void age2(struct Contact* pc, int ret)
{
printf("请写入新的信息\n");
scanf("%s", pc->data[ret].age);
}
static void tele2(struct Contact* pc, int ret)
{
printf("请写入新的信息\n");
scanf("%s", pc->data[ret].tele);
}
static void addr2(struct Contact* pc, int ret)
{
printf("请写入新的信息\n");
scanf("%s", pc->data[ret].addr);
}
4.修改联系人信息目录
void menu2()
{
printf("**********************************************\n");
printf("*********** 1. name 2. sex *******\n");
printf("*********** 3.age 4.tele *******\n");
printf("*********** 5.addr *********************\n");
printf("*********** 0.exit ************************\n");
printf("**********************************************\n");
printf("**********************************************\n");
}
5.通讯录功能函数的实现
(1)添加联系人
static int check_capacity(struct Contact* pc)
{
if (pc->sz == pc->Capacity)
{
struct PeoInfo* ptr=(struct PeoInfo*)realloc(pc->data, (pc->Capacity + INC_SZ) * sizeof(struct PeoInfo));
if (ptr != NULL)
{
pc->data = ptr;
pc->Capacity += INC_SZ;
printf("增容成功\n");
return 1;
}
}
else
{
return 1;
}
}
void AddContact(struct Contact* pc)
{
assert(pc);
if (0 == check_capacity(pc))
{
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].tele);
printf("请输入地址:>");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
printf("成功增加联系人\n");
}
(2)删除联系人
void DelContact(struct Contact* pc)
{
char name[MAX_NAME];
printf("请输入要删除的名字:");
scanf("%s",name);
int ret = FindByName(pc,name);
if (ret == -1)
{
printf("找不到该联系人\n");
}
else
{
int j = 0;
for (j = ret; j < pc->sz - 1; j++)
{
pc->data[j] = pc->data[j + 1];
}
pc->sz--;
printf("成功删除指定联系人\n");
}
}
(3)查找联系人
void SearchContact(const struct Contact* pc)
{
char name[MAX_NAME];
printf("请输入要查找的人的名字:>");
scanf("%s", name);
int ret = FindByName(pc, name);
if (ret == -1)
printf("要查找的人不存在\n");
else
{
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", "姓名", "性别", "年龄", "电话", "地址");
printf("%-20s\t%-5s\t%-5d\t%-12s\t%-30s\n",
pc->data[ret].name,
pc->data[ret].sex,
pc->data[ret].age,
pc->data[ret].tele,
pc->data[ret].addr);
}
}
(4)修改联系人
void ModifyContact(struct Contact* pc)
{
printf("请输入要修改人的名字:>\n");
char name[MAX_NAME];
scanf("%s", name);
int ret = FindByName(pc, name);
if (ret == -1)
printf("要修改的人不存在\n");
else
{
int input2 = 0;
printf("请输入你要修改的内容:");
menu2();
scanf("%d",&input2);
switch (input2)
{
case 1:
name2(pc,ret);
break;
case 2:
sex2(pc,ret);
break;
case 3:
age2(pc, ret);
break;
case 4:
tele2(pc, ret);
case 5:
addr2(pc, ret);
case 0:
break;
default:
break;
}
printf("修改成功\n");
}
}
(5)展示联系人信息
void ShowContact(const struct Contact* pc)
{
int i = 0;
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", "姓名", "性别", "年龄", "电话", "地址");
for (i = 0; i < pc->sz; i++)
{
printf("%-20s\t%-5s\t%-5d\t%-12s\t%-30s\n", pc->data[i].name,
pc->data[i].sex,
pc->data[i].age,
pc->data[i].tele,
pc->data[i].addr);
}
}
(6)对联系人进行排序
int CmpByName(const void* e1, const void* e2)
{
return strcmp(((struct PeoInfo*)e1)->name, ((struct PeoInfo*)e2)->name);
}
void SortContact(struct Contact* pc)
{
qsort(pc->data, pc->sz, sizeof(struct PeoInfo), CmpByName);
}
(7)退出程序保存信息,释放动态开辟内存空间
void DestroyContact(struct Contact* pc)
{
free(pc->data);
pc->data = NULL;
pc->Capacity = 0;
pc->sz = 0;
}
void SaveContact(struct Contact* pc)
{
FILE* pf = fopen("test.txt","wb");
if (pf == NULL)
{
perror("SaveContact:fopen");
return;
}
for (int i = 0; i < pc->sz; i++)
{
fwrite(pc->data+i,sizeof(struct PeoInfo),1,pf);
}
printf("保存信息成功");
fclose(pf);
pf = NULL;
}
三、contact.h(头文件)
1.函数的声明
void InitContact(struct Contact* pc);
void AddContact(struct Contact* pc);
void DelContact(struct Contact* pc);
void SearchContact(const struct Contact* pc);
void ModifyContact(struct Contact* pc);
void ShowContact(const struct Contact* pc);
void SortContact(struct Contact* pc);
void DestroyContact(struct Contact* pc);
void SaveContact(struct Contact* pc);
2.全局变量的定义
#define MAX 100
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 12
#define MAX_ADDR 30
#define DEFAULT_SZ 3
#define INC_SZ 2
3.头文件
#pragma once
#include<stdio.h>
#include<string.h>
#include <assert.h>
#include <stdlib.h>
4.结构体的创建
struct PeoInfo
{
char name[MAX_NAME];
char sex[MAX_SEX];
char tele[MAX_TELE];
int age;
char addr[MAX_ADDR];
};
struct Contact
{
struct PeoInfo* data;
int sz;
int Capacity;
};
最后:文章有什么不对的地方或者有什么更好的写法欢迎大家在评论区指出
|