[advanced C language] address book implemented in C language (simple version)

preface:

We need to use C language to simulate an address book, which can be used to store the information of 1000 people

Everyone's information includes:

Name, telephone number, gender, address and age

Functions include:

  1. New contact
  2. find contact
  3. Delete Contact
  4. Modify contact
  5. View all contacts
  6. Sort all contacts by name

Note: This version does not contain other contents, and a more comprehensive upgrade version will be issued later

Address book (C language easy version)

catalogue

1, Address book menu

2, Address book main function

3, Enumerate the internal options of the main function

4, Define contacts and contacts

        (1) Define contact content (structure)

        (2) Define address book content (structure)

5, Global variable declaration

6, Initialize address book

7, Find function by name

8, Realize the function of address book

        (1) New contact

        (2) Delete contact

        (3) Find contacts

        (4) Modify contact

        (5) Show all contacts

        (6) Sort all contacts by name

9, Header file

10, Complete code

1, Address book menu

Menus enable interaction with users.

So as an address book, how can there be no menu options?

The first step is to design a menu option

The code is as follows:

void menu()
{
	printf("*********************************\n");
	printf("**      1. Add a Contact           **\n");
	printf("**      2. Delete Contact           **\n");
	printf("**      3. find contact           **\n");
	printf("**      4. Modify contact          **\n");
	printf("**      5. Show all contacts      **\n");
	printf("**      6. Sort contacts by name    **\n");
	printf("**      0. exit                **\n");
	printf("*********************************\n");
}

2, Address book main function

After writing the address book menu, we will design the main function

The code is as follows:

int main()
{
    int input = 0;
    //Create address book
    struct Contact con;//con is the address book, which contains the number and size of 1000 elements
    //Initialize address book
    InitContact(&con);
    do
    {
        menu();
        printf("Please select:>");
        scanf("%d", &input);
        switch(input)
        {
        case ADD:
            AddContact(&con);
            break;
        case DEL:
            DelContact(&con);
            break;
        case SEARCH:
            SearchContact(&con);
            break;
        case MODIFY:
            MoidfyContact(&con);
            break;
        case SHOW:
            ShowContact(&con);
            break;
        case SORT:
            SortContact(&con);
            break;
        case EXIT:
            printf("Exit address book\n");
            break;
        default:
            printf("Selection error\n");
            break;
        }
    } while (input);
    return 0;
}

3, Enumerate the internal options of the main function

Because the characters we input in the case inside the main function above are easier to understand, but it is impossible to realize the original intention of using numbers for menu options at the beginning, so we need to enumerate these selection functions to achieve such an effect.

The code is as follows:

enum Choose
{
    EXIT,  //0
    ADD,   //1
    DEL,   //2
    SEARCH,//3
    MODIFY,//4
    SHOW,  //5
    SORT   //6
};

4, Define contacts and contacts

This step is to realize the address book content and contact content in the preface. Therefore, we need to use the structure function struct

(1) Define contact content (structure)

The code is as follows:

//Structure: the information of each member in the address book
typedef struct PeoInform
{
    char name[MAX_NAME];
    int age;
    char sex[MAX_SEX];
    char phone[MAX_PHONE];
    char address[MAX_ADDRESS];
}PeoInform;

 ( 2) Define address book content (structure)

The code is as follows:

//Address book type
struct Contact
{
    struct PeoInform data[MAX];//Store 1000 messages
    int size;//Record the number of existing elements in the current structure
};

5, Global variable declaration

In order to implement these contents inside our structure above, we need to make some declarations

The code is as follows:

#define MAX 1000
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_PHONE 12
#define MAX_ADDRESS 30

6, Initialize address book

Although this is a basic address book, it does not have the function of saving, but we should let it have an initialization function.

The code is as follows:

//Function to initialize address book
void InitContact(struct Contact *ps)
{
    memset(ps->data, 0, sizeof(ps->data));
    ps->size = 0;//Set the address book to have only 0 elements at first
}

7, Find function by name

In order to better realize the function of address book and improve its readability, a function is specially designed to realize the function of address book by looking up the name

The code is as follows:

//Modify FindByName function so that it is encapsulated inside the program and not exposed
static int FindByName(const struct Contact *ps, char name[MAX_NAME])
{
    int i = 0;
    for(i = 0; i < ps->size; i++)
    {
        if(0 == strcmp(ps->data[i].name, name))
        {
            return i;
        }
    }
    return -1;//Case not found
}

8, Realize the function of address book

(1) New contact

//Add a message to the address book
void AddContact(struct Contact *ps)
{
    if(ps->size == MAX)
    {
        printf("The address book is full and cannot be added\n");
    }
    else
    {
        printf("Please enter a name:>");
        scanf("%s", ps->data[ps->size].name);
        printf("Please enter age:>");
        scanf("%d", &(ps->data[ps->size].age));
        printf("Please enter gender:>");
        scanf("%s", ps->data[ps->size].sex);
        printf("Please enter phone number:>");
        scanf("%s", ps->data[ps->size].phone);
        printf("Please enter your home address:>");
        scanf("%s", ps->data[ps->size].address);

        ps->size++;
        printf("Added successfully\n");
    }
}

(2) Delete contact

//Delete the specified contact
void DelContact(struct Contact *ps)
{
    char name[MAX_NAME];
    printf("Please enter the name of the person to delete:>");
    scanf("%s", name);
    //1. Find out where the person to delete is
    //Found the subscript of the element where the return name is located
    //Return - 1 not found
    int pos = FindByName(ps, name);
    //2. Delete
    //Contact not found
    if (pos == -1)
    {
        printf("The contact to be deleted cannot be queried. Please try again\n");
    }
    else
    {
        //Delete data
        int j = 0;
        for(j = pos; j < ps->size-1; j++)
        {
            ps->data[j] = ps->data[j + 1];
            //Since this data is deleted, the following data will be replaced
        }
        ps->size--;
        printf("Delete succeeded\n");
    }
}

(3) Find contacts

//Find information for the specified person
void SearchContact(const struct Contact *ps)
{
    char name[MAX_NAME];
    printf("Please enter the name of the person you want to find:>");
    scanf("%s", name);
    int pos = FindByName(ps, name);
    if (pos == -1)
    {
        printf("The person you are looking for does not exist,Please try again\n");
    }
    else
    {
        printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "name", "Age", "Gender", "Telephone", "address");
        printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
            ps->data[pos].name,
            ps->data[pos].age,
            ps->data[pos].sex,
            ps->data[pos].phone,
            ps->data[pos].address);
    }
}

(4) Modify contact

//Modify the information of the specified contact
void MoidfyContact(struct Contact *ps)
{
    char name[MAX_NAME];
    printf("Please enter the name of the contact you want to modify:>");
    scanf("%s", name);
    int pos = FindByName(ps, name);
    if (pos == -1)
    {
        printf("The contact information you want to modify does not exist, please try again\n");
    }
    else
    {
        printf("Please enter a name:>");
        scanf("%s", ps->data[pos].name);
        printf("Please enter age:>");
        scanf("%s", &(ps->data[pos].age));
        printf("Please enter gender:>");
        scanf("%s", ps->data[pos].sex);
        printf("Please enter phone number:>");
        scanf("%s", ps->data[pos].phone);
        printf("Please enter your home address:>");
        scanf("%s", ps->data[pos].address);

        printf("Modification completed\n");
    }
}

(5) Show all contacts

//Show the contact information in the address book
void ShowContact(const struct Contact *ps)
{
    if(ps->size == 0)
    {
        printf("Address book is empty\n");
    }
    else
    {
        int i = 0;
        //title
        printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "name", "Age", "Gender", "Telephone", "address");
        //data
        for(i = 0; i < ps->size; i++)
        {
            printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
            ps->data[i].name,
            ps->data[i].age,
            ps->data[i].sex,
            ps->data[i].phone,
            ps->data[i].address);
        }
    }
}

(6) Sort all contacts by name

//Sort contacts by name
void SortContact(struct Contact *ps)
{
    if (ps->size <= 0){
		printf("There are no contacts in the address book, please add!\n");
	}
	int i = 0;
	int j = 0;
	for (i = 0; i< ps->size - 1; i++)
	{
		for (j = 0; j< ps->size - i - 1; j++)
		{
			if (strcmp( ps->data[j].name, ( ps->data[j + 1]).name) > 0)
			{
                PeoInform tmp;
				tmp = ps->data[j];
				ps->data[j] = ps->data[j + 1];
				ps->data[j + 1] = tmp;
			}
		}
		printf("Sorting succeeded!\n");
	}
}

9, Header file

Did we forget something here? Yes, that's the header file. If you don't reference the header file, an error will be reported, so we must add the header file when writing code!

The code is as follows:

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

10, Complete code

Finally, we present the complete code:

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

#define MAX 1000
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_PHONE 12
#define MAX_ADDRESS 30

//Enumerations: selecting features
enum Choose
{
    EXIT,  //0
    ADD,   //1
    DEL,   //2
    SEARCH,//3
    MODIFY,//4
    SHOW,  //5
    SORT   //6
};

//Structure: the information of each member in the address book
typedef struct PeoInform
{
    char name[MAX_NAME];
    int age;
    char sex[MAX_SEX];
    char phone[MAX_PHONE];
    char address[MAX_ADDRESS];
}PeoInform;

//Address book type
struct Contact
{
    struct PeoInform data[MAX];//Store 1000 messages
    int size;//Record the number of existing elements in the current structure
};


//Function to initialize address book
void InitContact(struct Contact *ps)
{
    memset(ps->data, 0, sizeof(ps->data));
    ps->size = 0;//Set the address book to have only 0 elements at first
}

//Add a message to the address book
void AddContact(struct Contact *ps)
{
    if(ps->size == MAX)
    {
        printf("The address book is full and cannot be added\n");
    }
    else
    {
        printf("Please enter a name:>");
        scanf("%s", ps->data[ps->size].name);
        printf("Please enter age:>");
        scanf("%d", &(ps->data[ps->size].age));
        printf("Please enter gender:>");
        scanf("%s", ps->data[ps->size].sex);
        printf("Please enter phone number:>");
        scanf("%s", ps->data[ps->size].phone);
        printf("Please enter your home address:>");
        scanf("%s", ps->data[ps->size].address);

        ps->size++;
        printf("Added successfully\n");
    }
}

//Modify FindByName function so that it is encapsulated inside the program and not exposed
static int FindByName(const struct Contact *ps, char name[MAX_NAME])
{
    int i = 0;
    for(i = 0; i < ps->size; i++)
    {
        if(0 == strcmp(ps->data[i].name, name))
        {
            return i;
        }
    }
    return -1;//Case not found
}

//Delete the specified contact
void DelContact(struct Contact *ps)
{
    char name[MAX_NAME];
    printf("Please enter the name of the person to delete:>");
    scanf("%s", name);
    //1. Find out where the person to delete is
    //Found the subscript of the element where the return name is located
    //Return - 1 not found
    int pos = FindByName(ps, name);
    //2. Delete
    //Contact not found
    if (pos == -1)
    {
        printf("The contact to be deleted cannot be queried. Please try again\n");
    }
    else
    {
        //Delete data
        int j = 0;
        for(j = pos; j < ps->size-1; j++)
        {
            ps->data[j] = ps->data[j + 1];
            //Since this data is deleted, the following data will be replaced
        }
        ps->size--;
        printf("Delete succeeded\n");
    }
}

//Find information for the specified person
void SearchContact(const struct Contact *ps)
{
    char name[MAX_NAME];
    printf("Please enter the name of the person you want to find:>");
    scanf("%s", name);
    int pos = FindByName(ps, name);
    if (pos == -1)
    {
        printf("The person you are looking for does not exist,Please try again\n");
    }
    else
    {
        printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "name", "Age", "Gender", "Telephone", "address");
        printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
            ps->data[pos].name,
            ps->data[pos].age,
            ps->data[pos].sex,
            ps->data[pos].phone,
            ps->data[pos].address);
    }
}

//Modify the information of the specified contact
void MoidfyContact(struct Contact *ps)
{
    char name[MAX_NAME];
    printf("Please enter the name of the contact you want to modify:>");
    scanf("%s", name);
    int pos = FindByName(ps, name);
    if (pos == -1)
    {
        printf("The contact information you want to modify does not exist, please try again\n");
    }
    else
    {
        printf("Please enter a name:>");
        scanf("%s", ps->data[pos].name);
        printf("Please enter age:>");
        scanf("%s", &(ps->data[pos].age));
        printf("Please enter gender:>");
        scanf("%s", ps->data[pos].sex);
        printf("Please enter phone number:>");
        scanf("%s", ps->data[pos].phone);
        printf("Please enter your home address:>");
        scanf("%s", ps->data[pos].address);

        printf("Modification completed\n");
    }
}


//Show the information in the address book
void ShowContact(const struct Contact *ps)
{
    if(ps->size == 0)
    {
        printf("Address book is empty\n");
    }
    else
    {
        int i = 0;
        //title
        printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "name", "Age", "Gender", "Telephone", "address");
        //data
        for(i = 0; i < ps->size; i++)
        {
            printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
            ps->data[i].name,
            ps->data[i].age,
            ps->data[i].sex,
            ps->data[i].phone,
            ps->data[i].address);
        }
    }
}

//Sort contacts by name
void SortContact(struct Contact *ps)
{
    if (ps->size <= 0){
		printf("There are no contacts in the address book, please add!\n");
	}
	int i = 0;
	int j = 0;
	for (i = 0; i< ps->size - 1; i++)
	{
		for (j = 0; j< ps->size - i - 1; j++)
		{
			if (strcmp( ps->data[j].name, ( ps->data[j + 1]).name) > 0)
			{
                PeoInform tmp;
				tmp = ps->data[j];
				ps->data[j] = ps->data[j + 1];
				ps->data[j + 1] = tmp;
			}
		}
		printf("Sorting succeeded!\n");
	}
}

void menu()
{
	printf("*********************************\n");
	printf("**      1. Add a Contact           **\n");
	printf("**      2. Delete Contact           **\n");
	printf("**      3. find contact           **\n");
	printf("**      4. Modify contact          **\n");
	printf("**      5. Show all contacts      **\n");
	printf("**      6. Sort contacts by name    **\n");
	printf("**      0. exit                **\n");
	printf("*********************************\n");
}

int main()
{
    int input = 0;
    //Create address book
    struct Contact con;//con is the address book, which contains the number and size of 1000 elements
    //Initialize address book
    InitContact(&con);
    do
    {
        menu();
        printf("Please select:>");
        scanf("%d", &input);
        switch(input)
        {
        case ADD:
            AddContact(&con);
            break;
        case DEL:
            DelContact(&con);
            break;
        case SEARCH:
            SearchContact(&con);
            break;
        case MODIFY:
            MoidfyContact(&con);
            break;
        case SHOW:
            ShowContact(&con);
            break;
        case SORT:
            SortContact(&con);
            break;
        case EXIT:
            printf("Exit address book\n");
            break;
        default:
            printf("Selection error\n");
            break;
        }
    } while (input);
    return 0;
}

Keywords: C

Added by bh on Sat, 04 Sep 2021 21:38:44 +0300