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

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/cusum.c,v 1.3 90/09/04 16:04:46 toy Exp $
  3.  * NAME
  4.  *    cusum - cumulative (running) sum of a vector
  5.  *
  6.  * SYNOPSIS
  7.  *    cusum [-H] [-P prec] [-c cols] [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:    cusum.c,v $
  15.  * Revision 1.3  90/09/04  16:04:46  toy
  16.  * Use print_help_strings to print out the help strings.
  17.  * Get the default precision from the environment variable STAT_PREC.
  18.  *
  19.  * Revision 1.2  90/09/01  17:01:38  toy
  20.  * Use message function to print out messages.
  21.  *
  22.  * Revision 1.1  90/09/01  14:07:22  toy
  23.  * Initial revision
  24.  *
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <math.h>
  29. #include <string.h>
  30.  
  31. #if    defined(__STDC__) || defined(__GNUC__)
  32. #include <stddef.h>
  33. #include <stdlib.h>
  34. #endif
  35.  
  36. #include "gps.h"
  37.  
  38. #define    DEF_PROGNAME    "cusum"
  39.  
  40. static const char RCSID[] = "@(#) $Id: cusum.c,v 1.3 90/09/04 16:04:46 toy Exp $";
  41.  
  42. extern int getopt ();
  43. extern int optind;
  44. extern char *optarg;
  45.  
  46. const char *progname;
  47.  
  48. #define    OPT_STRING    "HP:c:"
  49.  
  50. void
  51. help ()
  52. {
  53.   static const char *help_strings[] =
  54.   {
  55.     "\t-H\tThis help\n",
  56.     "\t-P p\tSet number of significant digits to p.  If not specified\n",
  57.     "\t\tuse value of STAT_PREC from environment, if available.  Else\n",
  58.     "\t\tuse default.\n",
  59.     "\t-c c\tSet number of output elements per line to n\n",
  60.     "\tvec...\tData vectors, or stdin\n",
  61.     "Print the cumulative (running) sum of the specified vectors.\n",
  62.     NULL
  63.   };
  64.   (void) fprintf (stderr, "%s\n", RCSID);
  65.   (void) fprintf (stderr, "Usage:  %s [-H] [-P prec] [-c cols] [vector ...]\n", progname);
  66.   print_help_strings (help_strings);
  67. }
  68.  
  69.  
  70.  
  71. #if    defined(__STDC__) || defined(__GNUC__)
  72. void
  73. do_func (FILE * fp)
  74. #else
  75. void
  76. do_func (fp)
  77.      FILE *fp;
  78. #endif
  79. {
  80.   int rc;
  81.   double x;
  82.   double cu_sum;
  83.  
  84.   cu_sum = 0;
  85.   do
  86.     {
  87.       rc = get_number (fp, &x);
  88.       if (rc == 1)
  89.     {
  90.       cu_sum += x;
  91.       print_number (stdout, cu_sum);
  92.     }
  93.       else if (rc == 0)
  94.     {
  95.       message ("Error reading number\n");
  96.     }
  97.   } while (rc == 1);
  98. }
  99.  
  100.  
  101. int
  102. main (argc, argv)
  103.      int argc;
  104.      char *argv[];
  105. {
  106.   int option;
  107.   double total;
  108.  
  109.   progname = get_my_name (argv[0], DEF_PROGNAME);
  110.   set_def_prec ();
  111.  
  112.  
  113.   /*
  114.    * Read options
  115.    */
  116.  
  117.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  118.     {
  119.       switch (option)
  120.     {
  121.     case 'H':        /* Help                 */
  122.       help ();
  123.       exit (0);
  124.       break;
  125.     case 'P':        /* Precision             */
  126.       (void) set_precision (atoi (optarg));
  127.       break;
  128.     case 'c':        /* Number of columns         */
  129.       (void) set_max_columns (atoi (optarg));
  130.       break;
  131.     default:
  132.       break;
  133.     }            /* endswitch     */
  134.     }                /* endwhile     */
  135.  
  136.   /*
  137.    * If no files are listed, use stdin.  Otherwise use
  138.    * the given files
  139.    */
  140.  
  141.   if (optind == argc)
  142.     {
  143.       do_func (stdin);
  144.     }
  145.   else
  146.     {
  147.       total = 0;
  148.       while (optind < argc)
  149.     {
  150.       if (freopen (argv[optind], "r", stdin) == NULL)
  151.         {
  152.           message ("Cannot open `%s'\n", argv[optind]);
  153.         }
  154.       else
  155.         {
  156.           do_func (stdin);
  157.         }
  158.       optind++;
  159.     }            /* endwhile     */
  160.     }                /* endif     */
  161.  
  162.   end_column (stdout);
  163.   return 0;
  164. }
  165.