Implementation of address book in C language (complete code at the end of the text)

Realization idea

Let's first think about the general functions of the address book. There are six main functions: adding contacts, deleting contacts, searching contacts, modifying contacts, printing the address book and classifying contacts. These are the basic functions we want to implement. Next, we can start to implement the code

Address book menu

This is a very classic step. We create a file called test.c to complete the preparation of the general process of the address book. First, let's complete the menu function

void menu()
{
	printf("*******************************");
	printf("****   1.Add    2.Del    ******");
	printf("****   3.Search 4.Modify ******");
	printf("****   5.Show   6.Sort   ******");
	printf("****   0.Exit            ******");
	printf("*******************************");
}

I hope that when we hit a number on the keyboard, the program can realize the corresponding function, so we need to add the following code

int main()
{
	do
	{
		int input = 0;
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			break;
		case 2:
			break;
		case 3:
			break;
		case 4:
			break;
		case 5:
			break;
		case 6:
			break;

		}

	} while()
	return 0;
}

We use the input variable to receive a value and realize the corresponding function according to the input value. We may think it is not intuitive to enter 123 to realize the corresponding function. Why can't we directly enter ADD, DEL ·? So I can create an enumeration

enum Option
{
	EXIT,//Represents 0
	ADD,//Representative 1
	DEL,//Representative 2
	SEARCH,//```
	MODIFY,
	SHOW,
	SORT
};

In this way, we can modify the above code to

int main()
{
	do
	{
		int input = 0;
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			break;
		case DEL:
			break;
		case SEARCH:
			break;
		case MODIFY:
			break;
		case SHOW:
			break;
		case SORT:
			break;

		}

	} while()
	return 0;
}

Address book creation

Let's create the second file "contact.h", which is a header file, which is mainly used to store the function declaration. We also implement the creation of address book here. Suppose we want to create an address book that can store 1000 people, then obviously we need an array with a capacity of 1000. What should be the variable type in this array? For complex objects such as contacts, it is obviously more appropriate to use structures. So let's create a structure to store contact information,

struct PeoInfo
{
	char name[NAME];
	int age;
	char sex[SEX];
	char tele[TELE];
	char addr[ADDR];
};

In order to enhance the readability of the code and facilitate the maintenance of the code in the future, I replace the numbers of the array size with letters with practical significance, so we need to add the following definitions

#define NAME 20
#define SEX 5
#define TELE 12
#define ADDR 30

The above code is enough to describe a contact, but in practice, we will find that the number of contacts is also extremely important, so we should also sub package the user information and his number in another structure

struct contact
{
	struct PeoInfo data[MAX];
	int sz;
};

I still use letters instead of numbers here, so we have to add a definition,

#define MAX 1000

Then we create an address book in the test.c file

struct contact con;

Unfortunately, after we create this address book, the address book con still stores a bunch of random values, so we have to initialize this address book first

Initialize address book

We need to create a function. For the implementation of the function, we can do it in the "contact.c" source file. Let's create a function called ContactInit to complete initialization. What parameters should we pass to this function? Because we want to change the address book itself, what we don't want to change is only a formal parameter. Therefore, it is appropriate to pass the address of the structure at this time. Now let's take a look at the implementation of the code. Let's first pass parameters to the function in the test.c file

ContactInit(&con);

Then go to contact.h to declare the function

void ContactInit(struct contact* pc);

Then complete the implementation of the function in contact.c

void ContactInit(struct contact* pc)
{
	pc->sz = 0;
	memset(pc->data, 0, MAX * sizeof(struct PeoInfo));
}

In this way, we can initialize the values in the address book. Next, we will start to write the function of the address book

Add a Contact

We write the add contact function in the contact.c file and name this function AddContact

void AddContact(struct contact* pc)
{
	if (pc -> sz == MAX)
	{
		printf("The address book is full\n");
	}
	else
	{
		printf("Please enter a name:>");
		scanf("%s", pc->data[pc->sz].name);
		printf("Please enter age:>");
		scanf("%d", &(pc->data[pc->sz].age));
		printf("Please enter gender:>");
		scanf("%s", pc->data[pc->sz].sex);
		printf("Please enter phone number:>");
		scanf("%s", pc->data[pc->sz].tele);
		prinitf("Please enter the address:>");
		scanf("%s", pc->data[pc->sz].addr);
		printf("Added successfully\n");
		pc->sz++;
	}
}

With this idea, we can simply write all the remaining functions, and then let's take a look at the address book printing

Print address book

void ShowContact(struct contact* pc)
{
	int i = 0;
	printf("%15s\t%5s\t%8s\t%15s\t%30s\n\n", "name", "age", "sex", "tele", "addr");
	for (i = 0; i < pc->sz; i++)
	{
		printf("%15s\t%5d\t%8s\t%15s\t%30s\n",
			pc->data[i].name,
			pc->data[i].age,
			pc->data[i].sex,
			pc->data[i].tele,
			pc->data[i].addr);
	}
}

Here we use the for loop to traverse all contacts in the address book and print them

Delete Contact

The idea of this function is slightly different from others, because we want to move the overall position of the contact behind the contact forward by one grid after deleting this contact

void DelContact(struct contact* pc)
{
	char name[NAME] = { 0 };
	printf("Please enter the name of the person to delete");
	scanf("%s", &name);
	//lookup
	//delete
}

This is the general framework of this function. In order to complete this function, its core is to find and delete two functions. For the search function, we will find that it will be used in the subsequent search and modification functions, so we can completely package it into a function

int FindContactByName(struct contact* pc, char name[])
{
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcpy(pc->data[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}

When the program finds the contact, the i it returns is the subscript of the array element. Next, we use pos to receive it

void DelContact(struct contact* pc)
{
	char name[NAME] = { 0 };
	printf("Please enter the name of the person to delete");
	scanf("%s", &name);
	//lookup
	int pos = FindContactByName(pc, name);
	//delete
	if (pos == -1)
	{
		printf("The contact does not exist\n");
	}
	else
	{
		int i = 0;
		for (i = 0; i < pc->sz-1; i++)
		{
			pc->data[i] = pc->data[i + 1];
		}
		pc->sz--;
		printf("Delete succeeded\n");
	}
}

Of course, this program still has some defects. When there are no contacts in this address book, the program should prompt us that we can't delete it at present, so we can add a line of code

if (pc->sz == 0)
	{
		printf("The address book is empty and cannot be deleted\n");
		return;
	}

Modify address book

void ModifyContact(struct contact* pc)
{
	char name[NAME] = { 0 };
	printf("Enter the name of the person to modify\n");
	scanf("%s", name);
	int pos = FindContactByName(pc, name);
	if (-1 == pos)
	{
		printf("The person you want to enter does not exist\n");
	}
	else
	{
		printf("Please enter a name:>");
		scanf("%s", pc->data[pos].name);
		printf("Please enter age:>");
		scanf("%d", &(pc->data[pos].age));
		printf("Please enter gender:>");
		scanf("%s", pc->data[pos].sex);
		printf("Please enter phone number:>");
		scanf("%s", pc->data[pos].tele);
		printf("Please enter the address:>");
		scanf("%s", pc->data[pos].addr);
	}
}

Complete code

test.c

#include "contact.h"
void menu()
{
	printf("*******************************\n");
	printf("****   1.Add    2.Del    ******\n");
	printf("****   3.Search 4.Modify ******\n");
	printf("****   5.Show   0.Exit   ******\n");
	printf("*******************************\n");
}
enum Option
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SHOW
};
int main()
{
	struct contact con;
	ContactInit(&con);
	int input = 0;
	do
	{
		menu();
		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;
		default:
			printf("Selection error\n");
		}
	} while (input);
	return 0;
}

contact.c

#include "contact.h"


void ContactInit(struct contact* pc)
{
	pc->sz = 0;
	memset(pc->data, 0, sizeof(pc->data));
}

void AddContact(struct contact* pc)
{
	if (pc->sz == MAX)
	{
		printf("The address book is full\n");
	}
	else
	{
		printf("Please enter a name:>");
		scanf("%s", pc->data[pc->sz].name);
		printf("Please enter age:>");
		scanf("%d", &(pc->data[pc->sz].age));
		printf("Please enter gender:>");
		scanf("%s", pc->data[pc->sz].sex);
		printf("Please enter phone number:>");
		scanf("%s", pc->data[pc->sz].tele);
		printf("Please enter the address:>");
		scanf("%s", pc->data[pc->sz].addr);
		printf("Added successfully\n");
		pc->sz++;
	}
}
void ShowContact(struct contact* pc)
{
	int i = 0;
	printf("%15s\t%5s\t%8s\t%15s\t%30s\n\n", "name", "age", "sex", "tele", "addr");
	for (i = 0; i < pc->sz; i++)
	{
		printf("%15s\t%5d\t%8s\t%15s\t%30s\n",
			pc->data[i].name,
			pc->data[i].age,
			pc->data[i].sex,
			pc->data[i].tele,
			pc->data[i].addr);
	}
}
int FindContactByName(const struct contact* pc, char name[])
{
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}
void DelContact(struct contact* pc)
{
	if (pc->sz == 0)
	{
		printf("The address book is empty and cannot be deleted\n");
		return;
	}
	char name[NAME] = { 0 };
	printf("Please enter the name of the person to delete");
	scanf("%s", name);
	//lookup
	int pos = FindContactByName(pc, name);
	//delete
	if (pos == -1)
	{
		printf("The contact does not exist\n");
	}
	else
	{
		int i = 0;
		for (i = 0; i < pc->sz-1; i++)
		{
			pc->data[i] = pc->data[i + 1];
		}
		pc->sz--;
		printf("Delete succeeded\n");
	}
}
void SearchContact(const struct contact* pc)
{
	if (pc->sz == 0)
	{
		printf("Address book is empty\n");
		return;
	}
	printf("Please enter contact name");
	int pos = 0;
	char name[NAME] = { 0 };
	scanf("%s", name);
	pos = FindContactByName(pc, name);
	if (pos == -1)
	{
		printf("No one was found\n");
	}
	else
	{
		printf("%15s\t%5s\t%8s\t%15s\t%30s\n\n", "name", "age", "sex", "tele", "addr");
		printf("%15s\t%5d\t%8s\t%15s\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(struct contact* pc)
{
	char name[NAME] = { 0 };
	printf("Enter the name of the person to modify\n");
	scanf("%s", name);
	int pos = FindContactByName(pc, name);
	if (-1 == pos)
	{
		printf("The person you want to enter does not exist\n");
	}
	else
	{
		printf("Please enter a name:>");
		scanf("%s", pc->data[pos].name);
		printf("Please enter age:>");
		scanf("%d", &(pc->data[pos].age));
		printf("Please enter gender:>");
		scanf("%s", pc->data[pos].sex);
		printf("Please enter phone number:>");
		scanf("%s", pc->data[pos].tele);
		printf("Please enter the address:>");
		scanf("%s", pc->data[pos].addr);
	}
}

contact.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

#define NAME 20
#define SEX 5
#define TELE 12
#define ADDR 30
#define MAX 1000
struct PeoInfo
{
	char name[NAME];
	int age;
	char sex[SEX];
	char tele[TELE];
	char addr[ADDR];
};

struct contact
{
	struct PeoInfo data[MAX];
	int sz;
};

void ContactInit(struct contact* pc);
void AddContact(struct contact* pc);
void ShowContact(struct contact* pc);
void DelContact(struct contact* pc);
void SearchContact(const struct contact* pc);
void ModifyContact(struct contact* pc);

Tags: C

Posted on Mon, 04 Oct 2021 18:37:33 -0400 by cmgmyr