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

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/hilo.c,v 1.4 90/09/04 17:39:53 toy Exp $
  3.  * NAME
  4.  *    hilo - hi and/or low values
  5.  *
  6.  * SYNOPSIS
  7.  *    hilo [-H] [-P prec] [-hl] [-o char] [vectors...]
  8.  *
  9.  * DESCRIPTION
  10.  *    Output is the cumulative (running) sum of the elements of
  11.  *    the specified vector(s).
  12.  *
  13.  * HISTORY
  14.  * $Log:    hilo.c,v $
  15.  * Revision 1.4  90/09/04  17:39:53  toy
  16.  * Set default precision from the environment variable STAT_PREC.
  17.  * Use print_help_strings to print out help.
  18.  *
  19.  * Revision 1.3  90/09/02  17:12:10  toy
  20.  * Fixed revision history
  21.  *
  22.  * Revision 1.2  90/09/01  17:01:53  toy
  23.  * Use message function to print out messages.
  24.  *
  25.  * Revision 1.1  90/09/01  14:33:46  toy
  26.  * Initial revision
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <math.h>
  32. #include <string.h>
  33.  
  34. #if    defined(__STDC__) || defined(__GNUC__)
  35. #include <stddef.h>
  36. #include <stdlib.h>
  37. #endif
  38.  
  39. #include "gps.h"
  40.  
  41. #define    DEF_PROGNAME    "hilo"
  42.  
  43. static const char RCSID[] = "@(#) $Id: hilo.c,v 1.4 90/09/04 17:39:53 toy Exp $";
  44.  
  45. extern int getopt ();
  46. extern int optind;
  47. extern char *optarg;
  48.  
  49. const char *progname;
  50.  
  51. #define    OPT_STRING    "HP:hlo:"
  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-h\tOnly output high value\n",
  63.     "\t-l\tOnly onput low value\n",
  64.     "\t-o c\tOutput values in option form with `c' prepended\n",
  65.     "Print the max and min values of a vector.  Use -h or -l to\n",
  66.     "specify only the max or min value, respectively.  If -o c is\n",
  67.     "given, the output is of the form '-ch high -cl low'.\n",
  68.     "\n",
  69.     "The high and/or low values are printed for each vector\n",
  70.     NULL
  71.   };
  72.   (void) fprintf (stderr, "%s\n", RCSID);
  73.   (void) fprintf (stderr, "Usage:  %s [-H] [-P prec] [-c cols] [vector ...]\n", progname);
  74.   print_help_strings (help_strings);
  75. }
  76.  
  77. #if    defined(__STDC__) || defined(__GNUC__)
  78. void
  79. do_func (FILE * fp, double *low, double *high)
  80. #else
  81. void
  82. do_func (fp, low, high)
  83.      FILE *fp;
  84.      double *low;
  85.      double *high;
  86. #endif
  87. {
  88.   int rc;
  89.   double x;
  90.  
  91.   do
  92.     {
  93.       rc = get_number (fp, &x);
  94.       if (rc == 1)
  95.     {
  96.       if (*low > x)
  97.         {
  98.           *low = x;
  99.         }
  100.       if (*high < x)
  101.         {
  102.           *high = x;
  103.         }
  104.     }
  105.       else if (rc == 0)
  106.     {
  107.       message ("Error reading number\n");
  108.     }
  109.   } while (rc == 1);
  110. }
  111.  
  112.  
  113. int
  114. main (argc, argv)
  115.      int argc;
  116.      char *argv[];
  117. {
  118.   char opt_char;
  119.   int option;
  120.   int output_lo;
  121.   int output_hi;
  122.   int precision;        /* Output precision    */
  123.   double low;            /* Lowest number    */
  124.   double high;            /* Highest number    */
  125.  
  126.  
  127.   progname = get_my_name (argv[0], DEF_PROGNAME);
  128.   set_def_prec ();
  129.  
  130.   /*
  131.    * We need to get the default precision for later
  132.    */
  133.   precision = set_precision (-1);
  134.   output_lo = output_hi = TRUE;
  135.   opt_char = '\0';
  136.  
  137.   /*
  138.    * Read options
  139.    */
  140.  
  141.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  142.     {
  143.       switch (option)
  144.     {
  145.     case 'H':        /* Help                 */
  146.       help ();
  147.       exit (0);
  148.       break;
  149.     case 'P':        /* Precision             */
  150.       precision = atoi (optarg);
  151.       if (precision < 0)
  152.         {
  153.           precision = set_precision (-1);    /* Get default    */
  154.         }
  155.       (void) set_precision (precision);
  156.       break;
  157.     case 'h':        /* Output high value only     */
  158.       output_lo = FALSE;
  159.       break;
  160.     case 'l':        /* Output low value only     */
  161.       output_hi = FALSE;
  162.       break;
  163.     case 'o':        /* Output in option form     */
  164.       opt_char = *optarg;
  165.       break;
  166.     default:
  167.       break;
  168.     }            /* endswitch     */
  169.     }                /* endwhile     */
  170.  
  171.   /*
  172.    * If no files are listed, use stdin.  Otherwise use
  173.    * the given files.
  174.    */
  175.  
  176.   low = HUGE;
  177.   high = -HUGE;
  178.  
  179.   if (optind == argc)
  180.     {
  181.       do_func (stdin, &low, &high);
  182.     }
  183.   else
  184.     {
  185.       while (optind < argc)
  186.     {
  187.       if (freopen (argv[optind], "r", stdin) == NULL)
  188.         {
  189.           message ("Cannot open `%s'\n", argv[optind]);
  190.         }
  191.       else
  192.         {
  193.           do_func (stdin, &low, &high);
  194.         }
  195.       optind++;
  196.     }            /* endwhile     */
  197.     }                /* endif     */
  198.  
  199.   if (opt_char != '\0')
  200.     {
  201.       if (output_hi)
  202.     {
  203.       printf ("-%ch%.*g ", opt_char, precision, high);
  204.     }
  205.       if (output_lo)
  206.     {
  207.       printf ("-%cl%.*g ", opt_char, precision, low);
  208.     }
  209.     }
  210.   else
  211.     {
  212.       if (output_hi)
  213.     {
  214.       print_number (stdout, high);
  215.       end_column (stdout);
  216.     }
  217.       if (output_lo)
  218.     {
  219.       print_number (stdout, low);
  220.     }
  221.     }
  222.  
  223.  
  224.   end_column (stdout);
  225.   return 0;
  226. }
  227.