catalogue
2.2.01. The first is our framework
2.2.02. Interface to implement
1. Preamble
hello ✨, Hello, everyone. This is the original 💖 💛💙, With more and more articles, there may be many partners who can't find the articles they want to read, so I came out. Here are links to various articles. 🎁 🎁 🎁
The introduction and deep analysis of C language are arranged in here Ha, the advanced chapter is also in a hurry. It should be finished in less than a week, and then here It's a personal homepage. It's better to find articles than nodding avatars. Then there is the primary data structure, leaving only binary tree and eight sorting. It will be summarized later, and the links will also be put here. Later, you should know more about c + + and linux. Of course, advanced data structures will also be updated in c + + 😜. If you want to learn about c\c + +, paying attention to bloggers will be helpful to you 🎁 🎁 🎁 . Finally, thank you for watching this article 💖, Thank you for your attention 💖🎈 🎈, Let's refuel together! 🎉 🎉 🎉
Finally, there is the chicken blood link: it is really difficult to change, but the result is worth taking a risk and showing some courage. It's still a long way to go. It's just the beginning now. The past is irreversible and the future can be changed. 🚀 🚀 🚀
For the dynamic and static difference of the address book, it is still very small, so this article first realizes the static and dynamic, just pick out the ones to be changed, so as to avoid the redundancy of the article. Note here that we use enumeration constants to replace the numbers in the original switch and case statements. This is the use of enumeration constants.
2. Static implementation
First of all, we need to know the functions we want to achieve. First, we need to add, delete, check and modify, then display members, sort, and finally exit.
2.1. Effect display diagram
2.2. Interface implementation
2.2.01. The first is our framework
int main() { do { menu(); switch () { case: default: } } while (); return 0; }
2.2.02. Interface to implement
2.2.03. initialization
2.2.04. Add a contact
2.2.05. delete
2.2.06. Search
Return address found, return - 1 not found.
2.2.07. Change member data
2.02.08. Show members
The names corresponding to each data are displayed here. Note that the length of% s is the same, '-' is left aligned, and / t is equivalent to 4 spaces.
2.02.09. Sort
Here we use the library function of qsort quick sort. Here we sort by name. Later, we will send out the articles to realize quick sort.
2.3. source code
test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"Contact.h" void menu() { printf("**************************************************************\n"); printf("******************** 1.Add 2.Delet ***********************\n"); printf("******************** 3.Search 4.Modify ***********************\n"); printf("******************** 5.Show 6.Sort ***********************\n"); printf("******************** 0.exitcontact ***********************\n"); printf("**************************************************************\n"); } int main() { int input=0; Contact con; char name[10] = { 0 }; InitContact(&con); do { menu(); printf("Please enter the option you want to select:\n"); scanf("%d", &input); switch (input) { case Add: printf("Add address book members:\n"); AddContact(&con); break; case Delet: printf("Delete address book member:\n"); printf("Please enter the name of the person to delete:"); char name[NAME_MAX] = { 0 }; scanf("%s", name); DeletContact(&con, name); break; case Search: printf("Find address book members:\n"); printf("Please enter the name of the person you want to find:"); scanf("%s", name); SearchContact(&con, name); break; case Modify: printf("Change address book member information:\n"); printf("Please enter the name of the person you want to find:"); scanf("%s", name); ModifyContact(&con, name); break; case Show: printf("Show address book members:\n"); ShowContact(&con); break; case Sort: printf("Sort address book members:\n"); SortContact(&con); break; case exitcontact: //DestoryContact(&con); printf("Exit address book:\n"); break; default: printf("Input error, please re-enter:"); break; } } while (input); return 0; }
Contact.h
#pragma once #include<stdio.h> #include<assert.h> #include<string.h> #include<stdlib.h> #define CONTACT_MAX 1000 #define NAME_MAX 10 #define SEX_MAX 5 #define TELEP_MAX 13 #define ADDRESS_MAX 20 typedef struct Student { char name[NAME_MAX]; char sex[SEX_MAX]; int age; char telep[TELEP_MAX]; char address[ADDRESS_MAX]; }Student; typedef struct Contact { Student date[CONTACT_MAX]; int sz; }Contact; enum contactmenu { exitcontact, Add, Delet, Search, Modify, Show, Sort, }; //initialization void InitContact(Contact* pc); //increase void AddContact(Contact* pc); //delete void DeletContact(Contact* pc, char name[]); //lookup void SearchContact(Contact* pc, char name[]); //modify void ModifyContact(Contact* pc, char name[]); //display void ShowContact(Contact* pc); //sort void SortContact(Contact* pc); //void DestoryContact(Contact* pc); int FindContact(Contact* pc, char name[]);
Contact.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"Contact.h" void InitContact(Contact* pc) { assert(pc); pc->sz = 0; memset(pc->date, 0, sizeof(pc->date)); } void AddContact(Contact* pc) { assert(pc); if (pc->sz == CONTACT_MAX) { printf("The address book is full and cannot be added\n"); return; } printf("Enter the name of the contact you want to add:"); scanf("%s", pc->date[pc->sz].name); printf("Enter the gender of the contact you want to add:"); scanf("%s", pc->date[pc->sz].sex); printf("Enter the age of the contact you want to add:"); scanf("%d", &(pc->date[pc->sz].age)); printf("Enter the phone number of the contact you want to add:"); scanf("%s", pc->date[pc->sz].telep); printf("Enter the address of the contact you want to add:"); scanf("%s", pc->date[pc->sz].address); pc->sz++; printf("Contact added successfully\n"); } void ShowContact(Contact* pc) { assert(pc); printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\t\n", "full name", "Gender", "Age", "Telephone", "address"); int i = 0; for (i = 0; i < pc->sz; i++) { printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n", pc->date[i].name,pc->date[i].sex,pc->date[i].age,pc->date[i].telep,pc->date[i].address); } } int FindContact(Contact* pc,char name[]) { assert(pc); int i = 0; for (i = 0; i < pc->sz; i++) { if (strcmp(pc->date[i].name, name)==0) { return i; } } return -1; } void DeletContact(Contact* pc, char name[]) { assert(pc); if (pc->sz == 0) { printf("There are no contacts,Cannot delete\n"); return; } int i = FindContact(pc, name); if (i != -1) { int j = 0; for (j=i; j < pc->sz-1; j++) { pc->date[j] = pc->date[j + 1]; } pc->sz--; printf("Deleted successfully!\n"); } else { printf("No one was found\n"); } } void SearchContact(Contact* pc, char name[]) { assert(pc); int ret=FindContact(pc, name); if (ret != -1) { printf("eureka!\n"); printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n", pc->date[ret].name, pc->date[ret].sex, pc->date[ret].age, pc->date[ret].telep, pc->date[ret].address); } else { printf("Can't find\n"); } } void ModifyContact(Contact* pc, char name[]) { assert(pc); int ret=FindContact(pc, name); if (ret == -1) { printf("No one was found\n"); } else { printf("eureka!\n"); printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n", pc->date[ret].name, pc->date[ret].sex, pc->date[ret].age, pc->date[ret].telep, pc->date[ret].address); printf("Please modify:\n"); printf("Enter the name of the contact you want to add:"); scanf("%s", pc->date[ret].name); printf("Enter the gender of the contact you want to add:"); scanf("%s", pc->date[ret].sex); printf("Enter the age of the contact you want to add:"); scanf("%d", &(pc->date[ret].age)); printf("Enter the phone number of the contact you want to add:"); scanf("%s", pc->date[ret].telep); printf("Enter the address of the contact you want to add:"); scanf("%s", pc->date[ret].address); printf("Modification succeeded!\n"); } } int cmp_name(const void* e1, const void* e2) { return strcmp(((Student*)e1)->name, ((Student*)e2)->name); } void SortContact(Contact* pc) { assert(pc); qsort(pc->date, pc->sz, sizeof(pc->date[0]), cmp_name); }
3. Dynamic version
The difference between the dynamic version and the static version is still very small. The difference is the creation of the address book, and then pay attention to capacity expansion when adding data. Pay attention to dynamic space development when initializing. The rest are the same.
3.1. Address book creation
Note that there is an additional parameter capacity, and then use sz to record it.
3.2. initialization
Pay attention to air judgment.
3.3. Add data
Dynamic version source code
For humanization, it may be difficult for small partners to change, so the dynamic version of the source code is also sent here.
test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"Contact.h" void menu() { printf("**************************************************************\n"); printf("******************** 1.Add 2.Delet ***********************\n"); printf("******************** 3.Search 4.Modify ***********************\n"); printf("******************** 5.Show 6.Sort ***********************\n"); printf("******************** 0.exitcontact ***********************\n"); printf("**************************************************************\n"); } int main() { int input = 0; Contact con; char name[10] = { 0 }; InitContact(&con); do { menu(); printf("Please enter the option you want to select:\n"); scanf("%d", &input); switch (input) { case Add: printf("Add address book members:\n"); AddContact(&con); break; case Delet: printf("Delete address book member:\n"); printf("Please enter the name of the person to delete:"); char name[NAME_MAX] = { 0 }; scanf("%s", name); DeletContact(&con, name); break; case Search: printf("Find address book members:\n"); printf("Please enter the name of the person you want to find:"); scanf("%s", name); SearchContact(&con, name); break; case Modify: printf("Change address book member information:\n"); printf("Please enter the name of the person you want to find:"); scanf("%s", name); ModifyContact(&con, name); break; case Show: printf("Show address book members:\n"); ShowContact(&con); break; case Sort: printf("Sort address book members:\n"); SortContact(&con); break; case exitcontact: DestoryContact(&con); printf("Exit address book:\n"); break; default: printf("Input error, please re-enter:"); break; } } while (input); return 0; }
Contact.h
#define _CRT_SECURE_NO_WARNINGS 1 #include"Contact.h" void InitContact(Contact* pc) { assert(pc); Student* new = (Student*)malloc(DEFAULT_SIZE * sizeof(Student)); if (new != NULL) { pc->date = new; pc->sz = 0; pc->capacity = DEFAULT_SIZE; } else { printf("InitContact:%s\n", strerror(errno)); return; } } void Add_Capacity(Contact* pc) { Student* new = (Student*)realloc(pc->date, sizeof(Student) * (pc->capacity + 2)); if (new != NULL) { pc->date = new; pc->capacity += 2; printf("Successful capacity increase!\n"); } else { printf("Add_Capccity():%s\n", strerror(errno)); return; } } void AddContact(Contact* pc) { assert(pc); if (pc->capacity == pc->sz) { Add_Capacity(pc); } printf("Enter the name of the contact you want to add:"); scanf("%s", pc->date[pc->sz].name); printf("Enter the gender of the contact you want to add:"); scanf("%s", pc->date[pc->sz].sex); printf("Enter the age of the contact you want to add:"); scanf("%d", &(pc->date[pc->sz].age)); printf("Enter the phone number of the contact you want to add:"); scanf("%s", pc->date[pc->sz].telep); printf("Enter the address of the contact you want to add:"); scanf("%s", pc->date[pc->sz].address); pc->sz++; printf("Contact added successfully\n"); } void ShowContact(Contact* pc) { assert(pc); printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\t\n", "full name", "Gender", "Age", "Telephone", "address"); int i = 0; for (i = 0; i < pc->sz; i++) { printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n", pc->date[i].name, pc->date[i].sex, pc->date[i].age, pc->date[i].telep, pc->date[i].address); } } int FindContact(Contact* pc, char name[]) { assert(pc); int i = 0; for (i = 0; i < pc->sz; i++) { if (strcmp(pc->date[i].name, name) == 0) { return i; } } return -1; } void DeletContact(Contact* pc, char name[]) { assert(pc); if (pc->sz == 0) { printf("There are no contacts,Cannot delete\n"); return; } int i = FindContact(pc, name); if (i != -1) { int j = 0; for (j = i; j < pc->sz - 1; j++) { pc->date[j] = pc->date[j + 1]; } pc->sz--; printf("Deleted successfully!\n"); } else { printf("No one was found\n"); } } void SearchContact(Contact* pc, char name[]) { assert(pc); int ret = FindContact(pc, name); if (ret != -1) { printf("eureka!\n"); printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n", pc->date[ret].name, pc->date[ret].sex, pc->date[ret].age, pc->date[ret].telep, pc->date[ret].address); } else { printf("Can't find\n"); } } void ModifyContact(Contact* pc, char name[]) { assert(pc); int ret = FindContact(pc, name); if (ret == -1) { printf("No one was found\n"); } else { printf("eureka!\n"); printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n", pc->date[ret].name, pc->date[ret].sex, pc->date[ret].age, pc->date[ret].telep, pc->date[ret].address); printf("Please modify:\n"); printf("Enter the name of the contact you want to add:"); scanf("%s", pc->date[ret].name); printf("Enter the gender of the contact you want to add:"); scanf("%s", pc->date[ret].sex); printf("Enter the age of the contact you want to add:"); scanf("%d", &(pc->date[ret].age)); printf("Enter the phone number of the contact you want to add:"); scanf("%s", pc->date[ret].telep); printf("Enter the address of the contact you want to add:"); scanf("%s", pc->date[ret].address); printf("Modification succeeded!\n"); } } int cmp_name(const void* e1, const void* e2) { return strcmp(((Student*)e1)->name, ((Student*)e2)->name); // return strcmp(((Contact*)e1)->date->name, ((Student*)e2)->date->name); } void SortContact(Contact* pc) { assert(pc); qsort(pc->date, pc->sz, sizeof(pc->date[0]), cmp_name); } void DestoryContact(Contact* pc) { assert(pc); free(pc->date); pc->date = NULL; pc->capacity = 0; pc->sz = 0; }
Contact.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"Contact.h" void InitContact(Contact* pc) { assert(pc); Student* new = (Student*)malloc(DEFAULT_SIZE * sizeof(Student)); if (new != NULL) { pc->date = new; pc->sz = 0; pc->capacity = DEFAULT_SIZE; } else { printf("InitContact:%s\n", strerror(errno)); return; } } void Add_Capacity(Contact* pc) { Student* new = (Student*)realloc(pc->date, sizeof(Student) * (pc->capacity + 2)); if (new != NULL) { pc->date = new; pc->capacity += 2; printf("Successful capacity increase!\n"); } else { printf("Add_Capccity():%s\n", strerror(errno)); return; } } void AddContact(Contact* pc) { assert(pc); if (pc->capacity == pc->sz) { Add_Capacity(pc); } printf("Enter the name of the contact you want to add:"); scanf("%s", pc->date[pc->sz].name); printf("Enter the gender of the contact you want to add:"); scanf("%s", pc->date[pc->sz].sex); printf("Enter the age of the contact you want to add:"); scanf("%d", &(pc->date[pc->sz].age)); printf("Enter the phone number of the contact you want to add:"); scanf("%s", pc->date[pc->sz].telep); printf("Enter the address of the contact you want to add:"); scanf("%s", pc->date[pc->sz].address); pc->sz++; printf("Contact added successfully\n"); } void ShowContact(Contact* pc) { assert(pc); printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\t\n", "full name", "Gender", "Age", "Telephone", "address"); int i = 0; for (i = 0; i < pc->sz; i++) { printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n", pc->date[i].name, pc->date[i].sex, pc->date[i].age, pc->date[i].telep, pc->date[i].address); } } int FindContact(Contact* pc, char name[]) { assert(pc); int i = 0; for (i = 0; i < pc->sz; i++) { if (strcmp(pc->date[i].name, name) == 0) { return i; } } return -1; } void DeletContact(Contact* pc, char name[]) { assert(pc); if (pc->sz == 0) { printf("There are no contacts,Cannot delete\n"); return; } int i = FindContact(pc, name); if (i != -1) { int j = 0; for (j = i; j < pc->sz - 1; j++) { pc->date[j] = pc->date[j + 1]; } pc->sz--; printf("Deleted successfully!\n"); } else { printf("No one was found\n"); } } void SearchContact(Contact* pc, char name[]) { assert(pc); int ret = FindContact(pc, name); if (ret != -1) { printf("eureka!\n"); printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n", pc->date[ret].name, pc->date[ret].sex, pc->date[ret].age, pc->date[ret].telep, pc->date[ret].address); } else { printf("Can't find\n"); } } void ModifyContact(Contact* pc, char name[]) { assert(pc); int ret = FindContact(pc, name); if (ret == -1) { printf("No one was found\n"); } else { printf("eureka!\n"); printf("%-10s\t%-5s\t%-5d\t%-13s\t%-20s\t\n", pc->date[ret].name, pc->date[ret].sex, pc->date[ret].age, pc->date[ret].telep, pc->date[ret].address); printf("Please modify:\n"); printf("Enter the name of the contact you want to add:"); scanf("%s", pc->date[ret].name); printf("Enter the gender of the contact you want to add:"); scanf("%s", pc->date[ret].sex); printf("Enter the age of the contact you want to add:"); scanf("%d", &(pc->date[ret].age)); printf("Enter the phone number of the contact you want to add:"); scanf("%s", pc->date[ret].telep); printf("Enter the address of the contact you want to add:"); scanf("%s", pc->date[ret].address); printf("Modification succeeded!\n"); } } int cmp_name(const void* e1, const void* e2) { return strcmp(((Student*)e1)->name, ((Student*)e2)->name); // return strcmp(((Contact*)e1)->date->name, ((Student*)e2)->date->name); } void SortContact(Contact* pc) { assert(pc); qsort(pc->date, pc->sz, sizeof(pc->date[0]), cmp_name); } void DestoryContact(Contact* pc) { assert(pc); free(pc->date); pc->date = NULL; pc->capacity = 0; pc->sz = 0; }
That's all for today!!!
If you think the author is a little helpful to you!
Just a little praise and attention!!! Of course, subscription is even more desirable!
Give someone a rose, the hand has a lingering fragrance =. =!
Finally, thank you for watching!!!
Your support is the greatest driving force for the author to write!!!
See you next time!!!