C语言实现通讯录(文件操作)

通讯录实现功能

实现一个通讯录;

通讯录可以用来存储1000个人的信息,每个人的信息包括:姓名、性别、年龄、电话、住址

提供方法:

1.添加联系人信息
2.删除指定联系人信息
3.查找指定联系人信息
4.修改指定联系人信息
5.显示所有联系人信息
6.清空所有联系人
7.以名字排序所有联系人
8. 添加一个函数,在退出通讯录的时候把信息到保存到文件中
9. 添加一个函数,在通讯录打开的时候,可以把文件中的信息加载到通讯录中

过程分析

数据结构

存储一个学生的信息,需要用到结构体。
现在需要存储任意人数的信息,由于人数不确定,所以采用柔性数组来实现空间的动态分配。

步骤

1.创建需要用到的数据结构
2.编写初始化空间的函数
3.依次编写每个功能模块
4.汇总,在主函数中调用

代码展示

此次编程采用多文件的方式,尽可能使代码简化。读者耐心阅读肯定可以看懂。
不再废话,直接上代码

contact.h

#include <stdio.h>
#include <windows.h>
#include <assert.h>
#include <stdlib.h>

#pragma warning(disable:4996)

#define DFL_NUM 2
#define INC_NUM 2
#define MY_FILE "my_file.bin"

typedef struct person{
	char name[32];
	char sex[4];
	int age;
	char telphone[16];
	char address[64];
}person_t;

typedef struct contact{
	int cap;//当前容量总数
	int size;//当前的实际人数
	person_t persons[0];//柔性数组
}contact_t;

extern void InitContact(contact_t **ctp);
extern void DestroyContact(contact_t **ctp);
extern void AddPerson(contact_t **ctp);
extern void  PrintContact(contact_t *ct);
extern void SearchPerson(contact_t *ct);
extern void DeletePerson(contact_t *ct);
extern void ModPerson(contact_t *ct);
extern void ClearContact(contact_t *ct);
extern void SortContact(contact_t *ct);
extern void My_File(contact_t *ct);

contact.c

#include "contact.h"

void InitContact(contact_t **ctp)
{
	FILE *fp = fopen(MY_FILE,"rb");
	if (NULL == fp)
	{
		//默认初始化
		*ctp = (contact_t*)malloc(sizeof(contact_t)+sizeof(person_t)*DFL_NUM);
		if (NULL == *ctp)
		{
			perror("malloc");
			exit(1);
		}
		(*ctp)->cap = DFL_NUM;
		(*ctp)->size = 0;
		printf("Default contact init..... done\n");
	}
	else
	{
		contact_t t;
		fread(&t,sizeof(contact_t),1,fp);
		*ctp = (contact_t *)malloc(sizeof(contact_t)+sizeof(person_t)*t.cap);
		if (NULL == *ctp)
		{
			perror("malloc");
			exit(2);
		}
		fread((*ctp)->persons, sizeof(person_t), t.size, fp);
		(*ctp)->cap = t.cap;
		(*ctp)->size = t.size;
		fclose(fp);
		printf("File contact init ... done\n");
	}

	
}

void DestroyContact(contact_t **ctp)
{
	free(*ctp);
	*ctp = NULL;
	printf("Destroy ccontact ....done\n");
}
static int Inc(contact_t **ctp)//返回值 1扩容成功  0失败
{
	contact_t *temp = NULL;
	temp = (contact_t *)realloc(*ctp, sizeof(contact_t)+sizeof(person_t)*((*ctp)->cap + INC_NUM));
	if (NULL == temp)
	{
		perror("realloc");
		return 0;
	}
	temp->cap = (*ctp)->cap + INC_NUM;
	temp->size = (*ctp)->size;
	*ctp = temp;
	printf("扩容成功!cap:%d  size:%d\n",temp->cap,temp->size);
	return 1;
}

static int isFull(contact_t *ct)// 返回值 1满了 0没满
{
	return (ct->cap == ct->size);
}

static void PrintContactHelper(person_t *p)
{
	printf("|%-5s|%-5s|%-2d|%10s|%10s|\n",p->name,p->sex,p->age,p->telphone,p->address);
}

static int FindPerson(const contact_t *ct,const char *name)
{
	const person_t *p = ct->persons;
	for (int i = 0; i < ct->size; i++)
	{
		if (strcmp(name, (p + i)->name) == 0)
		{
			return i;
		}
	}
	return -1;
}

static int CompareName(const void *xp,const void *yp)// 0 相等 1 前者大 2后者大
{
	const person_t *p = (const person_t *)xp;
	const person_t *q = (const person_t *)yp;
	return strcmp(p->name, q->name);
}
void AddPerson(contact_t **ctp)
{ 
	//是否可以添加??--->1.没满直接添加  2.满了,扩容成功后添加
	if (!isFull(*ctp) || Inc(ctp))
	{
		contact_t *ct = *ctp;
		person_t *p = ct->persons + ct->size;
		printf("请输入联系人姓名#");
		scanf("%s",p->name);
		int index = FindPerson(ct, p->name);
		if (index != -1)
		{
			printf("此联系人已经存在!\n");
			return;
		}
		printf("请输入联系人性别#");
		scanf("%s",p->sex);
		printf("请输入联系人年龄#");
		scanf("%d",&(p->age));
		printf("请输入联系人电话#");
		scanf("%s",p->telphone);
		printf("请输入联系人地址#");
		scanf("%s",p->address);
		ct->size++;
	}
}

void  PrintContact(contact_t *ct)
{
	for (int i = 0; i < ct->size; i++)
	{
		PrintContactHelper(ct->persons+i);
	}

}

void DeletePerson(contact_t *ct)
{
	//1.判断是否可以找到这个人
	char name[64];
	printf("请输入要删除人的姓名#");
	scanf("%s", name);
	int index = FindPerson(ct, name);
	if (index != -1)//找到此人,开始删除
	{
		memmove(ct->persons+index,ct->persons+ct->size-1,sizeof(person_t));
		ct->size--;
		printf("[%s] 删除成功!\n",name);
	}
	else//此人不存在,无法删除
	{
		printf("[%s]不存在,无法删除!", name);
		return;
	}

}

void SearchPerson(contact_t *ct)
{
	char name[64];
	printf("请输入要查找人的姓名#");
	scanf("%s",name);
	int index = FindPerson(ct,name);
	if (index != -1)
	{
		PrintContactHelper(ct->persons + index);
	}
	else
	{
		printf("[%s] 不存在!\n",name);
	}
}

void ModPerson(contact_t *ct)
{
	char name[64];
	printf("请输入要查找人的姓名#");
	scanf("%s", name);
	int index = FindPerson(ct, name);
	if (index != -1)//存在,修改信息
	{
		person_t *p = ct->persons + index;
		printf("请输入联系人姓名#");
		scanf("%s", p->name);
		printf("请输入联系人性别#");
		scanf("%s", p->sex);
		printf("请输入联系人年龄#");
		scanf("%d", &(p->age));
		printf("请输入联系人电话#");
		scanf("%s", p->telphone);
		printf("请输入联系人地址#");
		scanf("%s", p->address);
	}
	else
	{
		printf("[%s] 不存在,无法修改!\n", name);
	}
}

void ClearContact(contact_t *ct)
{
	ct->size = 0;
	printf("Clear contact...done\n");
}

void SortContact(contact_t *ct)
{
	qsort(ct->persons, ct->size, sizeof(person_t), CompareName);
}

void My_File(contact_t *ct)
{
	FILE *fp = fopen(MY_FILE,"wb");
	if (NULL == fp)
	{
		perror("fopen");
		return;
	}
	fwrite(ct,sizeof(contact_t),1,fp);
	fwrite(ct->persons, sizeof(person_t), ct->size, fp);
	fclose(fp);
}

main.c

#include "contact.h"

void menu()
{
	printf("#################################\n");
	printf("##    1.Add         2.Delete   ##\n");
	printf("##    3.Search      4.Modify   ##\n");
	printf("##    5.Show        6.Clear    ##\n");
	printf("##    7.Sort        0.Exit     ##\n");
	printf("#################################\n");
}


int main()
{
	contact_t *ct = NULL;
	InitContact(&ct);
	int quit = 0;
	int select = 0;
	while (!quit)
	{
		menu();
		printf("Please enter your choose#");
		scanf("%d", &select);
		switch (select)
		{
		case 1:
			AddPerson(&ct);
			break;
		case 2:
			DeletePerson(ct);
			break;
		case 3:
			SearchPerson(ct);
			break;
		case 4:
			ModPerson(ct);
			break;
		case 5:
			PrintContact(ct);
			break;
		case 6:
			ClearContact(ct);
			break;
		case 7:
			SortContact(ct);
			break;
		case 0:
			quit = 1;
			My_File(ct);
			break;
		default:
			printf("输入有误,请重新输入!\n");
			break;
		}
	}

	DestroyContact(&ct);
	system("pause");
	return 0;
}

读者可以copy到自己的环境下测试~~

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Suk-god

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值