【C语言】模拟通讯录(数组版、动态版、链表版)

目录

设计要求

实现框图

数组版

        1.state.h

        需要包含的头文件及定义

        定义的枚举类型以及结构体

        函数声明

        2.frame.c

        菜单

        main函数

        3.function.c

        初始化函数

        查找信息函数

        增加联系人函数

        删除联系人函数

        查找指定联系人函数

        更改联系人信息函数

        联系人列表函数

        按序整理函数

动态版

        state.h

        funtion.c

        初始化函数

        动态开辟函数

        frame.c

链表版

        frame.c

        菜单

        主函数

        function.c

        查找信息函数

        增加联系人函数

        删除联系人函数

        查找指定联系人函数

        更改联系人信息函数

        查看联系人列表函数

        排序函数

        state.h

        包含的头文件和定义

        枚举类型和结构体



设计要求

c语言模拟实现通讯录:😶‍🌫️

要求:用结构体建立一个通讯录,通讯录内容包括姓名、年龄、电话、地址,实现以下功能

❤️功能1:增加联系人

❤️功能2:减少联系人

❤️功能3:查找指定联系人信息

❤️功能4:修改指定联系人信息

❤️功能5:查看通讯录列表

❤️功能6:排序


实现框图

一起实现~

加油~


数组版

1.state.h

需要包含的头文件及定义

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

#define DATA_SZ 1000
#define NAME_MAX 20
#define TELE_MAX 20
#define ADDRESS_MAX 30

定义的枚举类型以及结构体

enum choose
{
	EXIT,
	ADD,
	SUB,
	SREACH,
	MODIFY,
	LIST,
	SORT
};

typedef struct content
{
	char name[NAME_MAX];
	int age;
	char telephone[TELE_MAX];
	char address[ADDRESS_MAX];
}content;

typedef struct address_book
{
	content data [DATA_SZ];
	int sz;
}address_book;

函数声明

//初始化通讯录
void Init_book(address_book* book);

//增加联系人
void Add_contacts(address_book* book);

//删除联系人
void Sub_contacts(address_book* book);

//查找指定联系人
void Find_contacts(address_book* book);

//更改联系人信息
void Modify_contacts(address_book* book);

//查看列表
void List_contacts(const address_book* book);

//排序
void Bubble_sort(address_book* book);

2.frame.c

菜单

void Menu()
{
	printf("*****************************************\n");
	printf("*****************************************\n");
	printf("********    1.Add     2.Sub     *********\n");
	printf("********    3.Sreach  4.modify  *********\n");
	printf("********    5.list    6.sort    *********\n");
	printf("********    0.exit              *********\n");
	printf("*****************************************\n");
}

main函数

int main()
{
	int input = 0;
	//创建通讯录
	address_book book;
	//初始化通讯录
	Init_book(&book);
	do
	{
		Menu();
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			Add_contacts(&book);
			break;
		case SUB:
			Sub_contacts(&book);
			break;
		case SREACH:
			Find_contacts(&book);
			break;
		case MODIFY:
			Modify_contacts(&book);
			break;
		case LIST:
			List_contacts(&book);
			break;
		case SORT:
			Bubble_sort(&book);
			break;
		case EXIT:
			printf("退出程序!\n");
			break;
		default:
			printf("输入非法,请重新输入!\n");
			break;
		}
	} while (input);
	return 0;
}

3.function.c

初始化函数

//初始化
void Init_book(address_book* book)
{
	memset(book->data, 0, sizeof(book->data));
	book->sz = 0;
}

查找信息函数

//查找信息
int Find(const address_book* book, const char* input)
{
	assert(book);
	int i = 0;
	for (i = 0; i < book->sz; i++)
	{
		if (strcmp(book->data[i].name, input) == 0)
		{
			return i;
		}
	}
	return -1;
}

增加联系人函数

//增加联系人
void Add_contacts(address_book* book)
{
	system("cls");
	assert(book);
	if (book->sz == DATA_SZ)
	{
		printf("通讯录已满,不可继续添加!\n");
	}
	else
	{
		printf("请输入联系人名字:");
		scanf("%s", book->data[book->sz].name);
		printf("请输入联系人年龄:");
		scanf("%d", &(book->data[book->sz].age));
		printf("请输入联系人电话:");
		scanf("%s", book->data[book->sz].telephone);
		printf("请输入联系人住址:");
		scanf("%s", book->data[book->sz].address);
		book->sz++;
		printf("添加成功!\n");
	}
	system("pause");
	system("cls");
}

删除联系人函数

//删除联系人
void Sub_contacts(address_book* book)
{
	system("cls");
	assert(book);
	if (book->sz == 0)
	{
		printf("联系人为空\n");
	}
	else
	{
		char input[NAME_MAX] = { 0 };
		printf("请输入需要删除联系人的人名:");
		scanf("%s", input);
		int ret = Find(book, input);//查找并对比信息
		if (ret != -1)
		{
			int i = 0;
			for (i = ret; i < book->sz - 1; i++)
			{
				book->data[i].age = book->data[i + 1].age;
				strcpy(book->data[i].name, book->data[i + 1].name);
				strcpy(book->data[i].telephone, book->data[i + 1].telephone);
				strcpy(book->data[i].address, book->data[i + 1].address);
			}
			book->sz--;
			printf("删除成功!\n");
		}
		else
		{
			printf("未能到此联系人!\n");
		}
	}
	system("pause");
	system("cls");
}

查找指定联系人函数

//查找指定联系人
void Find_contacts(address_book* book)
{
	system("cls");
	assert(book);
	if (book->sz == 0)
	{
		printf("联系人为空");
	}
	else
	{
		char input[NAME_MAX] = { 0 };
		printf("请输入姓名:");
		scanf("%s", input);
		int ret = Find(book, input);//查找并对比信息
		if (ret != -1)
		{
			printf("*****************************************\n");
			printf("%-6s\t%-4d\t%-12s\t%-10s\n",
				book->data[ret].name,
				book->data[ret].age,
				book->data[ret].telephone,
				book->data[ret].address);
			printf("*****************************************\n");
		}
		else
		{
			printf("未能到此联系人!\n");
		}
	}
	system("pause");
	system("cls");
}

更改联系人信息函数

//更改联系人信息
void Modify_contacts(address_book* book)
{
	system("cls");
	assert(book);
	if (book->sz == 0)
	{
		printf("联系人为空");
	}
	else
	{
		char input[NAME_MAX] = { 0 };
		printf("请输入要被更改信息的姓名:");
		scanf("%s", input);
		int ret = Find(book, input);//查找并对比信息
		if (ret != -1)
		{
			printf("请输入联系人名字:");
			scanf("%s", book->data[ret].name);
			printf("请输入联系人年龄:");
			scanf("%d", &(book->data[ret].age));
			printf("请输入联系人电话:");
			scanf("%s", book->data[ret].telephone);
			printf("请输入联系人住址:");
			scanf("%s", book->data[ret].address);
			printf("更改成功!\n");
		}
		else
		{
			printf("未能到此联系人!\n");
		}
	}
	system("pause");
	system("cls");
}

联系人列表函数

//联系人列表
void List_contacts(const address_book* book)
{
	system("cls");
	assert(book);
	printf("*****************************************\n");
	printf("%-6s\t%-4s\t%-12s\t%-10s\n", "名字", "年龄", "电话", "住址");
	int i = 0;
	for (i = 0; i < book->sz; i++)
	{
		printf("%-6s\t%-4d\t%-12s\t%-10s\n",
			book->data[i].name,
			book->data[i].age,
			book->data[i].telephone,
			book->data[i].address);
	}
	printf("*****************************************\n");
	system("pause");
	system("cls");
}

按序整理函数

//按序整理
void Bubble_sort(address_book* book)
{
	system("cls");
	assert(book);
	if (book->sz == 0)
	{
		printf("联系人为空!\n");
	}
	else
	{
		
		int i = 0;
		for (i = 0; i < book->sz - 1; i++)
		{
			int j = 0;
			for (j = 0; j < book->sz - i - 1; j++)
			{	//按名字升序
				if (strcmp(book->data[j].name, book->data[j + 1].name) > 0)
				{
					content tmp = book->data[j];
					book->data[j] = book->data[j + 1];
					book->data[j + 1] = tmp;
				}
			}
		}
		printf("整理完成!\n");
	}
	system("pause");
	system("cls");
}

动态版

无需大动干戈,只需要修改以下几个函数即可~

state.h

typedef struct address_book
{
	content* data;
	int sz;
	int capacity;
}address_book;

funtion.c

初始化函数

//初始化
void Init_book(address_book* book)
{
	book->data = (content*)malloc(sizeof(content));
	if (book->data == NULL)
	{
		printf("%s", strerror(errno));
		return;
	}
	memset(book->data, 0, sizeof(content));
	book->sz = 0;
	book->capacity = 1;
}

动态开辟函数

//动态开辟
void Add_space(address_book* book)
{
	book->data = (content*)realloc(book->data, sizeof(content) * (book->capacity + 1));
	if (book->data == NULL)
	{
		printf("%s\n", strerror(errno));
		return;
	}
	memset(book->data + book->sz, 0, sizeof(content));
	book->capacity++;
}

frame.c

无需修改~


链表版

可谓是大换血啊~

frame.c

菜单

void Menu()
{
	printf("*****************************************\n");
	printf("*****************************************\n");
	printf("********    1.Add     2.Sub     *********\n");
	printf("********    3.Sreach  4.modify  *********\n");
	printf("********    5.list    6.sort    *********\n");
	printf("********    0.exit              *********\n");
	printf("*****************************************\n");
}

主函数

int main()
{
	int input = 0;
	//初始化通讯录
	address_book* head = NULL;
	address_book* tail = NULL;
	do
	{
		Menu();
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			Add_contacts(&head, &tail);
			break;
		case SUB:
			Sub_contacts(&head, &tail);
			break;
		case SREACH:
			Find_contacts(&head, &tail);
			break;
		case MODIFY:
			Modify_contacts(&head, &tail);
			break;
		case LIST:
			List_contacts(&head);
			break;
		case SORT:
			Bubble_sort(&head);
			break;
		case EXIT:
			printf("退出程序!\n");
			break;
		default:
			printf("输入非法,请重新输入!\n");
			break;
		}
	} while (input);
	return 0;
}

function.c

查找信息函数

//查找信息
address_book* Find(const address_book** head, address_book** last, const char* input)
{
	assert(head);
	address_book* cur = *head;
	
	while (cur)
	{
		if (strcmp(cur->name, input) == 0)
		{
			return cur;
		}
		*last = cur;
		cur = cur->next;
	}
	return NULL;
}

增加联系人函数

//增加联系人
void Add_contacts(address_book** head, address_book** tail)
{
	system("cls");
	assert(head && tail);
	//开辟动态内存
	address_book* ptr = (address_book*)malloc(sizeof(address_book));
	if (ptr == NULL)
	{
		printf("%s", strerror(errno));
		return;
	}
	memset(ptr, 0, sizeof(address_book));
	printf("请输入联系人名字:");
	scanf("%s", ptr->name);
	printf("请输入联系人年龄:");
	scanf("%d",&(ptr->age));
	printf("请输入联系人电话:");
	scanf("%s", ptr->telephone);
	printf("请输入联系人住址:");
	scanf("%s", ptr->address);
	if (*head == NULL)
	{
		*head = ptr;
		*tail = ptr;
	}
	else
	{
		(*tail)->next = ptr;
		*tail = ptr;
	}

	printf("添加成功!\n");
	system("pause");
	system("cls");
}

删除联系人函数

//删除联系人
void Sub_contacts(const address_book** head, address_book** tail)
{
	system("cls");
	assert(head && tail);
	if (*head == NULL)
	{
		printf("联系人为空\n");
	}
	else
	{
		char input[NAME_MAX] = { 0 };
		printf("请输入需要删除联系人的人名:");
		scanf("%s", input);
		address_book* last = *head;//记录被删除的结点的上一个结点
		address_book* ret = Find(head, &last, input);//查找并对比信息
		if (ret != NULL)
		{
			if (ret == *head)
			{
				*head = ret->next;
				free(ret);
				ret = NULL;
			}
			else if (ret == *tail)
			{
				*tail = last;
				(*tail)->next = NULL;
				free(ret);
				ret = NULL;
			}
			else
			{
				last->next = ret->next;
				free(ret);
				ret = NULL;
			}
			printf("删除成功!\n");
		}
		else
		{
			printf("未能到此联系人!\n");
		}
	}
	system("pause");
	system("cls");
}

查找指定联系人函数

//查找指定联系人
void Find_contacts(const address_book** head)
{
	system("cls");
	assert(head);
	if (*head == NULL)
	{
		printf("联系人为空\n");
	}
	else
	{
		char input[NAME_MAX] = { 0 };
		printf("请输入需要查找的联系人的姓名:");
		scanf("%s", input);
		address_book* last = *head;
		address_book* ret = Find(head, &last, input);
		if (ret != NULL)
		{
			printf("*****************************************\n");
			printf("%-6s\t%-4d\t%-12s\t%-10s\n",
				ret->name, ret->age, ret->telephone, ret->address);
			printf("*****************************************\n");
		}
		else
		{
			printf("未能到此联系人!\n");
		}
	}
	system("pause");
	system("cls");
}

更改联系人信息函数

//更改联系人信息
void Modify_contacts(address_book** head)
{
	system("cls");
	assert(head);
	if (*head == NULL)
	{
		printf("联系人为空\n");
	}
	else
	{
		char input[NAME_MAX] = { 0 };
		printf("请输入需要查找的联系人的姓名:");
		scanf("%s", input);
		address_book* last = *head;
		address_book* ret = Find(head, &last, input);
		if (ret != NULL)
		{
			printf("请输入联系人名字:");
			scanf("%s", ret->name);
			printf("请输入联系人年龄:");
			scanf("%d", &(ret->age));
			printf("请输入联系人电话:");
			scanf("%s", ret->telephone);
			printf("请输入联系人住址:");
			scanf("%s", ret->address);
			printf("更改成功!\n");
		}
		else
		{
			printf("未能到此联系人!\n");
		}
	}
	system("pause");
	system("cls");
}

查看联系人列表函数

//联系人列表
void List_contacts(const address_book** head)
{
	system("cls");
	assert(head);
	address_book* cur = *head;
	printf("*****************************************\n");
	printf("%-6s\t%-4s\t%-12s\t%-10s\n", "名字", "年龄", "电话", "住址");
	while (cur)
	{
		printf("%-6s\t%-4d\t%-12s\t%-10s\n",
			cur->name, cur->age, cur->telephone, cur->address);
		cur = cur->next;
	}
	printf("*****************************************\n");
	system("pause");
	system("cls");
}

排序函数

//按序整理
void Bubble_sort(const address_book** head)
{
	system("cls");
	assert(head);
	if (*head == NULL)
	{
		printf("联系人为空\n");
	}
	else
	{
		char s_tmp[ADDRESS_MAX] = { 0 };
		int i_tmp = 0;
		int count = 0;//记录结点个数
		address_book* cur_ = *head;
		while (cur_)
		{
			count++;
			cur_ = cur_->next;
		}
		int i = 0;
		for (i = 0; i < count - 1; i++)
		{
			address_book* cur = *head;
			int j = 0;
			for (j = 0; j < count - 1 - i; j++)
			{
				if (strcmp(cur->name, cur->next->name) > 0)//升序
				{
					strcpy(s_tmp, cur->name);
					strcpy(cur->name, cur->next->name);
					strcpy(cur->next->name, s_tmp);
					int i_tmp = cur->age;
					cur->age = cur->next->age;
					cur->next->age = i_tmp;
					strcpy(s_tmp, cur->telephone);
					strcpy(cur->telephone, cur->next->telephone);
					strcpy(cur->next->telephone, s_tmp);
					strcpy(s_tmp, cur->address);
					strcpy(cur->address, cur->next->address);
					strcpy(cur->next->address, s_tmp);
				}
				cur = cur->next;
			}
		}
		printf("整理完成!\n");
	}
	system("pause");
	system("cls");
}

state.h

包含的头文件和定义

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

#define NAME_MAX 20
#define TELE_MAX 20
#define ADDRESS_MAX 20

枚举类型和结构体

enum choose
{
	EXIT,
	ADD,
	SUB,
	SREACH,
	MODIFY,
	LIST,
	SORT
};

typedef struct address_book
{
	char name[NAME_MAX];
	int age;
	char telephone[TELE_MAX];
	char address[ADDRESS_MAX];
	struct address_book* next;
}address_book;

函数声明

//增加联系人
void Add_contacts(address_book** head, address_book** tail);

//删除联系人
void Sub_contacts(const address_book** head, address_book** tail);

//查找指定联系人
void Find_contacts(const address_book** head);

//更改联系人信息
void Modify_contacts(address_book** head);

//查看列表
void List_contacts(const address_book** head);

//排序
void Bubble_sort(const address_book** head);

码字不易~ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

陈亦康

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

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

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

打赏作者

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

抵扣说明:

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

余额充值