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

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/round.c,v 1.2 90/10/25 17:13:51 toy Exp $
  3.  * NAME
  4.  *    round - round the elements of a vector
  5.  *
  6.  * SYNOPSIS
  7.  *    round [-H] [-P prec] [-c cols] [-p p] [-s sigdig] [vectors...]
  8.  *
  9.  * DESCRIPTION
  10.  *    Round the elements as specified.
  11.  *
  12.  * HISTORY
  13.  * $Log:    round.c,v $
  14.  * Revision 1.2  90/10/25  17:13:51  toy
  15.  * Was printing out an extra number at end-of-file.
  16.  *
  17.  * Revision 1.1  90/09/04  18:31:08  toy
  18.  * Initial revision
  19.  *
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <string.h>
  25.  
  26. #if    defined(__STDC__) || defined(__GNUC__)
  27. #include <stddef.h>
  28. #include <stdlib.h>
  29. #endif
  30.  
  31. #include "gps.h"
  32.  
  33. #define    DEF_PROGNAME    "round"
  34.  
  35. static const char RCSID[] = "@(#) $Id: round.c,v 1.2 90/10/25 17:13:51 toy Exp $";
  36.  
  37. extern int getopt ();
  38. extern int optind;
  39. extern char *optarg;
  40.  
  41. const char *progname;
  42.  
  43. #define    OPT_STRING    "HP:c:p:s:"
  44.  
  45. void
  46. help ()
  47. {
  48.   static const char *help_strings[] =
  49.   {
  50.     "\t-H\tThis help\n",
  51.     "\t-c c\tSet number of output elements per line to n.  (Def = 1.)\n",
  52.     "\t-p p\tNumbers are rounded to p places following the decimal\n",
  53.     "\t\tpoint with p is in the range 0 to 9.\n",
  54.     "\t-s s\tNumbers are rounded to s significant digits, with s in\n",
  55.     "\t\tthe range 0 to 9.  The default is 9.\n",
  56.     "Output is the rounded value of each element of the given vectors.\n",
  57.     NULL
  58.   };
  59.   (void) fprintf (stderr, "%s\n", RCSID);
  60.   (void) fprintf (stderr, "Usage:  %s [-H] [-P prec] [-c cols] [vector ...]\n", progname);
  61.   print_help_strings (help_strings);
  62. }
  63.  
  64.  
  65.  
  66. #if    defined(__STDC__) || defined(__GNUC__)
  67. void
  68. do_func (FILE * fp)
  69. #else
  70. void
  71. do_func (fp)
  72.      FILE *fp;
  73. #endif
  74. {
  75.   int rc;
  76.   long rank;
  77.   double x;
  78.  
  79.   rank = 0;
  80.   do
  81.     {
  82.       rc = get_number (fp, &x);
  83.       if (rc == 1)
  84.     {
  85.       print_number (stdout, x);
  86.     }
  87.       else if (rc == 0)
  88.     {
  89.       message ("Error reading number\n");
  90.     }
  91.   } while (rc == 1);
  92. }
  93.  
  94.  
  95. int
  96. main (argc, argv)
  97.      int argc;
  98.      char *argv[];
  99. {
  100.   int option;
  101.   int errcnt;
  102.   int precision;
  103.   double total;
  104.  
  105.   progname = get_my_name (argv[0], DEF_PROGNAME);
  106.  
  107.   /*
  108.    * Read options
  109.    */
  110.  
  111.   precision = 9;
  112.   errcnt = 0;
  113.  
  114.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  115.     {
  116.       switch (option)
  117.     {
  118.     case 'H':        /* Help                 */
  119.       help ();
  120.       exit (0);
  121.       break;
  122.     case 'c':        /* Number of columns         */
  123.       (void) set_max_columns (atoi (optarg));
  124.       break;
  125.     case 'p':        /* Places after decimal point     */
  126.       set_f_format ();
  127.       if (sscanf (optarg, "%d", &precision) != 1)
  128.         {
  129.           message ("cannot read -p value:  `%s'\n", optarg);
  130.           errcnt++;
  131.         }
  132.       break;
  133.     case 's':        /* Significant digits         */
  134.       if (sscanf (optarg, "%d", &precision) != 1)
  135.         {
  136.           message ("cannot read -s value:  `%s'\n", optarg);
  137.           errcnt++;
  138.         }
  139.       break;
  140.     default:
  141.       break;
  142.     }            /* endswitch     */
  143.     }                /* endwhile     */
  144.  
  145.   /*
  146.    * Check options
  147.    */
  148.  
  149.   if (errcnt > 0)
  150.     {
  151.       help ();
  152.     }
  153.   if (precision < 0)
  154.     {
  155.       precision = 0;
  156.     }
  157.   else if (precision > 9)
  158.     {
  159.       precision = 9;
  160.     }
  161.   set_precision (precision);
  162.  
  163.  
  164.   /*
  165.    * If no files are listed, use stdin.  Otherwise use
  166.    * the given files
  167.    */
  168.  
  169.   if (optind == argc)
  170.     {
  171.       do_func (stdin);
  172.     }
  173.   else
  174.     {
  175.       total = 0;
  176.       while (optind < argc)
  177.     {
  178.       if (freopen (argv[optind], "r", stdin) == NULL)
  179.         {
  180.           message ("Cannot open `%s'\n", argv[optind]);
  181.         }
  182.       else
  183.         {
  184.           do_func (stdin);
  185.         }
  186.       optind++;
  187.     }            /* endwhile     */
  188.     }                /* endif     */
  189.  
  190.   end_column (stdout);
  191.   return 0;
  192. }
  193.