通讯录

#ifndef _TONGXUN_H_
#define _TONGXUN_H_

#include 
#include 
#include 
#include 
#include 
#include
#pragma  warning(disable:4996)

#define MAX 500
#define NAME_SIZE 100
#define MAX_STREET 20
#define MAX_CITY 10
#define EIP_SIZE 20
#define STATE_SIZE 20
#define INC_SIZE 2  //一次扩容多少个
typedef struct  //每个人的信息
{
	char name[NAME_SIZE];
	char street[MAX_STREET];//street
	char city[MAX_CITY];  //city
	char eip[EIP_SIZE];  //eip  邮编
	char state[STATE_SIZE];  //state  国家
}person_t,*person_p,**person_pp;
typedef struct   //整个通讯录的信息
{
	int cap;
	int size;
	person_t list[0];
}contact_t, *contact_p,**contact_pp;

void InitContact(contact_pp _ctpp);   //初始化
void AddPerson(contact_pp ct);  //添加
void Sort(contact_p ct);    //  排序  
void Show(contact_p ct);    //展示
void Delperson(contact_p ct); //删除 
void Modperson(contact_p ct);  //修改  
int SearchPerson(contact_p ct);//查找   
void ClearPerson(contact_p ct);  //清空 


#endif
#include "tongxun.h"
static int  IsFull(contact_p ct)   //判断是否满
{
	return ct->size == ct->cap ? 1 : 0;     //如果通讯录中有的元素和通讯录中最大容量相等,即通讯录满
}
static int  Inc(contact_pp _ctpp)   //扩容
{
	int new_size = sizeof(contact_t)+((*_ctpp)->cap + INC_SIZE)*sizeof(person_t);    //一次性扩容INC_SIZE个大小的容量
	contact_p p = realloc(*_ctpp, new_size);   //在*_ctpp下扩容new_size个大小
	if (!p)   //扩容失败
	{
		printf("INC ERROR!\n");
		return 0;
	}
	p->cap = (*_ctpp)->cap + INC_SIZE;   //扩容成功总的容量发生改变
	*_ctpp = p;
	return 1;
}
void InitContact(contact_pp _ctpp)  //初始化
{
	*_ctpp = malloc(sizeof(contact_t) + sizeof(person_t)*MAX);   //申请空间大小为总的通讯录大小
	if (NULL == *_ctpp)
	{
		printf("%s:%d\n", strerror(errno), errno);
		exit(1);
	}
	(*_ctpp)->cap =MAX;   //申请成功将容量赋值MAX,其中含有0个元素
	(*_ctpp)->size = 0;
	printf("Init Contact Success!\n");
}
void Show(contact_p ct)   //显示
{
	if (ct->size == 0){
		printf("Contact Empty!");
		Sleep(3000);
		exit(0);
	}
	int top = ct->size;
	int i = 0;
	person_p p = ct->list;  //从第一个元素开始显示,总共显示SIZE个元素
	printf("|name  |street|city|eip         |state           |\n");
	for (i = 0; i < top; i++)
	{
		printf("|%-6s|%-6s|%-4s|%-12s|%-16s|\n", p[i].name, p[i].street, p[i].city, p[i].eip, p[i].state);
	}
}
void AddPerson(contact_pp ct)  //添加联系人
{
	if (!IsFull(*ct) || Inc(ct))   //通讯录没满或者扩容成功才可进行元素添加
	{
		person_p p = &((*ct)->list[(*ct)->size]);
		printf("please input Name:");
		scanf("  %s",p->name);
		printf("please input Street:");
		scanf("  %s",(p->street));
		printf("please input City:");
		scanf("  %s",(p->city));
		printf("please input Eip:");
		scanf("  %s",p->eip);
		printf("please input State:");
		scanf("  %s",p->state);
		((*ct)->size)++;
	}
	else
	{
		printf("Realloc error!\n");
	}
}
 void Delperson(contact_p ct)  //删除联系人
{
	assert(ct != NULL);
	int count = SearchPerson(ct);  //使用count进行接收查询后返回的值,返回值为i或者-1
	int i = 0;
	if (count!=-1)  //若返回值不为-1即返回的是查找的联系人的位置
	{
		for (i=count; i < ct->size; i++)  //从查找到的位置开始,后面的元素对前面的元素进行覆盖
		{
			ct->list[i] = ct->list[i + 1];  
		}
				(ct->size)--;   //删除结束后,将通讯录中的容量减少一
		printf("SUCCESS DELECT!\n");
	}
}
	
void Modperson(contact_p ct)  //修改联系人
{
	assert(ct != NULL);
	char name[15] = { 0 };
	int i = 0;
	printf("Please input the name you want to mod:");
	scanf("%s", name);
	for (i = 0; i < ct->size; i++){    //从第一个联系人与输入的姓名进行字符串比较,若结果为0,则匹配成功
		if (strcmp(name, ct->list[i].name) == 0)  //匹配成功进行联系人修改
		{
			printf("please input Name:");
			scanf("  %s", ct->list[i].name);
			printf("please input Street:");
			scanf("  %s", &(ct->list[i].street));
			printf("please input City:");
			scanf("  %s",&( ct->list[i].city));
			printf("please input EIP:");
			scanf("  %s", ct->list[i].eip);
			printf("please input State:");
			scanf("  %s", ct->list[i].state);
		}
	}
}
int SearchPerson(contact_p ct)  //查询联系人,从第一个元素开始对比,若匹配成功,输出联系人信息
{
	assert(ct != NULL);
	int i = 0;
	char name[15] = { 0 };
	if (ct->size == 0)
	{
		printf("Contact Empty\n");
		return -1;
	}
	printf("Please input the name:");
	scanf("%s", name);
	for (i = 0; i <= ct->size; i++)
	{
		if (strcmp(name, ct->list[i].name)==0)
		{
			printf("|%-6s|%-2s|%-3s|%-12s|%-16s|\n", ct->list[i].name, ct->list[i].street, ct->list[i].city, ct->list[i].eip,\
				ct->list[i].state);
			return i;
		}	
	}
	printf("No Contect!\n");
	return -1;
}
static int person_cmp(const void *x, const void *y)
{
	person_p _x = (person_p)x;
	person_p _y = (person_p)y;
	return strcmp(_x->name, _y->name);
}
void Sort(contact_p ct)   //根据姓名对联系人进行排序
{
	assert(ct != NULL);
	person_p p = ct->list;
	qsort(p, ct->size, sizeof(person_t), person_cmp);  //调用qsort函数进行排序
}
void ClearPerson(contact_p ct)   //清空联系人列表,只需要将里面元素的个数置1
{
	 ct->size = 0;
	 printf("Success Clear!\n");
}

#include "tongxun.h"
void Menu()
{
	printf("*********************************************\n");
	printf("**  1. Show      2. Add        3. Del  ******\n");
	printf("**  4. Mod       5. Search     6. Sort ******\n");
	printf("**  7. Clear                   8. Exit ******\n");
	printf("*********************************************\n");
}
int main()
{
	int input = 0;
	int quit = 1;
	contact_p ct;
	InitContact(&ct);
	while (quit)
	{
		 Menu();
		printf("Please chose the number:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			Show(ct);
			break;
		case 2:
			AddPerson(&ct);
			break;
		case 3:
			Delperson(ct);
			break;
		case 4:
			Modperson(ct);
			break;
		case 5:
			SearchPerson(ct);
			break;
		case 6:	
			Sort(ct);
			break;
		case 7:
			ClearPerson(ct);
			break;
		case 8:
			quit = 0;
			break;
		default:
			printf("Wrong Chose!Please Chose again!\n");
			break;
		}
	}
	system("pause");
	return 0;
}

你可能感兴趣的:(通讯录)