home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / FC20C.ZIP / TEXTNUM.C < prev    next >
C/C++ Source or Header  |  1990-08-20  |  2KB  |  77 lines

  1. #include \mc\stdio.h
  2.  
  3. /*
  4.  * Main program which processes all of its arguments, interpreting each
  5.  * one as a numeric value, and displaying that value as english text.
  6.  *
  7.  * Copyright 1990 Dave Dunfield
  8.  * All rights reserved.
  9.  */
  10. main(argc, argv)
  11.     int argc;
  12.     char *argv[];
  13. {
  14.     int i;
  15.  
  16.     if(argc < 2)                /* No arguments given */
  17.         abort("\nUse: textnum <value*>\n");
  18.  
  19.     for(i=1; i < argc; ++i) {    /* Display all arguments */
  20.         textnum(atoi(argv[i]));
  21.         putc('\n', stdout); }
  22. }
  23.  
  24. /*
  25.  * Text tables and associated function to display an unsigned integer
  26.  * value as a string of words. Note the use of RECURSION to display
  27.  * the number of thousands and hundreds.
  28.  */
  29.  
  30. /* Table of single digits and teens */
  31. char *digits[] = {
  32.     "Zero", "One", "Two", "Three", "Four", "Five", "Six",
  33.     "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",
  34.     "Thirteen", "Fourteen", "Fifteen", "Sixteen",
  35.     "Seventeen", "Eighteen", "Nineteen" };
  36.  
  37. /* Table of tens prefix's */
  38. char *tens[] = {
  39.     "Ten", "Twenty", "Thirty", "Fourty", "Fifty",
  40.     "Sixty", "Seventy", "Eighty", "Ninety" };
  41.  
  42. /* Function to display number as string */
  43. textnum(value)
  44.     unsigned value;
  45. {
  46.     char join_flag;
  47.  
  48.     join_flag = 0;
  49.  
  50.     if(value >= 1000) {                /* Display thousands */
  51.         textnum(value/1000);
  52.         fputs(" Thousand", stdout);
  53.         if(!(value %= 1000))
  54.             return;
  55.         join_flag = 1; }
  56.  
  57.     if(value >= 100) {                /* Display hundreds */
  58.         if(join_flag)
  59.             fputs(", ", stdout);
  60.         textnum(value/100);
  61.         fputs(" Hundred", stdout);
  62.         if(!(value %= 100))
  63.             return;
  64.         join_flag = 1; }
  65.  
  66.     if(join_flag)                    /* Separator if required */
  67.         fputs(" and ", stdout);
  68.  
  69.     if(value > 19) {                /* Display tens */
  70.         fputs(tens[(value/10)-1], stdout);
  71.         if(!(value %= 10))
  72.             return;
  73.         putc(' ', stdout); }
  74.  
  75.     fputs(digits[value], stdout);    /* Display digits */
  76. }
  77.