C language address book system

Keywords: Windows

Implement an address book:
The address book can be used to store 1000 people's information, including:
Name, gender, age, telephone, address
Methods:
1. Add contact information
2. Delete the specified contact information
3. Find the specified contact information
4. Modify the designated contact information
5. Display all contact information
6. Clear all contacts
7. Save all contacts
8. Print all contact information

Here is the code:

person_info.h

#pragma once

#include <stddef.h>

#Define name "size 200 / / name
#Define phone? Size 200 / / phone number
#define ADDR_SIZE 200 / / address

//contacts
typedef struct PersonInfo
{
    char name[NAME_SIZE];
    char phone[PHONE_SIZE];
    char addr[ADDR_SIZE];
}PersonInfo;

addr_book.h

#pragma once

#include "person_info.h"

#define FILE_PATH "./data.txt"

enum{
    DISPLAY = 1,
    ADD = 2,
    ERASE = 3,
    FIND = 4,
    MODIFY = 5,
    DESTROY = 6,
    EXIT = 0
};

typedef struct AddrBook
{
    PersonInfo* data;
    size_t size;           //Number of contacts
    size_t capacity;        //capacity
}AddrBook;

void AddrBookInit(AddrBook* addr_book);          //Initialize address book

void AddrBookDisplay(AddrBook* addr_book);       //Print all address book information

void AddrBookAdd(AddrBook* addr_book);           //Add entry to address book

void AddrBookErase(AddrBook* addr_book);         //Delete the specified entry in the address book

void AddrBookFind(AddrBook* addr_book);          //Find the specified entry

void AddrBookModify(AddrBook* addr_book);        //Modify the specified entry

void AddrBookDestroy(AddrBook* addr_book);       //Destroy address book

void AddrBookSave(AddrBook* addr_book);          //Save address book

void AddrBookLoad(AddrBook* addr_book);          //Load back to memory

addr_book.c

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

//Initialize address book
void AddrBookInit(AddrBook* addr_book)
{
    if (addr_book == NULL)                  //Sentence blank
    {
        printf("addr_book is null\n");
        return;
    }
    addr_book->size = 0;
    addr_book->capacity = 1;
    addr_book->data = (PersonInfo*)malloc(sizeof(PersonInfo)*addr_book->capacity);
}

//Print all address book information
void AddrBookDisplay(AddrBook* addr_book)
{
    if (addr_book == NULL)                     //Sentence blank
    {
        printf("addr_book is null\n");
        return;
    }
    printf("|----------------|------------------|-----------------|\n");
    printf("|     full name     |      Telephone      |      Address     |\n");
    printf("|----------------|------------------|-----------------|\n");
    size_t i = 0;
    for (; i < addr_book->size; ++i)
    {
        printf("|      %s      |    %s   |       %s      |\n",
                addr_book->data[i].name,
                addr_book->data[i].phone,
                addr_book->data[i].addr);
        printf("|----------------|------------------|-----------------|\n");
    }
}

void AddrBookRelloc(AddrBook* addr_book)
{
    if (addr_book == NULL)                    //Sentence blank
    {
        printf("addr_book is null\n");
        return;
    }
    addr_book->capacity = addr_book->capacity * 2 + 1;
    PersonInfo* new_data = (PersonInfo*)malloc(sizeof(PersonInfo)*addr_book->capacity);
    size_t i = 0;
    for (; i < addr_book->size; ++i)
    {
        new_data[i] = addr_book->data[i];              //Copy the contents of the original space to the new space
    }
    free(addr_book->data);         //Release the original space
    addr_book->data = new_data;
}
//Add entry to address book
void AddrBookAdd(AddrBook* addr_book)
{
    if (addr_book == NULL)            //Sentence blank
    {
        printf("addr_book is null\n");
        return;
    }
    if (addr_book->size >= addr_book->capacity)           //If the capacity is not enough, it needs to be expanded
    {
        AddrBookRelloc(addr_book);
    }
    size_t cur = addr_book->size;
    ++addr_book->size;
    printf("Start inserting data:\n");                   //Insert new data
    printf("Please enter your name:");
    scanf("%s", addr_book->data[cur].name);
    printf("Please enter the phone number:");
    scanf("%s", addr_book->data[cur].phone);
    printf("Please enter the address:");
    scanf("%s", addr_book->data[cur].addr);
    printf("Insert successfully!\n");
    return;
}

//Save address book
void AddrBookSave(AddrBook* addr_book)               //Sentence blank
{
    if (addr_book == NULL)
    {
        printf("addr_book is null\n");
        return;
    }
    FILE* fp = fopen(FILE_PATH, "w");                //Save as text
    if (fp == NULL)
    {
        printf("Failed to open file!%s\n", FILE_PATH);
        return;
    }
    fprintf(fp, "|----------------|-------------------|-----------------|\n");
    size_t i = 0;
    for (; i < addr_book->size; ++i)
    {
        fprintf(fp, "|      %s      |    %s    |       %s      |\n",
            addr_book->data[i].name,
            addr_book->data[i].phone,
            addr_book->data[i].addr);
        fprintf(fp, "|----------------|-------------------|-----------------|\n");
    }
    fclose(fp);
}

//Load back to memory
void AddrBookLoad(AddrBook* addr_book)
{
    if (addr_book == NULL)                    //Sentence blank
    {
        printf("addr_book is null\n");
        return;
    }
    FILE* fp = fopen(FILE_PATH, "r");
    if (fp == NULL)
    {
        printf("Failed to open file!%s\n", FILE_PATH);
        return;
    }
    while (!feof(fp))
    {
        if (addr_book->size >= addr_book->capacity)
        {
            AddrBookRelloc(addr_book);
        }
        size_t cur = addr_book->size;
        fscanf(fp, "%s%s%s\n",
            addr_book->data[cur].name,
            addr_book->data[cur].phone,
            addr_book->data[cur].addr);
        ++addr_book->size;
    }
    fclose(fp);
}

//Delete the specified entry in the address book
void AddrBookErase(AddrBook* addr_book)       
{
    if (addr_book == NULL)                    //Sentence blank
    {
        printf("addr_book is null\n");
        return;
    }
    char to_delete[200] = { 0 };
    printf("Please enter the contact name to delete:");          //Delete by contact name
    scanf("%s", &to_delete);
    size_t i = 0;
    for (; i < addr_book->size; ++i)
    {
        if (strcmp(addr_book->data[i].name, to_delete) == 0)
        {
            free(addr_book->data);
            addr_book->size--;
            printf("Delete successfully!\n");
            return;
        }
    }
    if (i == addr_book->size)
    {
        printf("No person, deletion failed!\n");
        return;
    }
}

//Find the specified entry
void AddrBookFind(AddrBook* addr_book)
{
    if (addr_book == NULL)                    //Sentence blank
    {
        printf("addr_book is null\n");
        return;
    }
    char name[200] = { 0 };
    printf("Please enter the name of the contact you want to find:");             //Search by name
    scanf("%s", &name);
    size_t i = 0;
    for (; i < addr_book->size; ++i)
    {
        if (strcmp(addr_book->data[i].name, name) == 0)
        {
            printf("eureka!\n");
            printf("This person's information is:\n full name:%s ,Telephone:%s ,Address:%s\n",
                addr_book->data[i].name,
                addr_book->data[i].phone,
                addr_book->data[i].addr);
            return;
        }
    }
    if (i == addr_book->size)
    {
        printf("No one!\n");
        return;
    }
}

//Modify the specified entry
void AddrBookModify(AddrBook* addr_book)
{
    if (addr_book == NULL)
    {
        printf("addr_book is null\n");
        return;
    }
    char name[200] = { 0 };
    printf("Please enter the contact name to modify:");
    scanf("%s", &name);
    size_t i = 0;
    for (; i < addr_book->size; ++i)
    {
        if (strcmp(addr_book->data[i].name, name) == 0)
        {
            printf("This is the first%lu Contacts:\n", i + 1);                    //Let's make sure it's the first contact
            printf("This person's information is:\n full name:%s ,Telephone:%s ,Address:%s\n",
                addr_book->data[i].name,
                addr_book->data[i].phone,
                addr_book->data[i].addr);
        }
    }
    size_t num = 0;
    printf("Please enter the contact number to be modified:");             //Modify by contact number
    scanf("%lu", &num);
    char name2[200];
    char phone[200];
    char addr[200];
    printf("Please enter a new name:");
    scanf("%s", name2);
    strcpy(addr_book->data[num - 1].name, name2);
    printf("Please enter a new phone number:");
    scanf("%s", phone);
    strcpy(addr_book->data[num - 1].phone, phone);
    printf("Please enter a new address:");
    scanf("%s", addr);
    strcpy(addr_book->data[num - 1].addr, addr);
    printf("Modification succeeded!\n");
    return;
}

//Destroy address book
void AddrBookDestroy(AddrBook* addr_book)
{
    if (addr_book == NULL)                        //Sentence blank
    {
        printf("addr_book is null\n");
        return;
    }
    else
    {
        addr_book->size = 0;
        addr_book->capacity = 0;
        free(addr_book->data);
        printf("Destroyed successfully!\n");
    }
}

Test.c

#include "addr_book.h"
#include <windows.h>
#include <stdio.h>

void Menu()
{
    AddrBook addr_book;
    AddrBookInit(&addr_book);
    AddrBookLoad(&addr_book);
    while (1)
    {
        printf("****************************************\n");
        printf("*************1.Show all entries*************\n");
        printf("*************2.New entry*****************\n");
        printf("*************3.Delete entry*****************\n");
        printf("*************4.Find entry*****************\n");
        printf("*************5.Modify entry*****************\n");
        printf("*************6.Destroy address book***************\n");
        printf("*************0.Exit address book***************\n");
        int choice = 0;
        printf("Please select:");
        scanf("%d", &choice);
        switch (choice)
        {
        case DISPLAY:
            AddrBookDisplay(&addr_book);
            break;
        case ADD:
            AddrBookAdd(&addr_book);
            AddrBookSave(&addr_book);
            break;
        case ERASE:
            AddrBookErase(&addr_book);
            break;
        case FIND:
            AddrBookFind(&addr_book);
            break;
        case MODIFY:
            AddrBookModify(&addr_book);
            break;
        case DESTROY:
            AddrBookDestroy(&addr_book);
            break;
        case EXIT:
            printf("Exit successfully!\n");
            return 0;
            break;
        default:
            printf("Illegal input, please input again!\n");
            break;
        }
    }
}

int main()
{
    Menu();
    system("pause");
    return 0;
}

Effect display:

1. Display items

2. New entry

3. Delete entry

4. Find items

5. Modify items

6. Destroy address book

7. Exit address book

8. Illegal input

9. Text

This is a simple version of the address book, if there are errors, please correct!

Posted by gnu2php on Sat, 02 May 2020 07:24:05 -0700