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 / enumindx.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  3KB  |  103 lines

  1. /*               enumindx.c
  2.  *
  3.  *   Synopsis  - Accepts input of the number sold of each color
  4.  *               of an item, stores the number in an array and
  5.  *               displays the contents of the array.
  6.  *   Objective - Illustrates the use of enum types for indexing
  7.  *               an array and the underlying connection between
  8.  *               enum types and ints.
  9.  */
  10.  
  11. /* Include Files */
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. /* Constant Definitions */
  16. #define REC_SIZE  5
  17. #define C_LGTH    6
  18.  
  19. /* Type Descriptions */
  20. enum color { jade, navy, white, coral, maize };       /* Note 1 */
  21.  
  22. /* Function Prototypes*/
  23. void makestring( enum color, char *string );          /* Note 2 */
  24. /*   PRECONDITION:  string contains the address of a character array 
  25.  *                  which must be declared by the calling function.
  26.  *   POSTCONDITION: Creates a string containing the color currently
  27.  *                  contained in item_color. Used to aid output.
  28.  */
  29.  
  30. void print_sales( int sales[] , int num_sales );
  31. /*   PRECONDITION:  sales[] in an array of integers with num_cell cells.
  32.  *   POSTCONDITION: Will display the color and contents of each cell 
  33.  *                  of sales[]. Uses the integer representation of the 
  34.  *                  colors.
  35.  */
  36.  
  37. void get_sales( int *salesrecord );
  38. /*   PRECONDITION:  salesrecord contains the address of an array of ints 
  39.  *                  which must be declared by the calling function.
  40.  *   POSTCONDITION: Accepts input of the number sold of each color 
  41.  *                  item from standard input. Uses the identifier form 
  42.  *                  of the color.
  43.  */
  44.  
  45. int main( void )
  46. {
  47.      int salesrecord[REC_SIZE];
  48.  
  49.      get_sales( salesrecord );
  50.      print_sales( salesrecord, REC_SIZE );
  51.      return 0;
  52. }
  53.  
  54. /*******************************makestring()********************/
  55.  
  56.                                                      /* Note 3 */
  57. void makestring( enum color item_color, char *string )
  58. {
  59.      switch ( item_color ) {                         /* Note 4 */
  60.           case jade:   strcpy( string, "jade" );
  61.                        break;
  62.           case navy:   strcpy( string, "navy" );
  63.                        break;
  64.           case white:  strcpy( string, "white" );
  65.                        break;
  66.           case coral:  strcpy( string, "coral" );
  67.                        break;
  68.           case maize:  strcpy( string, "maize" );
  69.     }
  70. }
  71.  
  72. /*******************************print_sales()*******************/
  73.  
  74. void print_sales( int sales[], int num_cells )
  75. {
  76.      int i;
  77.      char colorstr[C_LGTH];
  78.  
  79.      printf( "The sales record shows the following " );
  80.      printf( "sales by color.\n" );
  81.  
  82.      for( i = 0; i < num_cells; i++ ) {
  83.           makestring( i, colorstr );                 /* Note 5 */
  84.           printf( "%5s : %d\n", colorstr, sales[i] );
  85.      }
  86. }
  87.  
  88. /*******************************get_sales()*********************/
  89.  
  90. void get_sales( int *salesrecord )
  91. {
  92.      enum color d_color;
  93.      char colorstr[C_LGTH];
  94.  
  95.      printf( "Enter the number of each color sold:\n" );
  96.                                                      /* Note 6 */
  97.      for ( d_color = jade; d_color <= maize; d_color++ ) {
  98.           makestring( d_color, colorstr );           /* Note 7 */
  99.           printf( "%5s : ", colorstr );
  100.                                                      /* Note 8 */
  101.           scanf( "%d", &salesrecord[d_color] );
  102.      }
  103. }