Investigating the family : Tree « Data Structure Algorithm « C / ANSI-C

C / ANSI-C
1. assert.h
2. Console
3. ctype.h
4. Data Structure Algorithm
5. Data Type
6. Development
7. File
8. Function
9. Language Basics
10. Macro Preprocessor
11. Math
12. math.h
13. Memory
14. Pointer
15. setjmp.h
16. signal.h
17. Small Application
18. stdio.h
19. stdlib.h
20. String
21. string.h
22. Structure
23. time.h
24. wctype.h
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C / ANSI-C » Data Structure Algorithm » TreeScreenshots 
Investigating the family

/*
Beginning C, Third Edition
 By Ivor Horton
 ISBN: 1-59059-253-0
 Published: Apr 2004
 Publisher: apress

*/

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

char myfile[] "C:\\myfile.bin"/* Physical file name      */
FILE *pfile = NULL;               /* File pointer            */

struct Date                       /* Structure for a date    */
{
   int day;
   int month;
   int year;
};

typedef struct family        /* Family structure declaration */
{
   struct Date dob;
   char name[20];
   char pa_name[20];
   char ma_name[20];
}Family;

/* Function prototypes */
int get_person(Family *pfamily);       /* Input function           */
void show_person_data(void);           /* Output function          */
void get_parent_dob(Family *pfamily);  /* Function to find pa & ma */

void main()
{
   Family member;                      /* Stores a family structure */
   Family *pmember = &member;     /* Points to the family structure */

   if((pfile = fopen(myfile, "wb")) == NULL)
   {
     printf("\nUnable to open %s for writing.\n", myfile);
     abort();
   }

   while(get_person(pmember))           /* As long as we have input */
     fwrite(pmember, sizeof member, 1, pfile);  /*    write it away */

   fclose(pfile);                 /* Close the file now its written */

   show_person_data();            /* Show what we can find out      */

   if(remove(myfile))
     printf("\nUnable to delete %s.\n", myfile);
   else
     printf("\nDeleted %s OK.\n", myfile);
}

/* Function to input data on Family members */
int get_personFamily *temp)
{
   static char more = '\0';    /* Test value for ending input */

   printf("\nDo you want to enter details of a%s person (Y or N)? ",
                                       more != '\0'?"nother " "" );
   scanf(" %c", &more);

   if(tolower(more== 'n'
          return 0;

   printf("\nEnter the name of the person: ");
   scanf("%s", temp->name);         /* Read the Family's name */

   printf("\nEnter %s's date of birth (day month year); ", temp->name);
   scanf("%d %d %d", &temp->dob.day, &temp->dob.month, &temp->dob.year);

   printf("\nWho is %s's father? ", temp->name);
   scanf("%s", temp->pa_name);      /* Get the father's name  */

   printf("\nWho is %s's mother? ", temp->name);
   scanf("%s", temp->ma_name);      /* Get the mother's name  */

   return 1;
}

/* Function to output data on people on file   */
void show_person_data(void)
{
   Family member;             /* Structure to hold data from file  */
   Family *pmember = &member; /* Pointer to Family structure       */
   fpos_t current = 0;        /* File position                     */

   pfile = fopen(myfile, "rb")/* Open file for binary read  */

   /* Read data on person */
   while(fread(pmember, sizeof member, 1, pfile))   
   {
     fgetpos(pfile, &current);  /* Save current position      */
     printf("\n\n%s's father is %s, and mother is %s.",
              pmember->name, pmember->pa_name, pmember->ma_name);
     get_parent_dob(pmember);   /* Get parent data            */
     fsetpos(pfile, &current);  /* Position file to read next */
   }
    fclose(pfile);              /* Close the file             */
}

/* Function to find parents' dates of birth. */
void get_parent_dob(Family *pmember)
{
   Family testmem;             /* Stores a relative         */
   Family *ptestmem = &testmem; /* Pointer to the relative   */
   int num_found = 0;           /* Count of relatives found  */

   rewind(pfile);               /* Set file to the beginning */

   /* Get the stuff on a relative */
   while(fread(ptestmem, sizeof(Family)1, pfile))
   {
     if(strcmp(pmember->pa_name, ptestmem->name== 0)   /*Is it pa? */
     /* We have found dear old dad */
       printf("\n Pa was born on %d/%d/%d.",
             ptestmem->dob.day, ptestmem->dob.month, ptestmem->dob.year);  
 
       if(++num_found == 2)    /* Increment parent count    */
         return;               /* We got both so go home    */
     }
     else
       if(strcmp(pmember->ma_name, ptestmem->name== 0/*Is it ma? */
       /* We have found dear old ma */
         printf("\n Ma was born on %d/%d/%d.",
                ptestmem->dob.day, ptestmem->dob.month, ptestmem->dob.year);

         if(++num_found == 2)  /* Increment parent count    */
             return;           /* We got both so go home    */
       }
   }
}


           
       
Related examples in the same category
1. Basics of a family tree 2
2. Basics of a family tree
3. Displays a binary tree
w_w_w___.__j_a__v_a_2_s._c__om___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.