通讯录终极版(文件)

       之前我已经写过了两篇通讯录,第一篇只是简单的实现通讯录的一些操作,使用顺序表,但是它存在的缺点是向通讯录添加的人数是固定的,当通讯录满的时候就不能再添加了。

      这时才有了第二篇,第二篇是在第一篇的基础上改进的,给通讯录动态开辟空间,当通讯录满的时候还可以追加内存空间。

      不过第二版也有它的缺点,那就是每次执行程序时你都必须把通讯录中的成员信息再重新输入一遍,当通讯录中的人员数量特别大时,这样就会非常麻烦,这时为 了解决这个问题,我打算将写进通讯录中的信息全部写进文件里,再我再次需要这些信息的时候,只需要把这些信息再读出来就可以了。



#pragma once
#pragma warning (disable : 4996)
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>

#define INITSIZE 128
#define INCREMENT 128
#define FILENAME "contact.txt"

typedef struct person
{
	char name[32];
	char sex;
	unsigned char age;
	char phone[16];
	char addr[128];
}person_t,*person_p,**person_pp;

typedef struct contact
{
	person_p contactlist;
	int cap;
	int size;
}contact_t,*contact_p,**contact_pp;

void InitContactlist(contact_pp c);

void DestoryContactlist(contact_p c);

int isContactFull(contact_p c);

int Increment(contact_p c);

void Addcontact(contact_p c,person_p p);

void PrintContact(contact_p c);

void Delete(contact_p c,const char *name);

void ClearContact(contact_p c);

void SearchContact(contact_p c,const char *name);

void modcontact(contact_p c,const char *name);


void sort(contact_p c);

int fileload(contact_p c);

int filestore(contact_p c);



#include"contact.h"

void InitContactlist(contact_pp c)
{
	assert(c);
	*c = (contact_p)malloc(sizeof(contact_t));
	if (NULL == *c)
	{
		perror("malloc");
		exit(EXIT_FAILURE);
	}
	(*c)->contactlist = (person_p)malloc(sizeof(person_t) * INITSIZE );
	if (NULL == (*c)->contactlist)
	{
		perror("malloc");
		exit(EXIT_FAILURE);
	}
	(*c)->cap = INITSIZE;
	(*c)->size = 0;
	fileload(*c);
}


static int filestore(contact_p c)
{
	assert(c);
	FILE *fpstore = fopen(FILENAME, "wb");
	if (NULL == fpstore)
	{
		perror("fopen");
		return 0;
	}
	int i = 0;
	for (i = 0; i < c->size; i++)
	{
		fwrite(c->contactlist+i,sizeof(person_t),1,fpstore);
	}
	fclose(fpstore);
}

static int fileload(contact_p c)
{
	assert(c);
	FILE *fpload = fopen(FILENAME, "rb");
	if (NULL == fpload)
	{
		perror("fopen");
		return 0;
	}
	int i = 0;
	person_t tmp = {0};
	while (fread(&tmp,sizeof(person_t),1,fpload))
	{
		c->contactlist[i++] = tmp;
		c->size++;
	}
	fclose(fpload);
}


void DestoryContactlist(contact_p c)
{
	assert(c);
	filestore(c);
	free(c->contactlist);
	c->contactlist = NULL;
	free(c);
	c = NULL;
}

//full->1  not full->0 
static int isContactFull(contact_p c)
{
	assert(c);
	return c->size >= c->cap ? 1:0;
}


static int Increment(contact_p c)
{
	assert(c);
	person_p newbase = (person_p)realloc(c->contactlist,sizeof(person_t) * (INITSIZE + INCREMENT));
	if (NULL == newbase)
	{
		perror("realloc");
		return 0;
	}
	c->contactlist = newbase;
	c->cap += INCREMENT;
	return 1;
}


void Addcontact(contact_p c,person_p p)
{
	assert(c);
	assert(p);
	if (!isContactFull(c) || Increment(c) )//通讯录没满或增容成功
	{
		c->contactlist[c->size] = *p;
		c->size++;
	}
}


void swap(person_p xp,person_p yp )
{
	assert(xp);
	assert(yp);
	person_t tmp = *xp;
	*xp = *yp;
	*yp = *xp;
}


void Delete(contact_p c,const char *name)
{
	assert(c);
	assert(name);
	int i = 0;
	for (i = 0; i < c->size; i++)
	{
		if (strcmp(c->contactlist[i].name,name) == 0)
		{
			swap(c->contactlist + i, c->contactlist + c->size - 1);
			c->size--;
		}
	}
}


void PrintContact(contact_p c)
{
	assert(c);
	int i = 0;
	for ( i = 0; i < c->size; i++)
	{
		printf("%-16s %-3c %-16d %-16s %-32s\n", c->contactlist[i].name,\
			c->contactlist[i].sex,c->contactlist[i].age,c->contactlist[i].phone,\
			c->contactlist[i].phone,c->contactlist[i].addr);
	}
}



void ClearContact(contact_p c)
{
	assert(c);
	c->size = 0;
}


void SearchContact(contact_p c,const char *name)
{
	assert(c);
	assert(name);
	int i = 0;
	for (i = 0; i < c->size; i++)
	{
		if (strcmp(c->contactlist[i].name,name) == 0)
		{
			printf("%-16s %-3c %-16d %-16s %-32s\n", c->contactlist[i].name,\
				c->contactlist[i].sex,c->contactlist[i].age,c->contactlist[i].phone,\
				c->contactlist[i].phone,c->contactlist[i].addr);
			break;
		}
	}
	if (i == c->size)
	{
		printf("not found\n");
	}
}


void modcontact(contact_p c,const char *name)
{
	assert(c);
	assert(name);
	int i = 0;
	for (i = 0; i < c->size; i++)
	{
		if (strcmp(c->contactlist[i].name,name) == 0)
			{	
				printf("please enter your modified message<name sex age phone addr>\n");
				scanf("%s %c %d %s %s", c->contactlist[i].name,\
					&c->contactlist[i].sex,&c->contactlist[i].age,\
					&c->contactlist[i].phone,c->contactlist[i].addr);
				return ;
			}
	}
	printf("not find you modified name\n");
}


static int cmpContactlist(const void * xp,const void *yp)
{
	assert(xp);
	assert(yp);
	person_p _xp = (person_p)xp;
	person_p _yp = (person_p)yp;
	return strcmp(_xp->name,_yp->name);
}

void sort(contact_p c)
{
	assert(c);
	if (c->size > 1)
	{
		qsort(c->contactlist,c->size ,sizeof(person_t), cmpContactlist);
	}
}




#include"contact.h"

static void usage()
{
	printf("Usage: Please Enter Right code[0-7]!\n");
}

static void  my_add(contact_p c)
{
	assert(c);
	person_t p;
	printf("please enter :<name sex age phone addr>");
	scanf("%s %c %d %s %s",p.name,&p.sex,&p.age,p.phone,p.addr);
	Addcontact(c,&p);
	printf("add success\n");
}

static void my_delete(contact_p c)
{
	assert(c);
	printf("please enter your delete name\n");
	char name[16] = {0};
	scanf("%s",name);
	Delete(c,name);
}

int main()
{
	contact_p mycontact = NULL;
	InitContactlist(&mycontact);
	int s = 0;
	while (1)
	{
		printf("######################################\n");
		printf("##1.add    ######	2.delete  ########\n");
		printf("##3.modify ######	4.search  ########\n");
		printf("##5.print  ######	6.clear   ########\n");
		printf("##7.sort   ######	0.quit    ########\n");
		printf("######################################\n");
		printf("please select :<0 - 7>");
		scanf("%d",&s);
		switch(s)
		{
			case 0:
				printf("bye ~...");
				exit(EXIT_SUCCESS);
				break;
			case 1:
				  my_add(mycontact);
				break;
			case 2:
				my_delete(mycontact);
				break;
			case 3:
				char name[32];
				printf("please enter name (modify)");
				scanf("%s",name);
				modcontact(mycontact,name);
				break;
			case 4:
				char searchname[16];
				printf("please enter name (modify)");
				scanf("%s",searchname);
				SearchContact(mycontact,searchname);
				break;
			case 5:
				PrintContact(mycontact);
				break;
			case 6:
				  ClearContact(mycontact);
				break;
			case 7:
				sort(mycontact);
				break;
			default :
				usage();
				break;
		}
	}
	system("pause");
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值