Implementation of dynamic version of address book (source + analysis)

Address Book Function

The implemented address book is a dynamically increasing version of the address book, which can increase the number of contacts to be stored by itself on demand. The main functions are:
1. Ability to store contact information on demand (as much space as you need to store contacts)
Contact information: Name + Gender + Age + Phone + Address
2. Ability to increase people's information
3. Ability to delete specified person's information
4. Ability to find information about a designated person
5. Ability to modify the specified person's information
6. Ability to display address book

Implementation of key functions

1. Create and initialize address books

Create Address List: Address List can hold more than one contact's information, so we first create a structure that can contain all of a person's information, defined as struct PeoInfo; then we create an Address List, which can hold contacts and record the number of people in the Address List, so we create a nested structure, defined asStruct Contact.
The code is as follows:
struct PeoInfo (redefined as PeoInfo for later convenience)

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

struct Contact (redefined as Contact for later convenience)

typedef struct Contact
{
	PeoInfo* data;//Structure pointer, pointing to structure PeoInfo
	int sz;//sz is used to record the number of contacts before address book expansion
	int capacity;//Capacity is used to record the maximum capacity (i.e. how many contacts can be stored) of the address book after expansion
}Contact;

Initialize Address Book: We stipulate that the Address Book will initially be able to store 3 people (as required).
The following:

void InitContact(Contact* pc)
{
	pc->data = (PeoInfo*) malloc(MAX_SZ*sizeof(PeoInfo));//Define the number of contacts to store using dynamic memory functions
	if (pc->data == NULL)
	{
		perror("InitContact");//Print out error message when open memory fails
		return;
	}
	pc->sz = 0;
	pc->capacity = MAX_SZ;
}

2. Add contacts

void AddContact(Contact* pc)
{
	if (pc->sz == pc->capacity)//Determine dynamic capacity if the number of stored address books has reached maximum capacity
	{
	    //Dynamic increase of address book capacity through realloc function
		PeoInfo* ptr=(PeoInfo*)realloc(pc->data, (ADD_CAP + pc->capacity) * sizeof(PeoInfo));
		if (ptr != NULL)
		{
			pc->data = ptr;
			pc->capacity += ADD_CAP;//Provides that the capacity of two people will be increased each time the address book is full
			printf("Successful Capacity Increase!\n");
		}
		else
		{
			printf("Capacitance Failure\n");
			return;
		}
	}
	printf("Please enter your name:>");
	scanf("%s", pc->data[pc->sz].name);//Access structure members through pointers, as follows
	printf("Please enter gender:>");
	scanf("%s", pc->data[pc->sz].sex);
	printf("Please enter your age:>");
	scanf("%d", &(pc->data[pc->sz].age));
	printf("Please enter a phone:>");
	scanf("%s", pc->data[pc->sz].tele);
	printf("Please enter an address:>");
	scanf("%s", pc->data[pc->sz].addr);
	pc->sz++;
	printf("Increase Success!\n");
}

3. Delete Contacts

Note: Before you can delete a contact, you need to find the information to delete it, so add a function to find the deleted contact before deleting it.

static int FindContact(const Contact* pc,char name[])//Deleted contacts found, and the function is decorated with static to ensure that it can only be used within the file
{
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(name, (pc->data[i]).name) == 0)
			return i;//Return the contact's location in the address book
	}
	return -1;
}
void DelContact(Contact* pc)//Delete Contact
{
	char name[MAX_NAME] = { 0 };
	printf("Please enter a contact to delete:>");
	scanf("%s", name);
	int x = FindContact(pc,name);
	if (x < 0)
	{
		printf("No contact found\n");
	}
	else
	{
		int i = 0;
		for (i = x; x < pc->sz - 1; x++)
		{
			pc->data[x] = pc->data[x + 1];//The ability to delete is to overwrite the previous contact's information with the information of the latter contact
		}
		pc->sz--;//Delete completed, address book reduced by one person
		printf("Delete successful!\n");
	}
}

4. Find Contacts

Before finding a contact, you need to know where the person you are looking for is in the address book, so the FindContact function is also used in functions that implement the search function to find the location of the contact.

void SearchContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	printf("Please enter the contact you want to find:>");
	scanf("%s", name);
	int x = FindContact(pc, name);//x is the location of the contact being found
	if (x < 0)
	{
		printf("No contact found\n");
	}
	else
	{
		printf("%-20s\t%-5s\t%-5s\t%-12s\t%-20s\t\n", "Full name", "Gender", "Age", "Telephone", "address");
		printf("%-20s\t%-5s\t%-5d\t%-12s\t%-20s\t\n",
			pc->data[x].name,
			pc->data[x].sex,
			pc->data[x].age,
			pc->data[x].tele,
			pc->data[x].addr);
	}
}

5. Modify Contacts

As with delete and find, you need to know the location of the modified contact before modifying it, and use the FindContact function to return the location.

void ModifyContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	printf("Please enter a contact to modify:>");
	scanf("%s", name);
	int x = FindContact(pc, name);//x is the location of the person being modified
	if (x < 0)
	{
		printf("No contact found\n");
	}
	else
	{
		printf("Please enter your name:>");
		scanf("%s", pc->data[x].name);
		printf("Please enter gender:>");
		scanf("%s", pc->data[x].sex);
		printf("Please enter your age:>");
		scanf("%d", &(pc->data[x].age));
		printf("Please enter a phone:>");
		scanf("%s", pc->data[x].tele);
		printf("Please enter an address:>");
		scanf("%s", pc->data[x].addr);
		printf("Successful modification!\n");
	}
}

Code Overview

To better implement the address book, we divide the program into three files:
test.c - A module to test the address book
contact.h -- type definition, function declaration
contact.c - Implementation of Function

test.c

#include"contact.h" //reference header file
void menu()
{
	printf("***************************\n");
	printf("***   1.Add     2.Del   ***\n");
	printf("***   3.Search  4.Modify***\n");
	printf("***        5.Print      ***\n");
	printf("***        0.Exit       ***\n");
	printf("***************************\n");
}

enum Option //Define the name of each feature as a member of an enumeration type to make it more recognizable
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	PRINT,
};
 
int main()
{
	Contact con;
	InitContact(&con);
	int input = 0;
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		    case ADD:
				AddContact(&con);//Add a Contact
				break;
			case DEL:
				DelContact(&con);//Delete Contact
				break;
			case SEARCH:
				SearchContact(&con);//find contact
				break;
			case MODIFY:
				ModifyContact(&con);//Modify Contacts
				break;
			case PRINT:
				PrintContact(&con);//show address book
				break;
			case EXIT:
				DestoryContact(&con);//Destroy Address Book
				printf("Exit Address Book\n");
				break;
			default:
				printf("Selection error, please re-select:");
				break;
		}
	} while (input);
	return 0;
}

contact.h

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAX_NAME 20
#define SEX 10
#define MAX_ADDR 100
#define MAX_TELE 12
#define MAX_SZ 3
#define ADD_CAP 2
#define MAX 1000

typedef struct PeoInfo
{
	char name[MAX_NAME];
	char sex[SEX];
	int  age;
	char tele[MAX_TELE];
	char addr[MAX_ADDR];	
} PeoInfo;  //Redefinition For ease of use, the same below

typedef struct Contact
{
	PeoInfo* data;
	int sz;
	int capacity;
}Contact;  //redefinition

void InitContact(Contact* pc);  //Initialize Address Book

void AddContact(Contact* pc);  //Add a contact

void PrintContact(const Contact* pc);  //Print Address Book

void DelContact(Contact* pc);  //Delete Contact

void SearchContact(Contact* pc);  //find contact

void ModifyContact(Contact* pc);  //Modify Contacts

void DestoryContact(Contact* pc);  //Destroy Address Book

contact.c

#include"contact.h"
void InitContact(Contact* pc)
{
	pc->data = (PeoInfo*) malloc(MAX_SZ*sizeof(PeoInfo));
	if (pc->data == NULL)
	{
		perror("InitContact");
		return;
	}
	pc->sz = 0;
	pc->capacity = MAX_SZ;
}

void DestoryContact(Contact* pc)
{
	free(pc->data);
	pc->data = NULL;
	pc->sz = 0;
	pc->capacity = 0;
}

void AddContact(Contact* pc)
{
	if (pc->sz == pc->capacity)
	{
		PeoInfo* ptr=(PeoInfo*)realloc(pc->data, (ADD_CAP + pc->capacity) * sizeof(PeoInfo));
		if (ptr != NULL)
		{
			pc->data = ptr;
			pc->capacity += ADD_CAP;
			printf("Successful Capacity Increase!\n");
		}
		else
		{
			printf("Capacitance Failure\n");
			return;
		}
	}
	printf("Please enter your name:>");
	scanf("%s", pc->data[pc->sz].name);
	printf("Please enter gender:>");
	scanf("%s", pc->data[pc->sz].sex);
	printf("Please enter your age:>");
	scanf("%d", &(pc->data[pc->sz].age));
	printf("Please enter a phone:>");
	scanf("%s", pc->data[pc->sz].tele);
	printf("Please enter an address:>");
	scanf("%s", pc->data[pc->sz].addr);
	pc->sz++;
	printf("Increase Success!\n");
}

void PrintContact(const Contact* pc)
{
	printf("%-20s\t%-5s\t%-5s\t%-12s\t%-20s\t\n", "Full name", "Gender", "Age", "Telephone", "address");
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		printf("%-20s\t%-5s\t%-5d\t%-12s\t%-20s\t\n",
			pc->data[i].name,
			pc->data[i].sex,
			pc->data[i].age,
			pc->data[i].tele,
			pc->data[i].addr);
	}
}

static int FindContact(const Contact* pc,char name[])
{
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(name, (pc->data[i]).name) == 0)
			return i;
	}
	return -1;
}
void DelContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	printf("Please enter a contact to delete:>");
	scanf("%s", name);
	int x = FindContact(pc,name);
	if (x < 0)
	{
		printf("No contact found\n");
	}
	else
	{
		int i = 0;
		for (i = x; x < pc->sz - 1; x++)
		{
			pc->data[x] = pc->data[x + 1];
		}
		pc->sz--;
		printf("Delete successful!\n");
	}
}

void SearchContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	printf("Please enter the contact you want to find:>");
	scanf("%s", name);
	int x = FindContact(pc, name);
	if (x < 0)
	{
		printf("No contact found\n");
	}
	else
	{
		printf("%-20s\t%-5s\t%-5s\t%-12s\t%-20s\t\n", "Full name", "Gender", "Age", "Telephone", "address");
		printf("%-20s\t%-5s\t%-5d\t%-12s\t%-20s\t\n",
			pc->data[x].name,
			pc->data[x].sex,
			pc->data[x].age,
			pc->data[x].tele,
			pc->data[x].addr);
	}
}

void ModifyContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	printf("Please enter a contact to modify:>");
	scanf("%s", name);
	int x = FindContact(pc, name);
	if (x < 0)
	{
		printf("No contact found\n");
	}
	else
	{
		printf("Please enter your name:>");
		scanf("%s", pc->data[x].name);
		printf("Please enter gender:>");
		scanf("%s", pc->data[x].sex);
		printf("Please enter your age:>");
		scanf("%d", &(pc->data[x].age));
		printf("Please enter a phone:>");
		scanf("%s", pc->data[x].tele);
		printf("Please enter an address:>");
		scanf("%s", pc->data[x].addr);
		printf("Successful modification!\n");
	}
}

Running effect

Tags: C

Posted on Sun, 12 Sep 2021 00:22:49 -0400 by Kinneh