动态存储实现通讯录

动态存储方式

所谓动态存储方式是指在程序运行期间根据需要进行动态的分配存储空间的方式。动态存储变量是在程序执行过程中,使用它时才分配存储单元,使用完毕立即释放。典型的例子是函数的形式参数,在函数定义时并不给形参分配存储单元,只是在函数被调用时,才予以分配,调用函数完毕立即释放。如果一个函数被多次调用,则反复地分配、释放形参变量的存储单元。


头文件
#define _CRT_SECURE_NO_WARNINGS 1

#ifndef __CONTACT_H__
#define __CONTACT_H__

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


enum OP
{
	EXIT,
	ADD,
	DEL,
	MODIFY,
	SEARCH,
	DISPLAY,
	SORT,
	CLEAR
};

#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 12
#define ADDR_MAX 25
#define MAX 1000
#define DEFAULT_SZ 3
#define DEFAULT_INC 2
typedef struct PerInfo
{
	char name[NAME_MAX];
	int age;
	char sex[SEX_MAX];
	char tele[TELE_MAX];
	char addr[ADDR_MAX];
}PeoInfo;


//typedef struct Contact
//{
//	PeoInfo data[MAX];
//	int size;
//}Contact,*Pcon;
typedef struct contact{
	PeoInfo *data;
    int size;
    int capacity;
}Contact,*Pcon;

void init_contact(Pcon pcon);
void _destory_contact(Pcon pcon);
void _add_contact(Pcon pcon);
void _display_contact(Pcon pcon);
void _del_contact(Pcon pcon);
void _search_contact(Pcon pcon);
void _modify_contact(Pcon pcon);
void _sort_contact(Pcon pcon);
void _clear_contact(Pcon pcon);
//void _save_contact(Pcon pcon);
//void _load_contact(Pcon pcon);


#endif //__CONTACT_H__
函数程序
#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
//
//git
//svn
//
//void init_contact(Pcon pcon)
//{
//	memset(pcon->data, 0, MAX*sizeof(PeoInfo));
//	pcon->size = 0;
//}
void init_contact(Pcon pcon)
{
	pcon->size =0;
	pcon->capacity=DEFAULT_SZ;
	pcon->data =(PeoInfo*)malloc(pcon->capacity *sizeof(PeoInfo));
	if(pcon->data ==NULL)
	{
		memset(pcon->data ,0,pcon->capacity *sizeof(PeoInfo));
		exit(EXIT_FAILURE);
	}
	//_load_contact(pcon);
	
}
void check_capacity(Pcon pcon)
{
	PeoInfo * tmp=0;
	if(pcon->size =pcon->capacity)
	{
		realloc(pcon->data ,pcon->capacity+DEFAULT_INC);
		if(tmp==NULL)
		{
			;
		}
		else
		{
			pcon->data =tmp;
		}
		pcon->capacity+=DEFAULT_INC;
	}
}
void _destory_contact(Pcon pcon)
{
	if(pcon->data!=NULL)
	{
		free(pcon->data );
		pcon->data =NULL;
	}
}
void _add_contact(Pcon pcon)
{
	if(pcon->size >= MAX)
	{
		printf("电话本满了\n");
		return;
	}
	printf("请输入名字:>");
	scanf("%s",pcon->data[pcon->size].name);
	printf("请输入年龄:>");
	scanf("%d",&(pcon->data[pcon->size].age));
	printf("请输入性别:>");
	scanf("%s",pcon->data[pcon->size].sex);
	printf("请输入电话:>");
	scanf("%s",pcon->data[pcon->size].tele);
	printf("请输入地址:>");
	scanf("%s",pcon->data[pcon->size].addr);
	pcon->size++;
	printf("添加成功\n");
}

void _display_contact(Pcon pcon)
{	
	int i = 0;
	printf("%9s\t%3s\t%4s\t%11s\t%10s\n","name","age","sex","tele","addr");
	for(i = 0;i<pcon->size;i++)
	{
		printf("%9s\t%3d\t%4s\t%11s\t%10s\n",
			pcon->data[i].name,
			pcon->data[i].age,
			pcon->data[i].sex,
			pcon->data[i].tele,
			pcon->data[i].addr);
	}
}

static int find_entry(Pcon pcon, char *name)
{
	int i = 0;
	for(i = 0;i<pcon->size; i++)
	{
		if(strcmp(pcon->data[i].name,name) == 0)
		{
			return i;
		}
	}
	return -1;
}
void _del_contact(Pcon pcon)
{
	int pos = 0;
	int index = 0;
	char name[NAME_MAX];
	if(pcon->size == 0)
	{
		printf("电话本空\n");
		return;
	}
	printf("请输入要删除人的名字:>");
	scanf("%s",name);
	pos = find_entry(pcon, name);
	if(pos == -1)
	{
		printf("找不到要删除的人\n");
		return;
	}
	//
	for(index = pos; index < pcon->size; index++)
	{
		pcon->data[index] = pcon->data[index+1];
	}
	pcon->size--;
	printf("删除成功\n");
}
void _search_contact(Pcon pcon)
{
	char  name[NAME_MAX] = {0};
	int pos = 0;
	printf("请输入要查找人的名字:>");
	scanf("%s",name);
	pos = find_entry(pcon, name);
	if(pos == -1)
	{
		printf("指定联系人不存在\n");
		return;
	}
	else
	{
		printf("%9s\t%3s\t%4s\t%11s\t%10s\n","name","age","sex","tele","addr");

		printf("%9s\t%3d\t%4s\t%11s\t%10s\n",
				pcon->data[pos].name,
				pcon->data[pos].age,
				pcon->data[pos].sex,
				pcon->data[pos].tele,
				pcon->data[pos].addr);
	}
}
void _modify_contact(Pcon pcon)
{
	char  name[NAME_MAX] = {0};
	int pos = 0;
	printf("请输入要修改人的名字:>");
	scanf("%s",name);
	pos = find_entry(pcon, name);
	if(pos == -1)
	{
		printf("指定联系人不存在\n");
		return;
	}
	else
	{
		printf("请输入名字:>");
		scanf("%s",pcon->data[pos].name);
		printf("请输入年龄:>");
		scanf("%d",&(pcon->data[pos].age));
		printf("请输入性别:>");
		scanf("%s",pcon->data[pos].sex);
		printf("请输入电话:>");
		scanf("%s",pcon->data[pos].tele);
		printf("请输入地址:>");
		scanf("%s",pcon->data[pos].addr);
	}
}

//volatile
void _sort_contact(Pcon pcon)
{
	int i = 0;
	int j = 0;
	for(i = 0;i<pcon->size-1; i++)//控制排序趟数
	{
		for(j = 0; j<pcon->size-1-i; j++)
		{
			if(strcmp(pcon->data[j].name,pcon->data[j+1].name) > 0)
			{
				PeoInfo tmp = {0};
				tmp = pcon->data[j];
				pcon->data[j] = pcon->data[j+1];
				pcon->data[j+1] = tmp;
			}
		}
	}
}
void _clear_contact(Pcon pcon)
{
	pcon->size = 0;
}
//void _save_contact(Pcon pcon)
//{
//	int i=0;
//	FILE*fwrite=fopen("contact.dat","w");
//	if(pfwrite==NULL)
//	{
//		perror("open file for write");
//		exit(EXIT_FAULURE);
//		
//	}
//	for(i=0;i<pcon->size;i++)
//	{
//		fwrite(&pcon->data[i],sizeof(PeoInfo),1,pfwrite);
//		fclose(pfwrite);
//	}
//}
//void _load_contact(Pcon pcon)
//{
//	int i=0;
//	FILE*fread=fopen("contact.dat","r");
//	PeoInfo tmp=pcon->data[i];
//		if(pfread==NULL)
//	{
//		perror("open file for read");
//		exit(EXIT_FAULURE);
//		
//	}
//	while(&tmp,sizeof(PeoInfo),1,pread)
//	{
//		check_capatity(pcon);
//		pcon->data[i]=tmp;
//		i++;
//		pcon->size++;
//		
//	}
//}
//





测试程序
#define _CRT_SECURE_NO_WARNINGS 1

#include "contact.h"


void menu()
{
	printf("***** 1.add     2.del    ****\n");
	printf("***** 3.modify  4.search ****\n");
	printf("***** 5.display 6.sort   ****\n");
	printf("***** 7.clear   0.exit   ****\n");
}
int main()
{
	Contact con;
	char input = 1;
	init_contact(&con);
	while(input)
	{
		menu();
		printf("请选择>:");
		scanf("%d",&input);
		switch(input)
		{
		case ADD:
			_add_contact(&con);
			break;
		case DEL:
			_del_contact(&con);
			break;
		case SEARCH:
			_search_contact(&con);
			break;
		case MODIFY:
			_modify_contact(&con);
			break;
		case DISPLAY:
			_display_contact(&con);
			break;
		case SORT:
			_sort_contact(&con);
			break;
		case CLEAR:
			_clear_contact(&con);
			break;
		case EXIT:
			_destory_contact(&con);
			break;
		default:
			printf("选择错误\n");
			break;
		}
	}
	return 0;
}

动态存储实现完成


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值