动态管理内存的通讯录实现

contact.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<errno.h>
struct PeoInf
{
	char name[20];
	char sex[8];
	int age;
	char tel[12];
	char address[30];
};

struct Contaction
{
	struct PeoInf* p;
	int numbers;
	int capacity;
};
//创建通讯录
struct Contaction* creat_contaction();

//销毁通讯录
void distory_contaction(struct Contaction* con);

//添加联系人信息
void add_peoinf(struct Contaction* con);

//查找联系人信息
struct PeoInf* search_peoname(struct Contaction* con);

//删除联系人信息
void del_peoinf(struct Contaction* con);

//显示联系人信息
void show_peoinf(struct Contaction* con);

//修改联系人信息
void modify_peoinf(struct Contaction* con);

//清空通讯录信息
void clear_peoinf(struct Contaction* con);

contact.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"

struct Contaction*  creat_contaction()
{
	struct Contaction* ret = (struct Contaction*)malloc(sizeof(struct Contaction));
	if (ret != NULL)
	{
		memset(ret, 0, sizeof(struct Contaction));
		ret->p = (struct PeoInf*)malloc(10 * sizeof(struct PeoInf));
	}
	else
	{
		printf("%s", strerror(errno));
		exit(-1);
	}
	assert(ret->p);
	if (ret->p == NULL)
	{
		printf("%s", strerror(errno));
		exit(-1);
	}
	else
	{
		memset(ret->p, 0, 10 * sizeof(struct PeoInf));
		ret->capacity = 10;
	}
	return ret;
}


void distory_contaction(struct Contaction* con)
{
	assert(con);
	free(con->p);
	free(con);
}

//增容函数
int improve_capacity(struct Contaction* con)
{
	assert(con);
	struct PeoInf* temp = (struct PeoInf*)realloc(con->p, con->capacity * 2 * sizeof(struct PeoInf));
	if (temp == NULL)
	{
		printf("增容失败\n");
		return 0;
	}
	else
	{
		printf("增容成功\n");
		con->capacity *= 2;
		return 1;
	}
}
void add_peoinf(struct Contaction* con)
{
	assert(con);
	if (con->numbers == con->capacity)
	{
		if (improve_capacity(con) == 0)
			return;
	}
	printf("请输入添加信息:");
	printf("姓名:");
	scanf("%s", &(con->p[con->numbers].name));
	printf("性别:");
	scanf("%s", &(con->p + con->numbers)->sex);
	printf("年龄:");
	scanf("%d", &(con->p + con->numbers)->age);
	printf("电话号码:");
	scanf("%s", &(con->p + con->numbers)->tel);
	printf("地址:");
	scanf("%s", &(con->p + con->numbers)->address);
	con->numbers++;
}

struct PeoInf* search_peoname(struct Contaction* con)
{
	assert(con);
	struct PeoInf* ret = con->p;
	char find_name[20] = { 0 };
	printf("请输入要查找的姓名:");
	scanf("%s", &find_name);
	for (int i = 0; i < con->numbers; i++)
	{
		if (strcmp(find_name, con->p[i].name) == 0)
		{
			printf("找到了\n");
			return con->p + i;
		}
	}
	printf("没找着\n");
	return NULL;
}
void del_peoinf(struct Contaction* con)
{
	assert(con);
	if (con->numbers == 0)
	{
		printf("通讯录为空\n");
		return;
	}
	struct PeoInf* temp = search_peoname(con);
	if (temp == NULL)
	{
		return;
	}
	else
	{
		for (int i = temp - con->p; i < con->numbers - 1; i++)
		{
			con->p[i] = con->p[i + 1];
		}
	}
	con->numbers--;
}
void show_peoinf(struct Contaction* con)
{
	assert(con);
	if (con->numbers != 0)
	{
		printf("姓名  性别  年龄  电话          地址\n");
		for (int i = 0; i < con->numbers; i++)
		{
			printf("%-6s%-6s%-6d%-14s%-s\n", 
				con->p[i].name,
				con->p[i].sex, 
				con->p[i].age, 
				con->p[i].tel, 
				con->p[i].address);
		}
	}
}

void modify_peoinf(struct Contaction* con)
{
	assert(con);
	if (con->numbers == 0)
	{
		printf("通讯录为空\n");
		return;
	}
	struct PeoInf* temp = search_peoname(con);
	if (temp == NULL)
	{
		return;
	}
	else
	{
		printf("请输入修改信息:");
		printf("姓名:");
		scanf("%s", &temp->name);
		printf("性别:");
		scanf("%s", &temp->sex);
		printf("年龄:");
		scanf("%d", &temp->age);
		printf("电话号码:");
		scanf("%s", &temp->tel);
		printf("地址:");
		scanf("%s", &temp->address);
	}

}

void clear_peoinf(struct Contaction* con)
{
	assert(con);
	con->numbers = 0;
}

main.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"

void menu()
{
	printf("***** 1.add     2.del   *****\n");
	printf("***** 3.search  4.modify*****\n");
	printf("***** 5.show    6.clear *****\n");
	printf("***** 0.exit            *****\n");
}

int main()
{
	enum  function
	{
		exit,
		add,
		del,
		search,
		modify,
		show,
		clear
	};
	enum  function input =  exit;
	struct Contaction* con = creat_contaction();

	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case exit:
			distory_contaction(con);
			break;
		case add:
			add_peoinf(con);
			break;
		case del:
			del_peoinf(con);
			break;
		case search:
			search_peoname(con);
			break;
		case modify:
			modify_peoinf(con);
			break;
		case show:
			show_peoinf(con);
			break;
		case clear:
			clear_peoinf(con);
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JDSZGLLL

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

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

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

打赏作者

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

抵扣说明:

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

余额充值