Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Unit conversion Program to metres
- #include "../../std_lib_facilities.h"
- double metre_conversion(double input, string unit, string conversion_type);
- void error_prompt(string error_type);
- void vector_print_metres(vector<double> stored_values);
- bool check_vector_for_unit(vector<string> accepted_units, string input_unit);
- void print_new_smallest_value(double input_value, string input_unit, double smallest_value, string smallest_unit);
- void print_new_largest_value(double input_value, string input_unit, double largest_value, string largest_unit);
- int main()
- {
- vector<double> stored_values;
- vector<string> accepted_units{"m", "in", "ft", "cm"};
- double input_value, smallest_value, largest_value;
- string input_unit = "", largest_unit, smallest_unit;
- //First assignment to smallest and largest value/unit until true
- while (1)
- {
- cin >> input_value >> input_unit;
- if (input_value < 0)
- {
- error_prompt("value");
- continue;
- }
- if (check_vector_for_unit(accepted_units, input_unit))
- {
- error_prompt("unit");
- continue;
- }
- else
- {
- smallest_value = input_value;
- largest_value = input_value;
- smallest_unit = input_unit;
- largest_unit = input_unit;
- stored_values.push_back(input_value);
- break;
- }
- }
- //Reads inputs until a non-integer is entered
- while (cin >> input_value >> input_unit)
- {
- //Use vector member function to check if unit matches our assigned accepted_units
- if (check_vector_for_unit(accepted_units, input_unit))
- {
- error_prompt("unit");
- continue;
- }
- //Ensure that input value isn't negative
- if (input_value < 0)
- {
- error_prompt("value");
- continue;
- }
- //Input is new smallest number
- if (metre_conversion(input_value, input_unit, "convert_to_m") < metre_conversion(smallest_value, smallest_unit, "convert_to_m"))
- {
- print_new_smallest_value(input_value, input_unit, smallest_value, smallest_unit);
- //Assigns new Smallest value and prompts user
- smallest_value = input_value;
- smallest_unit = input_unit;
- }
- //Input is new Largest number
- else if (metre_conversion(input_value, input_unit, "convert_to_m") > metre_conversion(largest_value, largest_unit, "convert_to_m"))
- {
- print_new_largest_value(input_value, input_unit, largest_value, largest_unit);
- //Assigns new largest value and prompts user
- largest_value = input_value;
- largest_unit = input_unit;
- }
- else
- {
- cout << input_value << input_unit << " :: " << "Has been stored.\n";
- }
- stored_values.push_back(metre_conversion(input_value, input_unit, "convert_to_m"));
- }
- //Inputs have been terminated by user
- //Vector adds sum of all stored values in metres
- double stored_value_sum = 0;
- for (double i : stored_values) stored_value_sum += i;
- //Prints the smallest, largest, input count, inputs sum and all stored values in metres
- cout << "========== PROGRAM ANALYTICS ==========\n"
- << "Final Smallest Input: " << smallest_value << smallest_unit << '\n'
- << "Final Largest Input: " << largest_value << largest_unit << '\n'
- << "Amount of inputs: " << stored_values.size() - 1 << '\n'
- << "Total Sum of inputs (in Metres): " << stored_value_sum << "m\n";
- vector_print_metres(stored_values);
- return 0;
- }
- double metre_conversion(double input, string unit, string conversion_type)
- {
- double centimetre_ratio = 100;
- double inch_ratio = 39.37;
- double foot_ratio = 3.28;
- //Conversion from original unit to metres
- if (conversion_type == "convert_to_m")
- {
- if (unit == "cm")
- return input / centimetre_ratio;
- else if (unit == "in")
- return input / inch_ratio;
- else if (unit == "ft")
- return input / foot_ratio;
- else if (unit == "m")
- return input;
- else
- error_prompt("unit");
- return input;
- }
- //Conversion from metres to original unit
- else if (conversion_type == "convert_from_m")
- {
- if (unit == "cm")
- return input * centimetre_ratio;
- else if (unit == "in")
- return input * inch_ratio;
- else if (unit == "ft")
- return input * foot_ratio;
- else if (unit == "m")
- return input;
- else
- error_prompt("unit");
- return input;
- }
- //If input conversion string is invalid return original input and propmt.
- else
- {
- error_prompt("unit");
- return input;
- }
- }
- void vector_print_metres(vector<double> stored_values)
- {
- //Sort and print all stored values in metres
- sort(stored_values);
- for (int i = 0; i < stored_values.size() - 1; ++i)
- {
- cout << stored_values[i] << ", ";
- }
- cout << stored_values[stored_values.size()] << '\n';
- }
- void error_prompt(string error_type)
- {
- if(error_type == "value")
- cout << "Invalid Value, Please Revise\n";
- if (error_type == "unit")
- cout << "Invalid Unit Type, Please Revise\n";
- }
- void print_new_smallest_value(double input_value, string input_unit, double smallest_value, string smallest_unit)
- {
- cout << "Input: " << input_value << input_unit << '\n'
- << "Previous Smallest: " << smallest_value << smallest_unit << '\n'
- << "New smallest value: " << input_value << input_unit << '\n';
- }
- void print_new_largest_value(double input_value, string input_unit, double largest_value, string largest_unit)
- {
- cout << "Input: " << input_value << input_unit << '\n'
- << "Previous Smallest: " << largest_value << largest_unit << '\n'
- << "New smallest value: " << input_value << input_unit << '\n';
- }
- void program_termination();
- bool check_vector_for_unit(vector<string> accepted_units, string input_unit)
- {
- if (find(accepted_units.begin(), accepted_units.end(), input_unit) == accepted_units.end())
- return true;
- else
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement