"C Language" easy Address Book

Source: Internet
Author: User
Tags strcmp

We use multiple files for writing to implement "Contact.h" #ifndef  __CONTACT_H__#define __CONTACT_H__#define MAX_NAME    20#define max_sex 3#define max_tele 12#define max_addr 20#define max  1000#define INIT_SIZE 100#define INC 100#include <stdio.h> #include   <string.h> #include  <stdlib.h>enum OP{    EXIT,     ADD,    DEL,    SEARCH,    MODIFY,     show,    clr,    sort};typedef struct peo_ info{    char name[max_name];    char sex[max_sex];     int age;    char tele[max_tele];    char &NBSP;ADDR[MAX_ADDR];} Peo_info;typedef struct dhb{    peo_info *pinfo;    int &nbsP;count;    int size;} Dhb,*pdhb;void menu (); VOID&NBSP;INIT_DHB (PDHB&NBSP;PDHB); VOID&NBSP;ADD_DHB (PDHB&NBSP;PDHB); void del_ DHB (PDHB&NBSP;PDHB); VOID&NBSP;SEARCH_DHB (PDHB&NBSP;PDHB); VOID&NBSP;MODIFY_DHB (PDHB&NBSP;PDHB); void show_ DHB (PDHB&NBSP;PDHB); VOID&NBSP;CLEAR_DHB (PDHB&NBSP;PDHB); VOID&NBSP;SORT_DHB (PDHB&NBSP;PDHB); Void save (PDHB &NBSP;PDHB); #endif  //__contact_h__ "contact.c" #include   "Contact.h" Void menu () {     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*&Nbsp;name) {    int i = 0;    for  (i = 0;  i < pdhb->count; i++)     {         if  (0 == strcmp (name, pdhb->pinfo[i].name))          {            return i;         }    }    return  -1;} Static void init_add (PDHB&NBSP;PDHB,&NBSP;PEO_INFO&NBSP;*PEO) {    if  (( Pdhb->count >= pdhb->size)  &&  ((Pdhb->size < max))      {        if  (pdhb->size + inc  > max)         {             pdhb->size = max - pdhb->size;         }        else if  (pdhb->size + inc  <= max)         {             pdhb->size = pdhb->size + INC;         }        pdhb->pinfo = realloc ( Pdhb->pinfo, pdhb->size*sizeof (Peo_info));        if  ( Pdhb->pinfo == null)         {             printf ("out of memory\n");             exit (1);         }     }    else if  ((Pdhb->size >= max))     {         printf ("phone book is full \");        return;     }    pdhb->pinfo[pdhb->count] = *peo;     pdhb->count++;} VOID&NBSP;INIT_DHB (PDHB&NBSP;PDHB) {    peo_info buf;    file  *pfread = fopen ("Contact.dat", "R");    if  (Pfread == null)     {        perror ("open file for  Read ");         exit (exit_failure);    }     pdhb->count = 0;    pdhb->size = INIT_SIZE;     pdhb->pinfo =  (peo_info *) malloc ((pdhb->size)  *sizeof (Peo_info));    if  (pdhb->pinfo == null)     {         printf ("out of memory\n");         exit (1);    }        while  (Fread (&buf, sizeof (peo_info),  1, pfread)     {         init_add (PDHB,&NBSP;&AMP;BUF);     }    fclose ( Pfread);} VOID&NBSP;ADD_DHB (PDHB&NBSP;PDHB) {    if  ((pdhb->count >= pdhb-> Size)  &&  ((Pdhb->size < max))     {         if  (Pdhb->size + inc > max)          {            pdhb- >size =&nbsP max - pdhb->size;        }         else if  (Pdhb->size + inc <= max)          {            pdhb- >size = pdhb->size + INC;        }         pdhb->pinfo = realloc (pdhb->pinfo, pdhb-> Size*sizeof (Peo_info));        if  (pdhb->pinfo ==  NULL)         {             printf ("out of memory\n");             exit (1);        }    }     else if&nbSP; ((Pdhb->size >= max))     {         printf ("phone book is full \");        return;    }     printf ("name:>"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s",  pdhb->pinfo[pdhb->count]. name);     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");} VOID&NBSP;DEL_DHB (PDHB&NBSP;PDHB) {&NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;NAME[MAX_NAME];&NBSP;&NBSP;&NBSP;&Nbsp;int ret = 0;    printf ("Please enter the name of the person to be deleted:>");     scanf ("%s",  name);     ret = find_entry (Pdhb, name);     if  (ret == -1)     {         printf ("The person to remove does not exist \ n");        return;    }     else    {        int j  = 0;        for  (j = ret; j <  pdhb->count-1; j++)         {             pdhb->pinfo[j] = pdhb->pinfo[j+1];         }        pdhb->count--;         printf ("Delete succeeded \ n"); &NBSP;&NBSP;&NBSP;&NBSP;}}VOID&NBSP;SEARCH_DHB (PDHB&NBSP;PDHB) {     char name[MAX_NAME];    int ret = 0;     printf ("Please enter the name of the person you want to find:>"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s",  name);     ret =  find_entry (Pdhb, name);    if  (ret == -1)      {        printf ("The person you are looking for does not exist \ n");         return;    }    else    {         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,            &NBSP;PDHB-&GT;PINFO[RET].ADDR); &NBSP;&NBSP;&NBSP;&NBSP;}}VOID&NBSP;MODIFY_DHB (PDHB&NBSP;PDHB) {     char name[MAX_NAME];    int ret = 0;     printf ("Please enter the name of the person to be modified:>"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s",  name);     ret  = find_entry (Pdhb, name);    if  (ret == -1)      {        printf ("The person to be modified does not exist \ n");         return;    }    else    {         printf ("name:>"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s",  pdhb->pinfo[ Ret].name);         printf ("Gender:>");      &NBSP;&NBSP;&NBSP;SCANF ("%s",  pdhb->pinfo[ret].sex);         printf ("Age:>"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d",  &pdhb->pinfo[ret]. age);         printf ("Phone:>");       &NBSP;&NBSP;SCANF ("%s",  pdhb->pinfo[ret].tele);         printf ( "Address:>"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s",  pdhb->pinfo[ret].addr);  &NBSP;&NBSP;&NBSP;}}VOID&NBSP;SHOW_DHB (PDHB&NBSP;PDHB) {    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);     }    printf ("sz = %d\n",  pdhb->size);} VOID&NBSP;CLEAR_DHB (PDHB&NBSP;PDHB) {    pdhb->count = 0;} VOID&NBSP;SORT_DHB (PDHB&NBSP;PDHB) {    int i = 0;     int j = 0;    for  (i = 0; i < pdhb->count - 1; 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;            }         }    }}void save (PDHB&NBSP;PDHB) {    FILE * pfwrite = fopen ("Contact.dat",  "W");    if  (pfwrite ==  null)     {        perror ("Open file  for write ");         exit (exit_failure);     }    int i = 0;    for  (i = 0;  i < pdhb->count; i++)     {         fwrite (&pdhb->pinfo[i],sizeof (peo_info),  1, pfwrite);     }     fclose (pfwrite);} " TEST.c "#include  <stdlib.h> #include  " Contact.h "Int main () {   &nbsP;DHB&NBSP;DHB;&NBSP;&NBSP;&NBSP;&NBSP;INT&NBSP;INPUT&NBSP;=&NBSP;1;&NBSP;&NBSP;&NBSP;&NBSP;INIT_DHB (&AMP;DHB) ;    while  (Input)     {         menu ();         printf ("Please choose;:");      &NBSP;&NBSP;&NBSP;SCANF ("%d",  &input);        switch  ( Input)         {        case &NBSP;ADD:&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ADD_DHB (&AMP;DHB);             break;       &NBSP;&NBSP;CASE&NBSP;DEL:&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;DEL_DHB ( &AMP;DHB);            break;          CASE&NBSP;SEARCH:&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SEARCH_DHB (&AMP;DHB) ;            break;         case MODIFY:             MODIFY_DHB (&AMP;DHB);            break;         case SHOW:         &NBSP;&NBSP;&NBSP;&NBSP;SHOW_DHB (&AMP;DHB);             break;        case CLR:       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CLEAR_DHB (&AMP;DHB);             break;        case SORT:         &NBSP;&NBSP;&NBSP;&NBSP;SORT_DHB (&AMP;DHB);             break;        case EXIT:         {            save (&AMP;DHB);             free (Dhb.pinfo);             exit (exit_success);             break;        }                     }     }    return 0;}

This article from "vs LV" blog, declined reprint!

"C Language" easy Address Book

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.