Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.59 KB | None | 0 0
  1. //preprocessing directives//
  2. #include <stdio.h>
  3. #include <math.h>
  4. #define INCHES_TO_FEET (1/12.0)
  5. #define METERS_TO_FEET 0.3048
  6. #define CENTIMETERS_TO_METER (1/100.0)
  7. #define POUNDS_TO_KILO (1/2.2046)
  8. #define GRAMS_TO_KILO (1/1000.0)
  9. #define OUNCES_TO_POUND (1/16.0)
  10.  
  11. //function declarations//
  12. void clear_keyboard_buffer(void);
  13. void convert_length(void);
  14. void convert_weight(void);
  15. void length_to_metric(void);
  16. void length_to_us(void);
  17. void weight_to_metric(void);
  18. void weight_to_us(void);
  19. double us_to_metric_length_input(void);
  20. double us_to_metric_length_conversion(double us_length);
  21. void print_metric_length_conversion(double converted_length);
  22. double metric_to_us_length_input(void);
  23. double metric_to_us_length_conversion(double metric_length);
  24. void print_us_length_conversion(double converted_length);
  25. double us_to_metric_weight_input(void);
  26. double us_to_metric_weight_conversion(double us_weight);
  27. void print_metric_weight_conversion(double converted_weight);
  28. double metric_to_us_weight_input(void);
  29. double metric_to_us_weight_conversion(double metric_weight);
  30. void print_us_weight_conversion(double converted_weight);
  31.  
  32. int main(int argc, char* argv[])
  33. {
  34.     int choice;
  35.     //asks user what they wish to convert, keeps asking until valid input//
  36.     do
  37.     {
  38.         printf("Press (1) to convert lengths, (2) to convert weights, or (0) to exit: ");
  39.         scanf("%d", &choice);
  40.         clear_keyboard_buffer();
  41.         if (choice != 0 && choice != 1 && choice != 2)
  42.         {
  43.             printf("Please enter either a 1 for converting lengths, 2 for converting weights, or 0 to exit.\n");
  44.         }
  45.         switch (choice)
  46.         {
  47.         case 1:
  48.             convert_length();
  49.             break;
  50.         case 2:
  51.             convert_weight();
  52.             break;
  53.  
  54.  
  55.         }
  56.     } while (choice != 0);
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.     return 0;
  64. }
  65.  
  66.  
  67. void convert_length(void)
  68. {
  69.     int choice;
  70.     //asks user if they wish to convert to metric, US, or exit to main//
  71.     do
  72.     {
  73.         printf("\nPress (1) to convert from feet/inches to meters/centimeters\n");
  74.         printf("Press (2) to convert from meters/centimeters to feet/inches\n");
  75.         printf("Press (0) to exit.\n");
  76.         scanf("%d", &choice);
  77.         clear_keyboard_buffer();
  78.         if (choice != 0 && choice != 1 && choice != 2)
  79.         {
  80.             printf("Please enter a valid input.\n");
  81.         }
  82.         //switch to handle user input//
  83.         switch (choice)
  84.         {
  85.         case 1:
  86.             length_to_metric();
  87.             break;
  88.         case 2:
  89.             length_to_us();
  90.             break;
  91.  
  92.  
  93.         }
  94.  
  95.     } while (choice != 0);
  96.     return;
  97.  
  98.  
  99.  
  100. }
  101.  
  102.  
  103. void convert_weight(void)
  104. {
  105.     int choice;
  106.     //asks user what direction they wish to convert their weight or exit to main//
  107.     do
  108.     {
  109.         printf("\nPress (1) to convert from pounds/ounces to kilograms/grams\n");
  110.         printf("Press (2) to convert from kilograms/grams to pounds/ounces\n");
  111.         printf("Press (0) to exit.\n");
  112.         scanf("%d", &choice);
  113.         clear_keyboard_buffer();
  114.         if (choice != 0 && choice != 1 && choice != 2)
  115.         {
  116.             printf("Please enter a valid input.\n");
  117.         }
  118.         switch (choice)
  119.         {
  120.         case 1:
  121.             weight_to_metric();
  122.             break;
  123.         case 2:
  124.             weight_to_us();
  125.             break;
  126.  
  127.  
  128.         }
  129.  
  130.     } while (choice != 0);
  131.     return;
  132.  
  133.  
  134. }
  135.  
  136.  
  137. void length_to_metric(void)
  138. {
  139.     double total_length;
  140.     double converted_length;
  141.     //takes input for the total length in us
  142.     total_length = us_to_metric_length_input();
  143.     //converts the length from us to metric
  144.     converted_length = us_to_metric_length_conversion(total_length);
  145.     //prints out the metric lenth in meters and centimeters
  146.     print_metric_length_conversion(converted_length);
  147.  
  148.  
  149.  
  150.  
  151. }
  152.  
  153. void length_to_us(void)
  154. {
  155.  
  156.     double total_length;
  157.     double converted_length;
  158.     //takes input for the total length in metric
  159.     total_length = metric_to_us_length_input();
  160.     //converts the length from metric to us
  161.     converted_length = metric_to_us_length_conversion(total_length);
  162.     //prints out the metric lenth in feet and inches
  163.     print_us_length_conversion(converted_length);
  164. }
  165.  
  166. void weight_to_metric(void)
  167. {
  168.  
  169.     double total_weight;
  170.     double converted_weight;
  171.     //takes input for the total length in us
  172.     total_weight = us_to_metric_weight_input();
  173.     //converts the length from us to metric
  174.     converted_weight = us_to_metric_weight_conversion(total_weight);
  175.     //prints out the metric lenth in meters and centimeters
  176.     print_metric_weight_conversion(converted_weight);
  177. }
  178.  
  179. void weight_to_us(void)
  180. {
  181.     double total_weight;
  182.     double converted_weight;
  183.     //takes input for the total length in metric
  184.     total_weight = metric_to_us_weight_input();
  185.     //converts the length from metric to us
  186.     converted_weight = metric_to_us_weight_conversion(total_weight);
  187.     //prints out the us lenth in feet and inches
  188.     print_us_weight_conversion(converted_weight);
  189. }
  190.  
  191.  
  192.  
  193. double us_to_metric_length_input(void)
  194. {
  195.     //variable delcaration//
  196.     int feet;
  197.     double inches;
  198.     double total_length_in_feet;
  199.     //asks user to input total amount of feet and inches, then aggregates them into
  200.     //total length in feet variable. returns the total length in feet to the metric conversion function
  201.     printf("Please input the total number of feet: \n");
  202.     scanf("%d", &feet);
  203.     printf("Please enter the total number of inches: \n");
  204.     scanf("%f", &inches);
  205.     total_length_in_feet = feet + inches * INCHES_TO_FEET;
  206.     return total_length_in_feet;
  207.  
  208.  
  209. }
  210.  
  211. double us_to_metric_length_conversion(double us_length)
  212. {
  213.     //converts the us length into metric length //
  214.     double metric_converted_length = us_length * METERS_TO_FEET;
  215.  
  216.     return metric_converted_length;
  217.  
  218.  
  219.  
  220. }
  221.  
  222. void print_metric_length_conversion(double converted_length)
  223.  
  224. {
  225.     int meters;
  226.     double cm;
  227.     //takes the meters out of the total meters by taking the floor of the total meters
  228.     meters = floor(converted_length);
  229.     //converts the reamining length into centimeters
  230.     cm = (converted_length - meters) / CENTIMETERS_TO_METER;
  231.     //prints out the meters and centimeters
  232.     printf("The converted length is %d meters and %f centimeters\n", meters, cm);
  233.  
  234.  
  235. }
  236.  
  237. double metric_to_us_length_input(void)
  238. {
  239.     //variable delcaration//
  240.     int meters;
  241.     int centimeters;
  242.     double total_length_in_meters;
  243.     //asks user to input total amount of meters and cm, then aggregates them into
  244.     //total length in meters variable. returns the total length in feet to the us conversion function
  245.     printf("Please input the total number of meters: \n");
  246.     scanf("%d", &meters);
  247.     printf("Please enter the total number of centimeters: \n");
  248.     scanf("%d", &centimeters);
  249.     total_length_in_meters = meters + (centimeters * CENTIMETERS_TO_METER);
  250.     return total_length_in_meters;
  251. }
  252.  
  253. double metric_to_us_length_conversion(double metric_length)
  254. {
  255.     //converts the metric length into us length //
  256.     double us_converted_length = metric_length / METERS_TO_FEET;
  257.  
  258.     return us_converted_length;
  259.  
  260.  
  261.  
  262. }
  263.  
  264.  
  265. void print_us_length_conversion(double converted_length)
  266.  
  267. {
  268.     int feet;
  269.     double inches;
  270.     //takes the feet out of the total feet by taking the floor of the total feet
  271.     feet = floor(converted_length);
  272.     //converts the reamining length into inches
  273.     inches = (converted_length - feet) / INCHES_TO_FEET;
  274.     //prints out the feet and inches
  275.     printf("The converted length is %d feet and %f inches\n", feet, inches);
  276.  
  277.  
  278. }
  279.  
  280. double us_to_metric_weight_input(void)
  281. {
  282.     //variable delcaration//
  283.     int pounds;
  284.     int ounces;
  285.     double total_weight_in_pounds;
  286.     //asks user to input total amount of pounds and ounces, then aggregates them into
  287.     //total weight in pounds variable. returns the total weight in pounds to the metric conversion function
  288.     printf("Please input the total number of pounds: \n");
  289.     scanf("%d", &pounds);
  290.     printf("Please enter the total number of ounces: \n");
  291.     scanf("%d", &ounces);
  292.     total_weight_in_pounds = pounds + (ounces * OUNCES_TO_POUND);
  293.     return total_weight_in_pounds;
  294.  
  295.  
  296. }
  297.  
  298. double us_to_metric_weight_conversion(double us_weight)
  299. {
  300.     //converts the us length into metric length //
  301.     double metric_converted_weight = us_weight * POUNDS_TO_KILO;
  302.  
  303.     return metric_converted_weight;
  304.  
  305.  
  306.  
  307. }
  308.  
  309. void print_metric_weight_conversion(double converted_weight)
  310.  
  311. {
  312.     int kilograms;
  313.     double grams;
  314.     //takes the kilos out of the total kilos by taking the floor of the total kilos
  315.     kilograms = floor(converted_weight);
  316.     //converts the reamining fractions of kilos into grams
  317.     grams = (converted_weight - kilograms) / GRAMS_TO_KILO;
  318.     //prints out the kilos and grams
  319.     printf("The converted length is %d kilograms and %f grams\n", kilograms, grams);
  320. }
  321.  
  322.  
  323. double metric_to_us_weight_input(void)
  324. {
  325.     //variable delcaration//
  326.     int kilograms;
  327.     int grams;
  328.     double total_weight_in_kilograms;
  329.     //asks user to input total amount of kilos and g's, then aggregates them into
  330.     //total weight in kilos variable. returns the total weight in kilos to the us conversion function
  331.     printf("Please input the total number of kilograms: \n");
  332.     scanf("%d", &kilograms);
  333.     printf("Please enter the total number of grams: \n");
  334.     scanf("%d", &grams);
  335.     total_weight_in_kilograms = kilograms + (grams * GRAMS_TO_KILO);
  336.     return total_weight_in_kilograms;
  337.  
  338.  
  339. }
  340.  
  341. double metric_to_us_weight_conversion(double metric_weight)
  342. {
  343.     //converts the metric length into us length //
  344.     double us_converted_weight = metric_weight / POUNDS_TO_KILO;
  345.  
  346.     return us_converted_weight;
  347.  
  348.  
  349.  
  350. }
  351.  
  352. void print_us_weight_conversion(double converted_weight)
  353.  
  354. {
  355.     int pounds;
  356.     double ounces;
  357.     //takes the pounds out of the total pounds by taking the floor of the total pounds
  358.     pounds = floor(converted_weight);
  359.     //converts the reamining fractions of pounds into ounces
  360.     ounces = (converted_weight - pounds) / OUNCES_TO_POUND;
  361.     //prints out the pounds and ounces
  362.     printf("The converted length is %d pounds and %f ounces\n", pounds, ounces);
  363. }
  364.  
  365.  
  366. void clear_keyboard_buffer(void)
  367. {
  368.     char c;
  369.     scanf("%c", &c);
  370.  
  371.     while (c != '\n') {
  372.         scanf("%c", &c);
  373.  
  374.     }
  375.  
  376.  
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement