home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / graphics-0.17 / dist-stat / qsort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-13  |  3.9 KB  |  219 lines

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/qsort.c,v 1.3 90/09/04 17:40:42 toy Exp $
  3.  * NAME
  4.  *    qsort - sort the elements of the vector
  5.  *
  6.  * SYNOPSIS
  7.  *    qsort [-Hr] [-P prec] [-c cols] [vectors...]
  8.  *
  9.  * DESCRIPTION
  10.  *    Output is the sorted elements of the specified vector(s).
  11.  *
  12.  * HISTORY
  13.  * $Log:    qsort.c,v $
  14.  * Revision 1.3  90/09/04  17:40:42  toy
  15.  * Set default precision from the environment variable STAT_PREC.
  16.  * Use print_help_strings to print out help.
  17.  *
  18.  * Revision 1.2  90/09/02  17:14:26  toy
  19.  * Use read_vector routine instead of do_read.
  20.  *
  21.  * Revision 1.1  90/09/01  18:09:03  toy
  22.  * Initial revision
  23.  *
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <math.h>
  28. #include <string.h>
  29. #include <assert.h>
  30.  
  31. #if    defined(__STDC__) || defined(__GNUC__)
  32. #include <stddef.h>
  33. #include <stdlib.h>
  34. #else
  35. #include <malloc.h>
  36. #endif
  37.  
  38. #include "gps.h"
  39.  
  40. #define    DEF_PROGNAME    "qsort"
  41.  
  42. static const char RCSID[] = "@(#) $Id: qsort.c,v 1.3 90/09/04 17:40:42 toy Exp $";
  43.  
  44. extern int getopt ();
  45. extern int optind;
  46. extern char *optarg;
  47.  
  48. const char *progname;
  49. int (*sort_function) ();
  50.  
  51. #define    OPT_STRING    "HP:c:r"
  52.  
  53. void
  54. help ()
  55. {
  56.   static const char *help_strings[] =
  57.   {
  58.     "\t-H\tThis help\n",
  59.     "\t-P p\tSet number of significant digits to p.  If not specified\n",
  60.     "\t\tuse value of STAT_PREC from environment, if available.  Else\n",
  61.     "\t\tuse default.\n",
  62.     "\t-c c\tSet number of output elements per line to n\n",
  63.     "\t-r\tSort the elements in descending order\n",
  64.     "Sort the vectors in ascending or descending order.\n",
  65.     NULL
  66.   };
  67.   (void) fprintf (stderr, "%s\n", RCSID);
  68.   (void) fprintf (stderr, "Usage:  %s [-H] [-P prec] [-c cols] [vector ...]\n", progname);
  69.   print_help_strings (help_strings);
  70. }
  71.  
  72. #if    defined(__STDC__) || defined(__GNUC__)
  73. int
  74. ascending (const void *a, const void *b)
  75. #else
  76. int
  77. ascending (a, b)
  78.      char *a;
  79.      char *b;
  80. #endif
  81. {
  82.   double *x;
  83.   double *y;
  84.  
  85.   x = (double *) a;
  86.   y = (double *) b;
  87.  
  88.   if (*x < *y)
  89.     return -1;
  90.   else if (*x > *y)
  91.     return 1;
  92.   else
  93.     return 0;
  94. }
  95.  
  96.  
  97. #if    defined(__STDC__) || defined(__GNUC__)
  98. int
  99. descending (const void *a, const void *b)
  100. #else
  101. int
  102. descending (a, b)
  103.      char *a;
  104.      char *b;
  105. #endif
  106. {
  107.   double *x;
  108.   double *y;
  109.  
  110.   x = (double *) a;
  111.   y = (double *) b;
  112.  
  113.   if (*x < *y)
  114.     return 1;
  115.   else if (*x > *y)
  116.     return -1;
  117.   else
  118.     return 0;
  119. }
  120.  
  121. #if    defined(__STDC__) || defined(__GNUC__)
  122. void
  123. do_sort (double *vector, int rank)
  124. #else
  125. void
  126. do_sort (vector, rank)
  127.      double *vector;
  128.      int rank;
  129. #endif
  130. {
  131.   qsort ((char *) vector, rank, sizeof (double), sort_function);
  132.   while (rank-- > 0)
  133.     {
  134.       print_number (stdout, *vector++);
  135.     }
  136. }
  137.  
  138.  
  139.  
  140. int
  141. main (argc, argv)
  142.      int argc;
  143.      char *argv[];
  144. {
  145.   int option;
  146.   int errcnt;
  147.   int rank;
  148.   double *vector;
  149.  
  150.   progname = get_my_name (argv[0], DEF_PROGNAME);
  151.   set_def_prec ();
  152.  
  153.   /*
  154.    * Read options
  155.    */
  156.  
  157.   errcnt = 0;
  158.   sort_function = ascending;
  159.  
  160.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  161.     {
  162.       switch (option)
  163.     {
  164.     case 'H':        /* Help                 */
  165.       help ();
  166.       exit (0);
  167.       break;
  168.     case 'P':        /* Precision             */
  169.       (void) set_precision (atoi (optarg));
  170.       break;
  171.     case 'c':        /* Number of output columsn     */
  172.       (void) set_max_columns (atoi (optarg));
  173.       break;
  174.     case 'r':        /* Reverse order of sort     */
  175.       sort_function = descending;
  176.       break;
  177.     default:
  178.       break;
  179.     }            /* endswitch     */
  180.     }                /* endwhile     */
  181.  
  182.   if (errcnt > 0)
  183.     {
  184.       exit (1);
  185.     }
  186.  
  187.   /*
  188.    * If no files are listed, use stdin.  Otherwise use
  189.    * the given files
  190.    */
  191.  
  192.   if (optind == argc)
  193.     {
  194.       vector = read_vector (stdin, &rank);
  195.       do_sort (vector, rank);
  196.       free (vector);
  197.     }
  198.   else
  199.     {
  200.       while (optind < argc)
  201.     {
  202.       if (freopen (argv[optind], "r", stdin) == NULL)
  203.         {
  204.           message ("Cannot open `%s'\n", argv[optind]);
  205.         }
  206.       else
  207.         {
  208.           vector = read_vector (stdin, &rank);
  209.           do_sort (vector, rank);
  210.           free (vector);
  211.         }
  212.       optind++;
  213.     }            /* endwhile     */
  214.     }                /* endif     */
  215.  
  216.   end_column (stdout);
  217.   return 0;
  218. }
  219.