C语言添加动态内存的通讯录

#include <stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include "contact.h"
#define _CRT_SECURE_NO_WARNINGS 1


void menu();

int main()
{
	int input = 0;
    struct Contact con;
	InitContact(&con);
	do
	{
		

		menu();
		printf("chose");
		scanf_s("%d", &input);
		switch (input)
		{
		case ADD:
			AddContact(&con);
			break;
		case DEL:
			DelContact(&con);
			break;
		case SEARCH:
			SearchContact(&con);
			break;
		case MODIFY:
			ModifyContact(&con);
			break;
		case SHOW:
			ShowContact(&con);
			break;
		case SORT:
			SortContact(&con);
			break;
		case EXIT:
			DestoryContact(&con);
			printf("exit");
			break;
		default:
			printf("error");
			break;
		}

	} while (input);
	return 0;
}
void menu()
{
	printf("1. add 2.del 3.search 4.motify 5.show 6.sort 0.exit");

}

test.c

#define MAX 1000
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 12
#define MAX_ADDR 30
#define _CRT_SECURE_NO_WARNINGS 1
#define DEFAULT_SIZE 3
#include <stdio.h>
#include<stdlib.h>
enum Option
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SHOW,
	SORT

};
struct  PeoInfo
{
	char name[20];
	int age;
	char sex[5];
	char tele[12];
	char addr[30];
};
struct Contact
{
	//struct PeoInfo data[MAX];
	struct PeoInfo *data;
	int size;
	int capacity;

};  
void InitContact(struct Contact* ps);
void AddContact(struct Contact* ps);
void ShowContact(const struct Contact* ps);
void DelContact(struct Contact* ps);
void SearchContact(struct Contact* ps);
void ModifyContact(const struct Contact* ps);
void SortContact(struct Contact* ps);
void CheckCapacity(struct Contact* ps);
void DestoryContact(struct Contact* ps);

contact.h

#include "contact.h"
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
void InitContact(struct Contact* ps) {
	//memset(ps->data, 0, sizeof(ps->data));
	///ps->size = 0;
	ps->data = (struct ProInfo*)malloc(3 * sizeof(struct PeoInfo));
	if (ps->data==NULL)
	{
		return;

	}
	ps->size = 0;
	ps->capacity = DEFAULT_SIZE;

}
void CheckCapacity(struct Contact* ps)
{
	if (ps->size==ps->capacity)
	{
		struct PeoInfo* ptr=(struct PeoInfo*)realloc(ps->data, (ps->capacity + 2) * sizeof(struct PeoInfo));
		if (ptr!=NULL)
		{
			ps->data;
			ps->capacity += 2;
			printf("ok");
		}
		else
		{
			printf("no");
		}
	}
}
void AddContact(struct Contact* ps) {
	CheckCapacity(ps);
	/*if (ps->size==MAX)
	{
		printf("已经满了");


	}
	else
	{*/
	printf("请输入名字");
 	scanf_s("%s", ps->data[ps->size].name);
	printf("请输入年龄");
	scanf_s("%d", &(ps->data[ps->size].age));
	printf("请输入性别");
	scanf_s("%s", ps->data[ps->size].sex);
	printf("请输入电话");
	scanf_s("%s", ps->data[ps->size].tele);
	printf("请输入地址");
	scanf_s("%s", ps->data[ps->size].addr);
	ps->size++;
	
}
void ShowContact(const struct Contact* ps)
{
	if (ps->size==0)
	{
		printf("no ");
	}
	else
	{
		int i = 0;
		printf("%20s\t%4s\t%5s\t%12s\t%20s\n", "名字", "年龄", "性别", "电话", "地址");
		for ( i = 0; i < ps->size; i++)
		{
			printf("%20s\t%4d\t%5s\t%12s\t%20s\n", ps->data[i].name,
				ps->data[i].age,
				ps->data[i].sex,
				ps->data[i].tele,
				ps->data[i].addr);
		}
	}

}
static int FindByName(struct Contact* ps,char  name[MAX_NAME]);
static int FindByName(struct Contact* ps, char  name[MAX_NAME])
{
	for (i = 0; i < ps->size; i++)
	{
		if (0 == strcmp(ps->data[i].name, name))
		{
			return 1;
		}
	}
	return -1;
}
void DelContact(struct Contact* ps)
{
	char name[20];
	printf("名字");
	scanf("%s", name);
	int pos=FindByName(ps, name);
	int i = 0;
	
	if (pos==-1)
	{
		printf("nonono");
	}
	else
	{
		int j = 0;
		for ( j = 0; j < ps->size-1; j++)
		{
			ps->data[j]=ps->data[j + 1];
		}
		ps->size--;
		printf("yes");
	}
}
void SearchContact(const struct Contact* ps) {
	char name[MAX_NAME];
	printf("信息多少");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	if (pos==-1)
	{
		printf("no exist");
	}
	else
	{
		printf("%20s\t%4s\t%5s\t%12s\t%20s\n", "名字", "年龄", "性别", "电话", "地址");
		printf("%20s\t%4d\t%5s\t%12s\t%20s\n", 
		ps->data[pos].name,
		ps->data[pos].age,
		ps->data[pos].sex,
		ps->data[pos].tele,
		ps->data[pos].addr);
		
	}



}
void ModifyContact(const struct Contact* ps)
{
	char name[MAX_NAME];
	int pos = 0;
	printf("修改信息多少");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	if (pos == -1)
	{
		printf("no exist");
	}
	else
	{
		printf("请输入名字");
		scanf_s("%s", ps->data[pos].name);
		printf("请输入年龄");
		scanf_s("%d", &(ps->data[pos].age));
		printf("请输入性别");
		scanf_s("%s", ps->data[pos].sex);
		printf("请输入电话");
		scanf_s("%s", ps->data[pos].tele);
		printf("请输入地址");
		scanf_s("%s", ps->data[pos].addr);
		printf("ok");
	}

}
void DestoryContact(struct Contact* ps)
{
	free(ps->data);
	ps->data = NULL;

}

contact.c

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值