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

追求适度,才能走向成功;人在顶峰,迈步就是下坡;身在低谷,抬足既是登高;弦,绷得太紧会断;人,思虑过度会疯;水至清无鱼,人至真无友,山至高无树;适度,不是中庸,而是一种明智的生活态度。

导读:本篇文章讲解 【C语言】模拟通讯录(数组版、动态版、链表版),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

目录

设计要求

实现框图

数组版

        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:排序


实现框图

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

一起实现~

加油~

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


数组版

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");
}

动态版

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

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

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

无需修改~


链表版

可谓是大换血啊~

【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);

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

码字不易~ 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/129913.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!