The C language implements the address book
- 2020-09-16 07:40:28
- OfStack
This article shares the specific code of C language address book for your reference. The specific content is as follows
The functions realized include:
1. Initialize the address book;
2. Add an entry to the address book;
3. Delete the specified entry in the address book;
4. Find specific entries in the address book according to specified rules;
5. Print all the information in the address book;
6. Modify the specified entry in the address book;
7. Destroy the address book;
8. Save the address book contents in a file;
Load the contents of the file back into memory.
**Addr_Book.h**
#pragma once
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_SIZE 200
#define PHONE_SIZE 200
#define ADDR_SIZE 200
typedef struct PersonInfo {
char name[NAME_SIZE];
char phone[PHONE_SIZE];
char addr[ADDR_SIZE];
} PersonInfo;
#define FILE_PATH "./data.txt"
enum {
DISPLAY = 1,
ADD = 2,
ERASE = 3,
FIND = 4,
MODIFY = 5,
EXIT = 0
};
typedef struct AddrBook {
PersonInfo* data;// content
size_t size; // Number of contacts
size_t capacity; // Address book capacity
} AddrBook;
/**
* @brief Initialize the address book
*
* @param addr_book
*/
void AddrBookInit(AddrBook* addr_book);
/**
* @brief Add an entry to the address book
*
* @param addr_book
*/
void AddrBookAdd(AddrBook* addr_book);
/**
* @brief Deletes the specified entry in the address book
*
* @param addr_book
*/
void AddrBookErase(AddrBook* addr_book);
/**
* @brief Finds a specific entry in the address book by following the specified rule
*
* @param addr_book
*/
void AddrBookFind(AddrBook* addr_book);
/**
* @brief Print all the information in the address book
*
* @param addr_book
*/
void AddrBookDisplay(AddrBook* addr_book);
/**
* @brief Modifies the specified entry in the address book
*
* @param addr_book
*/
void AddrBookModify(AddrBook* addr_book);
/**
* @brief Destroy address book
*
* @param addr_book
*/
void AddrBookDestroy(AddrBook* addr_book);
/**
* @brief Save the address book contents in a file
*
* @param addr_book
*/
void AddrBookSave(AddrBook* addr_book);
/**
* @brief Loads the contents of the file back into memory .
*
* @param addr_book
*/
void AddrBookLoad(AddrBook* addr_book);
**Addr_Book.c**
#define _CRT_SECURE_NO_WARNINGS 1
#include "Addr_Book.h"
// Print address book
void AddrBookDisplay(AddrBook* addr_book){
if (addr_book == NULL){
return;
}
printf("\n==================== The address book =========================\n");
printf("| The name | number | 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");
}
}
// Initialize the address book
void AddrBookInit(AddrBook* addr_book){
if (addr_book == NULL){
return ;
}
addr_book->size = 0;
addr_book->capacity = 1;
addr_book->data = (PersonInfo*)malloc(sizeof(PersonInfo)*addr_book->capacity);
}
// Expand the memory
void AddrBookRelloc(AddrBook* addr_book){
if (addr_book == NULL){
return;
}
addr_book->capacity = addr_book->capacity * 2 + 1;// There is not enough memory, expand the capacity
PersonInfo* new_data = (PersonInfo*)malloc(sizeof(PersonInfo)*addr_book->capacity);// Open up new space sizes
size_t i = 0;
for (; i < addr_book->size; ++i){
new_data[i] = addr_book->data[i];// Assign old memory to new memory
}
free(addr_book->data);// Free up old memory
addr_book->data = new_data;
}
// Add contacts
void AddrBookAdd(AddrBook* addr_book){
if (addr_book == NULL){
return;
}
if (addr_book->size >= addr_book->capacity){ // There is not enough memory to expand memory
AddrBookRelloc(addr_book);
}
size_t cur = addr_book->size;
++addr_book->size;
printf(" Add contacts \n");
printf(" Enter name: ");
scanf("%s", addr_book->data[cur].name);
printf(" Input number: ");
scanf("%s", addr_book->data[cur].phone);
printf(" Input address: ");
scanf("%s", addr_book->data[cur].addr);
printf(" Add the end !\n");
}
// Delete the specified contact
void AddrBookErase(AddrBook* addr_book){
if (addr_book == NULL){
return;
}
char delete_name[200] = {0};
printf(" Enter the name of the contact to delete: ");
scanf("%s", &delete_name);
size_t i = 0;
for (; i <addr_book->size;++i){
if (strcmp(addr_book->data[i].name, delete_name)){
free(addr_book->data);
addr_book->size--;
printf(" Has been deleted! ");
}
else{
printf(" Delete failed, please re-enter the contact! ");
}
}
}
// Save the content
void AddrBookSave(AddrBook* addr_book){
FILE* fp = fopen(FILE_PATH, "w");
if (fp == NULL){
printf(" Failed to open file !\n");
return;
}
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);
}
fclose(fp);
}
// Find contacts by name
void AddrBookFind(AddrBook* addr_book){
if (addr_book == NULL){
return;
}
char delete_name[200] = { 0 };
printf(" Enter the name of the contact sought: ");
scanf("%s", &delete_name);
size_t i = 0;
for (; i <addr_book->size; ++i){
if (strcmp(addr_book->data[i].name, delete_name)){
printf(" Found, the contact information is: Name: %s, Number: %s, Address: %s\n",
addr_book->data[i].name, addr_book->data[i].phone, addr_book->data[i].addr);
}
else{
printf(" This person doesn't exist! \n");
}
}
}
// Modify contact information
void AddrBookModify(AddrBook* addr_book){
if (addr_book == NULL){
return;
}
char name[200] = { 0 };
printf(" Enter the name of the contact to be modified: ");
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 Contact person: \n", i + 1);
printf(" This person's information is: \n 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(" Enter the contact number you want to change: ");
scanf("%lu", &num);
char name2[200];
char phone[200];
char addr[200];
printf(" Please enter your new name: ");
scanf("%s", name2);
strcpy(addr_book->data[num - 1].name, name2);
printf(" Please enter your new phone number: ");
scanf("%s", phone);
strcpy(addr_book->data[num - 1].phone, phone);
printf(" Please enter the new address: ");
scanf("%s", addr);
strcpy(addr_book->data[num - 1].addr, addr);
printf(" Modified successfully! \n");
return;
}
// Destroy address book
void AddrBookDestroy(AddrBook* addr_book){
if (addr_book == NULL)
{
printf("addr_book is null\n");
return;
}
else
{
addr_book->size = 0;
addr_book->capacity = 0;
free(addr_book->data);
printf(" Destroy successfully! \n");
}
}
// Loads the contents of the file back into memory
void AddrBookLoad(AddrBook* addr_book){
if (addr_book = NULL){
printf(" The address book is empty! \n");
return;
}
FILE* fp = fopen(FILE_PATH,"r");
if (fp = NULL){
printf(" Failed to open file !\n");
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[i].name, addr_book->data[i].phone, addr_book->data[i].addr);
addr_book->size++;
}
fclose(fp);
}
//===============
// The following is the test code
//===============
void menu(){
AddrBook addr_book;
AddrBookInit(&addr_book);
AddrBookLoad(&addr_book);
while (1){
printf("============1. Print address book =============\n");
printf("============2. Add contacts =============\n");
printf("============3. Delete contacts =============\n");
printf("============4. Find contacts =============\n");
printf("============5. Modify contact =============\n");
printf("============6. Destroy address book =============\n");
printf("============0. Exit address book =============\n");
int choice = 0;
printf(" Please enter the required functions: ");
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);
AddrBookSave(&addr_book);
break;
case DESTROY:
AddrBookDestroy(&addr_book);
break;
case EXIT:
printf(" Quit the address book! \n");
break;
default:
printf(" Please enter the correct selection: ");
break;
}
}
}
int main(){
menu();
return 0;
}