c language: realize the address book

insert image description here


foreword

The address book needs to implement 7 functions, 1. Add contacts 2. Delete specified contacts 3. Find contacts 4. Modify contacts 5. Display contacts 6. Clear contacts 7. Sort contacts by name


1. Implementation ideas

1. The structure of the address book

First of all, the address book is a collection of contacts, and the contacts have attributes such as name, gender, age, phone number, and address. So for data like contacts, we obviously need a structure to implement contacts. That's it for the structure of the address book and we're done.
as follows:

#define MAXSIZE 100

typedef struct PeoInfo
{
    
    
	char name[20];
	char sex[5];
	int age;
	char tele[12];
	char adder[20];
}PeoInfo;


typedef struct Contact
{
    
    
	PeoInfo data[MAXSIZE];
	int sz;//方便我们操作data数组,记录我们将要使用的数组下标
}Contact;

2. Realization of simple interface of address book

(This is very simple, so I won't explain it)

//使用枚举类型增加代码的可读性
enum {
    
    
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SHOW,
	DESTROY,
	SORT,
};

void menu()
{
    
    
	printf("******************************\n");
	printf("**** 1.add     2.del      ****\n");
	printf("**** 3.search  4.modify   ****\n");
	printf("**** 5.show    6.destroy  ****\n");
	printf("**** 7.sort    8.exit     ****\n");
	printf("******************************\n");
}

int main()
{
    
    
	int input = 1;
	Contact con;
	InitContact(&con);
	while (input)
	{
    
    
		menu();
		printf("请输入所选择的标号:>");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case ADD:
			AddContact(&con);
			break;
		case DEL:
			DelContact(&con);
			break;
		case SEARCH:
			SearchContact(&con);
			break;
		case MODIFY:
			ModifyContact(&con);
			break;
		case SHOW:
			ShowContact(&con);
			break;
		case DESTROY:
			DestroyContact(&con);
			break;
		case SORT:
			SortConTact(&con);
			break;
		case EXIT:
			printf("退出通讯录\n");
			break;
		default:
			printf("输入错误,请从新输入\n");
			break;
		}
	}
	return 0;
}

3. Initialize the address book (InitContact)

First of all, sz in the structure Contact indicates the array subscript to be operated on, then we set sz = 0, does it mean that we are going to operate on the subscript 0 of the array, isn’t it equivalent to initializing the address book?

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

//初始化通讯录
void InitContact(Contact* pc)
{
    
    
	pc->sz = 0;
}

4. Add contact information (AddContact)

Just operate on the position of sz in the data array, and then add one to sz, that's it.

//添加联系人
void AddContact(Contact* pc);
//添加联系人
void AddContact(Contact* pc)
{
    
    
	assert(pc);//判断pc是否是NULL

	printf("请输入联系人名字:>");
	scanf("%s", pc->data[pc->sz].name);
	printf("请输入联系人性别:>");
	scanf("%s", pc->data[pc->sz].sex);
	printf("请输入联系人年龄:>");
	scanf("%d", &(pc->data[pc->sz].age));
	printf("请输入联系人电话:>");
	scanf("%s", pc->data[pc->sz].tele);
	printf("请输入联系人地址:>");
	scanf("%s", pc->data[pc->sz].adder);
	pc->sz += 1;
}

5. Display address book (ShowContact)

As long as there is a variable i, it can traverse the elements in [0, sz). But pay attention to the printing format. The following codes I use, such as %-20s and \t, respectively indicate that the printed string is left-aligned with a width of 20 characters and a horizontal tab (4 characters empty).

//打印通讯录
void ShowContact(Contact* pc);
void ShowContact(Contact* pc)
{
    
    
	assert(pc);

	printf("%-20s\t%-5s\t%-2s\t%-12s\t%-20s\n", "名字", "性别", "年龄", "电话", "地址");
	for (int i = 0; i < pc->sz; i++)
	{
    
    
		printf("%-20s\t%-5s\t%-2d\t%-12s\t%-20s\n"
			, pc->data[i].name
			, pc->data[i].sex
			, pc->data[i].age
			, pc->data[i].tele
			, pc->data[i].adder);
	}
}

6. Delete Contact (DelContact)

To delete a contact, we need the user to enter the name of the contact first, and then check whether the contact is in the address book. If true is returned, then the subscript of the contact is returned through the pointer, and the array elements after the subscript are moved forward to overwrite the contact to be deleted, and sz is decremented by one.
If not return false, print delete contact failed.

//删除联系人
void DelContact(Contact* pc);

//查找名字
bool FindName(Contact* pc, char* name, int* retIndex)
{
    
    
	for (int i = 0; i < pc->sz; i++)
	{
    
    
		if (strcmp(pc->data[i].name, name) == 0)
		{
    
    
			*retIndex = i;
			return true;
		}
	}

	return false;
}


//删除联系人
void DelContact(Contact* pc)
{
    
    
	assert(pc);
	
	char name[20] = {
    
     0 };
	printf("请输入要删除联系人的名字:>");
	scanf("%s", name);

	int retIndex = 0;
	if (FindName(pc, name, &retIndex) == true)
	{
    
    
		for (int i = retIndex; i < pc->sz - 1; i++)
		{
    
    
			pc->data[i] = pc->data[i + 1];
		}
		pc->sz -= 1;
		printf("删除联系人成功\n");
		return;
	}

	printf("删除联系人失败\n");
	return;
}

7. Find contacts (SearchContact)

To find a contact, the user needs to enter the name of the contact first, then find the contact, and print the contact information if it exists. If not present, prints that the contact does not exist.

//查找联系人
void SearchContact(Contact* pc);
//查找名字
bool FindName(Contact* pc, char* name, int* retIndex)
{
    
    
	for (int i = 0; i < pc->sz; i++)
	{
    
    
		if (strcmp(pc->data[i].name, name) == 0)
		{
    
    
			*retIndex = i;
			return true;
		}
	}

	return false;
}


//查找联系人
void SearchContact(Contact* pc)
{
    
    
	assert(pc);

	char name[20] = {
    
     0 };
	printf("请输入要查找联系人的名字:>");
	scanf("%s", name);

	int retIndex = 0;
	if (FindName(pc, name, &retIndex) == true)
	{
    
    
		printf("%-20s\t%-5s\t%-2s\t%-12s\t%-20s\n", "名字", "性别", "年龄", "电话", "地址");
		printf("%-20s\t%-5s\t%-2d\t%-12s\t%-20s\n"
			, pc->data[retIndex].name
			, pc->data[retIndex].sex
			, pc->data[retIndex].age
			, pc->data[retIndex].tele
			, pc->data[retIndex].adder);
		return;
	}

	printf("查找失败\n");
	return;
}

8. Destroy the address book (DestroyContact)

The idea of ​​destroying the address book is the same as that of initializing the address book, only sz is set to 0. Or reuse the InitContact function.

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

	pc->sz = 0;
	printf("销毁成功\n");
}

//复用InitContact
/*void DestroyContact(Contact* pc)
{
	assert(pc);
	
	InitContact(pc);
}*/


9. Modify Contact (ModifyContact)

To modify a contact, the user must first enter the name of the contact to be modified, and then search for the contact. If present, re-enter the contact's information via subscript. If not present, print modification failed.

//修改联系人
void ModifyContact(Contact* pc);
//修改联系人
void ModifyContact(Contact* pc)
{
    
    
	assert(pc);

	char name[20] = {
    
     0 };
	printf("请输入要修改联系人的名字:>");
	scanf("%s", name);

	int retIndex = 0;
	if (FindName(pc, name, &retIndex) == true)
	{
    
    
		printf("请输入联系人名字:>");
		scanf("%s", pc->data[retIndex].name);
		printf("请输入联系人性别:>");
		scanf("%s", pc->data[retIndex].sex);
		printf("请输入联系人年龄:>");
		scanf("%d", &(pc->data[retIndex].age));
		printf("请输入联系人电话:>");
		scanf("%s", pc->data[retIndex].tele);
		printf("请输入联系人地址:>");
		scanf("%s", pc->data[retIndex].adder);

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

	printf("修改失败\n");
	return;
}

10. Sort Contacts (SortConTact)

We sort the address book by name. We use the name of the first element of the data array as the standard to divide the array into an array larger than the first element and an array smaller than or equal to the first element, and then take the first element of the two arrays, and then divide them into arrays greater than or equal to the first element The array of the first element and the array less than or equal to the first element, repeat this operation until there is only one element in the array.

//排序通讯录
void SortConTact(Contact* pc);
//排序通讯录

void Swap(Contact* pc, int left, int right)
{
    
    
	PeoInfo tmp = pc->data[left];
	pc->data[left] = pc->data[right];
	pc->data[right] = tmp;
}

int PivotIndex(Contact* pc, int startIndex, int endIndex)
{
    
    
	PeoInfo tmp = pc->data[startIndex];

	int right = endIndex;
	int left = startIndex;
	while (left < right)
	{
    
    
		while (left < right && strcmp(pc->data[right].name, tmp.name) > 0)
		{
    
    
			right--;
		}

		while (left < right && strcmp(pc->data[left].name, tmp.name) <= 0)
		{
    
    
			left++;
		}

		Swap(pc, left, right);
	}
	pc->data[startIndex] = pc->data[left];
	pc->data[left] = tmp;

	return left;
}

void QuickSort(Contact* pc, int startIndex, int endIndex)
{
    
    
	if (startIndex > endIndex)
	{
    
    
		return;
	}

	int pivot = PivotIndex(pc, startIndex, endIndex);
	QuickSort(pc, startIndex, pivot - 1);
	QuickSort(pc, pivot + 1, endIndex);
}

void SortConTact(Contact* pc)
{
    
    
	assert(pc);

	QuickSort(pc, 0, pc->sz-1);
	printf("排序成功\n");
}

2. Code implementation

The test.c file is used to simulate the selection interface.
The contact.c file is used to store the implementation of the function.
The contact.h file is used to store the declaration of the function, the declaration of the structure, and the reference of the header file.

//test.c文件
#include "contact.h"


void menu()
{
    
    
	printf("******************************\n");
	printf("**** 1.add     2.del      ****\n");
	printf("**** 3.search  4.modify   ****\n");
	printf("**** 5.show    6.destroy  ****\n");
	printf("**** 7.sort    8.exit     ****\n");
	printf("******************************\n");
}

int main()
{
    
    
	int input = 1;
	Contact con;
	InitContact(&con);
	while (input)
	{
    
    
		menu();
		printf("请输入所选择的标号:>");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case ADD:
			AddContact(&con);
			break;
		case DEL:
			DelContact(&con);
			break;
		case SEARCH:
			SearchContact(&con);
			break;
		case MODIFY:
			ModifyContact(&con);
			break;
		case SHOW:
			ShowContact(&con);
			break;
		case DESTROY:
			DestroyContact(&con);
			break;
		case SORT:
			SortConTact(&con);
			break;
		case EXIT:
			printf("退出通讯录\n");
			break;
		default:
			printf("输入错误,请从新输入\n");
			break;
		}
	}
	return 0;
}
//contact.h文件

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
#include <string.h>

#define MAXSIZE 100

enum {
    
    
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SHOW,
	DESTROY,
	SORT,
};


typedef struct PeoInfo
{
    
    
	char name[20];
	char sex[5];
	int age;
	char tele[12];
	char adder[20];
}PeoInfo;


typedef struct Contact
{
    
    
	PeoInfo data[MAXSIZE];
	int sz;
}Contact;


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

//添加联系人
void AddContact(Contact* pc);

//打印通讯录
void ShowContact(Contact* pc);

//删除联系人
void DelContact(Contact* pc);

//查找联系人
void SearchContact(Contact* pc);

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

//修改联系人
void ModifyContact(Contact* pc);

//排序通讯录
void SortConTact(Contact* pc);
//contact.c文件

#include "contact.h"

//初始化通讯录
void InitContact(Contact* pc)
{
    
    
	pc->sz = 0;
}

//添加联系人
void AddContact(Contact* pc)
{
    
    
	assert(pc);

	printf("请输入联系人名字:>");
	scanf("%s", pc->data[pc->sz].name);
	printf("请输入联系人性别:>");
	scanf("%s", pc->data[pc->sz].sex);
	printf("请输入联系人年龄:>");
	scanf("%d", &(pc->data[pc->sz].age));
	printf("请输入联系人电话:>");
	scanf("%s", pc->data[pc->sz].tele);
	printf("请输入联系人地址:>");
	scanf("%s", pc->data[pc->sz].adder);
	pc->sz += 1;
}


//打印通讯录
void ShowContact(Contact* pc)
{
    
    
	assert(pc);

	printf("%-20s\t%-5s\t%-2s\t%-12s\t%-20s\n", "名字", "性别", "年龄", "电话", "地址");
	for (int i = 0; i < pc->sz; i++)
	{
    
    
		printf("%-20s\t%-5s\t%-2d\t%-12s\t%-20s\n"
			, pc->data[i].name
			, pc->data[i].sex
			, pc->data[i].age
			, pc->data[i].tele
			, pc->data[i].adder);
	}
}


//查找名字
bool FindName(Contact* pc, char* name, int* retIndex)
{
    
    
	for (int i = 0; i < pc->sz; i++)
	{
    
    
		if (strcmp(pc->data[i].name, name) == 0)
		{
    
    
			*retIndex = i;
			return true;
		}
	}

	return false;
}


//删除联系人
void DelContact(Contact* pc)
{
    
    
	assert(pc);
	
	char name[20] = {
    
     0 };
	printf("请输入要删除联系人的名字:>");
	scanf("%s", name);

	int retIndex = 0;
	if (FindName(pc, name, &retIndex) == true)
	{
    
    
		for (int i = retIndex; i < pc->sz - 1; i++)
		{
    
    
			pc->data[i] = pc->data[i + 1];
		}
		pc->sz -= 1;
		printf("删除联系人成功\n");
		return;
	}

	printf("删除联系人失败\n");
	return;
}



//查找联系人
void SearchContact(Contact* pc)
{
    
    
	assert(pc);

	char name[20] = {
    
     0 };
	printf("请输入要查找联系人的名字:>");
	scanf("%s", name);

	int retIndex = 0;
	if (FindName(pc, name, &retIndex) == true)
	{
    
    
		printf("%-20s\t%-5s\t%-2s\t%-12s\t%-20s\n", "名字", "性别", "年龄", "电话", "地址");
		printf("%-20s\t%-5s\t%-2d\t%-12s\t%-20s\n"
			, pc->data[retIndex].name
			, pc->data[retIndex].sex
			, pc->data[retIndex].age
			, pc->data[retIndex].tele
			, pc->data[retIndex].adder);
		return;
	}

	printf("查找失败\n");
	return;
}



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

	pc->sz = 0;
	printf("销毁成功\n");
}


//修改联系人
void ModifyContact(Contact* pc)
{
    
    
	assert(pc);

	char name[20] = {
    
     0 };
	printf("请输入要修改联系人的名字:>");
	scanf("%s", name);

	int retIndex = 0;
	if (FindName(pc, name, &retIndex) == true)
	{
    
    
		printf("请输入联系人名字:>");
		scanf("%s", pc->data[retIndex].name);
		printf("请输入联系人性别:>");
		scanf("%s", pc->data[retIndex].sex);
		printf("请输入联系人年龄:>");
		scanf("%d", &(pc->data[retIndex].age));
		printf("请输入联系人电话:>");
		scanf("%s", pc->data[retIndex].tele);
		printf("请输入联系人地址:>");
		scanf("%s", pc->data[retIndex].adder);

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

	printf("修改失败\n");
	return;
}


//排序通讯录

void Swap(Contact* pc, int left, int right)
{
    
    
	PeoInfo tmp = pc->data[left];
	pc->data[left] = pc->data[right];
	pc->data[right] = tmp;
}

int PivotIndex(Contact* pc, int startIndex, int endIndex)
{
    
    
	PeoInfo tmp = pc->data[startIndex];

	int right = endIndex;
	int left = startIndex;
	while (left < right)
	{
    
    
		while (left < right && strcmp(pc->data[right].name, tmp.name) > 0)
		{
    
    
			right--;
		}

		while (left < right && strcmp(pc->data[left].name, tmp.name) <= 0)
		{
    
    
			left++;
		}

		Swap(pc, left, right);
	}
	pc->data[startIndex] = pc->data[left];
	pc->data[left] = tmp;

	return left;
}

void QuickSort(Contact* pc, int startIndex, int endIndex)
{
    
    
	if (startIndex > endIndex)
	{
    
    
		return;
	}

	int pivot = PivotIndex(pc, startIndex, endIndex);
	QuickSort(pc, startIndex, pivot - 1);
	QuickSort(pc, pivot + 1, endIndex);
}

void SortConTact(Contact* pc)
{
    
    
	assert(pc);

	QuickSort(pc, 0, pc->sz-1);
	printf("排序成功\n");
}

Summarize

The above is the specific idea and code implementation of my address book.

insert image description here

Guess you like

Origin blog.csdn.net/li209779/article/details/131814275