c language realizes the function of adding, deleting, modifying and checking the address book, and can sort the contacts

Posted by StickeR on Wed, 26 Jan 2022 03:51:13 +0100

The idea of realizing address book is as follows:

1. When the program runs, the user should first see the Menu bar options and make a choice corresponding to the options given in the Menu bar. Here, we simply design a Menu() function to let the user see the optional items;

2. The user's optional range should be 1 ~ 7. In order to avoid the user making choices beyond the range, we can use the switch statement to judge the user's choice. If the number entered by the user is illegal, the user can continue to choose, and the while statement is required to continue to select this action, as well as the user's continuous operation of the address book;

3. If the user chooses different options, the program will call the corresponding function to realize its function, and we optimize the switch statement. If we directly judge the input number, we also need to judge the captured user selection against the menu bar options designed directly. In order to solve this problem, We use enumeration to improve the readability of the code;

enum menu
{
	ADD = 1,
	SHOW,
	MODIFIES,
	FIND,
	DELETE,
	SORT,
	EXITS,
};

4. Next, we implement the functions of the address book one by one. It is not difficult to think that to operate the contact information in the address book, there must be a carrier for storing the contact information, and each contact information includes name, age, gender, telephone number and address. Therefore, we first customize a message type, Then create a message array to store contact information. The size of the array is set by ourselves. It is best to use #define to set the size of num, which can be changed completely.

#define num 100
typedef struct s
{
	int age;
	char name[10];
	char number[20];
	char sex[5];
	char adress[20];
}message;

5. Add contact function: to add a contact, you have to operate on the array that stores all contact information, and also know how many elements the new contact information is stored in the array, so this addmessage function should receive these two parameters. Considering that the number of contacts in the address book should be counted and should be + +, after the contact is successfully added, Therefore, while creating the contact array, we should also define a variable to tell some function functions the number of contacts in the address book, and in the addmessage function, we also need to + 1 the value of this variable, so we should pass in the address of this variable to this function to modify the value of the variable.

void MyScanf(message arr[num], int* i)
{
	printf("Please enter contact name:\n");
	scanf("%s", (arr[*i].name));
	printf("Please enter contact age:\n");
	scanf("%d", &(arr[*i].age));
	printf("Please enter the contact phone number:\n");
	scanf("%s", (arr[*i].number));
	printf("Please enter contact gender:\n");
	scanf("%s", (arr[*i].sex));
	printf("Please enter the contact address:\n");
	scanf("%s", (arr[*i].adress));
}
void AddMessage(message arr[num],int* i )
{
	MyScanf(arr,i);
	(*i)++;
}

6. Show the function of all current contacts to the user: this function is easy to implement (that is, traversing the array elements). The show function only needs to know how many elements are in the contact array and the address of the array to complete the traversal, and then print out the traversed information. I won't repeat it here, but go directly to the code.

void MyPrintf(message arr[num],int j)
{
	printf("%s ", arr[j].name);
	printf("%d ", arr[j].age);
	printf("%s ", arr[j].number);
	printf("%s ", arr[j].sex);
	printf("%s ", arr[j].adress);
	printf("\n");
}
void Show(message arr[num],int i)
{
	for (int j = 0;j < i;j++)
	{
		MyPrintf(arr, j);
	}
}

7. Modify contact information function: as we all know, to modify the existing practitioner information, we should first find the contact information to be modified. First, we find the array index value of the contact according to the contact name to be modified entered by the user, and then re record the information (the same as the entry method when adding contact information), If the contact to be modified is not found in the address book, the user will be prompted that there is no information about this person in the address book.

void Modifies(message arr[num],int* i)
{
	printf("Please enter the contact name you want to modify:\n");
	char arr1[10] = { 0 };
	scanf("%s", arr1);
	int k = 0;
	for (int j = 0;j < (*i);j++)
	{
		if (strcmp(arr1, arr[j].name) == 0)
		{
			k = 1;
			MyPrintf(arr, j);
			int* a = &j;
			MyScanf(arr, a);
		}
	}
	if (k == 0)
	{
		printf("There is no such person in the address book!!!\n");
	}
}

8. Find contact information: the implementation of this function is particularly simple. It is nothing more than traversing the contact array to find the contact matching the user's search information and printing the contact information. If you traverse the whole contact array and can't find it, it will also prompt that there is no contact in the address book.

void Find(message arr[num],int i)
{
	char arr1[10] = {0};
	printf("Please enter the name of the contact you want to find:\n");
	scanf("%s",arr1);
	int k = 0;
	for (int j = 0;j < i;j++)
	{
		if (strcmp(arr1, arr[j].name) == 0)
		{
			k = 1;
			MyPrintf(arr, j);
		}
	}
	if (k == 0)
	{
		printf("There is no such person in the address book!!!\n");
	}
}

9. Delete contact information: similarly, to delete a contact in the address book, first find the location of the contact. If it is not found, the user will be prompted that there is no information of the contact in the address book. If it is found, the user should first show the information of the contact to the user, and then ask the user whether to delete the contact. If the user is sure to delete, proceed to the next step, To erase this data, you need to judge the location of the data. If the contact is the last contact (the last one in the existing contacts in the address book), you only need to - 1 the number of contacts in the address book. In this way, users can't access the elements of the location except adding contacts, When adding a contact, add it in this position, just erase the information of this element and let the newly added contact replace it. If the location of the element is not the end of the address book, copy the element of the next location to its location, and move the positions of the elements behind it forward one bit. After this operation, let the number of contacts be - 1.

int Delete(message arr[num], int* i)
{
	char arr1[10] = { 0 };
	printf("Please enter the contact name to delete:\n");
	scanf("%s", arr1);
	for (int j = 0;j < (*i);j++)
	{
		if (strcmp(arr1, arr[j].name) == 0)
		{
			MyPrintf(arr, j);
			int a = 0;
			printf(" Are you sure to delete?\n");
			printf("****1.YES****\n");
			printf("****2.NO ****\n");
			scanf("%d",&a);
			if (a == 1)
			{
				if ((j + 1) < (*i))
				{
					for (int k = j;k < (*i) - 1;k++)
					{
						arr[k] = arr[k + 1];
					}
				}
				(*i)--;
				
			}
			else
			{
				return -1;
			}
			return 1;
		}
	}
	printf("There is no such person in the address book!!!\n");
	return -1;
}

10. Sorting the contact information according to the first letter is an old problem. Here, we directly use bubble sorting to sort the existing contact information, but we use the strcmp function when judging whether to exchange the element position.

void SortByname(message arr[num], int i)
{
	for (int j = 0;j < i - 1;j++)
	{
		for (int k = 0;k < i - j - 1;k++)
		{
			if (strcmp(arr[k].name,arr[k+1].name) > 0)
			{
				message a = arr[k+1];
				arr[k + 1] = arr[k];
				arr[k] = a;
			}
		}
	}
}

Existing problems: after entering the program, a message type array with num elements is created. If num is large, whether the program can run is a problem. Even if the program runs, if I only add a few contacts, the program opens up a lot of space for these groups, resulting in a waste of memory, Another extreme condition is that if I want to add 10000 contact information, then obviously this program can't run at all, because it can't open up 10000 contact space for the message array. How can we improve this address book? Please look at the next article.

Here we give all the codes

Header file:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#pragma once
#pragma warning (disable:4996)
#define num 100
enum menu
{
	ADD = 1,
	SHOW,
	MODIFIES,
	FIND,
	DELETE,
	SORT,
	EXITS,
};
typedef struct s
{
	int age;
	char name[10];
	char number[20];
	char sex[5];
	char adress[20];
}message;
extern void Choose();
extern void AddMessage(message arr[num],int*);
extern void Show(message arr[num], int);
extern void Find(message arr[num],int);
extern void MyPrintf(message arr[num], int);
extern void Modifies(message arr[num],int*);
extern int Delete(message arr[num], int*);
extern void SortByname(message arr[num], int);

main function:

#include "AddressBook.h"
#pragma once
void Choose()
{
	printf("********1.Add new contact **********\n");
	printf("********2.Show existing contacts********\n");
	printf("********3.Modify contact information********\n");
	printf("********4.Find contact information********\n");
	printf("********5.Delete Contact ************\n");
	printf("********6.Sort existing contacts******\n");
	printf("********7.Exit address book************\n");
}

int main()
{
	message arr[num];
	int b = 0;
	while (1)
	{
		Choose();
		int i = 0;
		printf("Please enter your choice:\n");
		scanf("%d", &i);
		switch (i)
		{
		case ADD :
			AddMessage(arr,&b);
			break;
		case SHOW:
			Show(arr,b);
			break;
		case MODIFIES:
			Modifies(arr,&b);
			break;
		case FIND:
			Find(arr,b);
			break;
		case DELETE:
			Delete(arr,&b);
			break;
		case SORT:
			SortByname(arr,b);
			break;
		case EXITS:
			exit(0);
			break;
		default :
			printf("Your choice is wrong, choose again!\n");
			i = 0;
			break;
		}
	}
	return 0;
}

Menu function:

#pragma once
#include "AddressBook.h"
void MyScanf(message arr[num], int* i)
{
	printf("Please enter contact name:\n");
	scanf("%s", (arr[*i].name));
	printf("Please enter contact age:\n");
	scanf("%d", &(arr[*i].age));
	printf("Please enter the contact phone number:\n");
	scanf("%s", (arr[*i].number));
	printf("Please enter contact gender:\n");
	scanf("%s", (arr[*i].sex));
	printf("Please enter the contact address:\n");
	scanf("%s", (arr[*i].adress));
}
void AddMessage(message arr[num],int* i )
{
	MyScanf(arr,i);
	(*i)++;
}
void Show(message arr[num],int i)
{
	for (int j = 0;j < i;j++)
	{
		MyPrintf(arr, j);
	}
}
void Find(message arr[num],int i)
{
	char arr1[10] = {0};
	printf("Please enter the name of the contact you want to find:\n");
	scanf("%s",arr1);
	int k = 0;
	for (int j = 0;j < i;j++)
	{
		if (strcmp(arr1, arr[j].name) == 0)
		{
			k = 1;
			MyPrintf(arr, j);
		}
	}
	if (k == 0)
	{
		printf("There is no such person in the address book!!!\n");
	}
}
void MyPrintf(message arr[num],int j)
{
	printf("%s ", arr[j].name);
	printf("%d ", arr[j].age);
	printf("%s ", arr[j].number);
	printf("%s ", arr[j].sex);
	printf("%s ", arr[j].adress);
	printf("\n");
}
void Modifies(message arr[num],int* i)
{
	printf("Please enter the contact name you want to modify:\n");
	char arr1[10] = { 0 };
	scanf("%s", arr1);
	int k = 0;
	for (int j = 0;j < (*i);j++)
	{
		if (strcmp(arr1, arr[j].name) == 0)
		{
			k = 1;
			MyPrintf(arr, j);
			int* a = &j;
			MyScanf(arr, a);
		}
	}
	if (k == 0)
	{
		printf("There is no such person in the address book!!!\n");
	}
}
int Delete(message arr[num], int* i)
{
	char arr1[10] = { 0 };
	printf("Please enter the contact name to delete:\n");
	scanf("%s", arr1);
	for (int j = 0;j < (*i);j++)
	{
		if (strcmp(arr1, arr[j].name) == 0)
		{
			MyPrintf(arr, j);
			int a = 0;
			printf(" Are you sure to delete?\n");
			printf("****1.YES****\n");
			printf("****2.NO ****\n");
			scanf("%d",&a);
			if (a == 1)
			{
				if ((j + 1) < (*i))
				{
					for (int k = j;k < (*i) - 1;k++)
					{
						arr[k] = arr[k + 1];
					}
				}
				(*i)--;
				
			}
			else
			{
				return -1;
			}
			return 1;
		}
	}
	printf("There is no such person in the address book!!!\n");
	return -1;
}
void SortByname(message arr[num], int i)
{
	for (int j = 0;j < i - 1;j++)
	{
		for (int k = 0;k < i - j - 1;k++)
		{
			if (strcmp(arr[k].name,arr[k+1].name) > 0)
			{
				message a = arr[k+1];
				arr[k + 1] = arr[k];
				arr[k] = a;
			}
		}
	}
}

Topics: C Back-end