The C language implements the phonebook project
- 2020-06-23 01:14:00
- OfStack
The example of this paper shares the specific code of C language phonebook project for your reference. The specific content is as follows
Implementation approach
The program of the overall thinking and implementation is very simple, we use the way of dynamically allocated memory first create the phonebook contact type, then create a phone book class, its practical and global array composed of contact type, and then respectively, interface function, add and delete printing, etc, in the main function is to realize the overall train of thought, allow the user to select specific function calls using while a loop in a way that the overall implementation.
-- -- -- -- -- -- -- -- -- -- -- --
2019.3.12 update:
Using file manipulation enables the system to save data.
The implementation code
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define ADDRESS_INFOS_MAX 200
typedef struct AddressInfo
{
char name[1024];
char phone[1024];
}AddressInfo;
typedef struct AddressBook
{
AddressInfo* infos;
int size;
int capacity;// capacity
}AddressBook;
AddressBook g_addr_book;
// Initialize the
void Init(AddressBook* addr_book)
{
assert(addr_book != NULL);
addr_book->size = 0;
addr_book->capacity = 10;
addr_book->infos = (AddressInfo*)malloc(sizeof(AddressInfo) * addr_book->capacity);
return;
}
// The menu
int Menu()
{
printf("===========================\n");
printf("1 And the new \n");
printf("2 , delete, \n");
printf("3 , modify, \n");
printf("4 , find \n");
printf("5 , sorting, \n");
printf("6 , display all \n");
printf("7 , delete all \n");
printf("0 , exit \n");
printf("===========================\n");
int choice;
scanf("%d", &choice);
return choice;
}
// capacity
void Realloc(AddressBook* addr_book)
{
assert(addr_book != NULL);
//1 And expand the capacity The value of
addr_book->capacity *= 2;
//2 To apply for, 1 A bigger memory
AddressInfo* old_infos = addr_book->infos;
addr_book->infos = (AddressInfo*)malloc(sizeof(AddressInfo) * addr_book->capacity);
//3 And copy the data in the original memory
for(int i = 0; i < addr_book->size; i++)
{
addr_book->infos[i] = old_infos[i];
}
//4 , to free the original memory
free(old_infos);
// We customize the size of each expansion here
printf(" Successful expansion! \n");
}
// new
void AddAddressBook(AddressBook* addr_book)
{
assert(addr_book != NULL);
if(addr_book->size >= addr_book->capacity)
{
printf(" The space is full for expansion! \n");
Realloc(addr_book);// capacity
}
printf(" Please enter contact information! \n");
AddressInfo* p = &addr_book->infos[addr_book->size];
printf(" Please enter the contact name: ");
scanf("%s", p->name);
printf(" Please enter the contact number: ");
scanf("%s", p->phone);
addr_book->size++;
printf(" Added successfully! \n");
return;
}
// delete
void DelAddressBook(AddressBook* addr_book)
{
assert(addr_book != NULL);
int id;
printf(" Please enter the contact number to delete: ");
scanf("%d", &id);
if(id < 0 || id >= addr_book->size)
{
printf(" Input need error! \n");
return;
}
printf(" The serial number of deletion is [%d] , please enter to confirm deletion Y:", id);
char sure[1024] = {0};
scanf("%s", sure);
if(strcmp(sure, "Y") != 0)
{
printf(" Delete abort! \n");
return;
}
AddressInfo* from = &addr_book->infos[addr_book->size - 1];
AddressInfo* to = &addr_book->infos[0];
*to = *from;
addr_book->size--;
return;
}
// Modify the
void ModifyAddressBook(AddressBook* addr_book)
{
assert(addr_book != NULL);
printf(" Modify contacts! \n");
printf(" Please enter the contact serial number that needs to be modified: ");
int id = 0;
scanf("%d", &id);
if(id < 0 || id >= addr_book->size)
{
printf(" Error entering serial number! \n");
return;
}
AddressInfo* p = &addr_book->infos[id];
char input[1024] = {0};
printf(" Please enter the name you want to change: ");
scanf("%s", input);
if(strcmp(input, "#") != 0)
{
strcpy(p->name, input);
}
printf(" Please enter the number you want to modify: ");
scanf("%s", input);
if(strcmp(input, "#") != 0)
{
strcpy(p->phone, input);
}
printf(" Modified successfully! \n");
return;
}
// To find the
void FindAddressBook(AddressBook* addr_book)
{
assert(addr_book != NULL);
printf(" Start the search! \n");
printf(" Please enter the name you are looking for: ");
char name[1024] = {0};
scanf("%s", name);
int count = 0;
for(int i = 0; i < addr_book->size; i++)
{
AddressInfo* p = &addr_book->infos[i];
if(strcmp(name, p->name) == 0)
{
printf("[%d] %s\t %s", i, p->name, p->phone);
++count;
}
}
return;
}
// The sorting
void SortAddressBook(AddressBook* addr_book)
{
assert(addr_book != NULL);
for(int i = 0; i < addr_book->size - 1; i++)// Bubble sort
{
for(int j = 0; j < addr_book->size - i - 1; j++)
{
if(strcmp(addr_book->infos[j].name, addr_book->infos[j + 1].name) > 0)
{
AddressInfo temp = addr_book->infos[j];
addr_book->infos[j] = addr_book->infos[j + 1];
addr_book->infos[j + 1] = temp;
}
}
}
printf(" Sorting done! \n");
}
// Print all
void PrintAllAddressBook(AddressBook* addr_book)
{
assert(addr_book != NULL);
printf(" Show all contacts! \n");
for(int i = 0; i < addr_book->size; i++)
{
AddressInfo* p = &addr_book->infos[i];
printf("[%d] %s\t%s\n", i, p->name, p->phone);
}
printf(" A total of shows %d The data! \n", addr_book->size);
return;
}
// Remove all
void ClearAllAddressBook(AddressBook* addr_book)
{
assert(addr_book != NULL);
printf(" Are you sure you want to clear all the information? Please enter Y : ");
char sure[1024] = {0};
scanf("%s", sure);
if(strcmp(sure, "Y") != 0)
{
printf(" Clear is cancelled! \n");
return;
}
addr_book->size = 0;
return;
}
// File to read
size_t Read(AddressBook* addr_book)
{
FILE* fp = fopen("./AddrBookData.txt", "r");
if(fp == NULL)
{
fp = fopen("./AddrBookData.txt", "w+");
}
size_t n = 0;
char* buf[1024] = { 0 };
while(fgets(buf, sizeof(buf), fp) != NULL)
{
if(addr_book->size >= addr_book->capacity)
{
Realloc(addr_book);// capacity
}
AddressInfo* p = &addr_book->infos[addr_book->size];
sscanf(buf, "%s %s", p->name, p->phone);
addr_book->size++;
}
fclose(fp);
n = addr_book->size;
printf(" Read the %lu The data! \n", n);
return n;
}
// File storage
size_t Save(AddressBook* addr_book)
{
FILE* fp = fopen("./AddrBookData.txt", "w");
size_t n = 0;
for(int i = 0; i < addr_book->size; i++)
{
fprintf(fp, "%s %s\n", addr_book->infos[i].name, addr_book->infos[i].phone);
n++;
}
fclose(fp);
printf(" Store the %lu The data! \n", n);
return n;
}
int main()
{
Init(&g_addr_book);
Read(&g_addr_book);
typedef void (*ptr_func)(AddressBook*);
ptr_func table[] = {
AddAddressBook,
DelAddressBook,
ModifyAddressBook,
FindAddressBook,
SortAddressBook,
PrintAllAddressBook,
ClearAllAddressBook,
};
while(1)
{
int choice = Menu();
if(choice == 0)
{
printf(" Use finished, exit! \n");
Save(&g_addr_book);
return 0;
}
table[choice - 1](&g_addr_book);
}
}