Address book (dynamic implementation and file optimization version)

Implement an address book:

The address book can be used to store the information of 1000 individuals, each person's information includes: name, gender, age, phone number, address
Provide method:
        add contact information;
        delete specified contact information;
        find specified contact information;
        modify specified contact Information;
        display all contact information;
        clear all contacts;
        sort all contacts by name;

first step:

Create test.c, tool.c, and too1.h three files respectively to save the interface implementation, game implementation, and function declaration.

Step 2: test.c (source file)

Create a menu (menu) in the main function of the source file and tell the player how to operate it!

And use the do-while statement to enter the operation interface, and realize operation input and display through scanf.

The user performs different operations by performing different digital inputs according to requirements.

Since then, the basic operation of the source file has been completed.

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1


#include "test.h"

enum Option
{
	EXIT,
	ADD,
	DELE,
	SEARCH,
	MODIFY,
	SHOW,
	SORT,
	CLEAR
};


void menu()
{
	printf("****1.添加******\n");
	printf("****2.删除******\n");
	printf("****3.查找******\n");
	printf("****4.修改******\n");
	printf("****5.显示******\n");
	printf("****6.排序******\n");
	printf("****7.清空******\n");
	printf("****0.退出******\n");
}

int main()
{
	Contact con;//通讯录
	//初始化通讯录
	InitContact(&con);

	int input = 0;
	do
	{
		menu();
		printf("请输入要执行的操作:\n");
		scanf("%d",&input);
		switch (input)
		{
		case ADD:
			ADDContact(&con);
			break;
		case DELE:
			DELEContact(&con);
			break;
		case SEARCH:
			SEARCHContact(&con);
			break;
		case MODIFY:
			MODIFYContact(&con);
			break;
		case SHOW:
			SHOWContact(&con);
			break;
		case SORT:
			SORTContact(&con);
			break;
		case EXIT:
			SAVEContact(&con);
			DestroyContact(&con);
			printf("退出通讯录成功!\n");
			break;
		case CLEAR:
			CLEARContact(&con);
			printf("清空成功!\n");
			break;
		default :
			printf("输入格式错误,请重新输入!\n");
			break;
		}
	} while (input);
	return 0;
}

 The third step: tool.h (address book function declaration)

        Create the structure of the address book, and use this structure to call the structure that stores these information; complete the addition of contact list ADDContact() in tool.h, delete the specified contact DELEContact(), and find the specified contact SEARCHContact( ), modify the specified contact MODIFYContact(), print the information in the communication SHOWContact(), sort by name SORTContact(), save the information of the address book to the file SAVEContact(), destroy the address book CLEARContact(), load the information of the file to Address book LoadContact(), clear the function declaration of address book CLEARContact().

#pragma once
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>


//扩容时的扩容值
#define DEFAULT_SZ 3
#define INC_SZ 2


#define MAX 100
#define MAX_NAME  20
#define MAX_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30


//
typedef struct PeoInfo
{
	char name[MAX_NAME];
	int age;
	char sex[MAX_SEX];
	char tele[MAX_TELE];
	char addr[MAX_ADDR];
}PeoInfo;




//通讯录结构体
typedef struct Contact
{
	PeoInfo* data;//存放人的信息
	int count;//记录当前通讯录中实际人的个数
	int capacity;//当前通讯录的容量
}Contact;


//初始化通讯录
int InitContact(Contact* pc);

//销毁通讯录
void DestroyContact(Contact* pc);

//增加联系人都通讯录
void ADDContact(Contact* pc);

//打印通讯中的信息
void SHOWContact(const Contact* pc);

//删除指定联系人
void DELEContact(Contact* pc);

//查找指定联系人
void SEARCHContact(Contact* pc);

//修改指定联系人
void MODIFYContact(Contact* pc);

//排序通讯录中内容
//按照名字来排序
void SORTContact(Contact* pc);

//保存通讯录的信息到文件
void SAVEContact(const Contact* pc);

//加载文件的信息到通讯录
void LoadContact(Contact* pc);

//清空通讯录
void CLEARContact(Contact* pc);

Step 4: tool.c (address book implementation)

Complete the initialization of the address book InitContact() in tool.h, add contacts to the address book ADDContact(), delete the specified contact DELEContact(), find the specified contact SEARCHContact(), modify the specified contact MODIFYContact(), and print the         communication information SHOWContact(), sort SORTContact() by name, save address book information to file SAVEContact(), destroy address book DestroyContact(), load file information to address book LoadContact(), clear address book CLEARContact( ) accomplish.

first:

        Initialize InitContac) for the address book, open up (PeoInfo*) type dynamic memory , and set the dynamic capacity to DEFAULT_SZ and then load the previous address book information . If the opened memory is not enough, then dynamically open up INC_SZ more memory spaces .

#include "test.h"

//初始化通讯录
//void InitContact(Contact* pc)
//{
//	assert(pc);
//	pc->count = 0;
//	memset(pc->data,0,sizeof(pc->data));
//}

void CheckCapacity(Contact* pc)
{
	if (pc->count == pc->capacity)
	{
		PeoInfo* ptr = (PeoInfo*)realloc(pc->data, (pc->capacity + INC_SZ) * sizeof(PeoInfo));

		if (ptr == NULL)
		{
			printf("AddContact::%s\n", strerror(errno));
			return;
		}
		else
		{
			pc->data = ptr;
			pc->capacity += INC_SZ;
			printf("增容成功\n");
		}
	}
}

//动态的版本
int InitContact(Contact* pc)
{
	assert(pc);
	pc->count = 0;
	pc->data = (PeoInfo*)calloc(DEFAULT_SZ, sizeof(PeoInfo));
	if (pc->data == NULL)
	{
		printf("InitContact::%s\n", strerror(errno));
		return 1;
	}
	
	pc->capacity = DEFAULT_SZ;
	//加载文件的信息到通讯录中
	LoadContact(pc);

	return 0;
}

Load the information of the file to the address book:


void LoadContact(Contact* pc)
{
	FILE* pfRead = fopen("contact.txt", "rb");
	if (pfRead == NULL)
	{
		perror("LoadContact");
		return;
	}

	PeoInfo tmp = { 0 };
	while (fread(&tmp, sizeof(PeoInfo), 1, pfRead) == 1)
	{
		CheckCapacity(pc);

		pc->data[pc->count] = tmp;
		pc->count++;
	}
	fclose(pfRead);
	pfRead = NULL;
}

 Destroy address book:

void DestroyContact(Contact* pc)
{
	assert(pc);
	free(pc->data);
	pc->data = NULL;
}

Add all contacts to the address book:

//动态的版本
void ADDContact(Contact* pc)
{
	assert(pc);
	// 增容
	CheckCapacity(pc);
	/*if (pc->count == MAX)
	{
		printf("通讯录已满!\n");
		return;
	}*/

	printf("请输入名字:\n");
	scanf("%s",pc->data[pc->count].name);
	printf("请输入年龄:\n");
	scanf("%d",&(pc->data[pc->count].age));
	printf("请输入性别:\n");
	scanf("%s",pc->data[pc->count].sex);
	printf("请输入电话:\n");
	scanf("%s",pc->data[pc->count].tele);
	printf("请输入地址:\n");
	scanf("%s",pc->data[pc->count].addr);

	pc->count++;
	printf("添加成功!\n");
}

Print the information in the newsletter:

void SHOWContact(const Contact* pc)
{
	assert(pc);
	int i = 0;
	printf("%-20s\t%-5s\t%-5s\t%-12s\t%-20s\n","姓名","年龄","性别","电话","地址");
	for (i = 0;i < pc->count;i++)
	{
		printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n",pc->data[i].name,
													pc->data[i].age,
													pc->data[i].sex,
													pc->data[i].tele,
													pc->data[i].addr);
	}

}

Delete a specified contact:

static int FindByName(Contact* pc, char name[])
{
	assert(pc);
	int i = 0;
	for (i = 0; i < pc -> count; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}

void DELEContact(Contact* pc)
{
	assert(pc);
	int i = 0;
	char name[MAX_NAME] = { 0 };
	if (pc->count == 0)
	{
		printf("通讯录为空,没有可删除的信息!\n");
		return;
	}
	printf("请输入要删除的人的名字:\n");
	scanf("%s", name);
	//查找
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("需要删除的人不存在!\n");
		return;
	}
	//删除
	for (i = pos; i < pc->count - 1; i++)
	{
		pc->data[i] = pc->data[i + 1];
	}
	pc->count--;
	printf("删除成功!\n");

}

Find a specific contact:

void SEARCHContact(Contact* pc)
{
	assert(pc);
	char name[MAX_NAME] = { 0 };
	printf("请输入要查询的人的名字:\n");
	scanf("%s",name);
	int pos = FindByName(pc,name);
	if (pos == -1)
	{
		printf("没有查找到此人:\n");
		return;
	}
	//显示
	printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", "姓名", "年龄", "性别", "电话", "地址");
	printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n", pc->data[pos].name,
		pc->data[pos].age,
		pc->data[pos].sex,
		pc->data[pos].tele,
		pc->data[pos].addr);
}

Modify the specified contact:

void MODIFYContact(Contact* pc)
{
	assert(pc);
	char name[MAX_NAME] = { 0 };
	printf("请输入要修改信息的人的名字:\n");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("没有找到需要修改信息的人:\n");
		return;
	}
	printf("已找到需要修改信息的人,请修改:\n");
	//修改
	printf("请输入名字:\n");
	scanf("%s",pc->data[pos].name);
	printf("请输入年龄:\n");
	scanf("%d",&(pc->data[pos].age));
	printf("请输入性别:\n");
	scanf("%s",pc->data[pos].sex);
	printf("请输入电话:\n");
	scanf("%s",pc->data[pos].tele);
	printf("请输入地址:\n");
	scanf("%s",pc->data[pos].addr);

	printf("修改成功!\n");
}

Sort by name:

int cmp_Peo_age(const void* p1, const void* p2)
{
	return strcmp(((PeoInfo*)p1)->name, ((PeoInfo*)p2)->name);
}


//按照名字来排序
void SORTContact(Contact* pc)
{
	assert(pc);
	qsort(pc->data,pc->count,sizeof(PeoInfo),cmp_Peo_age);
	printf("排序完成!\n");
}

Save the address book information to a file:

void SAVEContact(const Contact* pc)
{
	assert(pc);
	FILE* pfWrite = fopen("contact.txt", "wb");
	if (pfWrite == NULL)
	{
		perror("SAVEContact");
		return;
	}
	//写文件-二进制的形式
	int i = 0;
	for (i = 0; i < pc->count; i++)
	{
		fwrite(pc->data+i,sizeof(PeoInfo),1,pfWrite);
	}
	fclose(pfWrite);
	pfWrite = NULL;
}

clear address book

        When exiting, save the information in the address book, and then release the dynamically opened memory

//清空
void CLEARContact(Contact* pc)
{
	assert(pc);
	pc->count = 0;
	pc->capacity = 0;
	memset(pc->data,0,sizeof(pc->data));

}

The following is a collection of all implementations of tool.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "test.h"



//初始化通讯录
//void InitContact(Contact* pc)
//{
//	assert(pc);
//	pc->count = 0;
//	memset(pc->data,0,sizeof(pc->data));
//}


void CheckCapacity(Contact* pc)
{
	if (pc->count == pc->capacity)
	{
		PeoInfo* ptr = (PeoInfo*)realloc(pc->data, (pc->capacity + INC_SZ) * sizeof(PeoInfo));

		if (ptr == NULL)
		{
			printf("AddContact::%s\n", strerror(errno));
			return;
		}
		else
		{
			pc->data = ptr;
			pc->capacity += INC_SZ;
			printf("增容成功\n");
		}
	}
}




void LoadContact(Contact* pc)
{
	FILE* pfRead = fopen("contact.txt", "rb");
	if (pfRead == NULL)
	{
		perror("LoadContact");
		return;
	}

	PeoInfo tmp = { 0 };
	while (fread(&tmp, sizeof(PeoInfo), 1, pfRead) == 1)
	{
		CheckCapacity(pc);

		pc->data[pc->count] = tmp;
		pc->count++;
	}
	fclose(pfRead);
	pfRead = NULL;
}




//动态的版本
int InitContact(Contact* pc)
{
	assert(pc);
	pc->count = 0;
	pc->data = (PeoInfo*)calloc(DEFAULT_SZ, sizeof(PeoInfo));
	if (pc->data == NULL)
	{
		printf("InitContact::%s\n", strerror(errno));
		return 1;
	}
	
	pc->capacity = DEFAULT_SZ;
	//加载文件的信息到通讯录中
	LoadContact(pc);

	return 0;
}






void DestroyContact(Contact* pc)
{
	assert(pc);
	free(pc->data);
	pc->data = NULL;
}


//动态的版本
void ADDContact(Contact* pc)
{
	assert(pc);
	// 增容
	CheckCapacity(pc);
	/*if (pc->count == MAX)
	{
		printf("通讯录已满!\n");
		return;
	}*/

	printf("请输入名字:\n");
	scanf("%s",pc->data[pc->count].name);
	printf("请输入年龄:\n");
	scanf("%d",&(pc->data[pc->count].age));
	printf("请输入性别:\n");
	scanf("%s",pc->data[pc->count].sex);
	printf("请输入电话:\n");
	scanf("%s",pc->data[pc->count].tele);
	printf("请输入地址:\n");
	scanf("%s",pc->data[pc->count].addr);

	pc->count++;
	printf("添加成功!\n");
}




void SHOWContact(const Contact* pc)
{
	assert(pc);
	int i = 0;
	printf("%-20s\t%-5s\t%-5s\t%-12s\t%-20s\n","姓名","年龄","性别","电话","地址");
	for (i = 0;i < pc->count;i++)
	{
		printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n",pc->data[i].name,
													pc->data[i].age,
													pc->data[i].sex,
													pc->data[i].tele,
													pc->data[i].addr);
	}

}


static int FindByName(Contact* pc, char name[])
{
	assert(pc);
	int i = 0;
	for (i = 0; i < pc -> count; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}



void DELEContact(Contact* pc)
{
	assert(pc);
	int i = 0;
	char name[MAX_NAME] = { 0 };
	if (pc->count == 0)
	{
		printf("通讯录为空,没有可删除的信息!\n");
		return;
	}
	printf("请输入要删除的人的名字:\n");
	scanf("%s", name);
	//查找
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("需要删除的人不存在!\n");
		return;
	}
	//删除
	for (i = pos; i < pc->count - 1; i++)
	{
		pc->data[i] = pc->data[i + 1];
	}
	pc->count--;
	printf("删除成功!\n");

}




void SEARCHContact(Contact* pc)
{
	assert(pc);
	char name[MAX_NAME] = { 0 };
	printf("请输入要查询的人的名字:\n");
	scanf("%s",name);
	int pos = FindByName(pc,name);
	if (pos == -1)
	{
		printf("没有查找到此人:\n");
		return;
	}
	//显示
	printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", "姓名", "年龄", "性别", "电话", "地址");
	printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n", pc->data[pos].name,
		pc->data[pos].age,
		pc->data[pos].sex,
		pc->data[pos].tele,
		pc->data[pos].addr);
}




void MODIFYContact(Contact* pc)
{
	assert(pc);
	char name[MAX_NAME] = { 0 };
	printf("请输入要修改信息的人的名字:\n");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("没有找到需要修改信息的人:\n");
		return;
	}
	printf("已找到需要修改信息的人,请修改:\n");
	//修改
	printf("请输入名字:\n");
	scanf("%s",pc->data[pos].name);
	printf("请输入年龄:\n");
	scanf("%d",&(pc->data[pos].age));
	printf("请输入性别:\n");
	scanf("%s",pc->data[pos].sex);
	printf("请输入电话:\n");
	scanf("%s",pc->data[pos].tele);
	printf("请输入地址:\n");
	scanf("%s",pc->data[pos].addr);

	printf("修改成功!\n");
}



int cmp_Peo_age(const void* p1, const void* p2)
{
	return strcmp(((PeoInfo*)p1)->name, ((PeoInfo*)p2)->name);
}


//按照名字来排序
void SORTContact(Contact* pc)
{
	assert(pc);
	qsort(pc->data,pc->count,sizeof(PeoInfo),cmp_Peo_age);
	printf("排序完成!\n");
}



void SAVEContact(const Contact* pc)
{
	assert(pc);
	FILE* pfWrite = fopen("contact.txt", "wb");
	if (pfWrite == NULL)
	{
		perror("SAVEContact");
		return;
	}
	//写文件-二进制的形式
	int i = 0;
	for (i = 0; i < pc->count; i++)
	{
		fwrite(pc->data+i,sizeof(PeoInfo),1,pfWrite);
	}
	fclose(pfWrite);
	pfWrite = NULL;
}



//清空
void CLEARContact(Contact* pc)
{
	assert(pc);
	pc->count = 0;
	pc->capacity = 0;
	memset(pc->data,0,sizeof(pc->data));

}

The above is the basic implementation of the address book, there may be missing operations, please understand!

Guess you like

Origin blog.csdn.net/weixin_71964780/article/details/132137839