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

  1. /*                         qsort.c
  2.  *
  3.  *   Synopsis  - Sorts an array of ints and an array of structures.
  4.  *               The structures are sorted by name (a string) and 
  5.  *               by age (an int). The arrays are displayed before 
  6.  *               and after sorting.
  7.  *
  8.  *   Objective - To study and use pointers to functions and study
  9.  *               an example using the library function qsort.
  10.  */
  11. #include <stdio.h>
  12. #include <stdlib.h>                                      /* Note 1 */
  13. #include <string.h>
  14. #define NUMELTS 5
  15. struct member {
  16.      char *name;
  17.      int age;
  18. };
  19.  
  20. /* Function Prototypes */                                /* Note 2 */
  21. int compare_ints( const void *a, const void *b);
  22. /* PRECONDITION:  a and b are pointers to the objects being 
  23.  *                compared.
  24.  *
  25.  * POSTCONDITION: as qsort() requires, the value returned is -1 if
  26.  *                a is less than *b, 0 if *a and *b are equal, and
  27.  *                1 if *a is greater than *b.
  28.  */
  29.  
  30. int compare_age( const void *x, const void *y);
  31. /* PRECONDITION:  a and b are pointers to the objects being 
  32.  *                compared.
  33.  *
  34.  * POSTCONDITION: as qsort() requires, the value returned is -1 if 
  35.  *                the age in *a is less
  36.  *                than the age in *b, 0 if the ages are equal, and
  37.  *                1 if the age in *a is greater than that in *b.
  38.  */
  39.  
  40. int compare_name( const void *x, const void *y);
  41. /* PRECONDITION:  a and b are pointers to the objects
  42.  *                being compared.
  43.  *
  44.  * POSTCONDITION: as qsort() requires, the value returned is -1 if 
  45.  *                the name in *a comes before the name in *b 
  46.  *                alphabetically, 0 if the names are identical, and
  47.  *                1 if the name in *a comes after the name in *b.
  48.  */
  49.  
  50. void print( char * l, struct member x[], int num );
  51. /* PRECONDITION:  l is a null-terminated string that will be output 
  52.  *                before the contents of the array; x is the array 
  53.  *                of struct members, and num is the number of 
  54.  *                elements in x.
  55.  *
  56.  * POSTCONDITION: the string l is displayed followed by
  57.  *                the contents of x.
  58.  */
  59.  
  60. void print_ints( char * label, int ints[], int num );
  61. /* PRECONDITION:  label is a null-terminated string that will be 
  62.  *                displayed before the contents of the array; x is 
  63.  *                the array of struct members, and num is the number  
  64.  *                of elements in x.
  65.  *
  66.  * POSTCONDITION: the string label is displayed followed 
  67.  *                by the contents of x.
  68.  */
  69.  
  70. int main(void)
  71. {
  72.      char *list[5] = { "cat", "cart", "cabin", "cap", "can" };
  73.      int integers[6] = { 45, 36, 12, 88, 99, 10 };
  74.      static struct member ary[NUMELTS] = { {"ralph", 42},
  75.                                            {"helen", 45},
  76.                                            {"todd", 20},
  77.                                            {"louise", 12},
  78.                                            {"sharon", 23},
  79.                                          };
  80.      print_ints( "Integers before sorting", integers, 6 );
  81.                                                        /* Note 3 */
  82.      qsort((void *)integers, 6, sizeof(integers[0]), compare_ints);
  83.      print_ints( "Integers after sorting", integers, 6 );
  84.  
  85.      print( "Original array", ary, 5 );
  86.                                                        /* Note 4 */
  87.      qsort( (void *)ary, NUMELTS, sizeof(struct member), compare_age );
  88.      print( "After sorting by age", ary, 5 );
  89.                                                        /* Note 5 */
  90.      qsort( (void *)ary, NUMELTS, sizeof(struct member), compare_name );
  91.      print( "After sorting by name", ary, 5 );
  92.      return 0;
  93. }
  94. /******************************* compare_ints() ******************/
  95.  
  96. int compare_ints( const void *a, const void *b)
  97. {
  98.                                                       /* Note 6 */
  99.      if ( *(int * )a < *( int * )b )
  100.           return(-1);
  101.      else if ( *(int *)a > *(int *)b )
  102.           return(1);
  103.      else
  104.           return(0);
  105. }
  106.  
  107. /******************************* compare_age() *******************/
  108.  
  109. int compare_age( const void *x, const void *y)
  110. {
  111.                                                       /* Note 7 */
  112.      if ( ((struct member * )x)->age < ( (struct member * )y)->age )
  113.           return(-1);
  114.      else if ( ((struct member *)x)->age > ((struct member *)y)-> age )
  115.           return(1);
  116.      else
  117.           return(0);
  118. }
  119.  
  120. /******************************* compare_name() ******************/
  121.  
  122. int compare_name( const void *x, const void *y)
  123. {
  124.                                                       /* Note 8 */
  125.      return ( strcmp( ((struct member * )x)->name, 
  126.                                     ((struct member * )y)->name ));
  127. }
  128.  
  129. /*******************************print()*************************/
  130.  
  131. void print( char *label, struct member array[], int num )
  132. {
  133.      int i;
  134.     
  135.      printf( "%s\n", label );
  136.      for (i = 0; i < num; i++)
  137.           printf("%s,\t %d\n", array[i].name, array[i].age);
  138.      printf("\n");
  139. }
  140.  
  141. /*******************************print_ints()********************/
  142.  
  143. void print_ints( char * label, int ints[], int num )
  144. {
  145.      int i;
  146.      printf( "%s\n", label );
  147.      for (i = 0; i < num; i++)
  148.           printf("%d ", ints[i]);
  149.      printf( "\n");
  150. }
  151.