其他分享
首页 > 其他分享> > 通讯录

通讯录

作者:互联网

实现一个通讯录; 
通讯录可以用来存储1000个人的信息,每个人的信息包括: 
姓名、性别、年龄、电话、住址 

提供方法: 
1. 添加联系人信息 
2. 删除指定联系人信息 
3. 查找指定联系人信息 
4. 修改指定联系人信息 
5. 显示所有联系人信息 
6. 清空所有联系人 
7. 以名字排序所有联系人 
8. 保存联系人到文件 
9. 加载联系人 
 

 

contact.h

#ifndef  ___CONTACT__H__
#define ___CONTACT__H__
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include<assert.h>
//#include<vld.h>
enum Oper
{
	EXIT,
	ADD,
	DEL,
	SEAR,
	SHOW,
	CLEAR,
	SORT,
	DESTORY
};
#define MAX_NAME 10
#define MAX_SEX  5
#define MAX_TELE  11
#define MAX_ADDR 20

#define MAX_NUMBER 1000

#define DEFAULT_SIZE 2

typedef struct PersonInfo
{
	char name[MAX_NAME];//姓名
	short age;
	char sex[MAX_SEX];//性别
	char tele[MAX_TELE];
	char addr[MAX_ADDR];

}PersonInfo;//每一个人的信息

typedef struct Contact
{
	//PersonInfo per[MAX_NUMBER]; 普通版本
	PersonInfo *per;
	int usedSize;//
	int cpaticty;//容量
}Contact;//通讯录

void InitContact(Contact *pcon);
void AddContact(Contact *pcon);
void ShowContact(Contact *pcon);
void DelContact(Contact *pcon);

void ClearContact(Contact *pcon);


void DestoryContact(Contact *pcon);
#endif 

 

contact.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"

void LoadContact(Contact *pcon)
{
	FILE *pf = fopen("Contact.bat","rb");
	PersonInfo tmp = {0};
	if(pf == NULL)
	{
		return;
	}
	//fread函数的返回值是:读取成功的字节数
	while(fread(&tmp,sizeof(PersonInfo),1,pf) > 0)
	{
		//必须判断是否为满,如果满了扩容
		CheckFullAndRe(pcon);
		pcon->per[pcon->usedSize++] = tmp;
	}
	fclose(pf);
	pf = NULL;
}

void InitContact(Contact *pcon)
{
	/*pcon->usedSize = 0; 普通版本
	memset(pcon->per,0,sizeof(pcon->per));*/

	pcon ->usedSize = 0;
	pcon->cpaticty = DEFAULT_SIZE;
	pcon->per = (PersonInfo *)malloc(
		sizeof(PersonInfo) * pcon->cpaticty);
	assert(pcon->per != NULL);

	LoadContact(pcon);
}
//返回值代表 扩容是否成功  1
static int CheckFullAndRe(Contact *pcon)
{
	if(pcon->usedSize == pcon->cpaticty)
	{
		PersonInfo * ptr = NULL;
		ptr = (PersonInfo *)realloc(pcon->per,
			sizeof(PersonInfo) * pcon->cpaticty * 2);
		if(ptr !=NULL)
		{
			pcon->per = ptr;
			pcon->cpaticty *= 2; 
			printf("扩容成功\n");
			return 1;
		}
		else
		{
			return 0;//扩容失败
		}
	}

	return 1;
}

void AddContact(Contact *pcon)
{
	/*if(pcon->usedSize == MAX_NUMBER)
	{
		printf("不好意思老铁,满了\n");
		return;
	}*/

	if(CheckFullAndRe(pcon) != 1)
	{
		printf("扩容失败\n");
		return;
	}

	printf("请输入姓名:");
	scanf("%s",pcon->per[pcon->usedSize].name);
	printf("请输入年龄:");
	scanf("%d",&(pcon->per[pcon->usedSize].age));
	printf("请输入性别:");
	scanf("%s",pcon->per[pcon->usedSize].sex);
	printf("请输入电话:");
	scanf("%s",pcon->per[pcon->usedSize].tele);
	printf("请输入住址:");
	scanf("%s",pcon->per[pcon->usedSize].addr);
	pcon->usedSize++;
	printf("添加成功\n");
}

int SearchContact(Contact *pcon)
{
	int i = 0;
	char name[MAX_NAME] = {0};
	if(pcon->usedSize == 0)
	{
		printf("通讯录为空\n");
		return -1;
	}
	printf("请输入你要删除的姓名:");
	scanf("%s",name);
	for(i = 0;i < pcon->usedSize;i++)
	{
		if(strcmp(pcon->per[i].name,name) == 0)
		{
			return i;
		}
	}
	return -1;
}

void DelContact(Contact *pcon)
{
	int index = SearchContact(pcon);
	int i = 0;
	if(index == -1)
	{
		printf("查无此人\n");
		return;
	}
	for(i = index;i < pcon->usedSize-1;i++)
	{
		pcon->per[i] = pcon->per[i+1];
	}
	pcon->usedSize--;
	printf("删除成功\n");
}


void ShowContact(Contact *pcon)
{
	int i = 0;
	printf("%-10s %-5s %-5s %-11s %-20s\n","姓名","年龄",
		"性别","电话","住址");
	for(i = 0;i < pcon->usedSize;i++)
	{
		printf("%-10s %-5d %-5s %-11s %-20s\n",
			pcon->per[i].name,pcon->per[i].age,
			pcon->per[i].sex,pcon->per[i].tele,
			pcon->per[i].addr);
	}
}

void ClearContact(Contact *pcon)
{
	pcon->usedSize = 0;
}
//........... 
void SaveContact(Contact *pcon)
{
	int i = 0;
	FILE *pf = fopen("Contact.bat","wb");
	assert(pf != NULL);
	for(i = 0;i < pcon->usedSize;i++)
	{
		fwrite(pcon->per+i,sizeof(PersonInfo),1,pf);
	}
	fclose(pf);
	pf = NULL;
}
void DestoryContact(Contact *pcon)
{
	SaveContact(pcon);
	free(pcon->per);
	pcon->per = NULL;//预防野指针  
	pcon->cpaticty = 0;
	pcon->usedSize = 0;
}

 

 

 

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"

//char *strncpy( char *strDest, 
//const char *strSource, size_t count );
char *MyStrncpy(char *dest,const char *src,int count)
{
	//char *p = dest;
	//while(*dest++ = *src++);
	//return p;
	char *p = dest;
	int i = 0;
	while(i < count)
	{
		*dest = *src;
		dest++;
		src++;
		i++;
	}
	return p;
}


//char *strncat( char *strDest, 
//const char *strSource, size_t count );
char * Mystrncat(char *dest,const char *src,int count)
{
	char *p = dest;
	int i = 0;
	while(*dest != '\0')
	{
		dest++;
	}
	//*dest = '\0';
	while(i < count)
	{
		*dest = *src;
		dest++;
		src++;
		i++;
	}
	return p;
}
//str1和str2比较
int MyStrcmp(const char *str1,const char *str2)
{
	while(*str1 == *str2)
	{
		if(*str1 == '\0')
		{
			return 0;
		}
		str1++;
		str2++;
	}
	if(*str1 > *str2)
	{
		return 1;
	}
	else
	{
		return -1;
	}
}
int MyStrncmp(const char *str1,const char *str2,int count)
{
	int i = 0;
	int differ = 0;
	while(i < count && ((differ = *str1-*str2)==0) && *str1 != '\0')
	{
		str1++;
		str2++;
		i++;
	}
	return differ;
}
int main()
{
	char str1[20] = "billo";
	char *str2 = "bit";
	int n = MyStrncmp(str1,str2,2);
	printf("%d\n",n);
	return 0;
}

#if 0

/*
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人
8.更新    
**/
void menu()
{
	printf("*************欢迎使用XXX通讯录**********\n");
	printf("********0.exit***********1.add************\n");
	printf("********2.del************3.search***********\n");
	printf("********4.show**********5.clear*************\n");
	printf("********6.sort***********7.destory***********\n");
	printf("***********************************************\n");
	
}


int main()
{
	int input = 0;
	Contact con;
	InitContact(&con);
	do
	{
		menu();
		printf("请输入您的操作:");
		scanf("%d",&input);
		switch(input)
		{
		case EXIT:
			//DestoryContact(&con);
			break;
		case ADD:
			AddContact(&con);
			break;
		case DEL:
			DelContact(&con);
			break;
		case SEAR:
			break;
		case SHOW:
			ShowContact(&con);
				break;
		case CLEAR:
			ClearContact(&con);
				break;
		case SORT:
				break;
		case DESTORY:
			DestoryContact(&con);
			break;
		default:
					break;
		}
	}while(input);
	return 0;
}
#endif

 

标签:pcon,per,char,int,Contact,通讯录,printf
来源: https://blog.csdn.net/NEFELIBATA_Reset/article/details/95920551