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

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/prod.c,v 1.7 90/09/10 12:23:28 toy Exp $
  3.  * NAME
  4.  *    prod, total - product or sum of the elements of a vector
  5.  *
  6.  * SYNOPSIS
  7.  *    prod [-HT] [-P prec] [vectors...]
  8.  *    total [-HT] [-P prec] [vectors...]
  9.  *
  10.  * DESCRIPTION
  11.  *    Output is the product of the elements of the input
  12.  *    vector(s).
  13.  *
  14.  * HISTORY
  15.  * $Log:    prod.c,v $
  16.  * Revision 1.7  90/09/10  12:23:28  toy
  17.  * Some help strings didn't have a comma in them.
  18.  *
  19.  * Revision 1.6  90/09/05  14:34:38  toy
  20.  * Merged total into this program.
  21.  *
  22.  * Revision 1.5  90/09/04  17:40:39  toy
  23.  * Set default precision from the environment variable STAT_PREC.
  24.  * Use print_help_strings to print out help.
  25.  *
  26.  * Revision 1.4  90/09/01  17:15:04  toy
  27.  * Some more lint removal.
  28.  *
  29.  * Revision 1.3  90/09/01  17:02:22  toy
  30.  * Use message function to print out messages.
  31.  *
  32.  * Revision 1.2  90/09/01  11:26:05  toy
  33.  * Changed option characters.
  34.  *
  35.  * Revision 1.1  90/08/31  15:56:16  toy
  36.  * Initial revision
  37.  *
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include <string.h>
  42.  
  43. #if    defined(__STDC__) || defined(__GNUC__)
  44. #include <stddef.h>
  45. #include <stdlib.h>
  46. #endif
  47.  
  48. #include "gps.h"
  49.  
  50. #define    DEF_PROGNAME    "prod|total"
  51. #define    PROD_NAME    "prod"
  52. #define    TOTAL_NAME    "total"
  53.  
  54. static const char RCSID[] = "@(#) $Id: prod.c,v 1.7 90/09/10 12:23:28 toy Exp $";
  55.  
  56. extern int getopt ();
  57. extern int optind;
  58. extern char *optarg;
  59.  
  60. const char *progname;
  61.  
  62. #define    OPT_STRING    "HTP:"
  63.  
  64. #if    defined(__STDC__) || defined(__GNUC__)
  65. void
  66. help (void)
  67. #else
  68. void
  69. help ()
  70. #endif
  71. {
  72.   static const char *help_strings[] =
  73.   {
  74.     "\t-H\tThis help\n",
  75.     "\t-T\tGrand total of all vectors listed\n",
  76.     "\t\t(otherwise, print result for each vector)\n",
  77.     "\t-P p\tSet number of significant digits to p.  If not specified\n",
  78.     "\t\tuse value of STAT_PREC from environment, if available.  Else\n",
  79.     "\t\tuse default.\n",
  80.     NULL
  81.   };
  82.   static const char *help_prod[] =
  83.   {
  84.     "Compute the product of all the elements of the vector.\n",
  85.     "A product is produced for each specified vector.  If -T is\n",
  86.     "given, the result is the same as if all the vectors were\n",
  87.     "concatenated together.\n",
  88.     NULL
  89.   };
  90.   static const char *help_total[] =
  91.   {
  92.     "Compute the total sum of all the elements of the vector.\n",
  93.     "A total is produced for each specified vector.  If -T is\n",
  94.     "given, the result is the same as if all the vectors were\n",
  95.     "concatenated together.\n",
  96.     NULL
  97.   };
  98.   (void) fprintf (stderr, "%s\n", RCSID);
  99.   (void) fprintf (stderr, "Usage:  %s [-HT] [-P prec] [vector ...]\n", progname);
  100.   print_help_strings (help_strings);
  101.   if (strcmp (progname, PROD_NAME) == 0)
  102.     {
  103.       print_help_strings (help_prod);
  104.     }
  105.   else
  106.     {
  107.       print_help_strings (help_total);
  108.     }
  109. }
  110.  
  111. #if    defined(__STDC__) || defined(__GNUC__)
  112. double
  113. do_prod (FILE * fp)
  114. #else
  115. double
  116. do_prod (fp)
  117.      FILE *fp;
  118. #endif
  119. {
  120.   int rc;
  121.   double x;
  122.   double prod;
  123.  
  124.   prod = 1;
  125.  
  126.   do
  127.     {
  128.       rc = get_number (fp, &x);
  129.       if (rc == 1)
  130.     {
  131.       prod *= x;
  132.     }
  133.       else if (rc == 0)
  134.     {
  135.       message ("Error reading number\n");
  136.     }
  137.   } while (rc == 1);
  138.  
  139.   return prod;
  140. }
  141.  
  142. #if    defined(__STDC__) || defined(__GNUC__)
  143. double
  144. do_total (FILE * fp)
  145. #else
  146. double
  147. do_total (fp)
  148.      FILE *fp;
  149. #endif
  150. {
  151.   int rc;
  152.   double x;
  153.   double sum;
  154.  
  155.   sum = 0;
  156.  
  157.   do
  158.     {
  159.       rc = get_number (fp, &x);
  160.       if (rc == 1)
  161.     {
  162.       sum += x;
  163.     }
  164.       else if (rc == 0)
  165.     {
  166.       message ("Error reading number\n");
  167.     }
  168.   } while (rc == 1);
  169.  
  170.   return sum;
  171. }
  172.  
  173. int
  174. main (argc, argv)
  175.      int argc;
  176.      char *argv[];
  177. {
  178.   int option;
  179.   int total_result;
  180.   int product;
  181.   double result;
  182.   double total;
  183.   double (*func) PARMS ((FILE *));
  184.  
  185.   set_def_prec ();
  186.   progname = get_my_name (argv[0], DEF_PROGNAME);
  187.  
  188.   /*
  189.    * What's my name?
  190.    */
  191.   if (strcmp (progname, PROD_NAME) == 0)
  192.     {
  193.       func = do_prod;
  194.       product = TRUE;
  195.     }
  196.   else if (strcmp (progname, TOTAL_NAME) == 0)
  197.     {
  198.       func = do_total;
  199.       product = FALSE;
  200.     }
  201.   else
  202.     {
  203.       message ("unknown program name.  Should be prod or total.\n");
  204.       exit (1);
  205.     }
  206.  
  207.   /*
  208.    * Read options
  209.    */
  210.  
  211.   total_result = FALSE;
  212.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  213.     {
  214.       switch (option)
  215.     {
  216.     case 'H':        /* Help                 */
  217.       help ();
  218.       exit (0);
  219.       break;
  220.     case 'T':        /* Grand total of all vectors     */
  221.       total_result = TRUE;
  222.       break;
  223.     case 'P':        /* Set precision         */
  224.       (void) set_precision (atoi (optarg));
  225.       break;
  226.     default:
  227.       break;
  228.     }            /* endswitch     */
  229.     }                /* endwhile     */
  230.  
  231.   /*
  232.    * If no files are listed, use stdin.  Otherwise use
  233.    * the given files
  234.    */
  235.  
  236.   if (optind == argc)
  237.     {
  238.       print_number (stdout, func (stdin));
  239.     }
  240.   else
  241.     {
  242.       total = 1;
  243.       while (optind < argc)
  244.     {
  245.       if (freopen (argv[optind], "r", stdin) == NULL)
  246.         {
  247.           message ("Cannot open `%s'\n", argv[optind]);
  248.         }
  249.       else
  250.         {
  251.           result = func (stdin);
  252.           if (total_result)
  253.         {
  254.           total = product ? total * result : total + result;
  255.         }
  256.           else
  257.         {
  258.           print_number (stdout, result);
  259.         }
  260.         }
  261.       optind++;
  262.     }            /* endwhile     */
  263.  
  264.       if (total_result)
  265.     {
  266.       print_number (stdout, total);
  267.     }
  268.     }                /* endif     */
  269.  
  270.   end_column (stdout);
  271.   return 0;
  272. }
  273.