home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / XBBS7200.ZIP / XBBS7200.TAR / today / nbrtxt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-11-14  |  3.4 KB  |  170 lines

  1. /*
  2.  *              Integer to Readable ASCII Conversion Routine.
  3.  *
  4.  * Synopsis:
  5.  *
  6.  *      char *nbrtxt(buffer, value, ordinal)
  7.  *      char    *buffer;        -- The output buffer
  8.  *      int     value;          -- The number to output
  9.  *      int     ordinal;        -- Non-zero for ordinal number
  10.  *
  11.  *
  12.  * The value is converted to a readable number and put in the output
  13.  * buffer (null-terminated).  A pointer to the first free location
  14.  * in the buffer (i.e., the null) is returned.  The ordinal
  15.  * flag distinguishes between cardinal and ordinal numbers:
  16.  *
  17.  *      nbrtxt(buffer, 1, 0) = "one"
  18.  *      nbrtxt(buffer, 1, 1) = "first"
  19.  *
  20.  * The longest output string is:
  21.  *
  22.  *      Twenty-seven thousand, three hundred and seventy-seventh.
  23.  *
  24.  *
  25.  *
  26.  *              Copy a String
  27.  *
  28.  * Synopsis
  29.  *
  30.  *      char *copyst(out, in)
  31.  *      char    *out;           -- The output string
  32.  *      char    *in;            -- The input string
  33.  *
  34.  * The input string is copied into the output string.  Copyst returns
  35.  * a pointer to the null trailer.
  36.  *
  37.  */
  38.  
  39. extern char     *nbrtxt();
  40. extern char     *copyst();
  41.  
  42. static char *cardinal[] = {
  43.     "zero",
  44.     "one",
  45.     "two",
  46.     "three",
  47.     "four",
  48.     "five",
  49.     "six",
  50.     "seven",
  51.     "eight",
  52.     "nine",
  53.     "ten",
  54.     "eleven",
  55.     "twelve",
  56.     "thirteen",
  57.     "fourteen",
  58.     "fifteen",
  59.     "sixteen",
  60.     "seventeen",
  61.     "eighteen",
  62.     "nineteen"
  63. };
  64.  
  65. static char *ordinal[] = {
  66.     "zeroth",
  67.     "first",
  68.     "second",
  69.     "third",
  70.     "fourth",
  71.     "fifth",
  72.     "sixth",
  73.     "seventh",
  74.     "eighth",
  75.     "ninth",
  76.     "tenth",
  77.     "eleventh",
  78.     "twelfth"
  79. };
  80.  
  81. static char *twenties[] = {
  82.     "twen",
  83.     "thir",
  84.     "for",
  85.     "fif",
  86.     "six",
  87.     "seven",
  88.     "eigh",
  89.     "nine"
  90. };
  91.  
  92. char *nbrtxt(buffer, datum, ordflag)
  93. char    *buffer;                        /* Output string buffer         */
  94. int     datum;                          /* what to translate            */
  95. int     ordflag;                        /* 0 if cardinal, 1 if ordinal  */
  96. /*
  97.  * Translate a number to a readable text string, punctuation and all.
  98.  * If ordflag is non-zero, ordinal numbers ("first, second") will
  99.  * be generated, rather than cardinal ("one, two").
  100.  * Note: nbrtxt() is recursive.
  101.  */
  102. {
  103.  
  104.     register int value;
  105.     register char   *op;
  106.  
  107.     op = buffer;
  108.     value = datum;
  109.     if (value < 0) {
  110.         op = copyst(op, "minus ");
  111.         value = (-value);
  112.         if (value < 0) {                /* Hack -32768          */
  113.             op = copyst(op, twenties[1]);
  114.             value = 2768;
  115.         }
  116.     }
  117.     if (value >= 1000) {
  118.         op = nbrtxt(op, value/1000, 0);
  119.         op = copyst(op, " thousand");
  120.         value = value % 1000;
  121.         if (value == 0) goto exit;
  122.         op = copyst(op, (value >= 100) ? ", " : " and ");
  123.     }
  124.     if (value >= 100) {
  125.         op = copyst(op, cardinal[value/100]);
  126.         op = copyst(op, " hundred");
  127.         value = value % 100;
  128.         if (value == 0) goto exit;
  129.         op = copyst(op, " ");
  130.     }
  131.     if (value >= 20) {
  132.         if (value == 90 && ordflag)
  133.             return(copyst(op, "nintieth"));
  134.         op = copyst(op, twenties[(value-20) / 10]);
  135.         value = value % 10;
  136.         if (value == 0) {
  137.             return(copyst(op, (ordflag) ? "tieth" : "ty"));
  138.         }
  139.         op = copyst(op, "ty-");
  140.     }
  141.     if (value <= 12) {
  142.         return(copyst(op,
  143.             (ordflag) ? ordinal[value] : cardinal[value]));
  144.     }
  145.     op = copyst(op, cardinal[value]);       /* fourteen, fourteenth */
  146.     /*
  147.      * Here on 100, 14000, etc.
  148.      */
  149. exit:   if (ordflag) op = copyst(op, "th");
  150.     return(op);
  151. }
  152.  
  153. char *
  154. copyst(buffer, string)
  155. char    *buffer;
  156. char    *string;
  157. /*
  158.  * Copy a string into buffer.  Return the free pointer.
  159.  */
  160. {
  161.     register char   *ip;
  162.     register char   *op;
  163.  
  164.     ip = string;
  165.     op = buffer;
  166.  
  167.     while ((*op = *ip++)) op++;
  168.     return (op);
  169. }
  170.