In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to use C language to realize the address book program". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Design requirements:
It can store information for 1000 people, including name, age, gender, telephone number and address.
Address book features include:
1. Add contacts
two。 Delete the specified contact
3. Find the specified contact
4. Modify the specified contact
5. Show all contacts
6. Exit the address book
Analysis of ideas:
First of all, we need to create three files, namely, the source file (test.c), the address book file (contact.c), and the address record header file (contact.h). The header file is used to contain the header file, sound name information, structure, create global variables, and macros.
We complete this program step by step:
1. Write a menu function menu () and union Option in the source program, of course, there is no header file at this time, so you can't run it. The menu function menu () clearly indicates the function corresponding to each number, which is 1. 5. Add contacts 2. Delete specified contact 3. Find the specified contact 4. Modify the specified contact 5. Show all contacts 0. Exit the address book. In the consortium, the corresponding member corresponds to the number 0,5.
Void menu () {printf ("\ n"); printf ("* *\ n"); printf ("* 1. Add 2. Del *\ n"); printf ("* 3. Search 4.modify *\ n") Printf ("* 5.show 0. Exit *\ n "); printf (" * *\ n ");} enum Option {EXIT, / / 0 ADD, / / 1 DEL, / / 2 SEARCH, / / 3 MODIFY, / / 4 SHOW / / 5}
two。 Write the main function main (), define input to represent the number to be entered, first call the menu function menu () in the do while loop, and then please enter a number, and then use the switch statement to correspond to the corresponding function. We said earlier that the members of the consortium represent the number 05th, so case ADD = = case case 1, and so on. The corresponding selection corresponds to the function of the corresponding function. If you select 1, enter case ADD, then call AddContact (), pass the parameter & con, and of course this function will be created later. What con is is also explained later.
Int main () {int input = 0; struct Contact con;// creates an address book InitContact (& con); / / initializes the address book do {menu (); printf ("Please select:"); scanf ("% d", & input); switch (input) {case ADD: AddContact (& con); break; case DEL: DelContact (& con); break; case SEARCH: SearchContact (& con); break; case MODIFY: ModifyContact (& con); break Case SHOW: ShowContact (& con); break; case EXIT: printf ("exit address book\ n"); break; default: printf ("selection error\ n"); break;}} while (input); return 0;}
3. Create the header file contact.h and define the structure types struct PeoInfo and struct Contact. The former is a person's information, creating his structure type variable represents the creation of a person's information; the latter is an address book data, creating a struct Contact type variable includes an struct PeoInfo type array data [] that stores 1000 data, and then defines a sz to calculate the number of people stored. Of course, we see that the symbols in the array are macros defined by # define to replace those numbers. The con you saw earlier is a struct Contact type variable created and initialized.
# define NAME_MAX 30#define SEX_MAX 5#define TELE_MAX 12#define ADDR_MAX 30#define MAX 1000 struct PeoInfo {char name [name _ MAX]; / / name int age; / / Age char sexes [SEX _ MAX]; / / gender char Telegraph [Tale _ MAX]; / / phone char addr [ADDR _ MAX]; / / address}; struct Contact {struct PeoInfo data [name]; / / data for 1000 people are stored in the data array int sz / / the number of currently valid elements in the address book}
4. Reference the header file and declare the function. At this point, contact.h is finished, because the header file already refers to # include and # include that you need to use next, so you can reference # include "contact.h" directly in test.c.
# define NAME_MAX 30#define SEX_MAX 5#define TELE_MAX 12#define ADDR_MAX 30#define MAX 1000 # include # include struct PeoInfo {char name [name _ MAX]; int age; char sexes [SEX _ MAX]; char Telegraph [Tale _ MAX]; char addr [ADDR _ MAX];}; struct Contact {struct PeoInfo data [MAX]; / 1000 people's data are stored in the data array the number of currently valid elements in the int sz;// address book}; void AddContact (struct Contact* pc) / / add contact void DelContact (struct Contact* pc); / / Delete specified contact void SearchContact (struct Contact* pc); / / find specified contact void ModifyContact (struct Contact* pc); / / modify specified contact void ShowContact (struct Contact* pc); / / Show all contacts
5. Write contact.c that is the specific implementation of the address book. Start with the header file # include "contact.h", and then write the initialization function InitContact (). Accept it with a structure pointer (1000 capacity). Assign sz to 0 and all members of the array to 0, using the memset function, the header file # include, which we have referenced before.
# include "contact.h" void InitContact (struct Contact* pc) {pc- > sz = 0 sizeof / default no message memset (pc- > data, 0, sizeof (pc- > data));}
6. Next, write the functions of each function:
AddContact () adds contacts and enters information to the corresponding member of the SZ bit of the array pointed to by the pointer. Sz starts with 0, which corresponds to the zero bit of the array.
Void AddContact (struct Contact* pc) {if (pc- > sz = = MAX) {printf ("address book full, unable to add information\ n");} else {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); printf ("add successfully\ n"); pc- > sz++;}}
DelContact () deletes the specified contact. If the sz is not 0, it means that there is information in the address book and can be deleted. After entering the name, you need to find it. Here, you can find it by writing a traversal to find the name function FindContactByName (), which uses the library function strcmp (string comparison function). If it is equal, it returns the array subscript pos, and then starts the cycle from the pos bit in DelContact (), assigns the value of the latter bit to the previous bit, indicating that the deletion is successful, and then sz--.
Int FindContactByName (const struct Contact* pc, const char* name) {int I = 0; for (I = 0; I
< pc->Sz; iBook +) {if (strcmp (pc- > data [I] .name, name) = 0) {return I;}} return-1;} void DelContact (struct Contact* pc) {if (pc- > sz = = 0) {printf ("address book is empty and cannot be deleted\ n"); return;} char name [name _ MAX] = {0}; printf ("Please enter the name of the person to be deleted:"); scanf ("% s", name) / / find int pos = FindContactByName (pc, name); if (pos = =-1) {printf ("specified contact does not exist\ n");} else {/ / delete int j = 0; for (j = pos; j)
< pc->Sz-1; jacks +) {pc- > data [j] = pc- > data [j + 1];} pc- > sz--; printf ("deleted successfully\ n");}}
SearchContact () finds the specified contact, calls FindContactByName () directly, and prints the information after finding it.
Void SearchContact (const struct Contact* pc) {char name [name _ MAX] = {0}; printf ("Please enter the name of the person you want to find:"); scanf ("% s", name); int pos = FindContactByName (pc, name); if (pos = =-1) {printf ("check no such person\ n") } else {printf ("s\ t% 5s\ t% 8s\ ts\ t% 30s\ n", "name", "age", "sex", "tele", "addr"); printf ("s\ t% 5d\ t% 8s\ ts\ t% 30s\ n", pc- > datapos] .name, pc- > datapos .age, pc- > datapos .sex, pc- > data [pos] .tele, pc- > data [pos] .addr);}}
ModifyContac () modifies the specified contact, looks it up using FindContactByName (), returns the array subscript pos if it exists, and then enters the new information to overwrite the pointer to the corresponding member of the array data with subscript pos.
Void ModifyContact (struct Contact* pc) {char name [name _ MAX] = {0}; printf ("Please enter the name of the person to be modified:"); scanf ("% s", name); int pos = FindContactByName (pc, name); if (pos = =-1) {printf ("the person to be modified does not exist\ n");} else {printf ("Please enter a new name:"); scanf ("% s", pc- > datapos] .name) Printf ("Please enter a new age:"); scanf ("% d", & (pc- > data [pos] .age)); printf ("Please enter a new gender:"); scanf ("% s", pc- > data [pos] .sex); printf ("Please enter a new phone number:"); scanf ("% s", pc- > datapos] .tele); printf ("Please enter a new address:") Scanf ("% s", pc- > data [pos] .addr); printf ("modified successfully");}}
ShowContact () displays all contacts, and the entry function first prints a row of headers, separated by "\ t", and then prints each person's data in a loop using for until the SZ individual stops.
Void ShowContact (struct Contact* pc) {int I = 0; printf ("s\ t% 5s\ t% 8s\ ts\ t% 30s\ n", "name", "age", "sex", "tele", "addr"); for (I = 0; I
< pc->Sz; iTunes +) {printf ("s\ t% 5d\ t% 8s\ ts\ t% 30s\ n", pc- > data [I] .name, pc- > data [I] .age, pc- > data [I] .sex, pc- > data [I] .tele, pc- > DataI [.addr);}}
At this point, the address book program has been completed, and all the code is shown below:
Test.c
# include "contact.h" void menu () {printf ("\ n"); printf ("* *\ n"); printf ("* 1. Add 2. Del *\ n"); printf ("* 3. Search 4.modify *\ n") Printf ("* 5.show 0. Exit *\ n "); printf (" * *\ n ");} enum Option {EXIT, ADD, DEL, SEARCH, MODIFY, SHOW}; int main () {int input = 0; struct Contact con;// creates an address book InitContact (& con); / / initializes the address book do {menu () Printf ("Please choose:"); scanf ("% d", & input); switch (input) {case ADD: AddContact (& con); break; case DEL: DelContact (& con); break; case SEARCH: SearchContact (& con); break; case MODIFY: ModifyContact (& con); break; case SHOW: ShowContact (& con); break; case EXIT: printf ("exit address Book\ n"); break Default: printf ("selection error\ n"); break;}} while (input); return 0;}
Contact.c
# include "contact.h" void InitContact (struct Contact* pc) {pc- > sz = 0 no message memset (pc- > data, 0, sizeof (pc- > data));} void AddContact (struct Contact* pc) {if (pc- > sz = = MAX) {printf ("address book full, unable to add information\ n");} else {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); printf ("added successfully\ n"); pc- > sz++;}} int FindContactByName (const struct Contact* pc, const char* name) {int I = 0; for (I = 0; I
< pc->Sz; iBook +) {if (strcmp (pc- > data [I] .name, name) = 0) {return I;}} return-1;} void DelContact (struct Contact* pc) {if (pc- > sz = = 0) {printf ("address book is empty and cannot be deleted\ n"); return;} char name [name _ MAX] = {0}; printf ("Please enter the name of the person to be deleted:"); scanf ("% s", name) / / find int pos = FindContactByName (pc, name); if (pos = =-1) {printf ("specified contact does not exist\ n");} else {/ / delete int j = 0; for (j = pos; j)
< pc->Sz-1; jackers +) {pc- > data [j] = pc- > data [j + 1];} pc- > sz--; printf ("deleted successfully\ n");}} void SearchContact (const struct Contact* pc) {char name [name _ MAX] = {0}; printf ("Please enter the person's name:"); scanf ("% s", name); int pos = FindContactByName (pc, name); if (pos =-1) {printf ("check no one\ n") } else {printf ("s\ t% 5s\ t% 8s\ ts\ t% 30s\ n", "name", "age", "sex", "tele", "addr"); printf ("s\ t% 5d\ t% 8s\ ts\ t% 30s\ n", pc- > datapos] .name, pc- > datapos .age, pc- > datapos .sex, pc- > data [pos] .tele, pc- > data.addr) } void ModifyContact (struct Contact* pc) {char name [name _ MAX] = {0}; printf ("Please enter the name of the person to be modified:"); scanf ("% s", name); int pos = FindContactByName (pc, name); if (pos = =-1) {printf ("the person to be modified does not exist\ n");} else {printf ("Please enter a new name:"); scanf ("% s", pc- > data [pos] .name) Printf ("Please enter a new age:"); scanf ("% d", & (pc- > data [pos] .age)); printf ("Please enter a new gender:"); scanf ("% s", pc- > data [pos] .sex); printf ("Please enter a new phone number:"); scanf ("% s", pc- > datapos] .tele); printf ("Please enter a new address:") Scanf ("% s", pc- > data [pos] .addr); printf ("modified successfully");}} void ShowContact (struct Contact* pc) {int I = 0; printf ("s\ t% 5s\ t% 8s\ ts\ t% 30s\ n", "name", "age", "sex", "tele", "addr"); for (I = 0; I
< pc->Sz; iTunes +) {printf ("s\ t% 5d\ t% 8s\ ts\ t% 30s\ n", pc- > data [I] .name, pc- > data [I] .age, pc- > data [I] .sex, pc- > data [I] .tele, pc- > DataI [.addr);}}
Contact.h
# define NAME_MAX 30#define SEX_MAX 5#define TELE_MAX 12#define ADDR_MAX 30#define MAX 1000 # include # include struct PeoInfo {char name [name _ MAX]; int age; char sexes [SEX _ MAX]; char Telegraph [Tale _ MAX]; char addr [ADDR _ MAX];}; struct Contact {struct PeoInfo data [MAX]; / 1000 people's data are stored in the data array the number of currently valid elements in the int sz;// address book}; void AddContact (struct Contact* pc) / / add contact void DelContact (struct Contact* pc); / / delete specified contact void SearchContact (struct Contact* pc); / / find specified contact void ModifyContact (struct Contact* pc); / / modify specified contact void ShowContact (struct Contact* pc); / / display all contacts "how to implement address book program in C language". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 223
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.