home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_09 / 8n09028a < prev    next >
Text File  |  1990-06-20  |  3KB  |  115 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define NUMELEM(a) (sizeof(a)/sizeof(a[0]))
  5.  
  6. struct tag {
  7.     int count;
  8.     char *desc;
  9. };
  10.  
  11. main()
  12. {
  13.     int cmpstia(const void *, const void *);
  14.     int cmpstid(const void *, const void *);
  15.     int cmpstsa(const void *, const void *);
  16.     int cmpstsd(const void *, const void *);
  17.  
  18.     static struct tag array[] = {
  19.         {123, "beds"},
  20.         {27,  "tables"},
  21.         {17,  "chairs"},
  22.         {95,  "desks"}
  23.     };
  24.     int (*cmpfun[])(const void *, const void *) = {
  25.         cmpstia, cmpstid, cmpstsa, cmpstsd
  26.     };
  27.     int option = 0;
  28.     int i;
  29.  
  30.     puts("1. Sort by ascending count");
  31.     puts("2. Sort by descending count");
  32.     puts("3. Sort by ascending description");
  33.     puts("4. Sort by descending description\n");
  34.  
  35.     while (1) {
  36.         printf("Enter option: ");
  37.         scanf("%1d", &option);
  38.         if (option < 1 || option > 4)
  39.             puts("Invalid option");
  40.         else
  41.             break;
  42.     }
  43.  
  44.     qsort(&array[0], NUMELEM(array), sizeof(struct tag),
  45.         cmpfun[option - 1]);
  46.  
  47.     for (i = 0; i < NUMELEM(array); ++i)
  48.         printf("array[%d]: count = %3d, desc = >%s<\n",
  49.             i, array[i].count, array[i].desc);
  50.  
  51.     return 0;
  52. }
  53.  
  54. /* compare ints in ascending order */
  55.  
  56. int cmpstia(const void *pe1, const void *pe2)
  57. {
  58.     return ((struct tag *)pe1)->count -
  59.         ((struct tag *)pe2)->count;
  60. }
  61.  
  62. /* cmpstid ints in descending order */
  63.  
  64. int cmpstid(const void *pe1, const void *pe2)
  65. {
  66.     return ((struct tag *)pe2)->count -
  67.         ((struct tag *)pe1)->count;
  68. }
  69.  
  70. /* compare strings in ascending order */
  71.  
  72. int cmpstsa(const void *pe1, const void *pe2)
  73. {
  74.     return strcmp(((struct tag *)pe1)->desc, 
  75.         ((struct tag *)pe2)->desc);
  76. }
  77.  
  78. /* compare strings in descending order */
  79.  
  80. int cmpstsd(const void *pe1, const void *pe2)
  81. {
  82.     return -strcmp(((struct tag *)pe1)->desc, 
  83.         ((struct tag *)pe2)->desc);
  84. }
  85.  
  86. 1. Sort by ascending count
  87. 2. Sort by descending count
  88. 3. Sort by ascending description
  89. 4. Sort by descending description
  90.  
  91. Enter option:1
  92. array[0]: count =  17, desc = >chairs<
  93. array[1]: count =  27, desc = >tables<
  94. array[2]: count =  95, desc = >desks<
  95. array[3]: count = 123, desc = >beds<
  96.  
  97. Enter option:2
  98. array[0]: count = 123, desc = >beds<
  99. array[1]: count =  95, desc = >desks<
  100. array[2]: count =  27, desc = >tables<
  101. array[3]: count =  17, desc = >chairs<
  102.  
  103. Enter option:3
  104. array[0]: count = 123, desc = >beds<
  105. array[1]: count =  17, desc = >chairs<
  106. array[2]: count =  95, desc = >desks<
  107. array[3]: count =  27, desc = >tables<
  108.  
  109. Enter option:4
  110. array[0]: count =  27, desc = >tables<
  111. array[1]: count =  95, desc = >desks<
  112. array[2]: count =  17, desc = >chairs<
  113. array[3]: count = 123, desc = >beds<
  114.  
  115.