C struct implements an address book

Source: Internet
Author: User

C struct implements an address book
Question: an address book can be used to store information of 1000 people. Each person's information includes: name, gender, age, phone number, and address. Method: 1. add Contact Information 2. delete the information of a specified contact. 3. query the information of a specified contact. 4. modify the information of a specified contact. show all contacts 6. clear all contacts 7. analysis on how to sort all contacts by name: 1. first, we can solve this problem in three modules. The first module requires a header file, which can contain some relevant information, when the implementation and test files contain their own defined header files, you can obtain some relevant information. Therefore, the header file should contain a struct that contains the name, gender, age, phone number, and address. You can also define a struct that contains the address book and the counting variable of the person in the address book. You can easily traverse the address book or perform other operations. 2. the second module is our test function. The test function can print the menu and receive different values to implement different operations. This is the implementation of the corresponding method, it is obvious that a switch statement can be used for control. 3. The third module is the function implemented by our method. The type defined in Module 2 is the address book address to each method, so that you can perform operations on the address book. Code implementation: Module 1:

# Pragma oncetypedef struct Peo // each person's information is received in a single structure, including name, gender, age, phone number, address {char name [20]; char sex [4]; int age; char tell [12]; char address [20];} Peo; typedef struct Contact // declare the address book and personnel count in the struct {Peo Dhb [1000]; int count;} Contact; void add_stu (Contact * p ); // function void del_stu (Contact * p); void find_stu (Contact * p); void modify_stu (Contact * p); void show_stu (Contact * p ); void clear_stu (Contact * p); void sort_stu (Contact * p );

 

Module 2: (test function)
# Include <stdio. h> # include "contact. h "# include <stdlib. h> Contact con; // The type is the Contact variable, which contains the telephone and counting variables void menu () {printf ("menu: \ n "); printf ("************ 1. add contact information ************ \ n "); printf (" *********** 2. deletes the specified contact information ********** \ n "); printf (" ********** 3. find the specified contact information ********** \ n "); printf (" ********** 4. modify the specified contact information ********** \ n "); printf (" ********** 5. display all contact information ********** \ n "); printf (" ************* 6. clear contact information ************ \ n "); printf (" ******* 7. sort all contacts by name ******** \ n ");} void test () // test function {int input = 1; menu (); while (input) {printf ("Enter the option:"); scanf_s ("% d", & input); switch (input) // implementation of the method selection function {case 1: add_stu (& con); break; case 2: del_stu (& con); break; case 3: find_stu (& con ); break; case 4: modify_stu (& con); break; case 5: show_stu (& con); break; case 6: clear_stu (& con); break; case 7: sort_stu (& con); break; case 0: exit (1); break ;}} int main () {test (); return 0 ;}

 

Module 3: (implementation of methods)
# Include "contact. h "# include <string. h> int search (Contact * p, char * pname) // The sub-assembly function facilitates deletion and search. The implementation of the modification method {int ret; int I; for (I = 0; I <p-> count; I ++) {if (strcmp (p-> Dhb [I]. name, pname) = 0) return I;} return-1;} void add_stu (Contact * p) // Add the Contact {printf ("Enter name :"); scanf ("% s", p-> Dhb [p-> count]. name); printf ("Enter Gender:"); scanf ("% s", p-> Dhb [p-> count]. sex); printf ("Enter age:"); scanf ("% d", & p-> Dhb [p-> count]. age); printf ("Enter the phone number:"); s Canf ("% s", p-> Dhb [p-> count]. tell); printf ("Enter the address:"); scanf ("% s", p-> Dhb [p-> count]. address); p-> count ++; printf ("Contact added successfully \ n");} void del_stu (Contact * p) // Delete the Contact {int ret; int n; int I = 0; char name [20]; printf ("enter the name of the person to delete:"); scanf ("% s", name ); ret = search (p, name); if (ret! =-1) {printf ("the person to be deleted exists \ n"); printf ("are you sure you want to delete this contact? \ N "); printf (" Confirm input 1, do not delete input 0 \ n "); scanf (" % d ", & n); if (n = 1) {for (I = 0; I <p-> count; I ++) {p-> Dhb [I] = p-> Dhb [I + 1]; // enable the elements after I to overwrite them forward.} printf ("deleted successfully \ n");} else {printf ("failed to delete \ n ");}}} void find_stu (Contact * p) // find the Contact {int ret; char name [20]; printf ("enter the name of the person to be searched :"); scanf ("% s", name); ret = search (p, name); if (ret! =-1) {printf ("the person to be searched exists \ n"); printf ("% s", p-> Dhb [ret]. name); printf ("% s", p-> Dhb [ret]. sex); printf ("% d", p-> Dhb [ret]. age); printf ("% s", p-> Dhb [ret]. tell); printf ("% s", p-> Dhb [ret]. address) ;}} void modify_stu (Contact * p) // modify the Contact {int ret; char name [20]; printf ("enter the name of the person to modify :"); scanf ("% s", name); ret = search (p, name); if (ret! =-1) {printf ("please change the name to:"); scanf ("% s", p-> Dhb [ret]. name); printf ("change gender to:"); scanf ("% s", p-> Dhb [ret]. sex); printf ("Change your age to:"); scanf ("% d", & p-> Dhb [ret]. age); printf ("change the phone number to:"); scanf ("% s", p-> Dhb [ret]. tell); printf ("change the address to:"); scanf ("% s", p-> Dhb [ret]. address);} else {printf ("This person \ n does not exist");} void show_stu (Contact * p) // display the Contact information {int I = 0; printf ("output everyone information:"); for (I = 0; I <(p-> count); I ++) {printf ("% s ", p-> Dhb [I]. name); printf ("% s", p-> Dhb [I]. sex); printf ("% d", p-> Dhb [I]. age); printf ("% s", p-> Dhb [I]. tell); printf ("% s", p-> Dhb [I]. address);} printf ("\ n");} void clear_stu (Contact * p) // clear the Contact {p-> count = 0; // It can be cleared if the variable of the person count is set to 0} void sort_stu (Contact * p) // sort all contacts by name {int I = 0; int j = 0; for (I = 0; I <p-> count-1; I ++) // Bubble Method for (j = 0; j <p-> count-1-I; j ++) {if (strcmp (p-> Dhb [j]. name, p-> Dhb [j + 1]. name)> 0) {Peo tmp; tmp = p-> Dhb [j]; p-> Dhb [j] = p-> Dhb [j + 1]; p-> Dhb [j + 1] = tmp ;}} void show_stu (p );}

 

Summary: This question only utilizes the knowledge of relevant structures. The main difficulty is that the division of labor between the three modules is hard to think about. Other convenience is just the implementation of the basic method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.