C address book < Basic Edition >

Posted by cottonbuds2005 on Sat, 05 Feb 2022 19:01:59 +0100

catalogue

1. Basic requirements

2. Function analysis of address book

    (1) Menu implementation

    (2) Initialization function

    (3) Add contacts and print

    (4) Delete specified contact

    (5) Find and print the specified contact

    (6) Modify designee information

    (7) Print all information

    (8) Sort address book information

3. Overall code

1. Basic requirements

  • The address book can store the information of 1000 people, including (name + age + gender + phone + address)
  • Functions include:
  1. Add human information
  2. Delete the information of the designated person
  3. Modify designee information
  4. Find designee information
  5. Sort address book information
  • Note: this code needs three files to complete. The functions are as follows:
  1. test.c file: module for testing address book
  2. contact.h file: type definition, function declaration
  3. contact.c file: function implementation

2. Function analysis of address book

(1) Menu implementation

  • test.c Documents:
#include"contact.h"
void menu()
{
	printf("*******************************************\n");
	printf("*******     1. add     2. del     *********\n");
	printf("*******     3. search  4. modify  *********\n");
	printf("*******     5. sort    6. print   *********\n");
	printf("*******     0. exit               *********\n");
	printf("*******************************************\n");
}

  • At this point, test C overall Code:
#include"contact.h"
void menu()
{
	printf("*******************************************\n");
	printf("*******     1. add     2. del     *********\n");
	printf("*******     3. search  4. modify  *********\n");
	printf("*******     5. sort    6. print   *********\n");
	printf("*******     0. exit               *********\n");
	printf("*******************************************\n");
}
enum Option
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SORT,
	PRINT
};
int main()
{
	int input = 0;
	do
	{
		menu();
		printf("Please select:>");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			//increase
			break;
		case DEL:
			//delete
			break;
		case SEARCH:
			//lookup
			break;
		case MODIFY:
			//modify
			break;
		case SORT:
			//sort
			break;
		case PRINT:
			//Print
			break;
		case EXIT:
			//sign out
			break;
		default:
			break;
		}
	} while (input);
	return 0;
}
  • be careful:

When the menu is printed, you need to select the number to realize the corresponding function. In order to facilitate the coder to know the specific function of the corresponding number, the enum keyword is used above

(2) Initialization function

Before initializing the address book, you have to create an address book:

  • contact.h documents:
#pragma once
#include<stdio.h>
#define MAX_NAME 20
#define MAI_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30
#define MAX 1000
//Store the information content of each contact
typedef struct PeoInfo
{
	char name[MAX_NAME];
	char sex[MAI_SEX];
	int age;
	char tele[MAX_TELE];
	char addr[MAX_ADDR];
}PeoInfo;
//Create address book
typedef struct Contact
{
	PeoInfo data[MAX]; //Store the information of the added person
	int sz; //Record the number of valid messages in the current address book
}Contact;

After creating the address book, proceed to initialization

  • contact.c Documents:
void InitContact(Contact* pc)
{
	pc->sz = 0;
	//memset();  Memory settings
	memset(pc->data, 0, sizeof(pc->data));
}
  • contact.h documents:
void InitContact(Contact* pc);
  • test.c Documents:

(3) Add contacts and print

  • contact.h file
void AddContact(Contact* pc);
  • contact.c Documents
void AddContact(Contact* pc)
{
	if (pc->sz == MAX)
	{
		printf("The address book is full and cannot be added\n");
		return;
	}
	//Add a person's information
	printf("Please enter your first name:>");
	scanf("%s", pc->data[pc->sz].name);
	printf("Please enter age:>");
	scanf("%s", &pc->data[pc->sz].age);
	printf("Please enter gender:>");
	scanf("%s", &pc->data[pc->sz].sex);
	printf("Please enter the phone number:>");
	scanf("%s", pc->data[pc->sz].tele);
	printf("Please enter the address:>");
	scanf("%s", pc->data[pc->sz].addr);;
	pc->sz++;
	printf("Increase success\n");
}

After adding contacts, we don't know whether they are added successfully, so we need to print them out

  • contact.h file
void PrintContact(const Contact* pc);
  • contact.c Documents
//Print contact information
void PrintContact(const Contact* pc)
{
	if (pc->sz == 0)
	{
		printf("Address book is empty!\n");
		return;
	}
	int i = 0;
	//Print title
	printf("%-12s\t%-5s\t%-5s\t%-12s\t%-20s\n", "name", "Age", "Gender", "Telephone", "address");
	//print data
	for (i = 0; i < pc->sz; i++)
	{
		printf("%-12s\t%-5d\t%-5s\t%-12s\t%-20s\n",
			    pc->data[i].name,           
			    pc->data[i].age, 
			    pc->data[i].sex, 
	            pc->data[i].tele, 
		        pc->data[i].addr
		       );
	}
}
  • The operation effect is as follows:

(4) Delete specified contact

  • Before we delete the specified contact, we must first find out whether there is the person to be deleted
	//1. You have to find the person you want to delete first
	//Yes / no
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("The person to delete does not exist\n");
		return;
	}
  • Next, implement the FindByName function:
//Find contacts to delete
static int FindByName(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; //can't find
}
  • After finding the person to be deleted, the next step is to delete. The total code is as follows:
  • contact.c Documents
//Delete contact information
void DelContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	if (pc->sz == 0)
	{
		printf("The address book is empty and does not need to be deleted\n");
		return;
	}
	printf("Please enter the name of the person to delete:>");
	scanf("%s", name);
	//1. You have to find the person you want to delete first
	//Yes / no
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("The person to delete does not exist\n");
		return;
	}
	//2. Delete
	int i = 0;
	for (i = pos; i < pc->sz - 1; i++)
	{
		pc->data[i] = pc->data[i + 1];
	}
	pc->sz--;
	printf("Deleted successfully\n");
}
  • The test results are as follows:

(5) Find and print the specified contact

  • contact.h documents:
void SearchContact(Contact* pc);
  • contact.c) documents:
//Find the specified contact
void SearchContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	printf("Please enter the name of the person you want to find:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("The person you are looking for does not exist\n");
		return;
	}
	else
	{
		int i = 0;
		//Print title
		printf("%-12s\t%-5s\t%-5s\t%-12s\t%-20s\n", "name", "Age", "Gender", "Telephone", "address");
		//print data
		printf("%-12s\t%-5d\t%-5s\t%-12s\t%-20s\n",
			pc->data[pos].name,
			pc->data[pos].age,
			pc->data[pos].sex,
			pc->data[pos].tele,
			pc->data[pos].addr);
	}
}
  • The operation results are as follows:

(6) Modify designee information

  • contact.h documents:
void ModifyContact(Contact* pc);
  • contact.c Documents:
//Modify specified contact
void ModifyContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	printf("Please enter the name of the person to modify:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("The person to modify does not exist\n");
		return;
	}
	else
	{
		printf("Please enter your first 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 the phone number:>");
		scanf("%s", pc->data[pos].tele);
		printf("Please enter the address:>");
		scanf("%s", pc->data[pos].addr);
		printf("Modified successfully\n");
	}
}
  • The operation results are as follows:

(7) Print all information

  • In fact, this code has appeared before. Let's review:
//Print contact information
void PrintContact(const Contact* pc)
{
	if (pc->sz == 0)
	{
		printf("Address book is empty!\n");
		return;
	}
	int i = 0;
	//Print title
	printf("%-12s\t%-5s\t%-5s\t%-12s\t%-20s\n", "name", "Age", "Gender", "Telephone", "address");
	//print data
	for (i = 0; i < pc->sz; i++)
	{
		printf("%-12s\t%-5d\t%-5s\t%-12s\t%-20s\n",
			    pc->data[i].name,           
			    pc->data[i].age, 
			    pc->data[i].sex, 
	            pc->data[i].tele, 
		        pc->data[i].addr
		       );
	}
}

(8) Sort address book information

  • contact.c Documents
//Sort by name
int CmpByName(const void* e1, const void* e2)
{
	return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name);
}
//Sort by age
int CmpByAge(const void* e1, const void* e2)
{
	return ((PeoInfo*)e1)->age - ((PeoInfo*)e2)->age;
}
//Sort the order of contacts in the address book
void SortContact(Contact* ps)
{
	int input = 0;
	printf("1,full name\t2,Age\n");
	printf("Please select how you want to sort:>");
	scanf("%d", &input);
	switch (input)
	{
	case 1:
		qsort(ps->data, ps->sz, sizeof(PeoInfo), CmpByName);//sort
		printf("Sorting succeeded\n");
		break;
	case 2:
		qsort(ps->data, ps->sz, sizeof(PeoInfo), CmpByAge);//sort
		printf("Sorting succeeded\n");
		break;
	default:
		printf("Please enter a valid number\n");
	}
}

3. Overall code

  • contact.h documents:
#pragma once
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_NAME 20
#define MAI_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30
#define MAX 1000

//Store everyone's information
typedef struct PeoInfo
{
	char name[MAX_NAME];
	char sex[MAI_SEX];
	int age;
	char tele[MAX_TELE];
	char addr[MAX_ADDR];
}PeoInfo;

//Create address book
typedef struct Contact
{
	PeoInfo data[MAX]; //Store the information of the added person
	int sz; //Record the number of valid messages in the current address book

}Contact;

//Initialize address book
void InitContact(Contact* pc);

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

//Print contact information
void PrintContact(const Contact* pc);

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

//Find the specified contact
void SearchContact(Contact* pc);

//Modify specified contact
void ModifyContact(Contact* pc);

//Sort contacts
void SortContact(Contact* ps);
  • contact.c Documents:
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
//Initialize address book
void InitContact(Contact* pc)
{
	pc->sz = 0;
	//memset();  Memory settings
	memset(pc->data, 0, sizeof(pc->data));
}
//Add a Contact 
void AddContact(Contact* pc)
{
	if (pc->sz == MAX)
	{
		printf("The address book is full and cannot be added\n");
		return;
	}
	//Add a person's information
	printf("Please enter your first 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 the phone number:>");
	scanf("%s", pc->data[pc->sz].tele);
	printf("Please enter the address:>");
	scanf("%s", pc->data[pc->sz].addr);
	pc->sz++;
	printf("Increase success\n");
}

//Print contact information
void PrintContact(const Contact* pc)
{
	if (pc->sz == 0)
	{
		printf("Address book is empty!\n");
		return;
	}
	int i = 0;
	//Print title
	printf("%-12s\t%-5s\t%-5s\t%-12s\t%-20s\n", "name", "Age", "Gender", "Telephone", "address");
	//print data
	for (i = 0; i < pc->sz; i++)
	{
		printf("%-12s\t%-5d\t%-5s\t%-12s\t%-20s\n",
			    pc->data[i].name,           
			    pc->data[i].age, 
			    pc->data[i].sex, 
	            pc->data[i].tele, 
		        pc->data[i].addr
		       );
	}
}

//Find contacts to delete
static int FindByName(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; //can't find
}

//Delete contact information
void DelContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	if (pc->sz == 0)
	{
		printf("The address book is empty and does not need to be deleted\n");
		return;
	}
	printf("Please enter the name of the person to delete:>");
	scanf("%s", name);
	//1. You have to find the person you want to delete first
	//Yes / no
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("The person to delete does not exist\n");
		return;
	}
	//2. Delete
	int i = 0;
	for (i = pos; i < pc->sz - 1; i++)
	{
		pc->data[i] = pc->data[i + 1];
	}
	pc->sz--;
	printf("Deleted successfully\n");
}


//Find the specified contact
void SearchContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	printf("Please enter the name of the person you want to find:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("The person you are looking for does not exist\n");
		return;
	}
	else
	{
		int i = 0;
		//Print title
		printf("%-12s\t%-5s\t%-5s\t%-12s\t%-20s\n", "name", "Age", "Gender", "Telephone", "address");
		//print data
		printf("%-12s\t%-5d\t%-5s\t%-12s\t%-20s\n",
			pc->data[pos].name,
			pc->data[pos].age,
			pc->data[pos].sex,
			pc->data[pos].tele,
			pc->data[pos].addr);
	}
}

//Modify specified contact
void ModifyContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	printf("Please enter the name of the person to modify:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("The person to modify does not exist\n");
		return;
	}
	else
	{
		printf("Please enter your first 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 the phone number:>");
		scanf("%s", pc->data[pos].tele);
		printf("Please enter the address:>");
		scanf("%s", pc->data[pos].addr);
		printf("Modified successfully\n");
	}
}

//Sort by name
int CmpByName(const void* e1, const void* e2)
{
	return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name);
}
//Sort by age
int CmpByAge(const void* e1, const void* e2)
{
	return ((PeoInfo*)e1)->age - ((PeoInfo*)e2)->age;
}
//Sort the order of contacts in the address book
void SortContact(Contact* ps)
{
	int input = 0;
	printf("1,full name\t2,Age\n");
	printf("Please select how you want to sort:>");
	scanf("%d", &input);
	switch (input)
	{
	case 1:
		qsort(ps->data, ps->sz, sizeof(PeoInfo), CmpByName);//sort
		printf("Sorting succeeded\n");
		break;
	case 2:
		qsort(ps->data, ps->sz, sizeof(PeoInfo), CmpByAge);//sort
		printf("Sorting succeeded\n");
		break;
	default:
		printf("Please enter a valid number\n");
	}
}
  • test.c Documents:
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void menu()
{
	printf("*******************************************\n");
	printf("*******     1. add     2. del     *********\n");
	printf("*******     3. search  4. modify  *********\n");
	printf("*******     5. sort    6. print   *********\n");
	printf("*******     0. exit               *********\n");
	printf("*******************************************\n");
}
enum Option
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SORT,
	PRINT
};
int main()
{
	int input = 0;
	//Create address book
	Contact con;//mail list
	//Initialize address book
	InitContact(&con);
	do
	{
		menu();
		printf("Please select:>");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			//increase
			AddContact(&con);
			break;
		case DEL:
			//delete
			DelContact(&con);
			break;
		case SEARCH:
			//lookup
			SearchContact(&con);
			break;
		case MODIFY:
			//modify
			ModifyContact(&con);
			break;
		case SORT:
			//sort
			SortContact(&con);
			break;
		case PRINT:
			//Print
			PrintContact(&con);
			break;
		case EXIT:
			//sign out
			printf("Exit address book\n");
			break;
		default:
			printf("Wrong selection, reselect\n");
			break;
		}
	} while (input);
	return 0;
}

Topics: C Back-end