To implement a telephone book with a structural body

Source: Internet
Author: User

structure is a C language and a type of storage data, then how to implement a structure of a simple can store 1000 people information of the phone book?

First, you need to define a structure, including a person's information (such as name, gender, age, telephone, address, etc.). Let's start by defining a struct.

typedef struct PDHB-INFO {char name[5];      Char sex[3];      int age;      Char tele[12];    Char addr[30]; }pdhb-info;

two. Need to store 1000 person's information, then means define a struct, member is an array of type pdhb-info, size is 1000.

typedef struct DHB {pdhb-info Pinfo[max];  Defines an array of type pdhb-info, size 1000, array named pinfo int count; Array element}DHB,*PDHB will be accessed as an array subscript; Define a pointer to this struct body

three. How the main function is implemented

         #define  _CRT_SECURE_NO_WARNINGS 1          #include <stdio.h>     #include <stdlib.h>      #include "contact.h"   //Add a header file that you wrote          Int main ()     {    dhb dhb;  //object of the struct that created the array   &NBSP;&NBSP;&NBSP;INT&NBSP;INPUT&NBSP;=&NBSP;1;&NBSP;&NBSP;&NBSP;&NBSP;INIT_DHB (&AMP;DHB);     while  (Input)         {    menu ();   //Print a Start menu      scanf ("%d",  &input);//get user input numbers      switch  (Input)   //Select the executed program     {    case ADD:  &NBSP;&NBSP;&NBSP;ADD_DHB (&AMP;DHB);    break;    case del:     dEL_DHB (&AMP;DHB);    break;    case search:    &NBSP;SEARCH_DHB (&AMP;DHB);    break;    case modify:   &NBSP;&NBSP;MODIFY_DHB (&AMP;DHB);    break;    case show:  &NBSP;&NBSP;&NBSP;SHOW_DHB (&AMP;DHB);     break;    case clear: &NBSP;&NBSP;&NBSP;&NBSP;CLEAR_DHB (&AMP;DHB);    break;    case  SORT:&NBSP;&NBSP;&NBSP;&NBSP;SORT_DHB (&AMP;DHB);     break;    case  exit:    exit (exit_success);     break;    }     }    return 0;    }

     Four. Contents of the header file

     #ifndef  __CONTACT_H__             #define  __CONTACT_H__    Define header file          # define max_name 20     #define  MAX_SEX 3     #define  MAX_TELE 13     #define  MAX_ADDR 20          #define  MAX 1000        enum OP    Enumeration Type     {    exit,  //0    add,   //1    del,   //2    search,  //3     MODIFY,  //4    SHOW,    //5     CLEAR,   //6    SORT    //7     };        typedef struct peo_info    {     char name[MAX_NAME];    char sex[MAX_SEX];     int age;    char tele[max_tele];    char addr[max _addr];    }peo_info;        typedef struct  dhb    {    peo_info pinfo[max];    int  count;    }dhb,*pdhb;        void menu ( ); The      //function's reputation &NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;INIT_DHB (PDHB&NBSP;PDHB);   &NBSP;&NBSP;VOID&NBSP;ADD_DHB (PDHB&NBSP;PDHB); &NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;DEL_DHB (PDHB&NBSP;PDHB);  &NBSP;&NBSP;&NBSP;VOID&NBSP;SEARCH_DHB (PDHB&NBSP;PDHB); &NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;MODIFY_DHB (pDhb  PDHB);  &nbsP;&NBSP;&NBSP;VOID&NBSP;SHOW_DHB (PDHB&NBSP;PDHB); &NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;CLEAR_DHB (PDHB&NBSP;PDHB); &NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;SORT_DHB (PDHB&NBSP;PDHB);              #endif  //__contact_h__

     Five. Implementation of the function

     #define  _CRT_SECURE_NO_WARNINGS 1              #include <stdio.h>     #include "contact.h"          void menu ()    //menu function Implementation     {     printf ("***************************************\n");     printf ("* * * *  1.add     **********  2.del ******\n ");     printf ("**** 3.search  ********** 4.modify*****\n");     printf ("****5. Show     **********  6.clear*****\n ");     printf (" * * * * 7.sort     **********  0.exit******\n ");     printf (" * * * \ n ");    }         static int find_entry (pdhb pdhb, const char*name)   //Find the function implementation of subscript     {     int i = 0;    for  (i = 0; i  < pdhb->count; i++)     {    if  (0 == &NBSP;STRCMP (name, pdhb->pinfo[i].name))     {    return i ;    }    }    return -1;    &NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;INIT_DHB (PDHB&NBSP;PDHB)    // Initialize     {    pdhb->count = 0;    } Implementation of &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;ADD_DHB (PDHB&NBSP;PDHB)    //add function     {    if  (Pdhb->count >= max)     //judge whether the number is greater than 1000    {    printf ("The phone book is full \ n");    return;     }    printf ("Please enter information \ n"),     printf ("name:->");       //assigns a value     scanf ("%s",  pdhb->pinfo[pdhb->count].name) to the array element;     printf ("Gender:->"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", pdhb->pinfo[pdhb-> Count].sex);     printf ("Age:->"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &pdhb-> Pinfo[pdhb->count].age);     printf ("Phone:->"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s",  pdhb->pinfo[pdhb->count].tele)     printf ("Address:->");     scanf ("%s",  pdhb->pinfo[pdhb->count].addr);    pdhb->count++;     printf ("Add success \ n"); &NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;DEL_DHB (PDHB&NBSP;PDHB The implementation of the    //delete function &Nbsp;   {    char name[max_name];    int ret  = 0;    printf ("Enter the name to delete \ n"),     scanf ("%s",  name);     ret = find_entry (pdhb, name),  //find the corresponding subscript, the bug in this program is that the name of the duplicate will only delete the subscript of the previous names     if  (ret == -1)     {    printf (" Name does not exist \ n ");    return;    }    else    //overwrites the contents of the preceding element directly with the trailing element of the found subscript     {    int j = 0;     for  (j = ret; j < pdhb->count-1; j++)      {    pdhb->pinfo[j] = pdhb->pinfo[j + 1];     }    pdhb->count--;    printf ("Delete succeeded \ n");     } &nbImplementation of SP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;SEARCH_DHB (PDHB&NBSP;PDHB)    //lookup function      {    char name[MAX_NAME];    int ret  = 0;    printf ("Input search name \ n"),     scanf ("%s",  name);     ret = find_entry (Pdhb, name)   //also find subscript     if   (ret == -1)     {    printf ("name does not exist \ n");     return;    }    else   will find the output of the element corresponding to the subscript      {    printf ("%10s\t%5s\t%4s\t%10s\t%10s\n",  "name",  "Sex",  "age ", " Tele ", " addr ")     printf ("%10s\t%5s\t%3d\t%10s\t%10s\n ",     pdhb->pinfo[ret].name,    pdhb->pinfo[ret].sex,    pdhb- >pinfo[ret].age,     pdhb->pinfo[ret].tele,    pdhb->pinfo[ret].addr);   &NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;MODIFY_DHB (PDHB&NBSP;PDHB)    Change of personal information     {    char name[max_name];    int  ret = 0;    printf ("Enter the name to be modified \ n"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s",  name);     ret = find_entry (pdhb, name);   //first find subscript      if  (ret == -1)     {    printf ("name does not exist \ n");     return;    }    else    // Let's re-assign it.     {    printf ("name:->"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s") ,  pdhb->pinfo[ret].name);     printf ("Gender:->"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s" ,  pdhb->pinfo[Ret].sex);     printf ("Age:->"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &pdhb-> Pinfo[ret].age);     printf ("Phone:->"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", pdhb-> Pinfo[ret].tele);     printf ("Address:->"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", pdhb-> PINFO[RET].ADDR);     printf ("Modified successfully");     }    } &NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;SHOW_DHB (PDHB&NBSP;PDHB)    //Show stored information     {     int i = 0;    printf ("%10s\t%5s\t%4s\t%10s\t%10s\n" ,  "name",  "Sex",  "age",  "Tele",  "addr");    for  (i =  0; i < pdhb->count; i++)     {     printf ("%10s\t%5s\t%3d\t%10s\t%10s\n",    pdhb->pinfo[i].name,     Pdhb->pinfo[i].sex,    pdhb->pinfo[i].age,    pdhb->pinfo[i].tele,    &NBSP;PDHB-&GT;PINFO[I].ADDR);    }    }      &NBSP;&NBSP;&NBSP;VOID&NBSP;CLEAR_DHB (PDHB&NBSP;PDHB)   //clear the phone book   just set Conunt to 0;     {    pdhb->count = 0;    }    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;SORT_DHB (PDHB&NBSP;PDHB)   //bubble sort, sort by name      {    int i = 0;    int j = 0;     for  (i = 0; i < pdhb->count; i++)      {    for  (j = 0; j < pdhb->count -  1 - i; j++)     {    if  (strcmp (pdhb-> pinfo[j].name, pdhb->pinfo[J + 1].name)  >0 )     {    peo_info tmp  = pdhb->pinfo[j];    pdhb->pinfo[j] = pdhb->pinfo[j+1];     pdhb->pinfo[j + 1] = tmp;    }     }    }        }

The above is my study in the process of some experience summary. Of course, my ability is limited, there will inevitably be flaws, I hope you can correct me.

This article is from the "make a small driver" blog, please be sure to keep this source http://10799170.blog.51cto.com/10789170/1718562

To implement a telephone book with a structural body

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.