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

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