home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / UNITS / U.C < prev    next >
C/C++ Source or Header  |  1993-12-01  |  18KB  |  465 lines

  1. /* unit.c - 1/17/87 */
  2.  
  3. /*
  4.         Copyright 1987 Gregory R. Simpson
  5.  
  6. UUCP: {ihnp4, seismo, decwrl, philabs, ucbvax}!decvax!cwruecmp!ncoast!simpsong
  7. CSNET: ncoast!simpsong@case.CSNET     
  8. ARPA:  ncoast!simpsong%case.CSNET@Csnet-Relay.ARPA
  9. AT&T:  (216)-473-1019
  10.  
  11.         This notice and any statement of authorship must be reproduced
  12.         on all copies.  The author does not make any warranty expressed
  13.         or implied, or assumes any liability or responsiblity for the
  14.         use of this software.
  15.  
  16.         Any distributor of copies of this software shall grant the
  17.         recipient permission for further redistribution as permitted
  18.         by this notice.  Any distributor must distribute this software
  19.         without any fee or other monetary gains, unless expressed written
  20.         permission is granted by the author.
  21.  
  22.         This software or its use shall not be: sold, rented, leased,
  23.         traded, or otherwise marketed without the expressed written
  24.         permission of the author.
  25.  
  26.         If the software is modified in a manner creating derivative
  27.         copyrights, appropriate legends may be placed on derivative
  28.         work in addition to that set forth above.
  29.  
  30.         Permission is hereby granted to copy, reproduce, redistribute or
  31.         otherwise use this software as long as the conditions above
  32.         are meet.
  33.  
  34.         All rights not granted by this notice are reserved.
  35.  
  36.  (This copyright notice was written by, Kurt Zeilenga (zeilenga@hc.dspo.gov)
  37.   thanks Kurt... -greg)
  38.  
  39. */
  40. /* This program is for Unit Conversion... */
  41.  
  42. #include <stdio.h>
  43.  
  44. #define ESC '\033'
  45. #define True 1
  46.  
  47. /* Definition for clearing Screen */
  48.  
  49. #define Clear printf("%c[2J%c[1;1f",ESC,ESC)
  50.  
  51. #define MAXGROUP 20       /* Maximum Number of Unit Groups - 1 */
  52.  
  53. struct of_units {
  54.        char  *name;       /* Name of Unit */
  55.        float value;       /* Value - Initially set to 0 */
  56.        float conversion;  /* Conversion factor to Primary Unit */
  57.        int   counter;     /* Number of units in this group */
  58.  
  59. #include "unit_table.h"
  60.  
  61. main(argc, argv)
  62. int argc;
  63. char *argv[];
  64. {
  65.        int   group, user_unit, counter, unit_counter, sameunit;
  66.        int   comp_unit, ref_unit, chart, screen;
  67.        float user_value, from_value, to_value, temp_value, value, increment;
  68.        float primary;
  69.        char c[10], *cfile, chartfile[100];
  70.        FILE *fpchart;
  71.  
  72.        /* default values */
  73.  
  74.        group      = 1;
  75.        user_unit  = 1;
  76.        user_value = 1; 
  77.  
  78.        sameunit   = 0;  /* New Unit Group */
  79.        chart      = 0;  /* Default is standard conversion */
  80.  
  81.        /* check for chart option */
  82.  
  83.        if (argc > 2)   /* usage message if extra command line arguments */
  84.        {
  85.           usage();
  86.        } else if (argc > 1) {
  87.           if ( argv[1][0] == '-' ) {
  88.              if ( argv[1][1] == 'C' || argv[1][1] == 'c' ) {
  89.                 chart = 1;
  90.              } else {
  91.                 usage();
  92.              }
  93.           } else {
  94.             usage();
  95.           }
  96.        }
  97.        while(True)        /* loop forever... */
  98.        {
  99.            if ( sameunit == 0 ) 
  100.            {
  101.               Clear;
  102.               printf("                Unit Group Selection\n");
  103.               printf("-------------------------------------------------------------\n");
  104.               printf("1.  Length                  2. Mass                \n");
  105.               printf("3.  Speed                   4. Volume              \n");
  106.               printf("5.  Area                    6. Density             \n");
  107.               printf("7.  Time                    8. Force               \n");
  108.               printf("9.  Energy/Heat/Work       10. Pressure            \n");
  109.               printf("11. Angle                  12. Power               \n");
  110.               printf("13. Electric Charge        14. Magnetic Induction  \n");
  111.               printf("15. Light                  16. Thermal Conductivity\n");
  112.               printf("17. Coeff of Heat Transfer 18. Heat Flux           \n");
  113.               printf("19. Viscosity              20. Temperature         \n");
  114.               printf("21. Constants                                      \n");
  115.               printf("---------------------------------------------------(C)grs----\n");
  116.  
  117.  
  118.               printf("Which Type of Unit? ");
  119.               getinteger(&group, MAXGROUP);
  120.               group = group - 1;   /* since array starts at 0 */
  121.  
  122.               unit_counter = unit[group][1].counter - 1;
  123.         
  124.            } /* end of sameunit if */
  125.  
  126.               switch (group)
  127.               {
  128.                  case 19:
  129.                           temperature();
  130.                           sameunit = 0;
  131.                           break;
  132.                  case 20:
  133.                           constants();
  134.                           sameunit = 0;
  135.                           break;
  136.                  default:
  137.  
  138.               Clear;
  139.               printf("                     Unit Selection\n");
  140.               printf("-------------------------------------------------------------\n");
  141.               for (counter = 0; counter <= unit_counter; counter = counter+2)
  142.               {
  143.                      printf("%2d. %-18s                       %2d. %-18s\n",
  144.                      (counter + 1), unit[group][counter].name, 
  145.                      (counter + 2), unit[group][counter + 1].name);
  146.               }
  147.               printf("-------------------------------------------------------------\n");
  148.  
  149. /* --------------------   Standard Unit Conversion   ----------------  */
  150.  
  151.               if (chart == 0) {
  152.  
  153.                      printf("Your Unit? ");
  154.                      getinteger(&user_unit, unit_counter);
  155.                      user_unit = user_unit - 1;
  156.  
  157.                      printf("\nHow many %s ? ", unit[group][user_unit].name );
  158.                      getnumber(&user_value, unit_counter);
  159.  
  160.                      Clear;
  161.                      printf("\n     ============================================================\n");
  162.  
  163.                      primary=user_value*(1/unit[group][user_unit].conversion);
  164.  
  165.                      printf("     %16.8g %s is equivalent to:\n",
  166.                      user_value, unit[group][user_unit].name);
  167.                      printf("     ------------------------------------------------------------\n\n");
  168.                      for (counter = unit_counter; counter >= 0; counter--) 
  169.                      {
  170.                             if (counter != user_unit) 
  171.                             {
  172.                                    unit[group][counter].value = 
  173.                                    primary * unit[group][counter].conversion;
  174.                                    printf("                  %16.8g %-18s\n",
  175.                                    unit[group][counter].value,
  176.                                    unit[group][counter].name);
  177.                             }
  178.                      }
  179.  
  180.                      printf("     ============================================================\n");
  181.                      printf("\n      (R)erun Unit type, (N)ew Unit type, (Q)uit, or (C)hart: ");
  182.                      scanf("%s",c);
  183.                      if ( c[0] == 'Q' || c[0] == 'q' ) {
  184.                             printf("\n    U... Units conversion, by Gregory R. Simpson - Copyright 1987 \n");
  185.                             exit();
  186.                      } 
  187.                      else if ( c[0] == 'R' || c[0] == 'r' ) {
  188.                             sameunit = 1;
  189.                      } 
  190.                      else if ( c[0] == 'N' || c[0] == 'n' ) {
  191.                             sameunit = 0;
  192.                      } 
  193.                      else if ( c[0] == 'C' || c[0] == 'c' ) {
  194.                             chart = 1;
  195.                             sameunit = 0;
  196.                      }
  197.               } 
  198.  
  199. /* ---------------------  Chart Option   --------------------- */
  200.  
  201.               else {    
  202.  
  203.                      printf("\nYour Reference Unit?  ");
  204.                      getinteger(&ref_unit, unit_counter);
  205.                      ref_unit = ref_unit - 1;
  206.                      printf("Your Comparison Unit? ");
  207.                      getinteger(&comp_unit, unit_counter);
  208.                      comp_unit = comp_unit - 1;
  209.  
  210.                      printf("\nFrom How Many %s?  ", unit[group][ref_unit].name);
  211.                      getnumber(&from_value);
  212.                      printf("To How Many %s?    ", unit[group][ref_unit].name);
  213.                      getnumber(&to_value);
  214.                      printf("By what increment? ");
  215.                      getnumber(&increment);
  216.  
  217.                      screen = 0;
  218.                      printf("\nFilename for chart? (<CR> for screen): ");
  219.                      cfile = gets(chartfile);
  220.                      if (cfile[0] == '\0') {
  221.                         fpchart = stdout;
  222.                         screen  = 1;
  223.                      } 
  224.                      else if ( ( fpchart = fopen(cfile,"a") ) == NULL ) {
  225.                         fprintf(stderr, "Can't open Chartfile.");
  226.                         exit(1);
  227.                      }                      
  228.  
  229.                      Clear;
  230.  
  231.                      /* Error Checking and Correction... */
  232.  
  233.                      if ( from_value > to_value )
  234.                      {
  235.                         fprintf(fpchart,
  236.                         "Your From value is Greater than your To value.\n");
  237.                         fprintf(fpchart,"Therefore, I swapped them.\n");
  238.                         temp_value = from_value;
  239.                         from_value = to_value;
  240.                         to_value   = temp_value;
  241.                      }
  242.                      if ( increment < 0 )
  243.                      {
  244.                        fprintf(fpchart,
  245.                        "Since your From value is Less than your To value,\n");
  246.                        fprintf(fpchart,
  247.                        "I will make your increment positive.\n");
  248.                        increment = -increment;
  249.                      }
  250.  
  251.                      fprintf(fpchart,
  252. "------------------------------------------------------------------------------\n");
  253.  
  254.                      for (value = from_value; value <= to_value; 
  255.                           value = value + increment) 
  256.                          {
  257.                            primary=value*(1/unit[group][ref_unit].conversion);
  258.                             unit[group][comp_unit].value = 
  259.                              primary * unit[group][comp_unit].conversion;
  260.                              fprintf(fpchart,
  261.                              "| %16.8g %-18s | %16.8g %-18s |\n",
  262.                              value,unit[group][ref_unit].name,
  263.                              unit[group][comp_unit].value,
  264.                              unit[group][comp_unit].name);
  265.                              fprintf(fpchart,
  266. "------------------------------------------------------------------------------\n");
  267.                      }
  268.                      if (screen == 0) {
  269.                          fclose(fpchart);
  270.                      }
  271.                      printf("\n (R)erun Unit type, (N)ew Unit type, (Q)uit, or (S)tandard Conversions: ");
  272.                      scanf("%s",c);
  273.                      if ( c[0] == 'Q' || c[0] == 'q' ) {
  274.                             printf("\n    U... Unit Conversion, by Gregory R. Simpson - Copyright 1987 \n");
  275.                             exit();
  276.                      } 
  277.                      else if ( c[0] == 'R' || c[0] == 'r' ) {
  278.                             sameunit = 1;
  279.                      } 
  280.                      else if ( c[0] == 'N' || c[0] == 'n' ) {
  281.                             sameunit = 0;
  282.                      } 
  283.                      else if ( c[0] == 'S' || c[0] == 's' ) {
  284.                             chart = 0;
  285.                             sameunit = 0;
  286.                      }
  287.  
  288.               } /* end of chart if else */
  289.           } /* end of switch */
  290.        } /* end of while */
  291. } /* ---- end of main program ---- */
  292.  
  293. /* getinteger - Get Positive Integer Routine - G.R.Simpson */
  294.  
  295. int getinteger(choice, max)
  296.  
  297. int *choice;
  298. int max;
  299. {
  300.        int status, c;
  301.        while(1) { 
  302.               status = scanf("%d", choice);
  303.               if (status == 0)
  304.               {
  305.                      scanf("%*s");
  306.                      printf("Please Use Positive Integers; try again: ");
  307.               }
  308.               else if (status == 1)
  309.               {
  310.                      while ((c = getchar()) != '\n' && c != EOF)
  311.                             ;
  312.                      if ( c == EOF )
  313.                      {
  314.                             ungetc(c, stdin);
  315.                      }
  316.                      if ( *choice > 0 && *choice <= max+1 ) 
  317.                      {
  318.                              return status;
  319.                      }
  320.                      printf("Please use a number from 1 to %d : ", max+1);
  321.               }
  322.               else  /* status is -1 */
  323.               {
  324.               printf("End of file encountered... \n");
  325.               *choice = 1;
  326.               return status;
  327.               }
  328. }
  329. }
  330.  
  331. /* getfloat - Get Floating Point Number - G.R.Simpson */
  332.  
  333. getnumber(fchoice)
  334.  
  335. float *fchoice;
  336. {
  337.        int status, c;
  338.        while( (status = scanf("%f", fchoice)) == 0)
  339.        {
  340.               scanf("%*s");
  341.               printf("Please Use Numeric Input; try again: ");
  342.        }
  343.        if (status == 1)
  344.        {
  345.               while ((c = getchar()) != '\n' && c != EOF)
  346.                      ;
  347.               if ( c == EOF )
  348.                      ungetc(c, stdin);
  349.        }
  350.        else  /* status is -1 */
  351.        printf("End of file encountered... \n");
  352.        return status;
  353. }
  354.  
  355. /* usage - Print a usage message - G.R.Simpson */
  356.  
  357. usage()
  358. {
  359.         printf("\nusage: u [-c] \n");
  360.         printf("-c : unit chart option \n\n");
  361.         printf("U... Unit conversions; Copyright, Gregory R. Simpson 1987\n");
  362.         exit();
  363. }
  364.  
  365. /* temperature - G.R.Simpson */
  366.  
  367. temperature()
  368. {
  369. static char *scale[4] = { "Fahrenheit", "Celsius", "Rankine", "Kelvin" };
  370. float fahrenheit, celsius, rankine, kelvin;
  371. float temp;
  372. char c[2];
  373. int    choice, tempagain;
  374.  
  375.         tempagain = 1;
  376.  
  377.         while(tempagain) {
  378.               Clear;
  379.               printf("                     Unit Selection\n");
  380.               printf("-------------------------------------------------------------\n");
  381.                      printf("1. Fahrenheit                    2. Celsius \n");
  382.                      printf("3. Rankine                       4. Kelvin \n");
  383.               printf("-------------------------------------------------------------\n");
  384.  
  385.         printf("Your Temperature Scale? ");
  386.         getinteger(&choice,3);
  387.         printf("\n How many degrees %s? ", scale[choice-1]);
  388.         getnumber(&temp);
  389.         switch(choice) {
  390.               case 1 :
  391.                     fahrenheit = temp;
  392.                     break;
  393.               case 2 :
  394.                     fahrenheit = temp*(9.0/5.0) + 32.0;
  395.                     break;
  396.               case 3 :
  397.                     fahrenheit = temp - 459.67;
  398.                     break;
  399.               case 4 :
  400.                     fahrenheit = ((temp-273.15)*(9.0/5.0)) + 32.0;
  401.                     break;
  402.         }
  403.         celsius = (fahrenheit - 32) * (5.0/9.0);
  404.         rankine = fahrenheit + 459.67;
  405.         kelvin  = celsius + 273.15;
  406.  
  407.         Clear;
  408.         printf("     %16.8g degrees %s is equivalent to:\n",
  409.                      temp,scale[choice-1]);
  410.         printf("                  ------------------------------------\n");
  411.         if (choice != 1) printf("                  %16.8g degrees Fahrenheit\n", fahrenheit);
  412.         if (choice != 2) printf("                  %16.8g degrees Celsius\n",    celsius);
  413.         if (choice != 3) printf("                  %16.8g degrees Rankine\n",    rankine);
  414.         if (choice != 4) printf("                  %16.8g degrees Kelvin\n",     kelvin);
  415.  
  416.         printf("                  ------------------------------------\n");
  417.         printf("\n (R)erun Unit type, (N)ew Unit type, or (Q)uit: ");
  418.         scanf("%s",c);
  419.         if ( c[0] == 'Q' || c[0] == 'q' ) {
  420.            printf("\n    U... Unit Conversion, by Gregory R. Simpson - Copyright 1987 \n");
  421.            exit();
  422.         } 
  423.         else if ( c[0] == 'R' || c[0] == 'r' ) {
  424.            tempagain = 1;
  425.         } 
  426.         else if ( c[0] == 'N' || c[0] == 'n' ) {
  427.            tempagain = 0;
  428.         } 
  429.   } /* end while(tempagain) */
  430.         c[0] = '\0';
  431.         return;
  432. }
  433.  
  434. /* constants - G.R.Simpson */
  435.  
  436. constants()
  437. {
  438.         char c[10];
  439.  
  440.         Clear;         
  441.         printf("\npi                      =  3.141592653589793238462643\n");
  442.         printf("e                       =  2.718281828459045235360287\n");
  443.         printf("atomic mass unit        =  1.66053 e-27 kg, e-24 gm \n");
  444.         printf("Avogadro's number N     =  6.02217 e23 mole^-1 \n");
  445.         printf("Boltzmann's constant    =  R/N = 1.3806 e-23 J/K, e-16 erg/K = 8.61708 e-5 eV/K \n");
  446.         printf("gas constant            =  8.3143 J/mole-K = 0.082054 l-atm/mole-K\n");
  447.         printf("gravitational constant  =  6.673 e-11 N-m^2/kg^2, J-m/kg^2, \n");
  448.         printf("mass of electron        =  9.10956 e-31 kg, e-28 gm =  5.48593 e-4 amu \n");
  449.         printf("mass of proton          =  1.67261 e-27 kg, e-24 gm =  1.0072766 amu \n");
  450.         printf("Planck's constant       =  h =  6.62620 e-34 J-sec,  e-27 erg-sec \n");
  451.         printf("h bar                   =  h/2*pi = 1.05459 e-34 J-sec, e-27 erg-sec \n");
  452.         printf("speed of light          =  2.997925 e8 m/sec, e10 cm/sec \n");
  453.         printf("Stefan-Boltzmann constant =  5.670 e-8 W/m^2-K^4 \n");
  454.         printf("\n      <CR> to continue or (Q)uit: ");
  455.         gets(c);
  456.         if ( c[0] == 'Q' || c[0] == 'q' ) {
  457.            printf("\n    U... Unit Conversion, by Gregory R. Simpson - Copyright 1987 \n");
  458.            exit();
  459.         } else {
  460.         c[0] = '\0';
  461.         return;
  462.         }
  463. }
  464.