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

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