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

How to write the code of 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--

In this issue, the editor will bring you how to write the code of the address book in C language. the article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

(1) realization idea 1. Address book function

Add friends, delete friends, find friends, modify friend information, sort friends

two。 Modularization realizes all kinds of functions.

A. Test.c

Test the address book function

B. Contact.c

Realize the address book function

C. Contact.h

The header file that contains the address book implementation

3. Code implementation (2) Source code A.test.c#define _ CRT_SECURE_NO_WARNINGS 1#include # include "contact.h" void menu () {printf ("* *\ n"); printf ("* * 1.Add 2.Del * *\ n") Printf ("* * 3.Search 4.Modify * *\ n"); printf ("* 5.Show 6.Sort * *\ n"); printf ("* 7.Help 8.About0 * *\ n"); printf ("* * 0.Exit * *\ n") Printf ("* *\ n");} int main () {int input = 0; / / create address book int size = 0; struct Contact con;// contains information about 1000 people and size in a structure / / size indicates the current number of friends in the address book / / initializes the address book InitContact (& con); / / initializes the initial value of size and friend information to 0 Do {menu (); printf ("Please select:"); scanf ("% d", & input); switch (input) {/ / add friend information case Add: AddContact (& con) Break; / / Delete friend case Del: DelContact (& con); break; / / find friend information case Search: SearchContact (& con) Break; / / modify friend information case Modify: ModifyContact (& con); break; / / display friend information case Show: ShowContact (& con) Break; / / friend sort case Sort: SortContact (& con); break; / / exit case Exit: printf ("exit address book!") ; use of break; / / address book and help case Help: HelpContact (); break; / about address book case About: AboutContact () Break; / / selection error default: printf ("wrong choice!") ; break;}} while (input); return 0 } B.Contact.h#define _ CRT_SECURE_NO_WARNINGS 1#define Max 1000#define Max_name 20#define Max_sex 5#define Max_tele 12#define Max_addr 30 enum Option {Exit, Add, Del, Search, Modify, Show, Sort, Help, About} / / create a structure to store the information to be stored in the address book: struct PeoInfo {char name [max _ name]; int age; char sexes [max _ sex]; char telex [max _ tele]; char addr [max _ addr];}; / / address book type struct Contact {struct PeoInfo data [Max] / / store a message, including friend name, age, gender, phone number, address int size;// record the number of existing elements / / save a friend, size plus 1, indicate an increase in the number of contacts}; / / declare the function / / 1. Initialize the address book void InitContact (struct Contact* ps); / add friend information void AddContact (struct Contact* ps); / delete friend void DelContact (struct Contact* ps); / / find friend void SearchContact (struct Contact* ps) with specified name; / / display friend information void ShowContact (const struct Contact* ps); / / modify friend information void ModifyContact (struct Contact* ps) / / sort friends in the address book by their initials void SortContact (struct Contact* ps); / / help use the address book void HelpContact (); / / about address book void AboutContact (); C.Contact.c#define _ CRT_SECURE_NO_WARNINGS 1 # include "contact.h" # include # include # include / / initialize the address book void InitContact (struct Contact* ps) {memset (ps- > data, 0, sizeof (ps- > data)) Ps- > size = 0ram / set address book initially there are only 0 elements} / / help using address book void HelpContact () {printf ("use of buttons:\ n"); printf ("1. Add friends\ n "); printf (" 2. Delete friends\ n "); printf (" 3. Find friend information\ n "); printf (" 4. Modify friend information\ n "); printf (" 5. Display friend information\ n "); printf (" 6. Sort your friends\ n "); printf (" 7. Help using the address book\ n "); printf (" 8. About address book\ n); printf ("Thank you for using address book! About address book void AboutContact () {printf ("address book generally refers to pen records in daily life, and also has this function in electronic products such as mobile phones, computers, electronic dictionaries, etc.). \ n ");} / / add a friend's phone number, message void AddContact (struct Contact* ps) {if (ps- > size = = Max) {printf (" address book is full, cannot be added! \ n ");} else {printf (" Please enter name: "); scanf ("% s ", ps- > data [PS-> size] .name); / / all are arrays except for age, directly using the array name printf (" Please enter age: ") Scanf ("% d", & (ps- > data [PS-> size] .age); / / Age is not an array, then & printf ("Please enter gender:"); scanf ("% s", ps- > data [PS-> size] .sex); printf ("Please enter phone number:") Scanf ("% s", ps- > data [PS-> size] .tele); printf ("Please enter address:"); scanf ("% s", ps- > data [PS-> size] .addr); ps- > size++; / / successfully add a 1 friend, size plus 1 Printf ("added successfully! \ n ");}} / / the deletion, search, and modification functions all involve finding the friend / / in order to avoid redundancy, we extract this part of the search from the function / / use it directly in the function, it will not appear to repeat static int Find_by_name (struct Contact* ps, char name [max _ name]) {int i; for (I = 0; I)

< ps->

Size; iTunes +) {/ / the name of the friend to be found is compared with the friend in the address book if (0 = = strcmp (ps- > data [I] .name, name) {/ / if found, return its subscript return I }} / / if both are looped, return-1 return-1;} / delete friend information void DelContact (struct Contact* ps) {int j; char name [max _ name]; printf ("Please enter the name of deleted friend:"); scanf ("% s", name); / / 1. Find the location of the person to be deleted int pos = Find_by_name (ps, name); / / call the function to find it. If found, return the subscript. If not, return-1; / / 2. Delete if (pos==-1) {printf ("this friend does not exist!\ n");} else {/ / delete data for (j = pos; j

< ps->

Size-1; size--; +) {ps- > data [j] = ps- > data [j + 1]; / / after deleting the data, the position of the following elements changes and moves forward;} ps- > size--; / / Delete a friend, size-1. Printf ("deleted successfully! \ n ");}} / / find the specified friend information void SearchContact (struct Contact* ps) {char name [max _ name]; printf (" enter the name of the friend you want to find: "); scanf ("% s ", name); int pos = Find_by_name (ps, name); if (pos = =-1) {printf (" this friend does not exist! Else {printf ("s\ t% 4s\ t% 5s\ ts\ t% 20s\ n", "name", "age", "gender", "phone", "address") Printf ("s\ t% 4d\ t% 5s\ ts\ t% 20s\ n", ps- > data [pos] .name, ps- > data [pos] .age, ps- > data [pos] .sex, ps- > data [pos] .tele Ps- > data [pos] .addr) }} / / modify friend information void ModifyContact (struct Contact* ps) {char name [max _ name]; printf ("Please enter the name of the friend who needs to modify information:"); scanf ("% s", name); int pos=Find_by_name (ps, name); if (pos= =-1) {printf ("this user does not exist!" \ n ");} else {printf (" Please enter name: "); scanf ("% s ", ps- > data [pos] .name); printf (" Please enter age: "); scanf ("% d ", & (ps- > data [pos] .age) Printf ("Please enter gender:"); scanf ("% s", ps- > data [pos] .sex); printf ("Please enter phone number:"); scanf ("% s", ps- > data [pos] .tele); printf ("Please enter address:") Scanf ("% s", ps- > data [pos] .addr); printf ("modified successfully! \ n ");}} / / shows the basic information of a friend void ShowContact (const struct Contact* ps) {if (ps- > size = = 0) {printf (" address book is empty! Else {int I = 0; printf ("s\ t% 4s\ t% 5s\ ts\ t% 20s\ n", "name", "age", "gender", "phone", "address"); for (I = 0; I

< ps->

Size Printf +) {printf ("s\ t% 4d\ t% 5s\ ts\ t% 20s\ n", ps- > data [I] .name, ps- > data [I] .age, ps- > data [I] .sex Ps- > data [I] .tele, ps- > data [I] .addr) Sort by initials int cmp_stu_by_name (const void* E1, const void* e2) {return (strcmp (struct Contact*) E1)-> data- > name, ((struct Contact*) e2)-> data- > name)) } / / We use the qsort () function to sort our friends by their initials void SortContact (struct Contact* ps) {qsort (ps- > data, ps- > size, sizeof (ps- > data [0]), cmp_stu_by_name); printf ("s\ t% 4s\ t% 5s\ ts\ t% 20s\ n", "name", "age", "gender", "phone", "address") Int i; for (I = 0; I)

< ps->

Size Printf +) {printf ("s\ t% 4d\ t% 5s\ ts\ t% 20s\ n", ps- > data [I] .name, ps- > data [I] .age, ps- > data [I] .sex, ps- > data [I] .tele Ps- > data [I] .addr) }} above is how the code for realizing the address book in C language shared by Xiaobian is written. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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