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

The method of realizing address Book dynamically and statically in C language

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

Share

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

This article mainly explains "the method of dynamic and static realization of 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 "the method of dynamic and static realization of address book in C language".

one。 Implementation of static address book 1. The division logic of environment

Because the process is a bit long and easy to understand, a total of three files will be used.

They are two source files, test.c, contact.c, and a header file, contact.h.

Test.c is the main body and the test logic of the address book

Contact.c is the implementation logic of the address book

Contact.h is a declaration that implements the address book function

two。 Functions to be implemented

Each function of the address book is relatively independent.

Therefore, different functions can be used to realize the functions of each part.

Contact.c contains the following functions:

1. Initialization of structure

two。 Add contact

3. Show contact

4. Delete contact

5. Find contact

6. Modify contact

The specific code is as follows:

The 3.contact.h#define _ CRT_SECURE_NO_WARNINGS 1#include#include// address book can be used to store the information of 1000 people, including name, sex, age, telephone number, address typedef struct Introduction {char name [20]; char sex [10]; int age; char tele [20]; char addr [30];} Introduction;typedef struct contact {Introduction data [1000] Initialization void Initcontact of int sz;} contact; / / structure (contact* pc); / / add contact void Add (contact* pc); / / display contact void show (contact* pc); / / delete contact void Dele (contact* pc); / / find contact void Find (contact* pc); / / modify contact void Exchange (contact* pc) Initialization of 4.contact.c#define _ CRT_SECURE_NO_WARNINGS 1#include "contact.h" void Initcontact (contact* pc) / / structure {pc- > sz = 0; memset (pc- > data, 0, sizeof (pc- > data));} void Add (contact* pc) / / add contacts {/ / determine whether if (pc- > sz = = 1000) {printf ("address book full\ n"); return } printf ("Please enter name >:"); scanf ("% s", pc- > data [PC-> sz] .name); printf ("Please enter age >:"); scanf ("% d", & (pc- > data [PC-> sz] .age)); printf ("Please enter gender: >"); scanf ("% s", pc- > data [PC-> sz] .sex); printf ("Please enter phone number: >") Scanf ("% s", pc- > data [PC-> sz] .tele); printf ("Please enter address: >"); scanf ("% s", pc- > data [PC-> sz] .addr); pc- > sz++; printf ("increase contact success\ n");} void show (contact* pc) / / display contact {int I = 0 Printf ("%-10s\ t% murf 10s\ t% Mustco 10s\ t\ n", "name", "age", "gender", "telephone", "address"); for (I = 0; I

< pc->

Sz; iTunes +) {printf ("%-10s\ t% Murray 10d\ t% int 10s\ t% Mustco 10s\ t% Mustco 10s\ t\ n", pc- > data [I] .name, pc- > data [I] .age, pc- > data [I] .sex, pc- > dataI [.tele, pc- > dataI [.addr);}} int SearchByName (contact* pc, char name []) {int I = 0; for (I = 0; I; I)

< pc->

Sz; iTunes +) {if (strcmp (pc- > data [I] .name, name) = 0) {return I;}} return-1;} void Dele (contact* pc) / / Delete contact {int I = 0; char name [10]; printf ("enter the name of the person to be deleted >:\ n"); scanf ("% s", name) If (pc- > sz = = 0) {printf ("address book is empty and cannot be deleted\ n");} / / determine whether there is a contact int pos = SearchByName (pc, name); if (pos = =-1) {printf ("contact not found\ n");} else {for (I = pos; I)

< pc->

Sz- 1; iTunes +) {pc- > data [I] = pc- > data [I + 1];} pc- > sz--; printf ("contact deleted successfully\ n");}} void Find (contact* pc) / / find contact {char name [10]; printf ("enter the name of the person to be found >:\ n"); scanf ("% s", name) Int pos = SearchByName (pc, name); if (pos = =-1) {printf ("No name found in address book");} else {printf ("found person information is: >") Printf ("%-10s\ t% murf 10s\ t% Mustco 10s\ t\ n", "name", "age", "gender", "phone", "address") Printf ("%-10s\ t% Mustco 10d\ t% Mustco 10s\ t% Muth10s\ t\ n", pc- > data [pos] .name, pc- > data [pos] .age, pc- > data [pos] .sex, pc- > data [pos] .tele, pc- > datapos [.addr);}} void Exchange (contact* pc) / / modify contact {char name [10] Printf ("enter the name of the person to be modified >:\ n"); scanf ("% s", name); int pos = SearchByName (pc, name); if (pos = =-1) {printf ("No name in the address book");} else {printf ("Please enter name >:"); scanf ("% s", pc- > datapos] .name) Printf ("Please enter age >:"); scanf ("% d", & (pc- > data [pos] .age)); printf ("Please enter gender: >"); scanf ("% s", pc- > data [pos] .sex); printf ("Please enter phone number: >"); scanf ("% s", pc- > datapos] .tele) Printf ("Please enter address: >"); scanf ("% s", pc- > data [pos] .addr); printf ("modified successfully") }} 5.test.c#define _ CRT_SECURE_NO_WARNINGS 1#include "contact.h" void menu () {printf ("*\ n"); printf ("* 1.Add 2.Dele *\ n") Printf ("* 3.Find 4.Exchange *\ n"); printf ("* 5.show 0.exit *\ n") Printf ("*\ n");} int main () {int input = 0; contact con; Initcontact (& con); do {menu (); printf ("Please enter a number >:") Scanf (% d, & input); switch (input) {case 1: Add (& con); break; case 2: Dele (& con); break Case 3: Find (& con); break; case 4: Exchange (& con); break; case 5: show (& con); break Case 0: printf ("exit address book\ n"); break; default: printf ("wrong choice\ n");}} while (input); return 0;} 6. Realize the effect

two。 The realization of address Book Dynamics

Compared with static implementation, there is not much difference between the two.

The former uses a fixed array to store elements.

The latter uses dynamically applied memory to achieve storage, which has the advantage of saving memory.

The difference is that

Initialization of the address book. Capcity capacity is referenced here to compare existing capacity.

To judge the amount of memory used, which is convenient for expansion.

The second is the Add function, which adds contacts.

Finally, the free memory is released.

The code is as follows:

1.contact.h#define _ CRT_SECURE_NO_WARNINGS 1#include#include#include#include typedef struct Introduction {char name [20]; char sex [10]; int age; char tele [20]; char addr [30];} Introduction; typedef struct contact {Introduction* data; int sz; int capcity;} contact; / / structure initialization void Initcontact (contact* pc) / / add contact void Add (contact*pc); / / Show contact void show (contact*pc); / / Delete contact void Dele (contact*pc); / / find contact void Find (contact*pc); / / modify contact void Exchange (contact*pc); / / destroy address book void destroy (contact*pc) 2.contact.c#define _ CRT_SECURE_NO_WARNINGS 1#include "contact.h" # define TARGET_sz 3 / / initial address book target capacity void Initcontact (contact* pc) / / initialization of the structure {assert (pc); / / determine the validity of the pointer pc- > sz = 0; contact* tmp= 0; tmp= (contact*) malloc (sizeof (Introduction) * TARGET_sz); if (tmpaired null) {pc- > data = tmp } else {printf ("expansion failed, please check the reason\ n"); return;} pc- > capcity = TARGET_sz;} void Add (contact* pc) / / add contact {assert (pc); if (pc- > capcity = = pc- > sz) {Introduction* tmp= 0; tmp= (Introduction*) realloc (pc- > data,sizeof (Introduction) * (pc- > capcity+2)) If (tmp! = NULL) {pc- > capcity + = 2; pc- > data = tmp; printf ("expansion succeeded\ n");} else {printf ("expansion failed, please check the reason\ n");}} printf ("Please enter name >:") Scanf ("% s", pc- > data [PC-> sz] .name); printf ("Please enter age >:"); scanf ("% d", & (pc- > data [PC-> sz] .age)); printf ("Please enter gender: >"); scanf ("% s", pc- > Data [PC-> sz] .sex); printf ("Please enter phone number: >") Scanf ("% s", pc- > data [PC-> sz] .tele); printf ("Please enter address: >"); scanf ("% s", pc- > data [PC-> sz] .addr); pc- > sz++; printf ("increase contact success\ n");} void show (contact* pc) / / display contact {int I = 0 Printf ("%-10s\ t% murf 10s\ t% Mustco 10s\ t\ n", "name", "age", "gender", "telephone", "address"); for (I = 0; I

< pc->

Sz; iTunes +) {printf ("%-10s\ t% Murray 10d\ t% int 10s\ t% Mustco 10s\ t% Mustco 10s\ t\ n", pc- > data [I] .name, pc- > data [I] .age, pc- > data [I] .sex, pc- > dataI [.tele, pc- > dataI [.addr);}} int SearchByName (contact* pc, char name []) {int I = 0; for (I = 0; I; I)

< pc->

Sz; iTunes +) {if (strcmp (pc- > data [I] .name, name) = 0) {return I;}} return-1;} void Dele (contact* pc) / / Delete contact {int I = 0; char name [10]; printf ("enter the name of the person to be deleted >:\ n"); scanf ("% s", name) If (pc- > sz = = 0) {printf ("address book is empty and cannot be deleted\ n");} / / determine whether there is a contact int pos = SearchByName (pc, name); if (pos = =-1) {printf ("contact not found\ n");} else {for (I = pos; I)

< pc->

Sz- 1; iTunes +) {pc- > data [I] = pc- > data [I + 1];} pc- > sz--; printf ("contact deleted successfully\ n");}} void Find (contact* pc) / / find contact {char name [10]; printf ("enter the name of the person to be found >:\ n"); scanf ("% s", name) Int pos = SearchByName (pc, name); if (pos = =-1) {printf ("No name found in address book");} else {printf ("found person information is: >") Printf ("%-10s\ t% murf 10s\ t% Mustco 10s\ t\ n", "name", "age", "gender", "phone", "address") Printf ("%-10s\ t% Mustco 10d\ t% Mustco 10s\ t% Muth10s\ t\ n", pc- > data [pos] .name, pc- > data [pos] .age, pc- > data [pos] .sex, pc- > data [pos] .tele, pc- > datapos [.addr);}} void Exchange (contact* pc) / / modify contact {char name [10] Printf ("enter the name of the person to be modified >:\ n"); scanf ("% s", name); int pos = SearchByName (pc, name); if (pos = =-1) {printf ("No name in the address book");} else {printf ("Please enter name >:"); scanf ("% s", pc- > datapos] .name) Printf ("Please enter age >:"); scanf ("% d", & (pc- > data [pos] .age)); printf ("Please enter gender: >"); scanf ("% s", pc- > data [pos] .sex); printf ("Please enter phone number: >"); scanf ("% s", pc- > datapos] .tele) Printf ("Please enter address: >"); scanf ("% s", pc- > data [pos] .addr); printf ("modified successfully");} void destroy (contact* pc) {assert (pc); free (pc- > data); pc- > data = NULL; pc- > capcity = 0; pc- > sz = 0 } 3.test.c#define _ CRT_SECURE_NO_WARNINGS 1#include "contact.h" void menu () {printf ("*\ n"); printf ("* 1.Add 2.Dele *\ n") Printf ("* 3.Find 4.Exchange *\ n"); printf ("* 5.show 0.exit *\ n") Printf ("*\ n");} int main () {int input = 0; contact con; Initcontact (& con); do {menu () Printf ("please enter a number >:"); scanf ("% d", & input); switch (input) {case 1: Add (& con); break Case 2: Dele (& con); break; case 3: Find (& con); break; case 4: Exchange (& con); break Case 5: show (& con); break; case 0: destroy (& con); printf ("exit address book\ n"); break Default: printf ("selection error\ n");}} while (input); return 0 } Thank you for your reading. The above is the content of "the method of dynamic and static realization of address book in C language respectively". After the study of this article, I believe you have a deeper understanding of the method of dynamic and static realization of 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