home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch7 / unions.c < prev   
C/C++ Source or Header  |  2005-06-16  |  3KB  |  102 lines

  1. /*               unions.c
  2.  *
  3.  *   Synopsis  - Accepts input of and displays information for 
  4.  *               one employee.
  5.  *
  6.  *   Objective - To demonstrate use of enum types to keep track
  7.  *               of the contents of a union.
  8.  */
  9.  
  10. /* Include Files */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. /* Constant Definitions */
  15. #define NAME_LGTH   20
  16. #define SSN_LGTH    12
  17. #define BUF_LGTH    80
  18.  
  19. /* Type Descriptions */
  20. enum paytype { hourly, salaried };               /* Note 1 */
  21.  
  22. union rate {                                     /* Note 2 */
  23.      double per_hour;
  24.      int   per_week;
  25. };
  26.  
  27. struct employee {
  28.      char  name[NAME_LGTH];
  29.      char  ssn[SSN_LGTH];
  30.      enum  paytype paytype;                      /* Note 3 */
  31.      union rate payrate;
  32. };
  33.  
  34. /* Function Prototypes */
  35. void reademp( struct employee *emptr );
  36. /*   PRECONDITION:  emptr contains the address of a struct employee.
  37.  *   POSTCONDITION: Accepts input information from the keyboard and 
  38.  *                  stores it in a struct employee.
  39.  */
  40.  
  41.  
  42. void printemp( struct employee emp );
  43. /*   PRECONDITION:  emp is a struct employee.
  44.  *   POSTCONDITION: Displays the contents of the struct employee 
  45.  *                  parameter.
  46.  */
  47.  
  48. int main( void )
  49. {
  50.      struct employee emp;
  51.      reademp( &emp );
  52.      printemp( emp );
  53.      return 0;
  54. }
  55.  
  56. /*******************************reademp()***********************/
  57. void reademp( struct employee *emptr )
  58. {
  59.      char instring[BUF_LGTH];
  60.  
  61.      printf( "Enter the information for an employee.\n" );
  62.      printf( "Name : " );
  63.      fgets( emptr->name, NAME_LGTH, stdin );
  64.      emptr->name[ strlen(emptr->name)-1 ] = '\0';
  65.      printf( "Social Security Number: " );
  66.      fgets( emptr->ssn, SSN_LGTH, stdin );
  67.      emptr->ssn[ strlen(emptr->ssn)-1 ] = '\0';
  68.      printf( "Hourly or salaried (h or s) : " );
  69.      fgets( instring, BUF_LGTH, stdin );
  70.                                                     /* Note 4 */
  71.      if ( ( *instring == 'h' ) || ( *instring == 'H' ) )
  72.           emptr->paytype = hourly;
  73.      else if ( ( *instring == 's' ) || ( *instring == 'S' ) )
  74.           emptr->paytype = salaried;
  75.      else {
  76.           printf( "Illegal paytype, program terminated.\n" );
  77.           exit( 1 );                                /* Note 5 */
  78.      }
  79.      if ( emptr->paytype == hourly ) {              /* Note 6 */
  80.           printf( "Enter the hourly rate: " );
  81.           fgets( instring, BUF_LGTH, stdin );
  82.           emptr->payrate.per_hour = atof( instring );
  83.      }
  84.      else {
  85.           printf( "Enter the weekly salary: " );
  86.           fgets( instring, BUF_LGTH, stdin );
  87.           emptr->payrate.per_week = atoi( instring );
  88.      }
  89. }
  90.  
  91. /*******************************printemp()**********************/
  92.  
  93. void printemp( struct employee emp )
  94. {
  95.      printf( "Name :   %s\n", emp.name );
  96.      printf( "Social Sec. No. : %s\n", emp.ssn );
  97.      if ( emp.paytype == hourly )                    /* Note 7 */
  98.           printf( "Hourly rate   :$%7.2f\n", emp.payrate.per_hour );
  99.      else
  100.      printf( "Weekly Salary :$%7d\n", emp.payrate.per_week);
  101. }
  102.