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

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/prnum.c,v 1.5 90/09/10 19:03:03 toy Exp $
  3.  * NAME
  4.  *    print_number, set_max_columns, set_precision, end_column - print out numbers in columns
  5.  *
  6.  * SYNOPSIS
  7.  *    void print_number(fp, value)
  8.  *    FILE    *fp;
  9.  *    double    value;
  10.  *
  11.  *    int set_max_columns(max)
  12.  *    int    max;
  13.  *
  14.  *    int set_precision(prec)
  15.  *    int    prec;
  16.  *
  17.  *    void set_f_format()
  18.  *
  19.  *    void set_g_format()
  20.  *
  21.  *    void end_column(fp)
  22.  *    FILE    *fp;
  23.  *
  24.  *    void next_column()
  25.  *
  26.  * DESCRIPTION
  27.  *    A table of numbers is printed out in neat columns.  The
  28.  *    routine "print_number" prints "value" to the file "fp" and
  29.  *    places newlines in the correct place.
  30.  *
  31.  *    The number of columns to be printed is set by the routine
  32.  *    set_max_columns.  If a non-positive number is set, the
  33.  *    default of 5 is used.  The previous value of the number
  34.  *    of columns is returned.
  35.  *
  36.  *    The precision of the numbers printed is controlled by the
  37.  *    routine "set_precision".  The precision is the number of
  38.  *    significant digits in the printed result.  If a non-
  39.  *    positive number value for "prec" is specified, the
  40.  *    default is used.  The previous precision is returned.
  41.  *
  42.  *    To set the format of the numbers, use set_f_format or
  43.  *    set_g_format, which sets the format to f or g, respectively.
  44.  *    (See printf(3) for an explanation).  The default is g format.
  45.  *
  46.  *    To neatly terminate the columnar output, use the
  47.  *    routine "end_column" to print a newline character,
  48.  *    if needed.
  49.  *
  50.  *    The routine "next_column" advances the column counter
  51.  *    to the next column and prints out the appropriate
  52.  *    column separator (either a tab or a newline).
  53.  *
  54.  * BUGS
  55.  *    The routines are not reentrant so if printing is done
  56.  *    to more than one file at a time, the columns are wrong.
  57.  *
  58.  * HISTORY
  59.  * $Log:    prnum.c,v $
  60.  * Revision 1.5  90/09/10  19:03:03  toy
  61.  * Added next_column routine.
  62.  *
  63.  * Revision 1.4  90/09/10  12:23:04  toy
  64.  * Need to included gps.h.
  65.  *
  66.  * Revision 1.3  90/09/04  18:31:27  toy
  67.  * Added routines to allow printing numbers in f or g format.
  68.  *
  69.  * Revision 1.2  90/09/02  11:16:06  toy
  70.  * More intelligent printing of numbers by tabs and
  71.  * newlines at the appropriate places.
  72.  *
  73.  * Revision 1.1  90/09/01  11:35:18  toy
  74.  * Initial revision
  75.  *
  76.  */
  77.  
  78. #include <stdio.h>
  79.  
  80. #include "gps.h"
  81.  
  82. #ifndef    lint
  83. static char RCSID[] = "@(#) $Id: prnum.c,v 1.5 90/09/10 19:03:03 toy Exp $";
  84. #endif
  85.  
  86. #define    DEF_PRECISION    6
  87. #define    DEF_MAX_COLUMNS    5
  88. #define    DEF_FORMAT    "%.*g"
  89.  
  90. static const char *format = DEF_FORMAT;
  91. static int max_columns = DEF_MAX_COLUMNS;
  92. static int precision = DEF_PRECISION;
  93. static int column = 0;
  94.  
  95. #if    defined(__STDC__) || defined(__GNUC__)
  96. int
  97. set_max_columns (int max)
  98. #else
  99. int
  100. set_max_columns (max)
  101.      int max;
  102. #endif
  103. {
  104.   int old_max;
  105.  
  106.   old_max = max_columns;
  107.   max_columns = (max <= 0) ? DEF_MAX_COLUMNS : max;
  108.  
  109.   return old_max;
  110. }
  111.  
  112. #if    defined(__STDC__) || defined(__GNUC__)
  113. int
  114. set_precision (int prec)
  115. #else
  116. int
  117. set_precision (prec)
  118.      int prec;
  119. #endif
  120. {
  121.   int old_prec;
  122.  
  123.   old_prec = precision;
  124.   precision = (prec <= 0) ? DEF_PRECISION : prec;
  125.  
  126.   return old_prec;
  127. }
  128.  
  129. #if    defined(__STDC__) || defined(__GNUC__)
  130. void
  131. print_number (FILE * fp, double value)
  132. #else
  133. void
  134. print_number (fp, value)
  135.      FILE *fp;
  136.      double value;
  137. #endif
  138. {
  139.   (void) fprintf (fp, format, precision, value);
  140.  
  141.   if (++column >= max_columns)
  142.     {
  143.       putc ('\n', fp);
  144.       column = 0;
  145.     }
  146.   else
  147.     {
  148.       putc ('\t', fp);
  149.     }
  150. }
  151.  
  152. #if    defined(__STDC__) || defined(__GNUC__)
  153. void
  154. set_f_format (void)
  155. #else
  156. void
  157. set_f_format ()
  158. #endif
  159. {
  160.   format = "%.*f";
  161. }
  162.  
  163. #if    defined(__STDC__) || defined(__GNUC__)
  164. void
  165. set_g_format (void)
  166. #else
  167. void
  168. set_g_format ()
  169. #endif
  170. {
  171.   format = DEF_FORMAT;
  172. }
  173.  
  174. #if    defined(__STDC__) || defined(__GNUC__)
  175. void
  176. end_column (FILE * fp)
  177. #else
  178. void
  179. end_column (fp)
  180.      FILE *fp;
  181. #endif
  182. {
  183.   if (column != 0)
  184.     {
  185.       putc ('\n', fp);
  186.     }
  187.   column = 0;
  188. }
  189.  
  190.  
  191. #if    defined(__STDC__) || defined(__GNUC__)
  192. void
  193. next_column (FILE * fp)
  194. #else
  195. void
  196. next_column (fp)
  197.      FILE *fp;
  198. #endif
  199. {
  200.   if (++column >= max_columns)
  201.     {
  202.       putc ('\n', fp);
  203.       column = 0;
  204.     }
  205.   else
  206.     {
  207.       putc ('\t', fp);
  208.     }
  209. }
  210.