Advertisement
Guest User

unitConverter

a guest
Aug 27th, 2010
1,677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.82 KB | None | 0 0
  1. /*
  2.  * File:   unitConverter.c
  3.  * Author: Nathan
  4.  *
  5.  * A Book On C: Fourth Edition. Chapter 2, Exercise 9
  6.  * A program that converts between ounces, pounds, grams, and kilograms
  7.  * uses symbolic constants defined in conversions.h for the conversion factors
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. #include "conversions.h" //contains conversion factors
  15.  
  16.  
  17. char fromUnit[50] = "No Unit";
  18. char toUnit[50] = "No Unit";
  19. double input = 0.0;
  20. double output = 42.0; //the answer to life, the universe, and everything
  21.  
  22. int main(int argc, char** argv) {
  23.  
  24. beginning:
  25.  
  26.     printf("Input the value you would like to convert.\n");
  27.    if  (scanf("%lf", &input) != 1){//how do I ensure this value is not passed to convertmore?
  28.        printf("Invalid value. It must be a double.\n");
  29.        goto convertmore;
  30.    } //takes an input value as a double
  31.    
  32.  
  33.     printf("Input the unit of your value. (ounce/pound/gram/kilogram)\n");
  34.     scanf("%s", fromUnit); //takes a unit type as a string
  35.  
  36.     printf("Input the unit to convert the value to. (ounce/pound/gram/kilogram)\n");
  37.     scanf("%s", toUnit); //takes a unit type as a string
  38.  
  39.  
  40.  
  41.  
  42.     if (strcmp(fromUnit, "ounce") == 0) { //from ounces
  43.         if (strcmp(toUnit, "pound") == 0) { //to pounds
  44.             output = input / 16; //an ounce is 1/16 of a pound
  45.         } else if (strcmp(toUnit, "gram") == 0) { //to grams
  46.             output = input * OUNCESTOGRAMS;
  47.         } else if (strcmp(toUnit, "kilogram") == 0) { //to kilograms
  48.             output = (input * OUNCESTOGRAMS) / 1000; // convert ounces to grams first,  and then to kilograms
  49.         }
  50.  
  51.     } else if (strcmp(fromUnit, "pound") == 0) { //from pounds
  52.         if (strcmp(toUnit, "ounce") == 0) { //to ounces
  53.             output = input * 16; //an ounce is 16 pounds
  54.         } else if (strcmp(toUnit, "gram") == 0) { //to grams
  55.             output = input * POUNDSTOGRAMS;
  56.         } else if (strcmp(toUnit, "kilogram") == 0) {//to kilograms
  57.             output = (input * POUNDSTOGRAMS) / 1000; //convert pounds to grams first, and then to kilograms
  58.         }
  59.  
  60.  
  61.     } else if (strcmp(fromUnit, "gram") == 0) { //from grams
  62.         if (strcmp(toUnit, "ounce") == 0) { //to ounces
  63.             output = input * GRAMSTOOUNCES;
  64.         } else if (strcmp(toUnit, "pound") == 0) { //to pounds
  65.             output = input * GRAMSTOPOUNDS;
  66.         } else if (strcmp(toUnit, "kilogram") == 0) {//to kilograms
  67.             output = input / 1000; // kilogram is 1/1000 of a gram
  68.         }
  69.  
  70.  
  71.     } else if (strcmp(fromUnit, "kilogram") == 0) { //from kilograms
  72.         if (strcmp(toUnit, "ounce") == 0) { //to ounces
  73.             output = (input * 1000) * GRAMSTOOUNCES; //convert to grams first, then grams to ounces
  74.         } else if (strcmp(toUnit, "pound") == 0) {
  75.             output = (input * 1000) * GRAMSTOPOUNDS; //convert to grams first, and then to pounds
  76.         } else if (strcmp(toUnit, "gram") == 0) {
  77.             output = input * 1000; // a gram is 1000 kilograms
  78.         }
  79.  
  80.     } else if (strcmp(fromUnit, toUnit) == 0) { //if the units are the same
  81.         output = input; //the output is the same as the input
  82.     }
  83.  
  84.     else {
  85.         printf("Unsupported unit type\n");
  86.         goto convertmore;
  87.     }
  88.  
  89.  
  90.     printf("%lf %s is equal to %lf %s\n", input, fromUnit, output, toUnit);
  91.  
  92. convertmore:
  93.     printf("Would you like to perform additional conversions? (y/n)\n");
  94.     char str[50];
  95.     scanf("%s", str);
  96.     if (strcmp(str, "y") == 0 || strcmp(str, "yes") == 0) {
  97.         goto beginning;
  98.     } else {
  99.         printf("Exiting.\n");
  100.         return (EXIT_SUCCESS);
  101.     }
  102. }
  103.  
  104. /*
  105.  * File:   conversions.h
  106.  * Author: Nathan
  107.  */
  108.  
  109.     #define POUNDSTOGRAMS 453.59237
  110.     #define GRAMSTOPOUNDS 0.00220462262
  111.     #define OUNCESTOGRAMS 28.3495231
  112.     #define GRAMSTOOUNCES 0.0352739619
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement