c設計 通訊錄 malloc動態實現

通訊錄:
通訊錄可以用來存儲多個聯繫人的信息,每個人的信息包括:
姓名、性別、年齡、電話、住址


提供方法:
1.    添加聯繫人信息
2.    刪除指定聯繫人信息
3.    查找指定聯繫人信息
4.    修改指定聯繫人信息
5.    顯示所有聯繫人信息
6.    清空所有聯繫人
7.    以名字排序所有聯繫人



contact.h

#define _CRT_SECURE_NO_DEPRECATE
#ifndef _CONTACT_
#define _CONTACT_

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

#define SIZE 128

typedef struct{
	char name[64];
	char sex[8]; 
	int age;
	char tel[12];
	char addr[128];
}man_t, *man_p, **man_pp;

typedef struct _contact{
	man_p listp;
	int size;
	int cap;
}contact_t, *contact_p, **contact_pp;

void init(contact_pp c, int cap);  //分配內存
void add(contact_pp c); //添加
void myShow(contact_p c);//顯示
void menu();
void destroy(contact_p c); //銷燬分配內存
void myDelete(contact_p c, char *name); //刪除
void mySearch(contact_p c, char *name); //查找
void myAlter(contact_p c, char *name); //修改
void mySweep(contact_p c); //清除
void mySort(contact_p c); //排序


#endif
contatc.c

#define _CRT_SECURE_NO_DEPRECATE
#include "contact.h"

static int inc(contact_p c)
{
	assert(c);
	if (c->size == c->cap)
	{
		c->listp = realloc(c->listp, c->cap + SIZE);
		if (NULL == c->listp)
		{
			perror("realloc");
			exit(3);
		}
		c->cap += SIZE;
		printf("inc space....\n");
	}
	return 1;
}

void init(contact_pp c, int cap)
{
	assert(c);
	assert(cap);
	*c = (contact_p)malloc(sizeof(contact_t));
	if (NULL == *c)
	{
		exit(1);
	}

	(*c)->listp = (man_p)malloc(sizeof(man_t)*cap);
	if (NULL == (*c)->listp)
	{
		exit(2);
	}

	(*c)->cap = cap;
	(*c)->size = 0;
}

void add(contact_pp c)
{
	assert(c);
	int i = (*c)->size;

	if (inc)
	{
		printf("請輸入姓名和地址:");
		scanf("%s %s", ((*c)->listp +i) ->name, ((*c)->listp +i) ->addr);

		printf("請輸入年齡:");
		while (1)
		{
			scanf("%d", &((*c)->listp + i)->age);
			if ((((*c)->listp + i)->age) > 0 && (((*c)->listp + i)->age) < 150)
			{
				break;
			}
			printf("你輸入年齡有誤,請重新輸入\n");
		}


		printf("請輸入性別:");
		while (1)
		{
			scanf("%s", ((*c)->listp + i)->sex);
			if (!(strcmp((((*c)->listp + i)->sex), "男")) || !(strcmp((((*c)->listp + i)->sex), "女")))
			{
				break;
			}
			printf("你輸入性別有誤,請重新輸入(男,女)\n");
		}


		printf("請輸入電話:");
		
		scanf("%s", ((*c)->listp + i)->tel);
		
		(*c)->size++;
	}

}

void myShow(contact_p c)   //顯示
{
	assert(c);
	man_p src = c->listp;
	int i = 0;
	for (; i < c->size;i++)
	{
		printf("聯繫人%d---------------------\n", i);
		printf("名字:%s\n",src->name);
		printf("性別:%s\n",src->sex);
		printf("年齡:%d\n",src->age);
		printf("電話:%s\n",src->tel);
		printf("住址:%s\n",src->addr);

		src++;
	}
	
}

void menu()
{
	printf("#####################################################\n");
	printf("##################     通訊錄    ####################\n");
	printf("##################    1.add      ####################\n");
	printf("##################    2.delate   ####################\n");
	printf("##################    3.search   ####################\n");
	printf("##################    4.alter    ####################\n");
	printf("##################    5.show_all ####################\n");
	printf("##################    6.sweep    ####################\n");
	printf("##################    7.sort     ####################\n");
	printf("##################    0.exit     ####################\n");
	printf("#####################################################\n");

}

void destroy(contact_p c) //銷燬分配內存
{
	assert(c);
	if (c->listp)
	{
		free(c->listp);
		c->listp = NULL;
	}
	free (c);
	c = NULL;
}

void myDelete(contact_p c, char *name) //刪除
{
	assert(c);
	assert(name);
	man_p src = c->listp;
	int i = 0;
	for (; i < c->size;i++)
	{
		if (0 == strcmp((src+i)->name,name))
		{
			*(src + i) = *(src + c->size - 1);
			c->size--;
			printf("刪除成功!\n");
			break;
		}
	}

	if (i > c->size)
	{
		printf("%s不存在\n",name);
	}
}

void mySearch(contact_p c, char *name)
{
	assert(c);
	assert(name);
	man_p src = c->listp;
	int i = 0;
	for (; i < c->size; i++)
	{
		if (0 == strcmp((src)->name, name))
		{
			printf("名字:%s\n", src->name);
			printf("性別:%s\n", src->sex);
			printf("年齡:%d\n", src->age);
			printf("電話:%s\n", src->tel);
			printf("住址:%s\n", src->addr);
			break;
		}
		src++;
	}

	if (i == c->size)
	{
		printf("%s不存在\n", name);
	}
}

void myAlter(contact_p c, char *name) //修改
{
	assert(c);
	assert(name);
	man_p src = c->listp;
	int i = 0;
	for (; i < c->size; i++)
	{
		if (0 == strcmp((src)->name, name))
		{
			printf("請輸入修改後的名字:");
			scanf("%s",c->listp->name);
			printf("請輸入修改後的地址:");
			scanf("%s", c->listp->addr);
			printf("請輸入修改後的性別:");
			scanf("%s", c->listp->sex);
			printf("請輸入修改後的年齡:");
			scanf("%d", &c->listp->age);
			printf("請輸入修改後的電話:");
			scanf("%s", c->listp->tel);
			break;
		}
		src++;
	}

	if (i == c->size)
	{
		printf("%s不存在\n", name);
	}

}

void mySweep(contact_p c) //清除
{
	assert(c);
	c->size = 0;
	printf("全部清除成功\n");
}
int str_cmp(const void * dest,const void *  src)
{
	return strcmp(((man_p)dest)->name,((man_p)src)->name);
}
void mySort(contact_p c) //排序
{
	assert(c);
	qsort(c->listp,c->size, sizeof(man_t), str_cmp);
	printf("按名字排序完成!\n");
}
main.c

#define _CRT_SECURE_NO_DEPRECATE
#include "contact.h"

main()
{
	contact_p person = NULL;
	init(&person, SIZE);
	int input = 0;
	while (1)
	{
		menu();
		printf("請輸入選擇:>");
		scanf("%d", &input);
		if (input < 0 || input >7)
		{
			printf("輸入有誤!\n");
			continue;
		}
		switch (input)
		{

		case 0:
		{
			destroy(person);
			exit(0);
			break;
		}

		case 1:
		{

			add(&person);

			break;
		}

		case 2:
		{
			char s_name[128];
			printf("請輸入要刪除的姓名:");
			scanf("%s",s_name);
			myDelete(person, s_name);
			break;
		}

		case 3:
		{
			char s_name[128];
			printf("請輸入要查找的姓名:");
			scanf("%s", s_name);
			mySearch(person, s_name);
			break;
		}

		case 4:
		{
			char s_name[128];
			printf("請輸入要修改的姓名:");
			scanf("%s", s_name);
			myAlter(person, s_name);
			break;
		}

		case 5:
		{
			myShow(person);
			break;
		}

		case 6:
		{
			mySweep(person);
			break;
		}

		case 7:
		{
			mySort(person);
			break;
		}
		/*default:
		{
			printf("輸入有誤!\n");
			break;
		}*/

		}
	}
	system("pause");
	return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章