Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. double convert_lengths();
  2. double convert_weights();
  3. double length_to_metric();
  4. double length_to_us();
  5. double weight_to_metric();
  6. double weight_to_us();
  7. void read_length_us(int* ft, double* in);
  8. void convert_length_to_metric(int ft, double in, int* m, double* cm);
  9. void output_length_metric(int ft, double in, int m, double cm);
  10. void read_length_metric(int* m, double* cm);
  11. void convert_length_to_us(int m, double cm, int* ft, double* in);
  12. void output_length_us(int m, double cm, int ft, double in);
  13. void read_weight_us(int* lb, double* oz);
  14. void convert_weight_to_metric(int lb, double oz, int* kg, double* g);
  15. void output_weight_metric(int lb, double oz, int kg, double g);
  16. void read_weight_metric(int* kg, double* g);
  17. void convert_weight_to_us(int kg, double g, int* lb, double* oz);
  18. void output_weight_us(int kg, double g, int lb, double oz);
  19. void clear_keyboard_buffer();
  20.  
  21. #include <stdio.h>
  22.  
  23. int main(int argc, char * argv[])
  24. {
  25. int temp, input;
  26. while(temp < 1 || input > 2 || input != 0)
  27. {
  28. temp = 0;
  29. printf("1. convert length\n2. convert weight\n0. Exit\nPlease choose from (1, 2, 0):\n");
  30. temp = scanf("%d", &input);
  31. clear_keyboard_buffer();
  32. if(input == 1)
  33. {
  34. convert_lengths();
  35. }
  36. if(input == 2)
  37. {
  38. convert_weights();
  39. }
  40. if(input == 0)
  41. {
  42. printf("User chose to exit.\n");
  43. break;
  44. }
  45. }
  46. return 0;
  47. }
  48.  
  49. double convert_lengths()
  50. {
  51. int input = 99, temp;
  52.  
  53. while(temp < 1 || input > 2 || input == 99 || input != 0)
  54. {
  55. printf("1. convert lengths to metrics\n");
  56. printf("2. convert lengths to US\n");
  57. printf("0. Return to Main Menu\n");
  58. temp = scanf("%d", &input);
  59. clear_keyboard_buffer();
  60. if(input == 1)
  61. {
  62. length_to_metric();
  63. }
  64. if(input == 2)
  65. {
  66. length_to_us();
  67. }
  68. if(input == 0)
  69. {
  70. printf("User chose to go main menu.\n");
  71. break;
  72. }
  73. }
  74. return 0;
  75. }
  76.  
  77. double length_to_metric()
  78. {
  79. printf("The user wants to convert length_to_metric.\n");
  80. int m, ft;
  81. double cm, in;
  82. read_length_us(&ft, &in);
  83. convert_length_to_metric(ft, in, &m, &cm);
  84. output_length_metric(ft, in, m, cm);
  85. return 0;
  86. }
  87.  
  88. void read_length_us(int* ft, double* in)
  89. {
  90. printf("Enter feet and inches (seperated by a space):\n");
  91. scanf("%d %lf", ft, in);
  92. }
  93.  
  94. void convert_length_to_metric(int ft, double in, int* m, double* cm)
  95. {
  96. double totalft = ft + (in / 12);
  97. double totalm = totalft * .3048;
  98. *m = (int)totalm;
  99. *cm = (totalm - (*m)) * 100.0;
  100. }
  101.  
  102. void output_length_metric(int ft, double in, int m, double cm)
  103. {
  104. printf("%d feet and %.4lf inches converted to %d meters and %.4lf centimeters.\n", ft, in, m, cm);
  105. }
  106.  
  107. double length_to_us()
  108. {
  109. printf("The user wants to convert length_to_us.\n");
  110. int m, ft;
  111. double cm, in;
  112. read_length_metric(&m, &cm);
  113. convert_length_to_us(m, cm, &ft, &in);
  114. output_length_us(m, cm, ft, in);
  115. return 0;
  116. }
  117.  
  118. void read_length_metric(int* m, double* cm)
  119. {
  120. printf("Enter meter and centimeters (seperated by a space):\n");
  121. scanf("%d %lf", m, cm);
  122. }
  123.  
  124. void convert_length_to_us(int m, double cm, int* ft, double* in)
  125. {
  126. double totalm = m + (cm / 100);
  127. double totalft = totalm / .3048;
  128. *ft = (int)totalft;
  129. *in = (totalft - (*ft)) * 12.0;
  130. }
  131.  
  132. void output_length_us(int m, double cm, int ft, double in)
  133. {
  134. printf("%d meters and %.4lf centimeters converted to %d feet and %.4lf inches.\n", m, cm, ft, in);
  135. }
  136.  
  137. double convert_weights()
  138. {
  139. int input = 99, temp;
  140.  
  141. while(temp < 1 || input > 2 || input == 99 || input != 0)
  142. {
  143. printf("1. convert weights to metrics\n");
  144. printf("2. convert weights to US\n");
  145. printf("0. Return to Main Menu\n");
  146. temp = scanf("%d", &input);
  147. clear_keyboard_buffer();
  148. if(input == 1)
  149. {
  150. weight_to_metric();
  151. }
  152. if(input == 2)
  153. {
  154. weight_to_us();
  155. }
  156. if(input == 0)
  157. {
  158. printf("User chose to go main menu.\n");
  159. break;
  160. }
  161. }
  162. return 0;
  163. }
  164.  
  165. double weight_to_metric()
  166. {
  167. printf("The user wants to convert weight_to_metric.\n");
  168. int lb, kg;
  169. double oz, g;
  170. read_weight_us(&lb, &oz);
  171. convert_weight_to_metric(lb, oz, &kg, &g);
  172. output_weight_metric(lb, oz, kg, g);
  173. return 0;
  174. }
  175.  
  176. void read_weight_us(int* lb, double* oz)
  177. {
  178. printf("Enter pound and ounces (seperated by a space):\n");
  179. scanf("%d %lf", lb, oz);
  180. }
  181.  
  182. void convert_weight_to_metric(int lb, double oz, int* kg, double* g)
  183. {
  184. double totallb = lb + (oz / 16);
  185. double totalkg = totallb / 2.2046;
  186. *kg = (int)totalkg;
  187. *g = (totalkg - (*kg)) * 1000;
  188. }
  189.  
  190. void output_weight_metric(int lb, double oz, int kg, double g)
  191. {
  192. printf("%d pounds and %.4lf ounces converted to %d kilograms and %.4lf grams.\n", lb, oz, kg, g);
  193. }
  194.  
  195. double weight_to_us()
  196. {
  197. printf("The user wants to convert weight_to_us.\n");
  198. int lb, kg;
  199. double oz, g;
  200. read_weight_metric(&kg, &g);
  201. convert_weight_to_us(kg, g, &lb, &oz);
  202. output_weight_us(kg, g, lb, oz);
  203. return 0;
  204. }
  205.  
  206. void read_weight_metric(int* kg, double* g)
  207. {
  208. printf("Enter kilograms and grams (seperated by a space):\n");
  209. scanf("%d %lf", kg, g);
  210. }
  211.  
  212. void convert_weight_to_us(int kg, double g, int* lb, double* oz)
  213. {
  214. double totalkg = kg + (g / 1000);
  215. double totallb = totalkg * 2.2046;
  216. *lb = (int)totallb;
  217. *oz = (totallb - (*lb)) * 16;
  218. }
  219.  
  220. void output_weight_us(int kg, double g, int lb, double oz)
  221. {
  222. printf("%d kilograms and %.4lf grams converted to %d pounds and %.4lf ounces.\n", kg, g, lb, oz);
  223. }
  224.  
  225. void clear_keyboard_buffer(void)
  226. {
  227. char ch;
  228. scanf("%c", &ch);
  229. while (ch != '\n' && ch != '\0')
  230. {
  231. scanf("%c", &ch);
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement