c language to achieve simple address book


Today, let's share a simple address book (implemented in c language)

First, I want to introduce the basic functions of the address book

The address book can be used to store the information of 1000 people. Each person's information includes: name, gender, age, telephone and address

Function:

  1. Add contact information
  2. Delete specified contact information
  3. Find specified contact information
  4. Modify the specified contact information
  5. Show all contact information

1. File creation

The first is the creation of files. We have created three files here, a header file, a test source file and a source file to realize functions. The purpose of writing code in this way is to understand the structure, hierarchy and functions of the code more clearly. When we implement the function of each function, we should declare the function in the header file

Realization of menu circulation function

After understanding the function, we need to analyze how to implement it. First, we need to write the circular selection function of the address book menu. It's easy to think of here. We'll go directly to the code

`void menu()
{
	printf("**************************\n");
	printf("***********1.add     *****\n");
	printf("***********2.delete  *****\n");
	printf("***********3.search  *****\n");
	printf("***********4.modify  *****\n"); 
	printf("***********5.show    *****\n");
	printf("***********0.exit    *****\n");
}`
    enum Option
{
	exit,
	add,
	delete,
	search,
	modify,
	show,
};
int main()
{
    int input=0;
do 
{
	menu();
	printf("Please select:\n");
	scanf("%d", &input);
	switch (input)
	{

	case exit:
		printf("Exit address book\n");
		break;
	case add:
		//add to
		ADDContact(&con);
		break;
	case delete:
		//delete
		Delete(&con);
		break;
	case search:
		//lookup
		Search(&con);
		break;
	case modify:
		//modify
		Modify(&con);
		break;
	case show:
		//display
		Show(&con);
		break;
	default:
		printf("Wrong selection. Please reselect:\n");
		break;

	}
} while (input);
return 0;
}

After writing this function, we can test it.

2. Create address book

Then we need to complete the function of adding, deleting and searching contacts. The first is the storage of contacts. Because a person has a lot of information and is a complex type, we use a structure to solve it. Then we can store so many people's information with a structure array.

struct People
{
	char name[NAME_MAX];
	int age;
	char sex[SEX_MAX];
	char number[NUMBER_MAX];
	char address[ADDRESS_MAX];
};

struct Contact
{
	//Create a structure array for storing data of 1000 people
	struct People data[MAX];
	int sz;   //Number of valid information
};

The address book structure Contact contains a structure array and records the number of people in the address book at this time. In this way, we can operate the address book structure.

Initialize address book

After creating this address book, we need to initialize it and write an initialization function void InitContact(struct Contact* pc);

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

Here, we use the library function of c to directly initialize the address book information to 0. We want to operate the address book, so we can directly transfer the address of the created address book to the function for use.

Add contact implementation

Adding a contact is also a direct operation on the address book, and the address of the address book should be transferred.

void ADDContact(struct Contact* pc)
{


	if (pc->sz == MAX)                       //Determine whether the contact is full
    {

		printf("The address book is full\n");
	}
	else
	{
		printf("Please enter your first name:\n");
		scanf("%s", pc->data[pc->sz].name);
		printf("Please enter age:\n");
		scanf("%d", &pc->data[pc->sz].age);
		printf("Please enter gender:\n");
		scanf("%s", pc->data[pc->sz].sex);
		printf("Please enter phone number:\n");
		scanf("%s", pc->data[pc->sz].number);
		printf("Please enter the address:\n");
		scanf("%s", pc->data[pc->sz].address);
		printf("Successfully added!\n");
		pc->sz++;                            //Record sz and record the number of people in the address book at this time
    }

}

Show contact implementation

Here we first implement this function to verify whether our other functions have been implemented.

//Show contacts
void Show(struct Contact* pc)
{
	printf("%15s\t%5s\t%8s\t%15s\t%30s\n", "name","age","sex","number","address"); //Name of each message
	for (int i = 0; i < pc->sz; i++)
	{
		printf("%15s\t%5d\t%8s\t%15s\t%30s\n",              //Printing of information
               pc->data[i].name,
               pc->data[i].age, pc->data[i].sex,
               pc->data[i].number,
               pc->data[i].address);            
	}
}

find contact

int FIND(struct Contact* pc, char name[])
{
	for (int i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)   //Traverse each person. If the person's position in the array is found, it returns - 1
		{
			return i;
		}
	}
	return -1;
}

void Search(struct Contact* pc)
{
	printf("Please enter the name of the contact you want to find:\n");
	char name[NAME_MAX] = { 0 };
	scanf("%s", &name);
	int flag = FIND(pc, name);
	if (flag != -1)
	{
		printf("%15s\t%5s\t%8s\t%15s\t%30s\n", "name", "age", "sex", "number", "address");
		printf("%15s\t%5d\t%8s\t%15s\t%30s\n",
               pc->data[flag].name,
               pc->data[flag].age, pc->data[flag].sex,
               pc->data[flag].number,
               pc->data[flag].address);     //If found, print this person's information directly
	}
	else
	{
		printf("No one was found\n");
	}

}

Delete contact implementation

To delete a contact, we just need to find this person first, move all the people behind forward and overwrite the person to be deleted.

//delete
void Delete(struct Contact* pc)
{
	if (pc->sz == 0)
	{
		printf("Contact cannot be deleted because the address book is empty\n");
		return;
	}
		printf("Please enter the name of the contact you want to delete:\n");
		char name[NAME_MAX] = { 0 };
		scanf("%s", &name);
		//lookup
		int flag = FIND(pc, name);
		if (flag == -1)
		{
			printf("No one was found\n");
		}
		else
		{
			//delete
			for (int j = flag; j < pc->sz - 1; j++)
			{
				pc->data[j] = pc->data[j + 1];
			}
			pc->sz--;             //Be sure to update the number of contacts at this time
			printf("Delete succeeded!\n");
		}
	
}

Modification function implementation

//modify
void Modify(struct Contact* pc)
{

	printf("Please enter the name of the contact you want to modify:\n");
	char name[NAME_MAX] = { 0 };
	scanf("%s", &name);
	//lookup
	int flag = FIND(pc, name);
	if (flag == -1)
	{
		printf("No one was found\n");
	}
	else
	{
		printf("Please enter your name:\n");
		scanf("%s", pc->data[flag].name);
		printf("Please enter age:\n");
		scanf("%d", &pc->data[flag].age);
		printf("Please enter gender:\n");
		scanf("%s", pc->data[flag].sex);
		printf("Please enter phone number:\n");
		scanf("%s", pc->data[flag].number);
		printf("Please enter the address:\n");
		scanf("%s", pc->data[flag].address);
		printf("Modification succeeded!\n");
	}

}

Result display

Header file

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<string.h>
#define NAME_MAX 20
#define SEX_MAX 5
#define NUMBER_MAX 12
#define ADDRESS_MAX 30
#define MAX 1000

struct People
{
	char name[NAME_MAX];
	int age;
	char sex[SEX_MAX];
	char number[NUMBER_MAX];
	char address[ADDRESS_MAX];
};

struct Contact
{
	//Create a structure array for storing data of 1000 people
	struct People data[MAX];
	int sz;   //Number of valid information
};

//Initialization function declaration
void InitContact(struct Contact* pc);
//Add contact function declaration
void ADDContact(struct Contact* pc);
//Show address book functions
void Show(struct Contact* pc);
//Find sb
void Search(struct Contact* pc);
//delete
void Delete(struct Contact* pc);
//modify
void Modify(struct Contact* pc);

Function implementation source file

#define _CRT_SECURE_NO_WARNINGS 1
#include"contach.h"

//Initialization function
void InitContact(struct Contact* pc)
{
	pc->sz = 0;
	memset(pc->data,0,sizeof(pc->data));
}
//Add contact function declaration
void ADDContact(struct Contact* pc)
{


	if (pc->sz == MAX)
	{

		printf("The address book is full\n");
	}
	else
	{
		printf("Please enter your first name:\n");
		scanf("%s", pc->data[pc->sz].name);
		printf("Please enter age:\n");
		scanf("%d", &pc->data[pc->sz].age);
		printf("Please enter gender:\n");
		scanf("%s", pc->data[pc->sz].sex);
		printf("Please enter phone number:\n");
		scanf("%s", pc->data[pc->sz].number);
		printf("Please enter the address:\n");
		scanf("%s", pc->data[pc->sz].address);
		printf("Successfully added!\n");
		pc->sz++;
	}

}
//Show contacts
void Show(struct Contact* pc)
{
	printf("%15s\t%5s\t%8s\t%15s\t%30s\n", "name","age","sex","number","address");
	for (int 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].number, pc->data[i].address);
	}
}
//lookup
int FIND(struct Contact* pc, char name[])
{
	for (int i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}

void Search(struct Contact* pc)
{
	printf("Please enter the name of the contact you want to find:\n");
	char name[NAME_MAX] = { 0 };
	scanf("%s", &name);
	int flag = FIND(pc, name);
	if (flag != -1)
	{
		printf("%15s\t%5s\t%8s\t%15s\t%30s\n", "name", "age", "sex", "number", "address");
		printf("%15s\t%5d\t%8s\t%15s\t%30s\n", pc->data[flag].name, pc->data[flag].age, pc->data[flag].sex, pc->data[flag].number, pc->data[flag].address);
	}
	else
	{
		printf("No one was found\n");
	}

}
//delete
void Delete(struct Contact* pc)
{
	if (pc->sz == 0)
	{
		printf("Contact cannot be deleted because the address book is empty\n");
		return;
	}
		printf("Please enter the name of the contact you want to delete:\n");
		char name[NAME_MAX] = { 0 };
		scanf("%s", &name);
		//lookup
		int flag = FIND(pc, name);
		if (flag == -1)
		{
			printf("No one was found\n");
		}
		else
		{
			//delete
			for (int j = flag; j < pc->sz - 1; j++)
			{
				pc->data[j] = pc->data[j + 1];
			}
			pc->sz--;
			printf("Delete succeeded!\n");
		}
	
}

//modify
void Modify(struct Contact* pc)
{

	printf("Please enter the name of the contact you want to modify:\n");
	char name[NAME_MAX] = { 0 };
	scanf("%s", &name);
	//lookup
	int flag = FIND(pc, name);
	if (flag == -1)
	{
		printf("No one was found\n");
	}
	else
	{
		printf("Please enter your name:\n");
		scanf("%s", pc->data[flag].name);
		printf("Please enter age:\n");
		scanf("%d", &pc->data[flag].age);
		printf("Please enter gender:\n");
		scanf("%s", pc->data[flag].sex);
		printf("Please enter phone number:\n");
		scanf("%s", pc->data[flag].number);
		printf("Please enter the address:\n");
		scanf("%s", pc->data[flag].address);
		printf("Modification succeeded!\n");
	}

}

Test source file

#define _CRT_SECURE_NO_WARNINGS 1
#include"contach.h"
void menu()
{
	printf("**************************\n");
	printf("***********1.add     *****\n");
	printf("***********2.delete  *****\n");
	printf("***********3.search  *****\n");
	printf("***********4.modify  *****\n"); 
	printf("***********5.show    *****\n");
	printf("***********0.exit    *****\n");
}

enum Option
{
	exit,
	add,
	delete,
	search,
	modify,
	show,
};

int main()
{
	int input = 0;
	//Create an address book
	struct Contact con;
	//Initialize address book
	 InitContact(&con);
    
	do 
	{
		menu();
		printf("Please select:\n");
		scanf("%d", &input);
		switch (input)
		{

		case exit:
			printf("Exit address book\n");
			break;
		case add:
			//add to
			ADDContact(&con);
			break;
		case delete:
			//delete
			Delete(&con);
			break;
		case search:
			//lookup
			Search(&con);
			break;
		case modify:
			//modify
			Modify(&con);
			break;
		case show:
			//display
			Show(&con);
			break;
		default:
			printf("Wrong selection. Please reselect:\n");
			break;

		}
	} while (input);
	return 0;
}

Beginner, if what is wrong, I hope you can help correct it. I will also work harder to learn from others and update the articles one after another.

Tags: C

Posted on Sat, 25 Sep 2021 22:41:30 -0400 by Drezard