Advertisement
Guest User

Program3_WorkingVersion

a guest
Nov 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.78 KB | None | 0 0
  1.     #include <stdio.h>
  2.     #include <math.h>
  3.  
  4.     // MAIN FUNCTIONS //
  5.     void convert_lengths();
  6.        
  7.         void lengths_to_metric(); // FIX ME //
  8.             void read_length_us();
  9.             void convert_length_to_metric();
  10.             void output_length_metric();
  11.         //
  12.         void lengths_to_us(); // FIX ME //
  13.             void read_length_metric();
  14.             void convert_length_to_us();
  15.             void output_length_us();
  16.  
  17.     /**/
  18.     void convert_weights();
  19.  
  20.         void weight_to_metric(); // FIX ME //
  21.             void read_weight_us();
  22.             void convert_weight_to_metric();
  23.             void output_weight_metric();
  24.  
  25.         void weight_to_us(); // FIX ME //
  26.             void read_weight_metric();
  27.             void convert_weight_to_us();
  28.             void output_weight_us();
  29.  
  30.  
  31.     void clear_keyboard_buffer();
  32.  
  33.     int main(int argc, char const *argv[])
  34.     {
  35.         int choice, clear;
  36.  
  37.         /* Validate Answer Loop */
  38.         while (clear < 1 || choice > 2 || choice != 0)
  39.         {
  40.             clear = 0;
  41.             printf("1. convert length\n2. convert weight\n0. Exit\nPlease choose from (1, 2, 0):\n");
  42.             clear = scanf("%d", &choice);
  43.             clear_keyboard_buffer();
  44.            
  45.             if (choice == 1)
  46.             {
  47.                 convert_lengths();
  48.             }
  49.            
  50.             if (choice == 2)
  51.             {
  52.                 convert_weights();
  53.             }
  54.            
  55.             if (choice == 0)
  56.             {
  57.                 printf("User chose to exit.\n");
  58.                 break;
  59.             }
  60.            
  61.            
  62.         }
  63.        
  64.        
  65.  
  66.         return 0;
  67.     }
  68.  
  69.                             // CONVERT LENGTHS //
  70.                     /******************************************/
  71.     void convert_lengths()
  72.     {
  73.         int choice = 99, clear;
  74.  
  75.         while (clear < 1 || choice > 2 || choice == 99 || choice != 0)
  76.         {
  77.             printf("1. convert lengths to metrics\n");
  78.             printf("2. convert lengths to US\n");
  79.             printf("0. Return to Main Menu\n");
  80.             clear = scanf("%d", &choice);
  81.             clear_keyboard_buffer();
  82.  
  83.         if (choice == 1)
  84.                 {
  85.                     lengths_to_metric();
  86.                 }
  87.                
  88.                 if (choice == 2)
  89.                 {
  90.                     lengths_to_us();
  91.                 }
  92.                
  93.                 if (choice == 0)
  94.                 {
  95.                     printf("User chose to go main menu.\n");
  96.                     break;
  97.                 }
  98.             }
  99.  
  100.     }
  101.  
  102.                             // LENGTHS FUNCTIONS //
  103.  
  104.         // FIX ME //
  105.         void lengths_to_metric()
  106.         {  
  107.             double ft=0, in=0, m=0, cm=0;
  108.            
  109.             read_length_us(&ft, &in);
  110.             convert_length_to_metric(&ft, &in, &m, &cm);
  111.             output_length_metric(&ft, &in, &m, &cm);
  112.         }
  113.             void read_length_us(double *ftPointer, double *inPointer)
  114.             {
  115.                 int verifyInput;
  116.  
  117.                 printf("Enter feet and inches (separated by a space):\n");
  118.                 verifyInput = scanf("%lf %lf", ftPointer, inPointer);
  119.                 clear_keyboard_buffer();
  120.                 while (verifyInput < 1)
  121.                 {
  122.                     printf("That's not an acceptable value\n");
  123.                     printf("Enter feet and inches (separated by a space):\n");
  124.                     verifyInput = scanf("%lf %lf", ftPointer, inPointer);
  125.                 }
  126.                
  127.             }
  128.             void convert_length_to_metric(double *ftPointer, double *inPointer,
  129.                                         double *mPointer, double *cmPointer)
  130.             {
  131.             *mPointer = (*ftPointer * 0.3048) + (*inPointer/12);
  132.             *cmPointer = (*mPointer * 100);
  133.             }
  134.             void output_length_metric(double *ftPointer, double *inPointer,
  135.                                     double *mPointer, double *cmPointer)
  136.             {
  137.                 printf("%.0lf feet and %.4lf inches converted to %.3lf meters and %lf centimeters.\n",
  138.                 *ftPointer, *inPointer, *mPointer, *cmPointer);
  139.             }
  140.  
  141.        
  142.        
  143.        
  144.        
  145.        
  146.         // FIX ME //
  147.         void lengths_to_us()
  148.         {
  149.             double ft=0, in=0, m=0, cm=0;
  150.  
  151.             read_length_metric(&m, &cm);
  152.             convert_length_to_metric(&m, &cm, &ft, &in);
  153.             output_length_us(&m, &cm, &ft, &in);
  154.         }
  155.             void read_length_metric(double *mPointer, double *cmPointer)
  156.             {
  157.                 int verifyInput =0;
  158.  
  159.                 printf("Enter meter and centimeters (separated by a space)\n");
  160.                 verifyInput = scanf("%lf %lf", mPointer, cmPointer);
  161.                 clear_keyboard_buffer();
  162.                 while (verifyInput < 1)
  163.                 {
  164.                     printf("That's not an acceptable value\n");
  165.                     printf("Enter meter and centimeters (separated by a space)\n");
  166.                     verifyInput = scanf("%lf %lf", mPointer, cmPointer);
  167.                     clear_keyboard_buffer();
  168.                 }
  169.             }
  170.            
  171.             void convert_length_to_us(double *mPointer, double *cmPointer, double *ftPointer, double *inPointer)
  172.             {
  173.                 *ftPointer = (*mPointer/0.3048) + (*cmPointer * 12);
  174.             *inPointer = (*ftPointer * 12);
  175.  
  176.             }
  177.  
  178.             void output_length_us(double *mPointer, double *cmPointer, double *ftPointer, double *inPointer)
  179.             {
  180.                 printf("%.0lf meters and %lf centimeters converted to %.0lf feet and %lf inches.\n\n", *mPointer,
  181.                 *cmPointer, *ftPointer, *inPointer);
  182.             }
  183.  
  184.     void convert_weights()
  185.     {
  186.         printf("The user wants to convert_weights.\n");
  187.         int choice = 99, clear;
  188.  
  189.         while (clear < 1 || choice > 2 || choice == 99 || choice != 0)
  190.         {
  191.             printf("1. convert weights to metrics\n");
  192.             printf("2. convert weights to US\n");
  193.             printf("0. Return to Main Menu\n");
  194.             clear = scanf("%d", &choice);
  195.             clear_keyboard_buffer();
  196.  
  197.         if (choice == 1)
  198.         {
  199.             weight_to_metric();
  200.         }
  201.                
  202.         if (choice == 2)
  203.         {
  204.             weight_to_us();
  205.         }
  206.                
  207.         if (choice == 0)
  208.         {
  209.             printf("User chose to go main menu.\n");
  210.             break;
  211.         }
  212.         }
  213.     }
  214.  
  215.         // WEIGHTS FUNCTIONS //
  216.         // FIX ME //
  217.         void weight_to_metric()
  218.         {
  219.             double lb=0, oz=0, kg=0, g=0;
  220.  
  221.             read_weight_us(&lb, &oz);
  222.             convert_length_to_metric(&lb, &oz, &kg, &g);
  223.             output_weight_metric(&lb, &oz, &kg, &g);
  224.         }
  225.  
  226.             void read_weight_us(double *lbPointer, double *ozPointer)
  227.             {
  228.                 int verifyInput = 0;
  229.                 printf("Enter pound and ounces (separated by a space)\n");
  230.                 verifyInput = scanf("%lf %lf", lbPointer, ozPointer);
  231.                 clear_keyboard_buffer();
  232.                 while (verifyInput < 1)
  233.                 {
  234.                     printf("That's not an acceptable value\n");
  235.                     printf("Enter pound and ounces (separated by a space)\n");
  236.                     verifyInput = scanf("%lf %lf", lbPointer, ozPointer);
  237.                     clear_keyboard_buffer();
  238.                 }
  239.             }
  240.  
  241.             void convert_weight_to_metric(double *lbPointer, double *ozPointer, double *kgPointer, double *gPointer)
  242.             {
  243.                 *kgPointer = *lbPointer / 2.2046;
  244.                 *gPointer = (*kgPointer * 1000) + 1000 * (*ozPointer/16) / 2.2046;
  245.                 *kgPointer = *kgPointer + (*gPointer/1000);
  246.             }
  247.  
  248.             void output_weight_metric(double *lbPointer, double *ozPointer, double *kgPointer, double *gPointer)
  249.             {
  250.                 printf("%.0lf pounds and %lf ounces converted to %.0lf kilograms and %lf grams.\n\n", *lbPointer,
  251.                 *ozPointer, *kgPointer, *gPointer);
  252.             }
  253.  
  254.        
  255.         // FIX ME //
  256.         void weight_to_us()
  257.         {
  258.             double lb=0, oz=0, kg=0, g=0;
  259.  
  260.             read_weight_metric(&kg, &g);
  261.             convert_length_to_us(&kg, &g, &lb, &oz);
  262.             output_weight_us(&kg, &g, &lb, &oz);
  263.         }
  264.  
  265.             void read_weight_metric(double *kgPointer, double *gPointer)
  266.             {
  267.                 int verifyInput = 0;
  268.  
  269.                 printf("Enter kilograms and grams (separated by a space)\n");
  270.                 verifyInput = scanf("%lf %lf", kgPointer, gPointer);
  271.                 clear_keyboard_buffer();
  272.                 while (verifyInput < 1)
  273.                 {
  274.                     printf("That's not an acceptable value\n");
  275.                     printf("Enter kilograms and grams (separated by a space)\n");
  276.                     verifyInput = scanf("%lf %lf", kgPointer, gPointer);
  277.                     clear_keyboard_buffer();
  278.                 }
  279.             }
  280.  
  281.             void convert_weight_to_us(double *kgPointer, double *gPointer, double *lbPointer, double *ozPointer)
  282.             {
  283.                 printf("FIX ME: convert_weight_to_us");
  284.             }
  285.  
  286.             void output_weight_us(double *kgPointer, double *gPointer, double *lbPointer,
  287.             double *ozPointer)
  288.             {
  289.                 printf("%.0lf kilograms and %lf grams converted to %.0lf pounds and %lf ounces\n",
  290.                 *kgPointer, *gPointer, *lbPointer, *ozPointer);
  291.             }
  292.  
  293.     void clear_keyboard_buffer(void)
  294.     {
  295.     char ch;
  296.     scanf("%c", &ch);
  297.     while (ch != '\n' && ch != '\0')
  298.         {
  299.             scanf("%c", &ch);
  300.         }
  301.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement