通讯录

151 阅读4分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第10天,点击查看活动详情


  • 🔽

本通讯录用来录入,查找,删除,修改,显示,排序等功能。 暂定这个通讯录的人包括姓名,年龄,性别,电话,住址。 依然是分模块实现 ==⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇== 点击获取通讯录源码

声明包含人的各种信息的结构体类型

  • 姓名,性别,年龄,电话,地址

#define name_max 15
#define sex_max 5
#define tele_max 12
#define address_max 30

typedef struct people
{
	char name[name_max];
	char sex[sex_max];
	int age;
	char tele[tele_max];
	char address[address_max];
}people;

声明包含所有人的信息的结构体类型

静态版本

先预设最大可以储存1000个人,count表示通讯录里面现有的人。

#define human_max 1000
typedef struct contacts
{
	people human[human_max];
	int count;
}contacts;

动态版本

typedef struct contacts
{
	people* human;
	int count;
	int max;
}contacts;

创建选项菜单

void menu()
{
	printf("############################################\n");
	printf("######          1.录入  2.查找     #########\n");
	printf("######          3.删除  4.修改     #########\n");
	printf("######          5.排序  6.显示     #########\n");
	printf("###########         0.退出     #############\n");
	printf("############################################\n");
}

用户进行选择

enum select
{
	quit,
	add,
	find,
	delete,
	change,
	sort,
	print
};
void test()
{
	int n;
	contacts con;
	do
	{
		menu();
		printf("请选择你的操作->");
		scanf("%d", &n);
		switch (n)
		{
		case quit:
			printf("退出程序\n");
			break;
			/*
		case quit:
			printf("退出程序\n");
			free(con.human);
			con.human = NULL;
			break;	动态版本修改部分
			*/				
		case add://添加
			break;
		case find://查找
			break;
		case delete://删除
			break;
		case change://修改
			break;
		case sort://排序
			break;
		case print://打印
			break;
		default:
			printf("操作非法,请重新选择\n");
			break;
		}
	} while (n);
}

初始化通讯录

静态版

void init_contacts(contacts* p)
{
	p->count = 0;
	memset(p->human, 0, sizeof(p->human));
}

动态版本

先预设可以存3个人的信息

void init_contacts(contacts* p)
{
	p->human = (people*)malloc(sizeof(people) * 3);
	if (p->human == NULL)
	{
		printf("初始化失败%s", strerror(errno));
		return;
	}
	p->count = 0;
	p->max = 3;
	memset(p->human, 0, p->max*sizeof(p->human));
}

文件版本

void init_contacts(contacts* p)
{
	p->human = (people*)malloc(sizeof(people) * 3);
	if (p->human == NULL)
	{
		printf("初始化失败%s", strerror(errno));
		return;
	}
	p->count = 0;
	p->max = 3;
	memset(p->human, 0, p->max*sizeof(p->human));
	add_store(p);
}
void add_store(contacts* p)
{
	FILE* s = fopen("通讯录.txt", "rb");
	if (s == NULL)
	{
		printf("文件信息读入出错%s", strerror(errno));
		return;
	}
	people temp;
	while (fread(&temp, sizeof(people), 1, s))
	{
		p->human[p->count] = temp;
		p->count++;
		if (p->count == p->max)
			enlarge(p);
	}
	fclose(s);
	s = NULL;
}

扩容

void enlarge(contacts* p)
{
	p->max += 3;
	people* temp=(people*)realloc(p->human,sizeof(people) * p->max);
	if (temp == NULL)
	{
		printf("%s\n", strerror(errno));
		return;
	}
	p->human = temp;
	
}

保存到文件中

void store(const contacts* p)
{
	FILE* s = fopen("通讯录.txt", "wb");
	if (s == NULL)
	{
		printf("文件信息读入出错%s", strerror(errno));
		return;
	}
	int i = 0;
	for (i = 0; i < p->count; i++)
	{
		fwrite(p->human+i, sizeof(people), 1, s);
	}
	fclose(s);
	s = NULL;
}

显示通讯录里面的信息

void print_contacts(const contacts* p)
{
	printf("%-15s%-5s%-5s%-15s%-30s\n", "姓名", "性别", "年龄", "电话", "地址");
	int i = 0;
	for (i = 0; i < p->count; i++)
	{
		printf("%-15s%-5s%-5d%-15s%-30s\n", p->human[i].name, p->human[i].sex, p->human[i].age, p->human[i].tele, p->human[i].address);
	}
}

录入人的信息

void add_contacts(contacts* p)
{
	if (p->count == human_max)
	{
		printf("通讯录已满");
		return;
	}
	/*
	if (p->count == p->max)
	{
		enlarge(p);
	}动态版修改部分
	*/
	printf("请输入人的姓名->");
	scanf("%s", p->human[p->count].name);
	printf("请输入人的性别->");
	scanf("%s", p->human[p->count].sex);
	printf("请输入人的年龄->");
	scanf("%d", &p->human[p->count].age);
	printf("请输入人的电话->");
	scanf("%s", p->human[p->count].tele);
	printf("请输入人的地址->");
	scanf("%s", p->human[p->count].address);
	p->count++;
	printf("录入成功\n");
}

查找人姓名的函数

找到返回对应的人的下标 找不到返回-1

int find_people(char* name_x,const contacts* p)
{
	int i = 0;
	for (i=0;i<p->count;i++)
	{
		if (strcmp(name_x, p->human[i].name) == 0)
			return i;
	}
	return -1;
}

查找人的信息

void find_contacts(const contacts* p)
{
	char name_x[name_max];
	printf("请输入人的姓名->");
	scanf("%s", name_x);
	int ret=find_people(name_x, p);
	if (ret == -1)
		printf("没有查找到该人\n");
	else
	{
		printf("%-15s%-5s%-5s%-15s%-30s\n", "姓名", "性别", "年龄", "电话", "地址");
		printf("%-15s%-5s%-5d%-15s%-30s\n", p->human[ret].name, p->human[ret].sex, p->human[ret].age,
			p->human[ret].tele, p->human[ret].address);
	}
}

删除人的信息

void delete_contacts(contacts* p)
{
	char name_x[name_max];
	printf("请输入人的姓名->");
	scanf("%s", name_x);
	int ret = find_people(name_x, p);
	if (ret == -1)
		printf("没有查找到该人\n");
	else
	{
		int i;
		for (i = ret; i < p->count-1; i++)
		{
			p->human[i] = p->human[i + 1];
		}
		p->count--;
		printf("删除成功\n");
	}
}

修改人的信息

void change_contacts(contacts* p)
{
	char name_x[name_max];
	printf("请输入人的姓名->");
	scanf("%s", name_x);
	int ret = find_people(name_x, p);
	if (ret == -1)
		printf("没有查找到该人\n");
	else
	{
		printf("请输入人的姓名->");
		scanf("%s", p->human[ret].name);
		printf("请输入人的性别->");
		scanf("%s", p->human[ret].sex);
		printf("请输入人的年龄->");
		scanf("%d", &p->human[ret].age);
		printf("请输入人的电话->");
		scanf("%s", p->human[ret].tele);
		printf("请输入人的地址->");
		scanf("%s", p->human[ret].address);
		printf("修改成功\n");
	}
}

按名字排序

int cmp_name(const void* e1, const void* e2)
{
	return strcmp(((people*)e1)->name,  ((people*)e2)->name);
}

void sort_contacts(contacts* p)
{
	qsort(p->human, p->count, sizeof(p->human[0]), cmp_name);
	printf("排序成功\n");
}