home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch6 / mergex.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  4KB  |  127 lines

  1. /*               mergex.c
  2.  *   Synopsis  - Allows a user to input elements into an array,
  3.  *               sorts the array, and displays it in sorted order.
  4.  *
  5.  *   Objective - To provide a program to test the merge sort
  6.  *               algorithm.
  7.  */
  8.  
  9. /* Include Files */
  10. #include "mergsort.h"                            /* Note 1 */
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include "mergsort.c"                            /* Note 2 */
  14.  
  15. /* Constant Definitions */
  16. #define DONE       0                             /* Note 3 */
  17. #define MORE       1
  18. #define ERROR     -1
  19. #define BUF_SIZE  80
  20.  
  21. /* Function Prototypes */
  22. void print_array( int array[], int first, int num);
  23. /*   PRECONDITION:  array is the array of ints to be displayed. first 
  24.  *                  and num are integer indices for array.
  25.  *
  26.  *   POSTCONDITION: Displays the elements of an array of ints from the
  27.  *                  position with index first to the position with 
  28.  *                  index num, inclusive.
  29.  */
  30.  
  31. int fill_array( int empty[], int max_array );
  32. /*   PRECONDITION:  empty is the integer array to fill.
  33. *
  34.  *   POSTCONDITION: Allows the user to enter integers for an array of
  35.  *                  integers. Returns the number of elements entered
  36.  *                  into the array.
  37.  */
  38.  
  39. int get_int( int *intptr );
  40. /*   PRECONDITION:  intptr contains the address of the integer 
  41.  *                  variable that will hold the input.
  42.  *
  43.  *   POSTCONDITION: Accepts input of an integer from standard input.
  44.  *                  Returns the integer in the location pointed to by 
  45.  *                  intptr, and returns DONE if user signals quit, 
  46.  *                  ERROR if a nondigit was seen, or MORE if neither 
  47.  *                  of the above occurred.
  48.  */
  49. int main( void )
  50. {
  51.     int num;
  52.     int array[MAX_ARRAY];
  53.  
  54.     num = fill_array( array, MAX_ARRAY );
  55.     merge_sort( array, 0, num-1 );
  56.     print_array( array, 0, num-1 );
  57.     return 0;
  58. }
  59.  
  60. /*******************************fill_array()********************/
  61.  
  62. int fill_array( int empty[], int max_array)
  63. {
  64.     int    count = -1,
  65.     more = MORE;
  66.  
  67.     printf( "Enter your integers now. Enter 'Q' to quit\n" );
  68.     printf( "%3d : ", ++count + 1 );
  69.  
  70.     while  ( count < max_array) {
  71.         more = get_int( empty + count );
  72.  
  73.         if ( more == ERROR ) {                         /* Note 4*/
  74.             printf( "Error in input - Try again\n" );
  75.             printf( "%3d : ", count+1 );
  76.             continue;
  77.         }
  78.  
  79.         if ( !more )                                   /* Note 5*/
  80.             break;
  81.         printf( "%3d : ", ++count+1 );
  82.     }
  83.     return ( count );
  84. }
  85.  
  86. /*******************************print_array()*******************/
  87.  
  88. void print_array( int array[], int first, int num )
  89. {
  90.     int index;
  91.  
  92.     for ( index = first; index <= num; index++ )
  93.         printf( "%3d: %5d\n", index+1, array[index] );
  94. }
  95.  
  96. /*******************************get_int()***********************/
  97.  
  98. int get_int (int *intptr)
  99. {
  100.     int ch;
  101.     char remainder[BUF_SIZE];
  102.     int     digit_count = 0;
  103.  
  104.     *intptr = 0;
  105.     while ( isdigit(ch = getchar() )) {
  106.                                                         /* Note 6 */
  107.         *intptr = *intptr * 10 + ( ch - '0' );
  108.         digit_count++;
  109.     }
  110.     switch ( ch ) {                                     /* Note 7 */
  111.         case 'Q':
  112.         case 'q':
  113.                    fgets( remainder, BUF_SIZE, stdin ); /* Note 8*/
  114.                    return ( DONE );
  115.         case ' ':
  116.                    fgets( remainder, BUF_SIZE, stdin );
  117.         case '\n':
  118.                    if ( digit_count )
  119.                         return ( MORE );
  120.                    else
  121.                         return ( ERROR );
  122.         default:
  123.                    fgets( remainder, BUF_SIZE, stdin );
  124.                    return ( ERROR );
  125.     }
  126. }
  127.