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

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/siline.c,v 1.3 90/09/04 18:34:13 toy Exp $
  3.  * NAME
  4.  *    siline - generate a line given the slope and intercept
  5.  *
  6.  * SYNOPSIS
  7.  *    siline [-H] [-P prec] [-c cols] [-i intercept] [-n nsamps] [-s slope]
  8.  *        [vectors ...]
  9.  *
  10.  * DESCRIPTION
  11.  *    Output is a line with the given slope and intercept
  12.  *
  13.  * HISTORY
  14.  * $Log:    siline.c,v $
  15.  * Revision 1.3  90/09/04  18:34:13  toy
  16.  * Wasn't printing out the help strings!
  17.  *
  18.  * Revision 1.2  90/09/04  17:40:51  toy
  19.  * Set default precision from the environment variable STAT_PREC.
  20.  * Use print_help_strings to print out help.
  21.  *
  22.  * Revision 1.1  90/09/02  17:52:57  toy
  23.  * Initial revision
  24.  *
  25.  */
  26.  
  27. #include <stdio.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    "siline"
  37.  
  38. static const char RCSID[] = "@(#) $Id: siline.c,v 1.3 90/09/04 18:34:13 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:"
  50.  
  51. void
  52. help ()
  53. {
  54.   static const char *help_strings[] =
  55.   {
  56.     "\t-H\tThis help\n",
  57.     "\t-P p\tSet number of significant digits to p.  If not specified\n",
  58.     "\t\tuse value of STAT_PREC from environment, if available.  Else\n",
  59.     "\t\tuse default.\n",
  60.     "\t-c c\tSet number of output elements per line to n\n",
  61.     "\t-i i\tIntercept of line, default = 0\n",
  62.     "\t-n n\tNumber of positive integers for x\n",
  63.     "\t-s s\tSlope of line, default = 1\n",
  64.     "The output consists of values from a line with the given\n",
  65.     "slope and intercept:  y = slope * x + intercept.  The x values\n",
  66.     "are taken from the vectors, if given, or from stdin.  If -n is\n",
  67.     "given, the x values are the sequence of positive integers from 1\n",
  68.     "to n.\n",
  69.     NULL
  70.   };
  71.  
  72.   (void) fprintf (stderr, "%s\n", RCSID);
  73.   (void) fprintf (stderr, "Usage:  %s [-H] [-P prec] [-c cols] [-i intercept] [-n nsamp] [-s slope]\n", progname);
  74.   (void) fprintf (stderr, "\t\t[vectors ...]\n");
  75.   print_help_strings (help_strings);
  76. }
  77.  
  78.  
  79. int
  80. main (argc, argv)
  81.      int argc;
  82.      char *argv[];
  83. {
  84.   int option;
  85.   int errcnt;
  86.   int k;
  87.   int got_nsamp;
  88.   int nsamp;
  89.   double intercept;
  90.   double slope;
  91.   double x;
  92.  
  93.   progname = get_my_name (argv[0], DEF_PROGNAME);
  94.   set_def_prec ();
  95.  
  96.   /*
  97.    * Read options
  98.    */
  99.  
  100.   errcnt = 0;
  101.   got_nsamp = FALSE;
  102.   intercept = 0;
  103.   slope = 1;
  104.  
  105.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  106.     {
  107.       switch (option)
  108.     {
  109.     case 'H':        /* Help                 */
  110.       help ();
  111.       exit (0);
  112.       break;
  113.     case 'P':        /* Precision             */
  114.       (void) set_precision (atoi (optarg));
  115.       break;
  116.     case 'c':        /* Number of columns         */
  117.       (void) set_max_columns (atoi (optarg));
  118.       break;
  119.     case 'i':        /* Intercept             */
  120.       if (sscanf (optarg, "%lf", &intercept) != 1)
  121.         {
  122.           message ("cannot read -i value: `%s'\n", optarg);
  123.           errcnt++;
  124.         }
  125.       break;
  126.     case 'n':        /* Number of positive integers     */
  127.       if (sscanf (optarg, "%d", &nsamp) != 1)
  128.         {
  129.           message ("cannot read -n value: `%s'\n", optarg);
  130.           errcnt++;
  131.         }
  132.       else
  133.         {
  134.           got_nsamp = TRUE;
  135.         }
  136.       break;
  137.     case 's':        /* Slope of line         */
  138.       if (sscanf (optarg, "%lf", &slope) != 1)
  139.         {
  140.           message ("cannot read -s value: `%s'\n", optarg);
  141.           errcnt++;
  142.         }
  143.       break;
  144.     default:
  145.       break;
  146.     }            /* endswitch     */
  147.     }                /* endwhile     */
  148.  
  149.   /*
  150.    * Exit if any errors found
  151.    */
  152.   if (errcnt > 0)
  153.     {
  154.       exit (1);
  155.     }
  156.  
  157.   /*
  158.    * Generate the desired line
  159.    */
  160.  
  161.   if (got_nsamp)
  162.     {
  163.       for (k = 1; k <= nsamp; k++)
  164.     {
  165.       print_number (stdout, intercept + slope * k);
  166.     }
  167.     }
  168.   else
  169.     {
  170.       while (optind < argc)
  171.     {
  172.       if (freopen (argv[optind], "r", stdin) == NULL)
  173.         {
  174.           message ("cannot open file `%s'\n", argv[optind]);
  175.           exit (1);
  176.         }
  177.       while (get_number (stdin, &x) == 1)
  178.         {
  179.           print_number (stdout, intercept + slope * x);
  180.         }
  181.       optind++;
  182.     }
  183.     }
  184.  
  185.   end_column (stdout);
  186.   return 0;
  187. }
  188.