Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation method of static and dynamic address book in C language?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the implementation method of static and dynamic address book in C language". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "what is the implementation of static and dynamic address book in C language"?

Static address book

After we have learned the structure, pointers and dynamic memory management of the C language, we can implement some interesting small projects that can deepen our understanding of the relevant knowledge.

The main requirements of static address book are

Static size, can record the information of 10 people (the size is self-determined)

The recorded information is as follows: name, sex, age, telephone number, address

It can realize the addition, deletion, query and modification of contacts.

In order to facilitate the management and maintenance of the code, we implement the above requirements by file.

Contact.h is used to declare related interfaces

Contact.c is used to implement related interfaces

Test.c is used to test related interfaces

Contact.h

In contact.h, we uniformly use preprocessing instructions to determine the size of the address book and the scope of each contact information to facilitate subsequent modifications.

After determining the scope, we realize the unified management of the data by defining the structure type.

After the structure type is determined, it is the declaration of the relevant interface.

/ / first introduce the header file # include#include#include// to determine the scope size of each contact's name, phone, etc. # define MAX_NAME 10#define MAX_PHONE 20#define MAX_ADDR 25 / determine the total size of the address book # define MAX_CAPACITY 10 / because each contact has multiple information Therefore, it is necessary to define a structure type to manage / / define the structure typedef struct PeoInfo {char name [Max _ NAME] of contact-related information. Char sex; int age; char phone [Max _ PHONE]; char address [Max _ ADDR];} peo;// address book contains multiple members, so it also needs unified management, so use structure / / to define an address book structure, manage address book and record the number of valid contacts typedef struct Contact {peo data [Max _ CAPACITY] / / here is an array, each element of the array is a structure int size;} con;//---// below is the declaration of the related interface / / menu interface void menu () / / initialize void InitContact (con* con) for a pair of structural variables; / / print address book information void Print (con* con); / / implement the interface to add contacts (con* con); / / implement the interface to delete contacts void DelContact (con* con); / / implement the interface to find contacts void SearchContact (con* con); / / implement the interface to modify contacts void ModifyContact (con* con); contact.c

Contact.c is the key to the whole project, and the relevant interfaces need to be defined.

/ / implementation of various interfaces of address book / / first introduce .h file # include "Contact.h" / / menu interface void menu () {printf ("* *\ n"); printf ("* static simple address book *\ n") Printf ("* 0. Exit address book *\ n "); printf (" * 1. Add contacts *\ n "); printf (" * 2. Delete contact *\ n "); printf (" * 3. Find contact *\ n "); printf (" * 4. Modify contact *\ n "); printf (" * 5. Print contact *\ n "); printf (" * *\ n "); printf ("\ n ") } / / initialization of a pair of structural variables void InitContact (con* con) {/ / initialization of structures is generally used memset / / We first let the array of address book structural variables start with 0 memset (con- > data, 0, sizeof (con- > data)); con- > size = 0 / / at the same time, because there is no contact information at this time, size is also 0} / / print address book information void Print (con* con) {int I = 0; if (con- > size = = 0) {printf ("no printable information\ n"); return;} printf ("name\ tsex\ tage\ tphone\ taddress\ n") For (I = 0; I)

< con->

Size; iTunes +) {printf ("% s\ t% c\ t% d\ t% s\ t% s\ n", con- > data [I] .name, con- > data [I] .sex, con- > data [I] .age, con- > data [I] .phone, con- > data [I] .address) }} / / implement the interface void AddContact (con* con) {if (con- > size = = MAX_CAPACITY) {printf ("capacity is full and cannot be added\ n"); return;} printf ("Please enter name:\ n"); scanf ("% s", con- > data [con-> size] .name); getchar () Printf ("Please choose gender: (M for male, w for female)\ n"); scanf ("% c", & (con- > data [con-> size] .sex)); printf ("Please enter age:\ n"); scanf ("% d", & (con- > dataCon-> size] .age); printf ("Please enter your mobile phone number:\ n") Scanf ("% s", con- > data [con-> size] .phone); printf ("Please enter address:\ n"); scanf ("% s", con- > data [con-> size] .address); con- > size++;} / / implement the interface void DelContact (con* con) {if (con- > size = = 0) {if (con- > size = = 0) {printf ("No information to delete") Return;} printf ("Please enter the name of the contact you want to delete:\ n"); char name [Max _ NAME] = {0}; scanf ("% s", name); int I = 0; for (I = 0; I

< con->

Size; iTunes +) {if (strcmp (name, (con- > data) [I] .name) = 0) {int j = 0; for (j = I; j)

< con->

Size- 1; jacks +) {memmove (& (con- > data [j]), & (con- > data [j + 1]), sizeof (con- > data [0]));} con- > size--; return }} printf ("the contact you entered does not exist\ n");} / / implement the interface for finding contacts void SearchContact (con* con) {if (con- > size = = 0) {printf ("no information found yet"); return } printf ("Please enter the name of the contact you are looking for:\ n"); char name [Max _ NAME] = {0}; scanf ("% s", name); int I = 0; for (I = 0; I

< con->

Size If (strcmp (name, (con- > data) [I] .name) = 0) {printf ("% s\ t% c\ t% d\ t% s\ t% s\ n", con- > data [I] .name, con- > data [I] .sex, con- > dataI [.age] .age Con- > data [I] .phone, con- > data [I] .address) Return;}} printf ("the contact you want to find does not exist\ n");} / implement the interface to modify the contact void ModifyContact (con* con) {if (con- > size = = 0) {printf ("no information that can be modified"); return } printf ("Please enter the name of the contact you want to modify:\ n"); char name [Max _ NAME] = {0}; scanf ("% s", name); int I = 0; for (I = 0; I

< con->

Size; iTunes +) {if (strcmp (name, (con- > data) [I] .name) = 0) {printf ("Please re-enter your name:\ n"); scanf ("% s", con- > data [I] .name); getchar () Printf ("Please re-select gender: (M for male, w for female)\ n"); scanf ("% c", & (con- > data [I] .sex); printf ("Please re-enter age:\ n") Scanf ("% d", & (con- > data [I] .age); printf ("Please re-enter your mobile phone number:\ n"); scanf ("% s", con- > data [I] .phone); printf ("Please re-enter your address:\ n") Scanf ("% s", con- > data [I] .address); return;}} printf ("the contact you want to modify does not exist\ n");}

Prompt

When we implement the interface, if the parameter is passed in a pointer, we generally need to determine whether the passed pointer is a null pointer to avoid illegal access, but because of my carelessness, I didn't write it down!

Test.c

Test.c is a file used to test the relevant interfaces, which can be set according to your own ideas.

# include "Contact.h" int main () {/ / create the structure and initialize con c; InitContact (& c); menu (); int input = 0; while (1) {printf ("Please enter your choice:\ n"); scanf ("% d", & input) Switch (input) {case 1: AddContact (& c); break; case 2: DelContact (& c); break; case 3: SearchContact (& c) Break; case 4: ModifyContact (& c); break; case 5: Print (& c); break Case 0: printf ("exit address book\ n"); break; default: printf ("you typed incorrectly, please re-select\ n"); break } if (input = = 0) {break;}} return 0;} dynamic address book

Static address book has a disadvantage, that is, the size of the address book, that is, only stored contacts are required to determine the size, the size is too much waste of space, and less is not enough.

Dynamic address book is to dynamically open up the size of space through the function of dynamic memory management to meet the needs.

The implementation of the contact.h// dynamic version of the address book # include#include#include#define MAX_NAME 15#define MAX_PHONE 15#define MAX_ADDR 25 ADDR / the default size of the given address book is 5, which is not enough to add the declaration typedef struct PeoInfo {char name [Max _ NAME] of the # define DefaultSize 5 user / contact structure; char sex; short age; char phone [Max _ PHONE]; char addr [Max _ ADDR];} peo / / statement of the address book structure typedef struct Contact {peo* pashing / We manage the number of contacts group int capacity; int size;} con;// menu function void menu () by pointer; / / initialize the address book void InitCon (con* c); / / destroy the address book void DestoryCon (con* c); / / check the capacity of the interface void CheckCapacity (con* c) / / print contact information void Print (con* c); / / add contact void AddCon (con* c); / / contact deletion void DelCon (con* c); / / contact search void SearchCon (con* c); / / contact modification void ModifyCon (con* c) / /-- / / here is how I sort void SortByname (con* c) by name and / / sort void SortByage by age (con* c) through the principle of fast arrangement, handwritten quick arrangement. Void Swap (char* p1, char* p2, int width); void QSort (void* p, int num, int width, int (* cmp) (const void*, const void*)); int cmpbyname (const void* p1 void* p2); int cmpbyage (const void* p1 void* p2); contact.c

The implementation of the key interface is similar to that of the static address book, except that we need to define one more interface to check the space.

Call this API every time you add contacts to increase capacity when it is full.

The realloc function is used to increase the capacity of the interface.

# include "contact.h" / / menu function void menu () {printf ("* *\ n"); printf ("* dynamic address Book *\ n") Printf ("* 1. Add contacts *\ n "); printf (" * 2. Delete contact *\ n "); printf (" * 3. Find contact *\ n "); printf (" * 4. Modify contact *\ n "); printf (" * 5. Sort by name *\ n "); printf (" * 6. Sort by age *\ n "); printf (" * 7. Print contact *\ n "); printf (" * 0. Exit address book *\ n "); printf (" * *\ n ") } / / initialize address book void InitCon (con* c) {/ / this can also be initialized with the relloc function, so that there will be an initial value c-> p = (con*) malloc (DefaultSize * sizeof (peo)); c-> capacity = DefaultSize; c-> size = 0;} / / destroy address book void DestoryCon (con* c) {free (c-> p); c-> p = NULL C-> capacity = 0; c-> size = 0;} / / check capacity interface void CheckCapacity (con* c) {if (c-> capacity = = c-> size) {/ / double the capacity each time peo* tmp = (peo*) realloc (c-> p, 2 * (c-> capacity) * sizeof (peo)) If (tmp! = NULL) {c-> p = tmp; c-> capacity = 2 * c-> capacity; printf ("capacity increase succeeded\ n");} else {printf ("capacity increase failed\ n") Exit (1);} / / print contact information void Print (con* c) {if (c-> size = = 0) {printf ("No information to print yet\ n"); return;} printf ("name\ tsex\ tage\ tphone\ taddress\ n") Int I = 0; for (I = 0; I

< c->

Size; iTunes +) {printf ("% s\ t% c\ t% d\ t% s\ t% s\ n", c-> p [I] .name, c-> p [I] .sex, c-> p [I] .age, c-> p [I] .phone, c-> p [I] .addr) }} / / increase of contacts void AddCon (con* c) {/ / each time you add a contact, check whether the capacity is sufficient CheckCapacity (c); printf ("Please enter name: >\ n"); scanf ("% s", c-> p [c-> size] .name); getchar () Printf ("Please enter gender: > (m for male, w for female)\ n"); scanf ("% c", & (c-> p [c-> size] .sex)); printf ("Please enter age: >\ n"); scanf ("% d", & (c-> p [c-> size] .age)); printf ("Please enter phone number: >\ n") Scanf ("% s", c-> p [c-> size] .phone); printf ("Please enter address: >\ n"); scanf ("% s", c-> p [c-> size] .addr); c-> size++; printf ("added successfully\ n") } / / contact deletion void DelCon (con* c) {printf ("Please enter the name of the contact you want to delete\ n"); char name [Max _ NAME]; scanf ("% s", name); int I = 0; for (I = 0; I

< c->

Size; iTunes +) {if (strcmp (name, c-> p [I] .name) = = 0) {int j = I; for (j = I; j

< c->

Size- 1; jacks +) {memmove (& (c-> p [j]), & (c-> p [j + 1]), sizeof (c-> p [0]));} c-> size--; return }} printf ("the contact you want to delete does not exist\ n");} / / contact search void SearchCon (con* c) {printf ("Please enter the name of the person you are looking for:\ n"); char name [Max _ NAME]; scanf ("% s", name); int I = 0; for (I = 0; I

< c->

Size If +) {if (strcmp (name, c-> p [I] .name) = 0) {printf ("% s\ t% c\ t% d\ t% s\ t% s\ n", c-> p [I] .name, c-> p [I] .sex, c-> p [I] .age, c-> p [I] .phone, c-> p [I] .addr) Return;}} printf ("the contact you want to find does not exist\ n");} / / modified void ModifyCon (con* c) {printf ("Please enter the name of the person you want to modify:\ n"); char name [Max _ NAME]; scanf ("% s", name); int I = 0 For (I = 0; I)

< c->

Size; iTunes +) {if (strcmp (name, c-> p [I] .name) = 0) {printf ("Please re-enter the name: >\ n"); scanf ("% s", c-> p [I] .name); getchar () Printf ("Please re-enter gender: > (m for male, w for female)\ n"); scanf ("% c", & (c-> p [I] .sex)); printf ("Please re-enter age: >\ n") Scanf ("% d", & (c-> p [I] .age)); printf ("Please re-enter phone: >\ n"); scanf ("% s", c-> p [I] .phone); printf ("Please re-enter address: >\ n") Scanf ("% s", c-> p [I] .addr); return;}} printf ("the contact you want to modify does not exist\ n");} / / sort void SortByname (con* c) {QSort (c-> p, c-> size, sizeof (c-> p [0]), cmpbyname) by name } / / sort void SortByage (con* c) {QSort (c-> p, c-> size, sizeof (c-> p [0]), cmpbyage) by age;} qsort.c

This is my handwritten quick sort according to the principle of quick sort.

# include "contact.h" / / the exchange function void Swap (char* p1, char* p2, int width) {int I = 0; for (I = 0; I)

< width; i++) { char tmp = *p1; *p1 = *p2; *p2 = tmp; p1++; p2++; }}//手写快排来进行通讯录排序void QSort(void* p, int num, int width, int(*cmp)(const void*, const void*)) { int i = 0; for (i = 0; i < num - 1; i++) { int j = 0; for (j = 0; j < num - 1 - i; j++) { if (cmp((char*)p + j * width, (char*)p + (j + 1) * width) >

0) {Swap ((char*) p + j * width, (char*) p + (j + 1) * width, width);}} int cmpbyage (const void* p1, const void* p2) {return (* (peo*) p1) .age-(* (peo*) p2) .age } int cmpbyname (const void* p1, const void* p2) {return strcmp (peo*) p1)-> name, ((peo*) p2)-> name);} test.c

Test.c is used to test related interfaces, which can be tested according to your own ideas.

# include "contact.h" int main () {menu (); int input = 0; con c; InitCon (& c); while (1) {printf ("Please enter your choice:\ n"); scanf ("% d", & input) Switch (input) {case 1: AddCon (& c); break; case 2: DelCon (& c); break; case 3: SearchCon (& c) Break; case 4: ModifyCon (& c); break; case 5: SortByname (& c); break; case 6: SortByage (& c) Break; case 7: Print (& c); break; case 0: printf ("exit address book\ n"); break } if (input = = 0) {DestoryCon (& c); break;}} return 0 Thank you for your reading. The above is the content of "what is the implementation method of static and dynamic address book in C language". After the study of this article, I believe you have a deeper understanding of what is the implementation method of static and dynamic address book in C language, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report