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

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/total.c,v 1.5 90/09/01 17:15:13 toy Exp $
  3.  * NAME
  4.  *    total - sum total of vectors
  5.  *
  6.  * SYNOPSIS
  7.  *    total [vectors...]
  8.  *
  9.  * DESCRIPTION
  10.  *    Output is the sum total of the elements of the input
  11.  *    vector(s).
  12.  *
  13.  * HISTORY
  14.  * $Log:    total.c,v $
  15.  * Revision 1.5  90/09/01  17:15:13  toy
  16.  * Some more lint removal.
  17.  *
  18.  * Revision 1.4  90/09/01  17:02:27  toy
  19.  * Use message function to print out messages.
  20.  *
  21.  * Revision 1.3  90/09/01  11:25:49  toy
  22.  * Changed option characters.
  23.  *
  24.  * Revision 1.2  90/08/31  15:55:46  toy
  25.  * Forgot to initialize total when we want a grand total.
  26.  *
  27.  * Revision 1.1  90/08/31  14:51:54  toy
  28.  * Initial revision
  29.  *
  30.  */
  31.  
  32. #include <stdio.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    "total"
  42.  
  43. static const char RCSID[] = "@(#) $Id: total.c,v 1.5 90/09/01 17:15:13 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    "HTP:"
  52.  
  53. void
  54. help ()
  55. {
  56.   (void) fprintf (stderr, "%s\n", RCSID);
  57.   (void) fprintf (stderr, "Usage:  %s [-HT] [-P prec][vector ...]\n", progname);
  58.   (void) fprintf (stderr, "\t-H\tThis help\n");
  59.   (void) fprintf (stderr, "\t-T\tGrand total of all vectors listed\n");
  60.   (void) fprintf (stderr, "\t\t(otherwise, print result for each vector)\n");
  61.   (void) fprintf (stderr, "\t-P p\tSet number of significant digits to p\n");
  62. }
  63.  
  64.  
  65. #if    defined(__STDC__) || defined(__GNUC__)
  66. double
  67. do_total (FILE * fp)
  68. #else
  69. double
  70. do_total (fp)
  71.      FILE *fp;
  72. #endif
  73. {
  74.   int rc;
  75.   double x;
  76.   double sum;
  77.  
  78.   sum = 0;
  79.  
  80.   do
  81.     {
  82.       rc = get_number (fp, &x);
  83.       if (rc == 1)
  84.     {
  85.       sum += x;
  86.     }
  87.       else if (rc == 0)
  88.     {
  89.       message ("Error reading number\n");
  90.     }
  91.   } while (rc == 1);
  92.  
  93.   return sum;
  94. }
  95.  
  96. int
  97. main (argc, argv)
  98.      int argc;
  99.      char *argv[];
  100. {
  101.   int option;
  102.   int total_sum;
  103.   double sum;
  104.   double total;
  105.  
  106.   progname = get_my_name (argv[0], DEF_PROGNAME);
  107.  
  108.   /*
  109.    * Read options
  110.    */
  111.  
  112.   total_sum = FALSE;
  113.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  114.     {
  115.       switch (option)
  116.     {
  117.     case 'H':        /* Help                 */
  118.       help ();
  119.       exit (0);
  120.       break;
  121.     case 'T':        /* Grand total of all vectors     */
  122.       total_sum = TRUE;
  123.       break;
  124.     case 'P':        /* Precision             */
  125.       (void) set_precision (atoi (optarg));
  126.       break;
  127.     default:
  128.       break;
  129.     }            /* endswitch     */
  130.     }                /* endwhile     */
  131.  
  132.   /*
  133.    * If no files are listed, use stdin.  Otherwise use
  134.    * the given files
  135.    */
  136.  
  137.   if (optind == argc)
  138.     {
  139.       print_number (stdout, do_total (stdin));
  140.     }
  141.   else
  142.     {
  143.       total = 0;
  144.       while (optind < argc)
  145.     {
  146.       if (freopen (argv[optind], "r", stdin) == NULL)
  147.         {
  148.           message ("Cannot open `%s'\n", argv[optind]);
  149.         }
  150.       else
  151.         {
  152.           sum = do_total (stdin);
  153.           if (total_sum)
  154.         {
  155.           total += sum;
  156.         }
  157.           else
  158.         {
  159.           print_number (stdout, sum);
  160.         }
  161.         }
  162.       optind++;
  163.     }            /* endwhile     */
  164.  
  165.       if (total_sum)
  166.     {
  167.       print_number (stdout, total);
  168.     }
  169.     }                /* endif     */
  170.  
  171.   end_column (stdout);
  172.   return 0;
  173. }
  174.