home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / INCHCVRT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  80 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  Convert English measurement units
  5. **
  6. **  Takes command line arguments in inches and converts to:
  7. **
  8. **  1. feet and inches (expressed as floating point)
  9. **  2. feet and inches (expressed as fraction)
  10. **
  11. **  public domain demo by Bob Stout
  12. **  uses ROUND.H from SNIPPETS
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include "round.h"
  19.  
  20. #define BASE 64.0
  21.  
  22. void cnvrt_inches(double  input,
  23.                   double *feet,
  24.                   double *inches,
  25.                   double *dec_inches,
  26.                   double *num_inches,
  27.                   double *den_inches)
  28. {
  29.       /*
  30.       **  Split feet and inches
  31.       */
  32.  
  33.       *feet   = floor(input / 12.0);
  34.       *inches = fmod(input, 12.0);
  35.  
  36.       /*
  37.       **  Get integer inches and fractions
  38.       */
  39.  
  40.       *num_inches = modf(*inches, dec_inches) * BASE;
  41.  
  42.       *num_inches = fround(*num_inches, 0);
  43.       if (0.0 == *num_inches)
  44.             return;
  45.  
  46.       /*
  47.       **  Reduce fractions to lowest common denominator
  48.       */
  49.  
  50.       for (*den_inches = BASE;
  51.             0.0 == fmod(*num_inches, 2.0);
  52.             *den_inches /= 2.0, *num_inches /= 2.0)
  53.       {
  54.             ;
  55.       }
  56. }
  57.  
  58. main(int argc, char *argv[])
  59. {
  60.       double arg, feet, inches, dec, num, den, dummy;
  61.  
  62.       while (--argc)
  63.       {
  64.             arg = atof(*(++argv));
  65.             cnvrt_inches(arg, &feet, &inches, &dec, &num, &den);
  66.             printf("%f Inches = %d' %.5f\" or %d' %d",
  67.                   arg, (int)feet, inches, (int)feet, (int)dec);
  68.             if (0.0 == num)
  69.                   puts("\"");
  70.             else
  71.             {
  72.                   printf("-%d/%d\"", (int)num, (int)den);
  73.                   if (modf(num, &dummy))
  74.                         puts(" (approx.)");
  75.                   else  puts("");
  76.             }
  77.       }
  78.       return EXIT_SUCCESS;
  79. }
  80.