(增删查改+排序+文件存储)通讯录实现(附源码)

本通讯录具体实现以下功能:
在这里插入图片描述

实现结果如下
在这里插入图片描述
其中排序分为姓名排序和年龄排序
在这里插入图片描述
附源码:

测试部分——

void menu()
{
    
    
	printf("*******************************************\n");
	printf("*******1.添加联系人     2.删除联系人*******\n");
	printf("*******3.查找联系人     4.修改联系人*******\n");
	printf("*******5.查看通讯录     6.排序联系人*******\n");
	printf("***************** 0.退出 ******************\n");
}
int main()
{
    
    
	int input = 0;
	contacts x;//创建通讯录结构体变量x
	Init_contacts(&x);//初始化通讯录
	do {
    
    
		menu();
		printf("请选择: >");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 添加联系人:
			contacts_add(&x);
			break;
		case 删除联系人:
			contacts_delete(&x);
			break;
		case 查找联系人:
			contacts_search(&x);
			break;
		case 修改联系人:
			contacts_modify(&x);
			break;
		case 查看通讯录:
			contacts_show(&x);
			break;
		case 排序联系人:
			contacts_sort(&x);
			break;
		case 退出:
			//销毁通讯录,销毁动态开辟的内存
			Savecontact(&x);
			Destroy(&x);
			printf("退出.");
			break;
		default:
			printf("输入错误数字!\n");
			break;
		}
	} while (input);
	return 0;
}

函数实现部分——

void Checkcapacity(struct contacts* p)
{
    
    
	if (p->capacity <= p->sz)
	{
    
    
		//增加容量
		info* tmp=(info*)realloc(p->x, (p->capacity * 2) * sizeof(info));
		if (tmp != NULL)
		{
    
    
			p->x = tmp;
			p->capacity *= 2;
		}
	}
}
void Readcontact(struct contacts* p)
{
    
    
	info tmp = {
    
     0 };
	FILE* pf = fopen("contacts.data", "rb");
	if (pf == NULL)
	{
    
    
		printf("%s\n", strerror(errno));
		return;
	}
	while (fread(&tmp, sizeof(struct info), 1, pf))
	{
    
    
		Checkcapacity(p);
		p->x[p->sz] = tmp;
		p->sz++;
	}
}
void Init_contacts(struct contacts* p)
{
    
    
	p->x = (info*)malloc(DEFAULT_SZ * sizeof(info));//默认通讯录存放三个联系人
	if (p->x == NULL)
	{
    
    
		return;
	}
	else
	{
    
    
		p->sz = 0;
		p->capacity = DEFAULT_SZ;
	}
	Readcontact(p);
}
void contacts_add(struct contacts* p)
{
    
    
	//检测通讯录容量
	//满了就增加容量
	Checkcapacity(p);
	printf("请输入姓名:>");
	scanf("%s", p->x[p->sz].name);
	printf("请输入年龄:>");
	scanf("%d", &p->x[p->sz].age);
	printf("请输入性别:>");
	scanf("%s", p->x[p->sz].sex);
	printf("请输入电话:>");
	scanf("%s", p->x[p->sz].tele);
	printf("请输入住址:>");
	scanf("%s", p->x[p->sz].address);
	printf("添加成功!\n");
	p->sz++;
}

void contacts_delete(struct contacts*p)
{
    
    
	if (p->sz == 0)
	{
    
    
		printf("无联系人可删除\n");
	}
	else
	{
    
    
		char name[MAX_NAME] = {
    
     0 };
		printf("请输入要删除的联系人姓名:>");
		scanf("%s", name);
		int ret=Findbyname(p, name);//查找联系人
		//删除联系人
		if (ret == -1)
		{
    
    
			printf("查无此联系人\n");
		}
		else
		{
    
    
			/*memmove(&(p->x[i]),&(p->x[i+1]),(p->sz-i)*sizeof(struct contacts));*/
			int j = ret;
			for (j = ret; j < p->sz-1; j++)
			{
    
    
				p->x[j] = p->x[j + 1];
			}
			p->sz--;
		}
	}
}

void contacts_search(struct contacts* p)
{
    
    
	char name[MAX_NAME] = {
    
     0 };
	printf("请输入联系人姓名:>");
	scanf("%s", name);
	int ret = Findbyname(p, name);
	if (ret == -1)
	{
    
    
		printf("查无此人!\n");
	}
	else
	{
    
    
		printf("%-5s\t%-4s\t%-5s\t%-15s\t%-20s\n", "姓名", "年龄", "性别", "电话", "住址");
		printf("%-5s\t%-4d\t%-5s\t%-15s\t%-20s\n",
				p->x[ret].name,
				p->x[ret].age,
				p->x[ret].sex,
				p->x[ret].tele,
				p->x[ret].address
			);
	}
}

void contacts_modify(struct contacts* p)
{
    
    
	char name[MAX_NAME] = {
    
     0 };
	printf("请输入要修改的联系人姓名:>");
	scanf("%s", name);
	int ret = Findbyname(p, name);
	if (ret == -1)
		printf("查无此人\n");
	else
	{
    
    
		printf("请输入姓名:>");
		scanf("%s", p->x[ret].name);
		printf("请输入年龄:>");
		scanf("%d", &p->x[ret].age);
		printf("请输入性别:>");
		scanf("%s", p->x[ret].sex);
		printf("请输入电话:>");
		scanf("%s", p->x[ret].tele);
		printf("请输入住址:>");
		scanf("%s", p->x[ret].address);
		printf("修改成功!\n");
	}
}

void contacts_show(const struct contacts*p)
{
    
    
	if (p->sz == 0)
	{
    
    
		printf("联系人为空!\n");
	}
	else
	{
    
    
		printf("%-5s\t%-4s\t%-5s\t%-15s\t%-20s\n","姓名","年龄","性别","电话","住址");
		int i = 0;
		for (i = 0; i < p->sz; i++)
		{
    
    
			printf("%-5s\t%-4d\t%-5s\t%-15s\t%-20s\n",
				p->x[i].name,
				p->x[i].age,
				p->x[i].sex,
				p->x[i].tele,
				p->x[i].address
			);
		}
	}
}

void contacts_sort(struct contacts* p)
{
    
    
	int input = 0;
	printf("请选择排序方式:>\n");
	printf("1.姓名    2.年龄\n");
	scanf("%d", &input);
	switch (input)
	{
    
    
	case 1:
		qsort(p->x, p->sz,sizeof(info), name_cmp);
		break;
	case 2:
		qsort(p->x, p->sz, sizeof(info), age_cmp);
		break;
	default:
		printf("选择错误\n");
		break;
	}
}
int name_cmp(const void* e1,const void*e2)
{
    
    
	return strcmp(((info*)e1)->name, ((info*)e2)->name);
}
int age_cmp(const void* e1, const void* e2)
{
    
    
	return ((info*)e1)->age - ((info*)e2)->age;
}
void Savecontact(struct contacts* p)
{
    
    
	FILE* pf = fopen("contacts.data", "wb");
	if (pf == NULL)
	{
    
    
		printf("%s\n", strerror(errno));
		return;
	}
	int i = 0;
	for (i = 0; i < p->sz; i++)
	{
    
    
		fwrite(&(p->x[i]),sizeof(struct info),1,pf);
	}
	fclose(pf);
	pf = NULL;
}
void Destroy(struct contacts* p)
{
    
    
	free(p->x);
	p->x = NULL;
}
static int Findbyname(const struct contacts* p, char* name)
{
    
    
	int i = 0;
	for (i = 0; i < p->sz; i++)
	{
    
    
		if (strcmp(p->x[i].name, name) == 0)
		{
    
    
			return i;
		}
	}
	if (i == p->sz)
	{
    
    
		return -1;
	}
}

头文件部分——

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

#define DEFAULT_SZ 3
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 15
#define MAX_ADDRESS 30

enum menu
{
    
    
	退出,
	添加联系人,
	删除联系人,
	查找联系人,
	修改联系人,
	查看通讯录,
	排序联系人
};

typedef struct info {
    
    
	char name   [MAX_NAME]   ;
	int  age                 ;
	char sex    [MAX_SEX]    ;
	char tele   [MAX_TELE]   ;
	char address[MAX_ADDRESS];
}info;

typedef struct contacts {
    
    
	info* x;
	int capacity;//当前通讯录的最大容量
	int sz;//记录当前已有联系人数
}contacts;

void Init_contacts(struct contacts* p);
void contacts_add(struct contacts* p);
void contacts_delete(struct contacts* p);
void contacts_search(struct contacts* p);
void contacts_modify(struct contacts* p);
void contacts_show(const struct contacts* p);
void contacts_sort(struct contacts* p);
void Destroy(struct contacts* p);
void Savecontact(struct contacts* p);
int name_cmp(const void* e1, const void* e2);
int age_cmp(const void* e1, const void* e2)

//作者定期分享C语言学习路上的经验,欢迎关注哦

猜你喜欢

转载自blog.csdn.net/Wyf_Fj/article/details/114165101