Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.26 KB | None | 0 0
  1. /***********************************************
  2. Program: daily16.c
  3. Author: Thanh Ly
  4. Date: 6/14/2019
  5. Sources of Help: Dr. Adams
  6. Time Spent: 15 minutes
  7. Purpose: Given three options 1. Ask user to convert to lengths.
  8. 2. Ask user to convert to weights. 0. Ask user to exit program
  9. Convert user input from emperical to metric or vice versa.
  10. ***********************************************/
  11.  
  12. #include <stdio.h>
  13. #include <math.h>
  14. #pragma warning(disable : 4996);
  15.  
  16. void convert_lengths(void);
  17. void convert_weights();
  18. void length_to_metric(void);
  19. void length_to_us(void);
  20. void weight_to_metric();
  21. void weight_to_us(void);
  22. void clear_keyboard_buffer(void);
  23. void userInput(double* first_number, double* second_number);
  24.  
  25. /* Lengths Conversions */
  26. #define INCES_TO_FEET 12
  27. #define FEET_TO_METERS 0.3048
  28. #define METERS_TO_CM 100
  29.  
  30. /*Weights Conversions */
  31. #define OUNCES_TO_POUND 16
  32. #define POUND_TO_KILOGRAM 2.2046
  33. #define KILOGRAM_TO_GRAM 1000
  34.  
  35. int main(int argc, char* argv[])
  36. {
  37.     int options;
  38.     //asks user if they want to convert values that are lengths or weights.
  39.     do {
  40.         printf("\t[1] Convert Lengths\n");
  41.         printf("\t[2] Convert Weights\n");
  42.         printf("\t[0] Exit\n");
  43.         printf("Choose from the menu: ");
  44.         scanf("%d", &options);
  45.         printf("\n");
  46.         clear_keyboard_buffer();
  47.  
  48.         switch (options)
  49.         {
  50.         case 1:
  51.             convert_lengths();
  52.             break;
  53.         case 2:
  54.             convert_weights();
  55.             break;
  56.         }
  57.     } while (options != 0);
  58.  
  59.     return 0;
  60. }
  61.  
  62. /***********************************************************/
  63. /*Start of convert lengths functions*/
  64. /***********************************************************/
  65. void convert_lengths(void)
  66. {
  67.     int options;
  68.     do {
  69.         printf("\t\t[1] Convert feet/inches to meters/centimeters: \n");
  70.         printf("\t\t[2] Convert meters/centimeters to feet/inches: \n");
  71.         printf("\t\t[0] Exit: \n");
  72.         printf("Choose from the menu: ");
  73.         scanf("%d", &options);
  74.         clear_keyboard_buffer();
  75.  
  76.         switch (options)
  77.         {
  78.         case 1:
  79.             length_to_metric();
  80.             break;
  81.         case 2:
  82.             length_to_us();
  83.             break;
  84.         }
  85.     } while (options != 0);
  86.     return;
  87. }
  88.  
  89. void length_to_metric(void)
  90. {
  91.     double total_meters = 0, temp = 0, temp2 = 0;
  92.     double feet, inches;
  93.     printf("Enter emperical unit in feet , inches: ");
  94.     userInput(&feet, &inches);
  95.  
  96.     printf("%1.0lf feet %.2lf inches to %.2lf meters %.2lf centimeters\n\n", feet, inches, temp2, temp);
  97. }
  98. void length_to_metric_math(double* total_meters, double* temp, double* temp2)
  99. {
  100.     if(inches >= 12)
  101.     {
  102.         inches = inches / INCES_TO_FEET;
  103.     }
  104.     total_meters = ((feet * INCES_TO_FEET) + inches) / INCES_TO_FEET;
  105.     total_meters = total_meters * FEET_TO_METERS;
  106.     temp = total_meters * 100;
  107.     temp = fmod(temp, 100);
  108.     temp2 = ((total_meters * 100) - temp) / 100;
  109. }
  110.  
  111. void length_to_us(void)
  112. {
  113.     double total_feet = 0, temp = 0;
  114.     double meters, centimeters;
  115.     printf("Enter metrics unit in meters , centimeters: ");
  116.     userInput(&meters, &centimeters);
  117.     total_feet = ((meters * METERS_TO_CM) + centimeters) / METERS_TO_CM;
  118.     temp = total_feet;
  119.     total_feet = total_feet / FEET_TO_METERS;
  120.     printf("%lf meters/centimeters to %lf feet/inches \n\n", temp, total_feet);
  121. }
  122.  
  123. /***********************************************************/
  124. /* start of convert weights function */
  125. /***********************************************************/
  126.  
  127. void convert_weights(void)
  128. {
  129.     int options;
  130.     do {
  131.         printf("\t\t[1] Convert weights pounds/ounces to kilograms/grams: \n");
  132.         printf("\t\t[2] Convert weights kilograms/grams to pounds/ounces: \n");
  133.         printf("\t\t[0] Exit: \n");
  134.         printf("Choose from the menu: ");
  135.         scanf("%d", &options);
  136.         clear_keyboard_buffer();
  137.  
  138.         switch (options)
  139.         {
  140.         case 1:
  141.             weight_to_metric();
  142.             break;
  143.         case 2:
  144.             weight_to_us();
  145.             break;
  146.         }
  147.     } while (options != 0);
  148.     return;
  149. }
  150.  
  151. void weight_to_metric(void)
  152. {
  153.     double total_pounds = 0, temp = 0;
  154.     double pounds, ounces;
  155.     printf("Enter emperical unit in pounds , ounces: ");
  156.     userInput(&pounds, &ounces);
  157.     total_pounds = ((pounds * OUNCES_TO_POUND) + ounces) / OUNCES_TO_POUND;
  158.     temp = total_pounds;
  159.     total_pounds = total_pounds / POUND_TO_KILOGRAM;
  160.     printf("%lf pounds/ounces to  %lf kilograms/grams\n\n", temp, total_pounds);
  161. }
  162.  
  163. void weight_to_us(void)
  164. {
  165.     double total_kilograms = 0, temp = 0;
  166.     double kilograms, grams;
  167.     printf("Enter emperical unit in kilograms , grams: ");
  168.     userInput(&kilograms, &grams);
  169.     total_kilograms = ((kilograms * KILOGRAM_TO_GRAM) + grams) / KILOGRAM_TO_GRAM;
  170.     temp = total_kilograms;
  171.     total_kilograms = total_kilograms * POUND_TO_KILOGRAM;
  172.     printf("%lf kilograms/grams to %lf pounds/ounces \n\n", temp, total_kilograms);
  173. }
  174.  
  175. /***********************************************************/
  176. /* Start of userInput function */
  177. /***********************************************************/
  178. void userInput(double* first_number, double* second_number)
  179. {
  180.     int noc;
  181.     noc = scanf("%lf %lf", first_number, second_number);
  182.     clear_keyboard_buffer();
  183.     while (noc == 0)
  184.     {
  185.         printf("Please enter two number seperated by a space: ");
  186.         noc = scanf("%lf %lf", first_number, second_number);
  187.         clear_keyboard_buffer();
  188.     }
  189.     return;
  190. }
  191.  
  192. void clear_keyboard_buffer(void)
  193. {
  194.     char c;
  195.     int noc;
  196.     noc = scanf("%c", &c);
  197.     while (c != '\n' && noc != EOF)
  198.     {
  199.  
  200.         noc = scanf("%c", &c);
  201.     }
  202. }
  203. /* End of userInput function */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement