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

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/gas.c,v 1.3 90/11/04 18:18:05 toy Exp $
  3.  * NAME
  4.  *    gas - generate additive sequence
  5.  *
  6.  * SYNOPSIS
  7.  *    gas [-H] [-P prec] [-c cols] [-i interval] [-n number] [-s start] [-t terminate]
  8.  *
  9.  * DESCRIPTION
  10.  *    Output is an additive sequence
  11.  *
  12.  * HISTORY
  13.  * $Log:    gas.c,v $
  14.  * Revision 1.3  90/11/04  18:18:05  toy
  15.  * Fixed help message.
  16.  *
  17.  * Revision 1.2  90/09/04  16:04:48  toy
  18.  * Use print_help_strings to print out the help strings.
  19.  * Get the default precision from the environment variable STAT_PREC.
  20.  *
  21.  * Revision 1.1  90/09/02  16:33:31  toy
  22.  * Initial revision
  23.  *
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <math.h>
  28.  
  29. #if    defined(__STDC__) || defined(__GNUC__)
  30. #include <stddef.h>
  31. #include <stdlib.h>
  32. #endif
  33.  
  34. #include "gps.h"
  35.  
  36. #define    DEF_PROGNAME    "gas"
  37.  
  38. static const char RCSID[] = "@(#) $Id: gas.c,v 1.3 90/11/04 18:18:05 toy Exp $";
  39.  
  40. extern int getopt ();
  41. extern int optind;
  42. extern char *optarg;
  43.  
  44. extern long random PARMS ((void));
  45. extern void srandom PARMS ((unsigned));
  46.  
  47. const char *progname;
  48.  
  49. #define    OPT_STRING    "HP:c:i:n:s:t:"
  50.  
  51. void
  52. help ()
  53. {
  54.  
  55.   static const char *help_strings[] =
  56.   {
  57.     "\t\t[-m mult] [-n num] [-s seed]\n",
  58.     "\t-H\tThis help\n",
  59.     "\t-P p\tSet number of significant digits to p.  If not specified\n",
  60.     "\t\tuse value of STAT_PREC from environment, if available.  Else\n",
  61.     "\t\tuse default.\n",
  62.     "\t-c c\tSet number of output elements per line to n\n",
  63.     "\t-i i\tInterval between numbers\n",
  64.     "\t-n n\tNumber of values to generate (default is 10, if -t is not\n",
  65.     "\t\tgiven)\n",
  66.     "\t-s s\tStarting value (default is 1)\n",
  67.     "\t-t t\tTerminating value (default is infinity, but -n usually\n",
  68.     "\t\tterminates the sequence)\n",
  69.     "Generate an additive sequence of specified length and spacing\n",
  70.     "Examples:\n",
  71.     "\tgas                     generates the numbers 1 to 10\n",
  72.     "\tgas -s.01 -t.05 -i .01  generates .01, .02, .03, .04, .05\n",
  73.     "\tgas -s3 -i2 -n4 -t5     generates 3, 5, 3, 5\n",
  74.     NULL
  75.   };
  76.  
  77.   (void) fprintf (stderr, "%s\n", RCSID);
  78.   (void) fprintf (stderr, "\
  79. Usage:  %s [-H] [-P prec] [-c cols] [-i interval] [-n num] [-s start]\n\
  80. \t[-t end]\n",
  81.           progname);
  82.   print_help_strings (help_strings);
  83. }
  84.  
  85.  
  86. int
  87. main (argc, argv)
  88.      int argc;
  89.      char *argv[];
  90. {
  91.   int option;
  92.   int errcnt;
  93.   int k;
  94.   int got_terminate;
  95.   int got_number;
  96.   int number;            /* Number of elements     */
  97.   double interval;        /* Step size         */
  98.   double start;            /* Starting point     */
  99.   double terminate;        /* Ending point         */
  100.   double x;
  101.  
  102.   progname = get_my_name (argv[0], DEF_PROGNAME);
  103.   set_def_prec ();
  104.   set_def_prec ();
  105.  
  106.   errcnt = 0;
  107.   interval = 1;
  108.   number = 10;
  109.   start = 1;
  110.   got_number = got_terminate = FALSE;
  111.  
  112.   /*
  113.    * Read options
  114.    */
  115.  
  116.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  117.     {
  118.       switch (option)
  119.     {
  120.     case 'H':        /* Help                 */
  121.       help ();
  122.       exit (0);
  123.       break;
  124.     case 'P':        /* Precision             */
  125.       (void) set_precision (atoi (optarg));
  126.       break;
  127.     case 'c':        /* Number of columns         */
  128.       (void) set_max_columns (atoi (optarg));
  129.       break;
  130.     case 'i':        /* Interval             */
  131.       if (sscanf (optarg, "%lf", &interval) != 1)
  132.         {
  133.           message ("cannot read -i value: `%s'\n", optarg);
  134.           errcnt++;
  135.         }
  136.       break;
  137.     case 'n':        /* Number of values to generate     */
  138.       if (sscanf (optarg, "%d", &number) != 1)
  139.         {
  140.           message ("cannot read -n value: `%s'\n", optarg);
  141.           errcnt++;
  142.         }
  143.       else
  144.         {
  145.           got_number = TRUE;
  146.         }
  147.       break;
  148.     case 's':        /* Start             */
  149.       if (sscanf (optarg, "%lf", &start) != 1)
  150.         {
  151.           message ("cannot read -s value: `%s'\n", optarg);
  152.           errcnt++;
  153.         }
  154.       break;
  155.     case 't':        /* Terminating value         */
  156.       if (sscanf (optarg, "%lf", &terminate) != 1)
  157.         {
  158.           message ("cannot read -t value: `%s'\n", optarg);
  159.           errcnt++;
  160.         }
  161.       else
  162.         {
  163.           got_terminate = TRUE;
  164.         }
  165.       break;
  166.     default:
  167.       break;
  168.     }            /* endswitch     */
  169.     }                /* endwhile     */
  170.  
  171.   /*
  172.    * Check options
  173.    */
  174.  
  175.   if (number <= 0)
  176.     {
  177.       message ("number of elements must be positive\n");
  178.       errcnt++;
  179.     }
  180.  
  181.   /*
  182.    * Exit if any errors found
  183.    */
  184.  
  185.   if (errcnt > 0)
  186.     {
  187.       exit (1);
  188.     }
  189.  
  190.   /*
  191.    * Figure out how many numbers to generate
  192.    */
  193.  
  194.   if (got_terminate && !got_number)
  195.     {
  196.       number = floor (fabs ((terminate - start) / interval) + 1 + 0.5);
  197.       printf ("number = %d\n", number);
  198.     }
  199.  
  200.   /*
  201.    * Generate the specified sequence
  202.    */
  203.  
  204.   if (got_terminate)
  205.     {
  206.       for (k = 0; k < number; k++)
  207.     {
  208.       x = start + fmod (k * interval, terminate - start + interval);
  209.       print_number (stdout, x);
  210.     }
  211.     }
  212.   else
  213.     {
  214.       for (k = 0; k < number; k++)
  215.     {
  216.       x = start + k * interval;
  217.       print_number (stdout, x);
  218.     }
  219.     }
  220.  
  221.   end_column (stdout);
  222.   return 0;
  223. }
  224.