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 use C language script to realize simple address Book

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

Share

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

This article mainly introduces "how to use C language script to achieve simple address book". In daily operation, I believe that many people have doubts about how to use C language script to achieve simple address book. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use C language script to achieve simple address book". Next, please follow the editor to study!

Goal: the address book can store 1000 person information, person information: gender, name, age, telephone, address menu printing

1. Add contacts

two。 Delete contact

3. Find contact (modify)

4. First name sort contact

5. Show

6. Empty

Analysis:

First of all, the address book stores contact information, and you also need to know the number of contacts, so it is a structural type.

Secondly, there are many types of information, so each contact should also be a structural type.

Description:

The code order shown under each heading is: test.c (main function test run)-> contact.h (function declaration)-> contact.c (function implementation)

Menu printing: void menu () {printf ("* *\ n"); printf ("* * 1.add 2.del * *\ n"); printf ("* 3.serch (modify) *\ n"); printf ("* * 4.sort 5.show * *\ n") Printf ("* * 0.exit 6.clear * *\ n"); printf ("* *\ n");} int main () {int input = 0; do {menu (); printf ("Please select->") Scanf ("% d", & input); switch (input) {case 1: / / add information break; case 2: / / delete break Case 3: / / find (modify) break; case 4: / / sort by name break; case 5: / / display contact information break Case 6: / / clear contact break; case 0: / / exit address book break; dafault: break;}} while (input) } enumerate optimizations:

The above menu can be optimized for code readability with enumerations, with the following improvements:

Enum Oprion {Exit, Add, Del, Serch, Sort, Show, Clear}; void menu () {printf ("* *\ n"); printf ("* * 1.add 2.del * *\ n") Printf ("* * 3.serch (modify) *\ n"); printf ("* 4.sort 5.show * *\ n"); printf ("* * 0.exit 6.clear * *\ n"); printf ("* *\ n") } int main () {int input = 0; do {menu (); printf ("Please select->"); scanf ("% d", & input) Switch (input) {case Add: / / add information break; case Del: / / delete break Case Serch: / / find (modify) break; case Sort: / / sort by name break; case Show: / / display contact information break Case Clear: / / clear contacts break; case Exit: / / exit address book break; dafault: break;}} while (input) } define structure # define Max 1000#define name_Max 20#define sex_Max 5#define addr_Max 30#define phone_Max 12 / / structure person information typedef struct Peoinfo {char name [name _ Max]; int age; char sexes [sex _ Max]; char addr [addr _ Max]; char phone [phone _ Max];} Peoinfo; / / structure address book information typedef struct Contact {Peoinfo data [1000] / / Storage data int sz; / / number of valid information in the address book (there are several people in it)} Contact; initialization / / perfect menu main function part int main () {int input = 0; Contact con;// address book InitContact (& con); / / initialize address book do// initialize address book void InitContact (Contact* pc) / / initialize address book void InitContact (Contact* pc) {assert (pc); pc- > sz = 0; memset (pc- > data,0,sizeof (pc- > data)); / / initialize the array to 0} add information case Add: / / add information AddContact (& con); break / / add information to address book void AddContact (Contact* pc); / / add information void AddContact (Contact* pc) {assert (pc); if (pc- > sz==Max) {printf ("address book is full and cannot be added\ n"); return 0;} / / enter information printf ("Please enter name:") Scanf ("% s", pc- > data [PC-> sz] .name); / / after the original valid information location, use pc- > sz to point to the corresponding location printf ("Please enter age:"); scanf ("% d", & (pc- > data [PC-> sz] .age)); / / age is a variable, note that you need to take the address printf ("Please enter gender:") Scanf ("% s", pc- > data [PC-> sz] .sex); printf ("Please enter address:"); scanf ("% s", pc- > data [PC-> sz] .addr); printf ("Please enter phone:"); scanf ("% s", pc- > data [PC-> sz] .phone); pc- > sz++) / / the number of elements plus one printf ("added successfully\ n");}

Here, we should pay attention to the situation that the address book is full, and when adding information, we should pay attention to the address that is added after the original valid information to prevent repetition.

Delete message case Del: / / delete DeletContact (& con); break; / delete void DeletContact (Contact*pc); int FindByname (Contact*pc, char name []) {int I = 0; for (I = 0 int isz) Return +) {if (strcmp (pc- > data [I] .name, name) = 0) {return I;}} printf ("not found\ n"); return-1;} / delete message void DeletContact (Contact* pc) {char name [name _ name] If (pc- > sz = = 0) {printf ("address book is empty and cannot be deleted\ n");} printf ("Please enter the name of the person to delete:"); scanf ("% s", name); / / implement the lookup function int pos=FindByname (pc,name) / / if the return subscript is found, if it cannot be found,-1 if (pos==-1) {printf ("no such person\ n") is returned;} else {/ / delete-to delete the element after the element, overwrite int j = 0; for (j = pos;jsz-1) Pc- +) {pc- > data [j] = pc- > data [j + 1];} pc- > sz--; printf ("deleted successfully\ n");}}

Here to pay attention to a search function, enter the name to find the contact, find it before you can delete it.

Find (modify) case Serch: printf ("Please enter the name of the person you want to find:"); scanf (% s ", name); FindByname (& con,name); break; / / find int FindByname (const Contact*pc, char name []); int FindByname (Contact*pc, char name []) {int I = 0 For (I = 0% isz +) {if (strcmp (pc- > data [I] .name, name) = = 0) {int put = 0 Printf ("%-10s\ t% copyright 5d\ t% 5s\ t% 15s\ t% 20s\ n", pc- > data [I] .name, pc- > data [I] .age, pc- > data [I] .sex, pc- > data [I] .addr, pc- > Data.phone) / / after finding it, you can display the content to printf ("found\ nPlease choose whether to modify: 1. It's 0. No\ n "); scanf ("% d ", & put); if (1 = = put) {Modify (pc- > data,i);} return I }} printf ("not found\ n"); return-1;}

Here I declare the lookup function of the second deletion function, and add the modification function to the lookup function, the two functions are combined, but there will be repetition when using the delete function, which should be noted here.

The modified function is as follows:

/ / modify void Modify (Contact* pc,int I) {int bud = 0; do {printf ("Please select the content to be modified\ N1. Name 2. Age 3. Gender 4. Address 5. Phone number 0. Cancel\ nPlease enter: "); scanf ("% d ", & bud); switch (bud) {char chars [30] = {0}; case 1: {printf (" Please enter content-> ") Scanf ("% s", chars); strcpy (pc- > data [I] .name, chars);} break; case 2: {int num = 0 Printf ("Please enter->"); scanf ("% d", & num); pc- > data [I] .age = num;} break Case 3: {printf ("Please enter content->"); scanf ("% s", chars); strcpy (pc- > data [I] .sex, chars);} break Case 4: printf ("Please enter->"); scanf ("% s", chars); strcpy (pc- > data [I] .addr, chars); break Case 5: {printf ("Please enter content->"); scanf ("% s", chars); strcpy (pc- > data [I] .phone, chars);} break Dafault: break;}} while (bud);}

The string function strcpy is used for modification.

Name sort case Sort: Sortname (& con); break; / / name sort void Sortname (Contact* pc); / / name sort int cmp_int (const void*e1,const void*e2) {return (strcmp (Peoinfo*) E1)-> name, ((Peoinfo*) e2)-> name)) } void Sortname (Contact* pc) {qsort (pc- > data,pc- > sz,sizeof (pc- > data [0]), cmp_int); ShowContact (pc);}

Sort by name here, I use the quick sort qsort function to do this. After sorting, the display function is called to print to observe the sorting effect.

Display information case Show: / / display ShowContact (& con); break; / / display information void ShowContact (const Contact* pc); / / display information void ShowContact (const Contact* pc) {assert (pc); int I = 0; for (I = 0 position I)

< pc->

Sz;i++) {printf ("%-10s\ t% muri 5d\ t% Mustco 5s\ t% Mustco 15s\ t% 20s\ n", pc- > Data [I] .name, pc- > Data [I] .age, pc- > Data [I] .sex, pc- > Data [I] .addr, pc- > data [I] .phone) / / add minus sign left alignment, add / t to unify the distance between each element}} clear contact case Clear: ClearContact (& con); break; / / clear all contacts void ClearContact (Contact* pc); / / clear all contacts void ClearContact (Contact* pc) {InitContact (pc);}

Emptying contacts is equivalent to initializing the address book again.

Header files are included in contact.h:

# include # include

Test.c and contact.c refer to the header file contact.h uniformly

# the general program of include "contact.h" is as follows: test.c#include "contact.h" enum Oprion {Exit, Add, Del, Serch, Sort, Show, Clear}; void menu () {printf ("* *\ n") Printf ("* * 1.add 2.del * *\ n"); printf ("* 3.serch (modify) *\ n"); printf ("* 4.sort 5.show * *\ n"); printf ("* * 0.exit 6.clear * *\ n") Printf ("* *\ n");} int main () {int input = 0; Contact con;// address book InitContact (& con); / / initialize address book do {char name [name _ Max]; menu () Printf ("Please select->"); scanf ("% d", & input); switch (input) {case Add: / / add Information AddContact (& con); break Case Del: / / delete DeletContact (& con); break; case Serch: printf ("Please enter the name of the person you want to find:"); scanf ("% s", name) FindByname (& con,name); break; / / case 4: / / modify / / Modify (& con); / / break; case Sort: Sortname (& con) Break; case Show: / / display ShowContact (& con); break; case Clear: ClearContact (& con); break Case Exit: printf ("exit address book\ n"); break; dafault: break;}} while (input) } contact.h// statement # include # define Max 1000#define name_Max 20#define sex_Max 5#define addr_Max 30#define phone_Max 12 / / Information about structure person typedef struct Peoinfo {char name [name _ Max]; int age; char sexuality [name _ Max]; char addr [addr _ Max]; char phone [phone _ Max];} Peoinfo / / structure address book information typedef struct Contact {Peoinfo data [1000]; / store data int sz; / / number of valid information in the address book (several people in it)} Contact; / / initialize address book void InitContact (Contact* pc); / / add information to address book void AddContact (Contact* pc); / / display information void ShowContact (const Contact* pc); / / delete void DeletContact (Contact* pc) / / find int FindByname (const Contact*pc, char name []); / / sort by name void Sortname (Contact*pc); / / modify void Modify (Contact*pc, int I); / / clear all contacts void ClearContact (Contact*pc); contact.c#include "contact.h" / / initialize address book void InitContact (Contact*pc) {assert (pc); pc- > sz = 0 Memset (pc- > data,0,sizeof (pc- > data)); / / initialize the array to 0} / / add information void AddContact (Contact* pc) {assert (pc); if (pc- > sz==Max) {printf ("address book is full, cannot be added\ n"); return 0 } / / enter information printf ("Please enter name:"); scanf ("% s", pc- > data [PC-> sz] .name); / / after the original valid information location, use pc- > sz to point to the corresponding location printf ("Please enter age:"); scanf ("% d", & (pc- > Data [PC-> sz] .age) / / age is a variable. Note that the address printf ("Please enter gender:"); scanf ("% s", pc- > data [PC-> sz] .sex); printf ("Please enter address:"); scanf ("% s", pc- > data [PC-> sz] .addr); printf ("Please enter phone number:") Scanf ("% s", pc- > data [PC-> sz] .phone); pc- > sz++; / / number of elements plus one printf ("added successfully\ n");} / / display information void ShowContact (const Contact* pc) {assert (pc); int I = 0; for (I = 0 pc I)

< pc->

Sz;i++) {printf ("%-10s\ t% muri 5d\ t% Mustco 5s\ t% Mustco 15s\ t% 20s\ n", pc- > Data [I] .name, pc- > Data [I] .age, pc- > Data [I] .sex, pc- > Data [I] .addr, pc- > data [I] .phone) / add a minus sign to align to the left, add / t to unify the distance between each element}} int FindByname (Contact*pc, char name []) {int I = 0; for (I = 0 × isz [I] +) {if (pc- > data [I] .name, name) = 0) {int put = 0 Printf ("%-10s\ t% copyright 5d\ t% 5s\ t% 15s\ t% 20s\ n", pc- > data [I] .name, pc- > data [I] .age, pc- > data [I] .sex, pc- > data [I] .addr, pc- > data [I] .phone); printf ("Please select whether to modify: 1. It's 0. No\ n "); scanf ("% d ", & put); if (1 = = put) {Modify (pc- > data,i);} return I }} printf ("not found\ n"); return-1;} / delete message void DeletContact (Contact* pc) {char name [name _ Max]; if (pc- > sz = = 0) {printf ("address book is empty and cannot be deleted\ n") } printf ("Please enter the name of the person to be deleted:"); scanf ("% s", name); / / implement the lookup function int pos=FindByname (pc,name); / / if the subscript is found and cannot be found, return-1 if (pos==-1) {printf ("No such person\ n") } else {/ / delete-to delete the element after the element, overwrite int j = 0; for (j = pos;jsz-1;j++) {pc- > data [j] = pc- > data [j + 1] } pc- > sz--; printf ("deleted successfully\ n");}} / / name sort int cmp_int (const void*e1,const void*e2) {return (strcmp (Peoinfo*) E1)-> name, ((Peoinfo*) e2)-> name) } void Sortname (Contact* pc) {qsort (pc- > data,pc- > sz,sizeof (pc- > data [0]), cmp_int); ShowContact (pc);} / modify void Modify (Contact* pc,int I) {int bud = 0; do {printf ("Please select the content to be modified\ N1. Name 2. Age 3. Gender 4. Address 5. Phone number 0. Cancel\ nPlease enter: "); scanf ("% d ", & bud); switch (bud) {char chars [30] = {0}; case 1: {printf (" Please enter content-> ") Scanf ("% s", chars); strcpy (pc- > data [I] .name, chars);} break; case 2: {int num = 0 Printf ("Please enter->"); scanf ("% d", & num); pc- > data [I] .age = num;} break Case 3: {printf ("Please enter content->"); scanf ("% s", chars); strcpy (pc- > data [I] .sex, chars);} break Case 4: printf ("Please enter->"); scanf ("% s", chars); strcpy (pc- > data [I] .addr, chars); break Case 5: {printf ("Please enter content->"); scanf ("% s", chars); strcpy (pc- > data [I] .phone, chars);} break Dafault: break;}} while (bud);} / clear all contacts void ClearContact (Contact* pc) {InitContact (pc);} at this point, the study on "how to use C language script to implement a simple address book" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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