Knowledge points involved in this article:
Dynamic memory functions: use of malloc, realloc, and free;
Use of qsort function (quick sort);
Structure, structure parameter passing, enumeration;
string function;
Contact.h file
#pragma once
#define NAME 10
#define TELE 12
#define ADDR 20
#define DEFAULT 10
#define EXPAND 5
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
//Enumeration operation options
enum Option
{ EXIT, ADD, DELETE, FIND, MODIFY, SHOW, SORT };
//Each contact information type
struct Peoinfo
{ char name[NAME]; int age; char tele[TELE]; char address[ADDR]; };
//Address book type
struct Contact
{ struct Peoinfo* data; //Point to address book information int sz; //Statistics of contacts int capacity; //Statistics of address book capacity };
//Initialize the address book
int INITIALIZE_Contact(struct Contact* pI);
//Menu display
void menu();
//Add contact
void ADD_Contact(struct Contact* pA);
//Show contact
void SHOW_Contact(struct Contact* pSH);
//Query contact
void FIND_Contact(struct Contact* pF);
//Modify contact
void MODIFY_Contact(struct Contact* pM);
//Delete contact
void DELETE_Contact(struct Contact* pD);
//Contact sorting
void SORT_Contact(struct Contact* pSO);
//Exit the program
void EXIT_Contact(struct Contact* pE);
Contact.c file
#define _CRT_SECURE_NO_WARNINGS
#include"Contact.h"
//菜单显示
void menu()
{
printf("*****************************\n");
printf("***** 1.ADD 2.DELETE *****\n");
printf("***** 3.FIND 4.MODIFY *****\n");
printf("***** 5.SHOW 6.SORT *****\n");
printf("***** 0.EXIT *****\n");
printf("*****************************\n");
}
//Find contacts by name (not declared in the header file, can only be used in the .c file)
int FindByName(const struct Contact* C, const char* N)
{ int i = 0; for (i = 0 ; i < C->sz; i++) { if (0 == strcmp(&(C->data[i].name), N)) { printf("Found:\n"); printf("% -10s%-10s%-20s%-20s\n", "name", "age", "phone", "address"); printf("%- 10s%-10d%-20s%-20s\n" , &(C->data[i].name), C->data[i].age, &(C->data[i].tele), &(C->data[i].address)) ; return i; } } printf("The contact cannot be found\n"); return -1; }
//Sort by name (not declared in the header file, can only be used in the .c file)
int CmpByName(const struct Contact* c1, const struct Contact* c2)
{ return strcmp(c1->data->name, c2->data->name); }
//Initialize the address book
int INITIALIZE_Contact(struct Contact* pI)
{ pI->data = malloc(DEFAULT * sizeof(struct Peoinfo)); if (pI->data == NULL) { printf("INITIALIZE_Contact failed: %s\ n", strerror(errno)); return 0; //return 0 if initialization fails } pI->sz = 0; pI->capacity = DEFAULT; printf("The address book was initialized successfully, the current capacity is %d\n", pI->capacity); return 1; //return 1 if initialization is successful }
//Add a contact
void ADD_Contact(struct Contact* pA)
{ //Determine whether the address book is full if (pA->sz == pA->capacity) { //Expand the capacity struct Peoinfo* ptr = realloc(pA->data , (pA->capacity + EXPAND) * sizeof(struct Peoinfo)); if (ptr == NULL) { printf("The address book is full, expansion failed\n"); return; } else { pA->data = ptr; pA->capacity += EXPAND; printf("Capacity expansion successful, current capacity is %d\n", pA->capacity); } } // Create a new contact printf("Please enter your name:\n");
scanf("%s", &((pA->data[pA->sz]).name));
printf("Please enter your age:\n");
scanf("%d", &((pA- >data[pA->sz]).age));
printf("Please enter your phone number:\n");
scanf("%s", &((pA->data[pA->sz]).tele) );
printf("Please enter the address:\n");
scanf("%s", &((pA->data[pA->sz]).address));
printf("Added successfully\n");
pA->sz++;
}
//Display the contact
void SHOW_Contact(struct Contact* pSH)
{ printf("%-10s%-10s%-20s%-20s\n", "Name", "Age", "Telephone", "Address"); //%-10s: 10 refers to the length space of 10 characters when printed; -10 refers to the left alignment (- sign) occupying 10 characters of length space to print the next data // If it is %10s, then It prints 10 character length space with right alignment and then prints the next data int i = 0; for (i = 0; i < pSH->sz; i++) { printf("%-10s%-10d%-20s%-20s \n", &(pSH->data[i].name), pSH->data[i].age, &(pSH->data[i].tele), &(pSH->data[i]. address)); } }
//Query contact
void FIND_Contact(struct Contact* pF)
{ char name[20] = { 0 }; printf("Please enter the contact name:\n"); scanf("%s", name); FindByName( pF, name); }
//Modify contact
void MODIFY_Contact(struct Contact* pM)
{ char name[20] = { 0 }; printf("Please enter the name of the contact to be modified:\n"); scanf("%s", name) ; int i = FindByName(pM, name); if (i != -1) { printf("Please enter your name:\n"); scanf("%s", &(pM->data[i].name )); printf("Please enter your age:\n"); scanf("%d", &(pM->data[i].age)); printf("Please enter your phone number:\n"); scanf( "%s", &(pM->data[i].tele)); printf("Please enter the address:\n"); scanf("%s", &(pM->data[i].address)); printf("Modification successful\n"); } }
//Delete contact
void DELETE_Contact(struct Contact* pD)
{ char name[20] = { 0 }; printf("Please enter the name of the contact to be modified:\n"); scanf("%s", name) ; int i = FindByName(pD, name); if (i != -1) { printf("Please enter to confirm deletion: 1\nTo cancel deletion, please enter: 2\n"); int j = 0; scanf(" %d",&j); if (1 == j) { for (j = i; j < pD->sz - 1; j++) { pD->data[j] = pD->data[j + 1]; } printf( "Contact deleted successfully\n"); pD->sz--; } } return; }
//Contact sorting
void SORT_Contact(struct Contact* pSO)
{ qsort(pSO->data, pSO->sz, sizeof(pSO->data[0]), CmpByName); //Note the parameters of the qsort() function Question, practice qsort sorting of various types of data }
//Exit the program
void EXIT_Contact(struct Contact* pE)
{ free(pE->data); pE->data = NULL; printf("This memory block has been released and recycled\n"); }
test.c file
#define _CRT_SECURE_NO_WARNINGS
#include"Contact.h"
int main()
{ int input; struct Contact C; struct Contact* p = &C; if (0 == INITIALIZE_Contact(p)) { printf("Failed to create address book\n"); return 0; } else { do { menu(); printf("Please select\n"); scanf("%d", &input); switch (input) { case ADD: ADD_Contact(p); break; case DELETE: DELETE_Contact(p); break;
case FIND:
FIND_Contact(p);
break;
case MODIFY:
MODIFY_Contact(p);
break;
case SHOW:
SHOW_Contact(p);
break;
case SORT:
SORT_Contact(p);
break;
case EXIT:
EXIT_Contact(p);
break;
default:
break;
}
} while (input);
}
return 0;
}