通讯录(文本版)

与之前三子棋和扫雷一样,将实现函数先封装到头文件contact.h中,之后有两个源文件,test.c程序运行和contact.c的函数实现


Contact.h

#define _CRT_SECURE_NO_WARNINGS 1

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

#ifndef _CONTACT_H__
#define _CONTACT_H__

#define DEFAULT_SZ 3
#define DEFAULT_INC 2
#define Max_name 20
#define Max_sex  10
#define Max_tel  12
#define Max_addr 20
#define FILENAME "contact.dat"

typedef struct PeoInfo
{
	char name[Max_name];
	int age;
	char sex[Max_sex];
	char tel[Max_tel];
	char addr[Max_addr];

}PeoInfo;

typedef struct Contact
{
	PeoInfo *contact; //数据存放的位置
	int capacity;     //容量
	int sz;           
}Contact,*pContact;

void InitContact(pContact pcontact);
void AddContact(pContact pcontact);
void ShowContact(const pContact pcontact);
void SearchContact(pContact pcontact);
int FindContact(pContact pcontact,char name[]);
void DelContact(pContact pcontact);
void ModifyContact(pContact pcontact);
void EmptyContact(pContact pcontact);
void SortContact(pContact pcontact);
void DistoryContact(pContact pcontact);
int CheckContact(pContact pcontact);
void LoadContact(pContact pcontact);
void SaveContact(pContact pcontact);
#endif //_CONTACT_H__

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"

enum SEL
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SHOW,
	EMPTY,
	SORT
};
void menu()
{
	printf("**********************************\n");
	printf("*****    1.add        2.del    ***\n");
	printf("*****    3.search     4.modify ***\n");
	printf("*****    5.show       6.empty  ***\n");
	printf("*****    7.sort       0.exit   ***\n");
	printf("**********************************\n");

}
void test()
{ 
	 int i = 0;
     Contact my_contact;
	 InitContact(&my_contact);
     
	 do 
	 {
		 menu();
		 printf("请输入:>");
		 scanf("%d",&i);

		 switch(i)
		 {
		 case ADD:
			 AddContact(&my_contact);break;
		 case DEL:
			 DelContact(&my_contact);break;
		 case SEARCH:
			 SearchContact(&my_contact);break;
		 case MODIFY:
			 ModifyContact(&my_contact);break;
		 case SHOW:
			 ShowContact(&my_contact);break;
		 case EMPTY:
			 EmptyContact(&my_contact);break;
		 case SORT:
			 SortContact(&my_contact);break;
		 case EXIT:
			 SaveContact(&my_contact);
			 DistoryContact(&my_contact);break;
		 default:printf("选择错误\n");
			 break;
		 }
	 } while (i);
	
}

int main()
{ 
	test();
	return 0;
}

contact.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"

void _ADD(PeoInfo *tmp,pContact pcontact)
{
    CheckContact(pcontact);
	pcontact->contact[pcontact->sz] = *tmp;
	pcontact->sz++;
}
void LoadContact(pContact pcontact)
{
	PeoInfo tmp = {0};
	int i = 0;
   FILE* pfin = fopen(FILENAME,"r");
   if (pfin == NULL)
   {
	   perror("open the file for read");
	   exit(EXIT_FAILURE);
   }
   while (fread(&tmp,sizeof(PeoInfo),1,pfin))
   {
	   _ADD(&tmp,pcontact);
   }
   fclose(pfin);
}
void SaveContact(pContact pcontact)
{
	FILE* pfout = fopen(FILENAME,"w");
	int i = 0;
	if (pfout == NULL)
	{
		perror("open the file for write");
		exit(EXIT_FAILURE);
	}
    for (i=0; i<pcontact->sz; i++)
    {
		fwrite(pcontact->contact+i,sizeof(PeoInfo),1,pfout);
    }
	fclose(pfout);
}
void InitContact(pContact pcontact)
{
	pcontact->sz = 0;
	
	pcontact->contact = (PeoInfo*)malloc(DEFAULT_SZ*sizeof(PeoInfo));
    if (NULL == pcontact->contact)
    {
		perror("InitContact::malloc");
		exit(EXIT_FAILURE);
    }
	memset(pcontact->contact,0,DEFAULT_SZ*sizeof(PeoInfo));
    pcontact->capacity = DEFAULT_SZ;
	LoadContact(pcontact);
}
void DistoryContact(pContact pcontact)
{
	free(pcontact->contact);
	pcontact->contact = NULL;
	pcontact->sz = 0;
	pcontact->capacity = 0;
}
int CheckContact(pContact pcontact)
{
   if (pcontact->sz == pcontact->capacity)
   {
	  PeoInfo* tmp =  realloc(pcontact->contact, (pcontact->capacity+DEFAULT_INC)*sizeof(PeoInfo));
      if (tmp!=NULL)
      {
		  pcontact->contact = tmp;
		  pcontact->capacity += DEFAULT_INC;
		  printf("增容成功");
		  return 1;
      }
	  else
	  {
		  return 0;
	  }
   }
   return 1;
}
void AddContact(pContact pcontact)
{
	CheckContact(pcontact);
	if (CheckContact(pcontact)==0)
	{
		return;
	}
	
   printf("请输入名字:>");
   scanf("%s",pcontact->contact[pcontact->sz].name);
   printf("请输入年龄:>");
   scanf("%d",&(pcontact->contact[pcontact->sz].age));
   printf("请输入性别:>");
   scanf("%s",pcontact->contact[pcontact->sz].sex);
   printf("请输入电话:>");
   scanf("%s",pcontact->contact[pcontact->sz].tel);
   printf("请输入地址:>");
   scanf("%s",pcontact->contact[pcontact->sz].addr);
   
   pcontact->sz++;

   printf("添加成功\n");
}
void ShowContact(const pContact pcontact)
{
	int i = 0;
	printf("%5s\t%5s\t%5s\t%15s\t%20s\t\n","Name","Age","Sex","Tel","Addr");
    for (i=0;i<pcontact->sz;i++)
    {
		printf("%5s\t%5d\t%5s\t%15s\t%20s\t\n",pcontact->contact[i].name,
			                pcontact->contact[i].age ,
							pcontact->contact[i].sex ,
							pcontact->contact[i].tel ,
							pcontact->contact[i].addr);
    }
	
}
int FindContact(pContact pcontact,char name[])
{
	int i = 0;
	for (i=0;i<pcontact->sz;i++)
	{
		if (strcmp(name,pcontact->contact[i].name)==0)
		{
			return i;
		}
	}
	return -1;
}
void SearchContact(pContact pcontact)
{
	int ret = 0;
	char name[Max_name] = {0};
	printf("请输入需要查找的姓名:>");
	scanf("%s",name);
	
	ret = FindContact(pcontact,name);
	if (ret == -1)
	{
		printf("不存在该联系人\n");
		return;
	}
	printf("%5s\t%5s\t%5s\t%15s\t%20s\t\n","Name","Age","Sex","Tel","Addr");
	printf("%5s\t%5d\t%5s\t%15s\t%20s\t\n",pcontact->contact[ret].name,
		pcontact->contact[ret].age ,
		pcontact->contact[ret].sex ,
		pcontact->contact[ret].tel ,
		pcontact->contact[ret].addr);
	return ;
}
void DelContact(pContact pcontact)
{
	int ret = 0;
	char name[Max_name] = {0};
	printf("请输入需要删除人的姓名:>");
	scanf("%s",name);

	 ret = FindContact(pcontact,name);
	if (ret == -1)
	{
		printf("不存在该联系人\n");
		return;
	}
	memmove(pcontact->contact+ret,
		    pcontact->contact+ret+1,
		   (pcontact->sz-ret-1)*sizeof(PeoInfo));
	pcontact->sz--;
	printf("删除成功\n");

	return ;
}

void ModifyContact(pContact pcontact)
{
	int ret = 0;
	char name[Max_name] = {0};
	printf("请输入需要修改人的姓名:>");
	scanf("%s",name);

	 ret = FindContact(pcontact,name);
	if (ret == -1)
	{
		printf("不存在该联系人\n");
		return;
	}
	printf("请重新输入该联系人的信息:>");
	printf("请输入名字:>");
	scanf("%s",pcontact->contact[ret].name);
	printf("请输入年龄:>");
	scanf("%d",&(pcontact->contact[ret].age));
	printf("请输入性别:>");
	scanf("%s",pcontact->contact[ret].sex);
	printf("请输入电话:>");
	scanf("%s",pcontact->contact[ret].tel);
	printf("请输入地址:>");
	scanf("%s",pcontact->contact[ret].addr);
   
	printf("修改成功\n");
	printf("%5s\t%5s\t%5s\t%15s\t%20s\t\n","Name","Age","Sex","Tel","Addr");
	printf("%5s\t%5d\t%5s\t%15s\t%20s\t\n",pcontact->contact[ret].name,
		pcontact->contact[ret].age ,
		pcontact->contact[ret].sex ,
		pcontact->contact[ret].tel ,
		pcontact->contact[ret].addr);
   return;
}

void EmptyContact(pContact pcontact)
{
	InitContact(pcontact);
}
void SortContact(pContact pcontact)
{
	int i = 0;
	int j = 0;
	int count = 0;
   for (i=0; i<pcontact->sz-1; i++)
   {
	   for (j=0; j<pcontact->sz-i-1; j++)
	   {
		   if (strcmp(pcontact->contact[j].name,pcontact->contact[j+1].name)==1)
		   {
			   PeoInfo tmp = pcontact->contact[j];
               pcontact->contact[j] = pcontact->contact[j+1];
			   pcontact->contact[j+1] = tmp;
			   count++;
		   }
	   }
	   if (count!=0)
	   {
		   continue;
	   }
	   else
		   return;
   }
   ShowContact(pcontact);
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值