c语言动态内存开辟,实现通讯录的动态创建

c语言动态内存开辟,实现通讯录的动态创建,第1张

目录

contact.h头文件

contact.c源文件

main.c源文件


通讯录功能:

  1. 增加联系人
  2. 删除联系人
  3. 查找联系人
  4. 修改联系人信息
  5. 打印通讯录
  6. 清空通讯录
  7. 通讯录按联系人名字进行排序
  8. 退出程序(销毁通讯录)

该项目思路:

  1. 创建contact.h头文件进行函数声明
  2. 创建contact.c源文件进行函数编写
  3. 创建main.c源文件进行主函数的运行

源代码:

contact.h头文件
#include 
#include 
#include 
#include 

#define DEFAULT_SZ 3

typedef struct PeoInf    //people information
{
	char name[15];
	char number[15];
	char address[15];
}PeoInf;

typedef struct Contact
{
	PeoInf* data;
	int sz;			//当前存储的人数
	int capacity;	//通讯录容量
}Contact;

enum contact	//用枚举常量提高代码可读性
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MADIFI,
	PRINT,
	EMPTY,
	SORT
};

void Init(Contact* pcon);	//初始化

void Add(Contact* pcon);	//增加

void Del(Contact* pcon);	//删除

void Search(Contact* pcon);	//查找

void Madifi(Contact* pcon);	//修改

void Print(Contact* pcon);	//打印

void Empty(Contact* pcon);	//清空

void Sort(Contact* pcon);	//排序

void DestoryContact(Contact* pcon);		//销毁通讯录
contact.c源文件
#include "contact.h"

void Init(Contact* pcon)
{
	assert(pcon);
	pcon->sz = 0;
	pcon->capacity = DEFAULT_SZ;
	pcon->data = (PeoInf*)calloc(DEFAULT_SZ, sizeof(PeoInf));
	if (pcon->data == NULL)
	{
		perror("Init::calloc");
	}
}

void CheckCapacity(Contact* con)    //判断是否扩容
{
	if (con->sz == con->capacity)
	{
		PeoInf* p = (PeoInf*)realloc(con->data, sizeof(con->data) + sizeof(PeoInf) * 2);
		if (p != NULL)
		{
			con->data = p;
			con->capacity += 2;
		}
	}
}

void AddMenu()
{
	printf("\n");
	printf("***********************\n");
	printf("****  1. add next  ****\n");
	printf("****  0. exit add  ****\n");
	printf("***********************\n");
}

void Add(Contact* pcon)
{
	CheckCapacity(pcon);
	int input;
	do
	{
		printf("请输入新建联系人号码:");
		scanf("%s", pcon->data[pcon->sz].number);
		printf("请输入新建联系人姓名:");
		scanf("%s", pcon->data[pcon->sz].name);
		printf("请输入新建联系人地址:");
		scanf("%s", pcon->data[pcon->sz].address);
		pcon->sz++;
		printf("添加成功\n");
		AddMenu();
		printf("请选择 *** 作:");
		scanf("%d", &input);
		while (input != 0 && input != 1)
		{
			printf("选择错误,请重新选择:");
			int tmp;
			scanf("%d", &tmp);
			input = tmp;
		}
	} while (input);
	printf("\n");
}

int FindName(Contact* pcon, char* findname)
{
	assert(pcon && findname);
	int i = 0;
	while (i < pcon->sz)
	{
		if (0 == strcmp(pcon->data[i].name, findname))
		{
			return i;
		}
		i++;
	}
	return -1;
}

void Del(Contact* pcon)
{
	assert(pcon);
	printf("请输入删除联系人姓名:");
	char delname[15];
	scanf("%s", delname);
	int num = FindName(pcon, delname);
	if (-1 == num)
	{
		printf("未找到该联系人\n");
	}
	else
	{
		while (num < pcon->sz - 1)
		{
			strcpy(pcon->data[num].number, pcon->data[num + 1].number);
			strcpy(pcon->data[num].name, pcon->data[num + 1].name);
			strcpy(pcon->data[num].address, pcon->data[num + 1].address);
			num++;
		}
		strcpy(pcon->data[num].number, "\0");
		strcpy(pcon->data[num].number, "\0");
		strcpy(pcon->data[num].number, "\0");
		pcon->sz--;
		printf("联系人 %-15s信息已删除\n\n", delname);
	}
}

void Search(Contact* pcon)
{
	assert(pcon);
	printf("请输入要查找联系人的姓名:");
	char schname[15];
	scanf("%s", schname);
	int num = FindName(pcon, schname);
	if (-1 == num)
	{
		printf("未找到该联系人\n\n");
	}
	else
	{
		printf("已找到该联系人信息:\n");
		printf("号码:%-15s", pcon->data[num].number);
		printf("姓名:%-15s", pcon->data[num].name);
		printf("地址:%-20s\n", pcon->data[num].address);
	}
	printf("\n");
}

void MdfMenu()
{
	printf("\n");
	printf("*********************\n");
	printf("****  1.number   ****\n");
	printf("****  2.name     ****\n");
	printf("****  3.address  ****\n");
	printf("****  0.exit mdf ****\n");
	printf("*********************\n");
}

void Madifi(Contact* pcon)
{
	assert(pcon);
	printf("请输入要修改信息的联系人姓名:");
	char mdf_name[15];
	scanf("%s", mdf_name);
	int num = FindName(pcon, mdf_name);
	if (-1 == num)
	{
		printf("未找到该联系人\n\n");
	}
	else
	{
		MdfMenu();
		printf("请选择需要修改的信息:");
		int input;
		char tmp[20];
		do
		{
			scanf("%d", &input);
			switch (input)
			{
			case 1:
				printf("请修改电话号码为:");
				scanf("%s", tmp);
				strcpy(pcon->data[num].number, tmp);
				printf("电话号码修改成功\n");
				MdfMenu();
				printf("请选择需要修改的信息:");
				break;
			case 2:
				printf("请修改姓名为:");
				scanf("%s", tmp);
				strcpy(pcon->data[num].name, tmp);
				printf("姓名修改成功\n");
				MdfMenu();
				printf("请选择需要修改的信息:");
				break;
			case 3:
				printf("请修改地址为:");
				scanf("%s", tmp);
				strcpy(pcon->data[num].address, tmp);
				printf("地址修改成功\n");
				MdfMenu();
				printf("请选择需要修改的信息:");
				break;
			case 0:
				printf("退出联系人信息修改\n");
				break;
			default:
				printf("选择错误\n");
				break;
			}
		} while (input);
		printf("\n");
	}
}

void Print(Contact* pcon)
{
	assert(pcon);
	if (pcon->sz == 0)
	{
		printf("通讯录为空\n\n");
		return;
	}
	printf("\n%-15s%-15s%-20s\n", "号码", "姓名", "地址");
	int i = 0;
	while (i < pcon->sz)
	{
		printf("%-15s", pcon->data[i].number);
		printf("%-15s", pcon->data[i].name);
		printf("%-20s", pcon->data[i].address);
		printf("\n");
		i++;
	}
	printf("\n");
}

void Empty(Contact* pcon)
{
	memset(pcon->data, 0, sizeof(pcon->data));
	PeoInf* p =(PeoInf*)realloc(pcon->data, sizeof(PeoInf) * DEFAULT_SZ);
	if (p != NULL)
	{
		pcon->data = p;
		pcon->sz = 0;
		pcon->capacity = 0;
		printf("通讯录已清空\n\n");

	}
	else
	{
		perror("Empty::realloc");
	}
}

void Sort(Contact* pcon)
{
	assert(pcon);
	int i = 1;
	while (i < pcon->sz)
	{
		int j = i - 1;
		char tmp[3][15];
		strcpy(tmp[0], pcon->data[i].name);
		strcpy(tmp[1], pcon->data[i].number);
		strcpy(tmp[2], pcon->data[i].address);
		while (j >= 0 && strcmp(pcon->data[j].name, tmp[0]) > 0)
		{
			strcpy(pcon->data[j + 1].name, pcon->data[j].name);
			strcpy(pcon->data[j + 1].number, pcon->data[j].number);
			strcpy(pcon->data[j + 1].address, pcon->data[j].address);
			j--;
		}
		strcpy(pcon->data[j + 1].name, tmp[0]);
		strcpy(pcon->data[j + 1].number, tmp[1]);
		strcpy(pcon->data[j + 1].address, tmp[2]);
		i++;
	}
	printf("已经按名字排序成功\n\n");
}

void DestoryContact(Contact* pcon)
{
	free(pcon->data);
	pcon->data = NULL;
	pcon->sz = 0;
	pcon->capacity = 0;
}
main.c源文件
#include "contact.h"

void menu()
{
	printf("***********************************\n");
	printf("****   1.add       2.delete    ****\n");
	printf("****   3.search    4.madifi    ****\n");
	printf("****   5.print     6.empty     ****\n");
	printf("****   7.sort      0.exit      ****\n");
	printf("***********************************\n");
	printf("\n");
}

int main()
{
	int input;
	Contact* con = (Contact*)malloc(sizeof(Contact));
	Init(con);
	do
	{
		menu();
		printf("请选择 *** 作:");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			Add(con);
			break;
		case DEL:
			Del(con);
			break;
		case SEARCH:
			Search(con);
			break;
		case MADIFI:
			Madifi(con);
			break;
		case PRINT:
			Print(con);
			break;
		case EMPTY:
			Empty(con);
			break;
		case SORT:
			Sort(con);
			break;
		case EXIT:
			DestoryContact(con);
			break;
		default:
			printf("\n选择错误,请重新选择\n");
			break;
		}
	} while (input);
	free(con);
	con = NULL;
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/564479.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-06
下一篇 2022-04-06

发表评论

登录后才能评论

评论列表(0条)

保存