C language to realize the address book (static version)

1. Introduction of address book function

1. Add contacts
2. Delete contacts
3. Modify contact information
4. Find contacts
5. Display all contact information
6. Sort the address book
0. Exit the program

2. Program file design

Divided into three files, one header file and two source files
MaliList.h—mainly used to include header files, define constants, declare structures, and declare functions
test.c—used to test the entire program, realize the print menu, and judge user input information and other interactive functions
MailList.c- used to implement the functions declared in the header file.

2. The specific implementation of each module

2.1 Create a structure for contacting people and create a structure for address book classes

The members of the contact structure include the following points: 1.姓名 2.性别 3. 年龄 4.电话号码 5.家庭住址
The members of the address book structure include the following points: 1.联系人结构体类型的数组 2.已经存储的联系人数量
Implementation code:

//define 宏定义
#define MAX_DATA 100
#define MAX_NAME 20
#define MAX_SEX 6
#define MAX_TELE 12
#define MAX_ADDR 30

//创建结构体
//通讯录成员信息
struct Information
{
    
    
	char name[MAX_NAME];
	char sex[MAX_SEX];
	int age;
	char tele[MAX_TELE];
	char addr[MAX_ADDR];
};
//通讯录
struct MailList
{
    
    
	struct Information data[MAX_DATA];
	int count;
};

2.2 Initialize address book——InintMailList

Implementation code:

//初始化通讯录
void InintMailList(struct MailList* pl);
void InintMailList(struct MailList* pl)
{
    
    
	assert(pl);
	memset(pl, 0, MAX_DATA * sizeof(struct Information) + sizeof(int));
}

Using memsetfunctions can easily initialize a piece of memory space.

MAX_DATA * sizeof(struct Information) + sizeof(int)

The meaning of this code is to initialize the size of the memory space, which MAX_DATA*sizeof(struct Information)represents the size of the entire contact information array, that is, struct Information data[MAX_DATA];the size of the occupied space; sizeof(int)it represents countthe size of the occupied memory space.

2.3 Add contact module——AddInformation

Implementation code:

//添加联系人
void AddInformation(struct MailList* pl);
void AddInformation(struct MailList* pl)
{
    
    
	if (pl->count == MAX_DATA)
	{
    
    
		printf("通讯录已满,无法添加联系人\n请删除部分联系人后再进行添加\n");
	}
	else
	{
    
    
		printf("请输入想要添加的联系人的姓名:>\n");
		scanf("%s", pl->data[pl->count].name);
		printf("请输入想要添加的联系人的年龄:>\n");
		scanf("%d", &pl->data[pl->count].age);
		printf("请输入想要添加的联系人的性别:>\n");
		scanf("%s", pl->data[pl->count].sex);
		printf("请输入想要添加的联系人的电话:>\n");
		scanf("%s", pl->data[pl->count].tele);
		printf("请输入想要添加的联系人的住址:>\n");
		scanf("%s", pl->data[pl->count].addr);
		pl->count++;
		printf("添加联系人成功!");
	}
}

2.4 Display address book information module——ShowMailList

Implementation code:

//显示通讯录信息
void ShowMailList(struct MailList* pl);
void ShowMailList(struct MailList* pl)
{
    
    
	if (!pl->count)
	{
    
    
		printf("通讯录为空!\n");
	}
	else
	{
    
    
		printf("%-20s\t%-6s\t%-6s\t%-12s\t%-30s\n", "姓名", "性别", "年龄", "电话", "地址");
		for (int i = 0; i < pl->count; i++)
		{
    
    
			printf("%-20s\t%-6s\t%-d\t%-12s\t%-30s\n", pl->data[i].name,
				pl->data[i].sex,
				pl->data[i].age,
				pl->data[i].tele,
				pl->data[i].addr);
		}
	}
}

One thing to pay attention to is printthe alignment of the f function when printing data

2.5 Auxiliary modules——FindByName

Implementation code:

//通过名字查找联系人
static int FindByName(struct MailList* pl, char n[])
{
    
    
	for (int i = 0; i < pl->count; i++)
	{
    
    
		if (0 == strcmp(pl->data[i].name, n))
		{
    
    
			return i;
		}
	}
	return -1;
}

2.6 Find Contacts——SearchInformation

Implementation code:

//查找联系人
void SearchInformation(struct MailList* pl);
void SearchInformation(struct MailList* pl)
{
    
    
	char name[MAX_NAME] = {
    
     0 };
	printf("请输入想要查找的联系人的姓名:>\n");
	scanf("%s", name);
	int ret = FindByName(pl, name);
	if (-1 == ret)
	{
    
    
		printf("没找到该联系人!\n");
	}
	else
	{
    
    
		printf("找到了!\n");
		printf("%-20s\t%-6s\t%-6s\t%-12s\t%-30s\n", "姓名", "性别", "年龄", "电话", "地址");
		printf("%-20s\t%-6s\t%-d\t%-12s\t%-30s\n", pl->data[ret].name,
			pl->data[ret].sex,
			pl->data[ret].age,
			pl->data[ret].tele,
			pl->data[ret].addr);
	}
}

2.7 Delete the contact module——DelInformation

Implementation code:

//删除联系人
void DelInformation(struct MailList* pl);
/删除联系人
void DelInformation(struct MailList* pl)
{
    
    
	char name[MAX_NAME] = {
    
     0 };
	printf("请输入想要删除的联系人的名字:>\n");
	scanf("%s", name);
	int ret = FindByName(pl, name);
	if (-1 == ret)
	{
    
    
		printf("没找到该联系人!\n");
	}
	else
	{
    
    
		for (int i = ret; i < pl->count - 1; i++)
		{
    
    
			pl->data[i] = pl->data[i + 1];
		}
		pl->count--;
		printf("删除成功!\n");
	}
}

2.8 Modify Contact——ModifyInformation

Implementation code:

//修改联系人信息
void ModifyInformation(struct MailList* pl);
void ModifyInformation(struct MailList* pl)
{
    
    
	char name[MAX_NAME];
	printf("请输入想要修改的联系人的姓名:>\n");
	scanf("%s", name);
	int ret = FindByName(pl, name);
	if (-1 == ret)
	{
    
    
		printf("没有找到该联系人!\n");
	}
	else
	{
    
    
		printf("请输入想要添加的联系人的姓名:>\n");
		scanf("%s", pl->data[pl->count].name);
		printf("请输入想要添加的联系人的年龄:>\n");
		scanf("%d", &pl->data[pl->count].age);
		printf("请输入想要添加的联系人的性别:>\n");
		scanf("%s", pl->data[pl->count].sex);
		printf("请输入想要添加的联系人的电话:>\n");
		scanf("%s", pl->data[pl->count].tele);
		printf("请输入想要添加的联系人的住址:>\n");
		scanf("%s", pl->data[pl->count].addr);
		printf("修改联系人成功! \n");
	}
}

2.9 Sort contacts - SortMailListsort by name

Implementation code:

//按名字排序联系人
void SortMailList(struct MailList* pl);
int CmpByName(const void* e1, const void* e2)
{
    
    
	return strcmp(((struct Information*)e1)->name, ((struct Information*)e2)->name);
}
void SortMailList(struct MailList* pl)
{
    
    
	printf("排序中...\n");
	qsort(pl, pl->count, sizeof(struct Information), CmpByName);
	Sleep(3000);
	printf("排序完成!\n");
}

3. Overall source code and program running effect

Source code: gitee link
Program running effect display:
insert image description here
insert image description here
insert image description here
here is only the approximate effect, you can test it yourself

Summarize

This blog mainly introduces the simple implementation of the static version of the address book. The dynamic version and the version that can perform file operations will be written in a future blog. I hope everyone has to help!

Guess you like

Origin blog.csdn.net/m0_72482689/article/details/127051490