Advertisement
Guest User

Unit Conversion Program v2.0

a guest
Oct 28th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.56 KB | None | 0 0
  1. //Unit conversion Program to metres
  2. #include "../../std_lib_facilities.h"
  3.  
  4. double metre_conversion(double input, string unit, string conversion_type);
  5. void error_prompt(string error_type);
  6. void vector_print_metres(vector<double> stored_values);
  7. bool check_vector_for_unit(vector<string> accepted_units, string input_unit);
  8. void print_new_smallest_value(double input_value, string input_unit, double smallest_value, string smallest_unit);
  9. void print_new_largest_value(double input_value, string input_unit, double largest_value, string largest_unit);
  10.  
  11. int main()
  12. {
  13.     vector<double> stored_values;
  14.     vector<string> accepted_units{"m", "in", "ft", "cm"};
  15.     double input_value, smallest_value, largest_value;
  16.     string input_unit = "", largest_unit, smallest_unit;
  17.  
  18.     //First assignment to smallest and largest value/unit until true
  19.     while (1)
  20.     {
  21.         cin >> input_value >> input_unit;
  22.         if (input_value < 0)
  23.         {
  24.             error_prompt("value");
  25.             continue;
  26.         }
  27.         if (check_vector_for_unit(accepted_units, input_unit))
  28.         {
  29.             error_prompt("unit");
  30.             continue;
  31.         }
  32.         else
  33.         {
  34.             smallest_value = input_value;
  35.             largest_value = input_value;
  36.             smallest_unit = input_unit;
  37.             largest_unit = input_unit;
  38.             stored_values.push_back(input_value);
  39.             break;
  40.         }
  41.     }
  42.  
  43.     //Reads inputs until a non-integer is entered
  44.     while (cin >> input_value >> input_unit)
  45.     {
  46.         //Use vector member function to check if unit matches our assigned accepted_units
  47.         if (check_vector_for_unit(accepted_units, input_unit))
  48.         {
  49.             error_prompt("unit");
  50.             continue;
  51.         }
  52.         //Ensure that input value isn't negative
  53.         if (input_value < 0)
  54.         {
  55.             error_prompt("value");
  56.             continue;
  57.         }
  58.  
  59.         //Input is new smallest number
  60.         if (metre_conversion(input_value, input_unit, "convert_to_m") < metre_conversion(smallest_value, smallest_unit, "convert_to_m"))
  61.         {
  62.             print_new_smallest_value(input_value, input_unit, smallest_value, smallest_unit);
  63.             //Assigns new Smallest value and prompts user
  64.             smallest_value = input_value;
  65.             smallest_unit = input_unit;
  66.         }
  67.  
  68.         //Input is new Largest number
  69.         else if (metre_conversion(input_value, input_unit, "convert_to_m") > metre_conversion(largest_value, largest_unit, "convert_to_m"))
  70.         {
  71.             print_new_largest_value(input_value, input_unit, largest_value, largest_unit);
  72.             //Assigns new largest value and prompts user
  73.             largest_value = input_value;
  74.             largest_unit = input_unit;
  75.         }
  76.         else
  77.         {
  78.             cout << input_value << input_unit << " :: " << "Has been stored.\n";
  79.         }
  80.  
  81.         stored_values.push_back(metre_conversion(input_value, input_unit, "convert_to_m"));
  82.     }
  83.     //Inputs have been terminated by user
  84.  
  85.     //Vector adds sum of all stored values in metres
  86.     double stored_value_sum = 0;
  87.     for (double i : stored_values) stored_value_sum += i;
  88.  
  89.     //Prints the smallest, largest, input count, inputs sum and all stored values in metres
  90.     cout << "========== PROGRAM ANALYTICS ==========\n"
  91.         << "Final Smallest Input: " << smallest_value << smallest_unit << '\n'
  92.         << "Final Largest Input: " << largest_value << largest_unit << '\n'
  93.         << "Amount of inputs: " << stored_values.size() - 1 << '\n'
  94.         << "Total Sum of inputs (in Metres): " << stored_value_sum << "m\n";
  95.     vector_print_metres(stored_values);
  96.     return 0;
  97. }
  98.  
  99. double metre_conversion(double input, string unit, string conversion_type)
  100. {
  101.     double centimetre_ratio = 100;
  102.     double inch_ratio = 39.37;
  103.     double foot_ratio = 3.28;
  104.  
  105.     //Conversion from original unit to metres
  106.     if (conversion_type == "convert_to_m")
  107.     {
  108.         if (unit == "cm")
  109.             return input / centimetre_ratio;
  110.         else if (unit == "in")
  111.             return input / inch_ratio;
  112.         else if (unit == "ft")
  113.             return input / foot_ratio;
  114.         else if (unit == "m")
  115.             return input;
  116.         else
  117.             error_prompt("unit");
  118.             return input;
  119.     }
  120.     //Conversion from metres to original unit
  121.     else if (conversion_type == "convert_from_m")
  122.     {
  123.         if (unit == "cm")
  124.             return input * centimetre_ratio;
  125.         else if (unit == "in")
  126.             return input * inch_ratio;
  127.         else if (unit == "ft")
  128.             return input * foot_ratio;
  129.         else if (unit == "m")
  130.             return input;
  131.         else
  132.             error_prompt("unit");
  133.         return input;
  134.     }
  135.     //If input conversion string is invalid return original input and propmt.
  136.     else
  137.     {
  138.         error_prompt("unit");
  139.         return input;
  140.     }
  141. }
  142.  
  143. void vector_print_metres(vector<double> stored_values)
  144. {
  145.     //Sort and print all stored values in metres
  146.     sort(stored_values);
  147.  
  148.     for (int i = 0; i < stored_values.size() - 1; ++i)
  149.     {
  150.         cout << stored_values[i] << ", ";
  151.     }
  152.    
  153.     cout << stored_values[stored_values.size()] << '\n';
  154. }
  155.  
  156. void error_prompt(string error_type)
  157. {
  158.     if(error_type == "value")
  159.         cout << "Invalid Value, Please Revise\n";
  160.     if (error_type == "unit")
  161.         cout << "Invalid Unit Type, Please Revise\n";
  162. }
  163.  
  164. void print_new_smallest_value(double input_value, string input_unit, double smallest_value, string smallest_unit)
  165. {
  166.     cout << "Input: " << input_value << input_unit << '\n'
  167.          << "Previous Smallest: " << smallest_value << smallest_unit << '\n'
  168.          << "New smallest value: " << input_value << input_unit << '\n';
  169. }
  170. void print_new_largest_value(double input_value, string input_unit, double largest_value, string largest_unit)
  171. {
  172.     cout << "Input: " << input_value << input_unit << '\n'
  173.         << "Previous Smallest: " << largest_value << largest_unit << '\n'
  174.         << "New smallest value: " << input_value << input_unit << '\n';
  175. }
  176.  
  177. void program_termination();
  178.  
  179. bool check_vector_for_unit(vector<string> accepted_units, string input_unit)
  180. {
  181.     if (find(accepted_units.begin(), accepted_units.end(), input_unit) == accepted_units.end())
  182.         return true;
  183.     else
  184.         return false;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement