Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it
preface
1, Design ideas
Dynamic address book creation
The first is to build the whole address book framework
For contacts:
1. Create a structure iden to store the attributes (name, gender, age, telephone number and address) of each person in the address book. In order to conveniently represent the capacity of the address book and the number of information stored in the address book, define a structure Contact to store a structure pointer pointing to the address book ident, the capacity of the address book and the number of information stored in the address book
2. Initialization: initialize the number of information stored in the address book to zero, and the initial capacity of the address book is to store the information of 3 people. The reason is to redefine the capacity of a variable address book instead of using a structure array to represent the information of the address book. It is convenient to compare whether the capacity needs to be expanded later
Use of address book:
It is divided into initial interface: use menu function to create a menu
Implementation function: add, delete, change, query, sort, display all information and exit
Module analysis
Add information module
First of all, because this is a dynamic address book, the initial capacity can only store the information of three people, so it is necessary to judge whether the current capacity is full. If it is full, first use the realloc function to expand the capacity, and set + 2 each time
Then add the information. You only need several scanf functions to input the information required by the address book (to change the information of the address book, you only need to change it in the structure iden, and then add the module)
Finally, remember that the number of information saved in the address book + 1
Print information module
First, judge whether the address book is empty. If it is empty, it will not be printed
Then a for loop can be traversed
Delete information module
First, judge whether the address book is empty. If it is empty, the deletion fails
Then we use the name as the reference body, output a name, and use the strcmp function to compare the name, that is, the string. If the return value of the comparison is 0, there is this person, then we can return the location of this person, that is, the array subscript, and then overwrite it forward at one time according to the subscript, and then the number of saved information in the communication record - 1 can be deleted successfully
Find information module
Similar to the deletion module, find the location of the person you need, return the subscript, and print the corresponding information according to the subscript
Modify information module
Similar to deleting the module, find the location of the required person, return the subscript, and re assign the corresponding information according to the following table
Sorting information module
It is realized by using the idea of fast scheduling
2, Concrete implementation
1.test.c
#define _CRT_SECURE_NO_WARNINGS 1 //1000 personal information stored in the address book #include "contact.h" void menu() { printf("*******************************************\n"); printf("*******************************************\n"); printf("********** 1.Add personnel information *************\n"); printf("********** 2.Delete personnel information *************\n"); printf("********** 3.Modify personnel information *************\n"); printf("********** 4.Query personnel information *************\n"); printf("********** 5.Display all personnel information *************\n"); printf("********** 6.Sort all personnel information *************\n"); printf("********** 0.Exit address book *************\n"); printf("*******************************************\n"); printf("*******************************************\n"); } int main() { int input = 0; struct contact con; InitContact(&con);//Initialization function do { //Main menu menu(); printf("Please enter the selection mode:>"); scanf("%d", &input); switch (input) { case exitt: printf("sign out\n"); //In order to avoid memory leakage and other problems, we manually release the address book memory when exiting the program Free_Memory(&con); /* SaveContact(&con);*/ break; case add: AddContact(&con); Sleep(6100); system("cls");/*Clear text screen*/ break; case dele: DeleContact(&con); Sleep(6100); system("cls"); break; case modify://modify ModifyContact(&con); Sleep(3000); system("cls"); break; case inquiry:/*query*/ InquiryContact(&con); Sleep(6100); system("cls"); break; case print: PrintContact(&con); system("pause"); system("cls"); break; case sort: SortContact(&con); Sleep(6100); system("cls"); break; break; default: printf("Input error, please re-enter\n"); break; } } while (input); return 0; }
2.contact.h
#include<stdio.h> #include<string.h> #include <stdlib.h> #include <windows.h> //Name, gender, age, telephone number, address #define MAX_NAME 50 #define MAX_SEX 20 #define MAX_TELE 15 #define MAX_ADDR 20 enum Id { exitt, add, dele, modify,//modify inquiry,//query print,//Print sort,//sort save//preservation }; struct ident { char name[MAX_NAME]; char sex[MAX_SEX]; int age; char tele[MAX_TELE]; char addr[MAX_ADDR]; }; struct contact { struct ident* data; int size;//Number of people entered int number;//Indicates the current address book capacity }; //Initialization function void InitContact(struct contact* ps); //Add personnel information void AddContact(struct contact* ps); //Print current address book personnel information void PrintContact(const struct contact* ps); //Modify information void ModifyContact(struct contact* ps); //Delete information void DeleContact(struct contact* ps); //Query some information void InquiryContact(const struct contact* ps); //Sort all information by first letter void SortContact(struct contact* ps); //Free memory void Free_Memory(struct contact* ps);
3.contact.c
#define _CRT_SECURE_NO_WARNINGS 1 #include "contact.h" void InitContact(struct contact* ps) { /*memset(ps->data, 0,sizeof(ps->data));*/ ps->data = (struct ident*)malloc(3 * sizeof(struct contact)); if (ps->data == NULL) { return; } ps->size = 0;//Indicates that there is no person information in the current address book ps->number = 3; } void IF_MAX(struct contact* ps) { struct ident* ptr=0; if (ps->size == ps->number) { //When the capacity of the address book is insufficient, it will automatically expand the capacity of the address book by two positions each time ptr = (struct ident*)realloc(ps->data, (ps->number)* sizeof(struct contact)); } //Judge whether the expansion is successful if (NULL != ptr) { ps->data = ptr; ps->number += 2; } } void AddContact(struct contact* ps) { //First, determine whether the address book is full IF_MAX(ps); //Add information printf("Please enter your name:>"); scanf("%s", ps->data[ps->size].name); printf("Please enter gender:>"); scanf("%s", ps->data[ps->size].sex); printf("Please enter age:>"); scanf("%d", &(ps->data[ps->size].age)); printf("Please enter the phone number:>"); scanf("%s", ps->data[ps->size].tele); printf("Please enter the address:>"); scanf("%s", ps->data[ps->size].addr); printf("Entered successfully\n"); ps->size++; } void PrintContact(const struct contact* ps) { if (0 == ps->size) { printf("Personnel information has not been entered\n"); } else { int i = 0; printf("%-5s\t%-5s\t%-5s\t%-12s\t%-20s\n", "full name", "Gender", "Age", "Telephone", "address"); for (i = 0 ; i < ps->size ; i++) { printf("%-5s\t%-5s\t%-5d\t%-12s\t%-20s\n", ps->data[i].name, ps->data[i].sex, ps->data[i].age, ps->data[i].tele, ps->data[i].addr); } } } //Modify information void ModifyContact(struct contact* ps) { if (0 == ps->size) { printf("Personnel information has not been entered\n"); } printf("Please enter the name of the person to modify:>"); char m_name[MAX_NAME] = { 0 }; scanf("%s", &m_name); int i = 0; for (i = 0; i < ps->size; i++) { if (0 == strcmp(m_name, ps->data[i].name)) break; } printf("Please enter your name:>"); scanf("%s", ps->data[i].name); printf("Please enter gender:>"); scanf("%s", ps->data[i].sex); printf("Please enter age:>"); scanf("%d", &(ps->data[i].age)); printf("Please enter the phone number:>"); scanf("%s", ps->data[i].tele); printf("Please enter the address:>"); scanf("%s", ps->data[i].addr); printf("Entered successfully\n"); } //Delete information void DeleContact(struct contact* ps) { if (0 == ps->size) { printf("Personnel information has not been entered\n"); } printf("Please enter the name of the person to delete:>"); char m_name[MAX_NAME] = { 0 }; scanf("%s", &m_name); int i = 0; for (i = 0; i < ps->size; i++) { if (0 == strcmp(m_name, ps->data[i].name)) break; } if (0 == strcmp(m_name, ps->data[i].name)) { int j; for (j = i; j < ps->size; j++) { ps->data[j] = ps->data[j + 1]; } ps->size--; printf("Deleted successfully\n"); } else { printf("Wrong name entered\n"); } } //Query some information void InquiryContact(const struct contact* ps) { if (0 == ps->size) { printf("Personnel information has not been entered\n"); } printf("Please enter the name of the person to query:>"); char m_name[MAX_NAME] = { 0 }; scanf("%s", &m_name); int i = 0; for (i = 0; i < ps->size; i++) { if (0 == strcmp(m_name, ps->data[i].name)) break; } if (0 == strcmp(m_name, ps->data[i].name)) { printf("%-5s\t%-5s\t%-5s\t%-12s\t%-20s\n", "full name", "Gender", "Age", "Telephone", "address"); printf("%-5s\t%-5s\t%-5d\t%-12s\t%-20s\n", ps->data[i].name, ps->data[i].sex, ps->data[i].age, ps->data[i].tele, ps->data[i].addr); } else { printf("Wrong name entered\n"); } } int comp(const void *a, const void *b){ return *(int *)a - *(int *)b; } //Sort all information by first letter void SortContact(struct contact* ps) { if (0 == ps->size) { printf("Personnel information has not been entered\n"); } qsort(ps->data, ps->size, sizeof(ps->data[0]), comp); printf("Sorting succeeded\n"); } //Free memory void Free_Memory(struct contact* ps) { free(ps->data); ps->data = NULL; }
summary
For dynamic development of memory, remember to release memory at the end to avoid memory leakage and other problems