1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <iostream>
#include <math.h>
using namespace std;
enum ConversionType {LENGTH_TO_METRIC, LENGTH_TO_US, WEIGHT_TO_METRIC, WEIGHT_TO_US};
void convert_lengths();
void convert_weights();
void length_to_metric(ConversionType conversion);
void length_to_us(ConversionType conversion);
void weight_to_metric(ConversionType conversion);
void weight_to_us(ConversionType conversion);
void prompt_user(int& large_unit, double& small_unit, ConversionType conversion);
void convert(int large_unit, double small_unit, int& new_large, double& new_small, ConversionType conversion);
void output_results(int large_before, double small_before, int large_after, double small_after, ConversionType conversion);
int main()
{
int value;
do {
do {
cout <<"Would you like to convert the weight or length:\n"
"Press 1 for Lengths\n"
"Press 2 for Weight or\n"
"Press 0 to terminate program\n";
cin >> value;
if (value != 0 && value != 1 && value != 2)
cout << "Invalid entry." << endl;
} while (value != 0 && value != 1 && value != 2);
switch (value)
{
case 0:
break;
case 1: convert_lengths();
break;
case 2: convert_weights();
break;
}
} while(value != 0);
return 0;
}
void convert_lengths()
{
cout << "We are going to convert lengths for you." << endl;
int value;
do
{
cout << "Would you like to convert:\n"
"Feet/Inches to Meters/Centimeters? If so press 1.\n"
"Meters/Centimeters to Feet/Inches? If so press 2. \n"
"Press 0 to quit.\n";
cin >> value;
if (value !=0 &&value !=1 && value !=2)
cout << "Invalad entry." << endl;
} while (value !=0 &&value !=1 && value !=2);
switch (value)
{
case 0: break;
case 1: length_to_metric(LENGTH_TO_METRIC); break;
case 2: length_to_us(LENGTH_TO_US); break;
}
}
void convert_weights()
{
cout << "We are going to convert weights for you." << endl;
int value;
do
{
cout << "Would you like to convert:\n"
"Pounds/ounces to Kilograms/grams? If so press 1.\n"
"Kilograms/Grams to pounds/ounces? If so press 2.\n"
"Press 0 to quit.\n";
cin >> value;
if (value !=0 &&value !=1 && value !=2)
cout << "Invalad entry." << endl;
} while (value !=0 &&value !=1 && value !=2);
switch (value)
{
case 0: break;
case 1: weight_to_metric(WEIGHT_TO_METRIC); break;
case 2: weight_to_us(WEIGHT_TO_US); break;
}
}
void length_to_metric(ConversionType conversion)
{
int feet, meters;
double inches, centimeters;
cout << "You have chosen to convert Feet/Inches to Meters/Centimeters.\n";
prompt_user(feet, inches, LENGTH_TO_METRIC);
convert(feet, inches, meters, centimeters, LENGTH_TO_METRIC);
output_results(feet, inches, meters, centimeters, LENGTH_TO_METRIC);
}
void length_to_us(ConversionType conversion)
{
int meters, feet;
double centimeters, inches;
cout << "You have chosen to convert Meters/Centimeters to Feet/Inches.\n";
prompt_user(meters, centimeters, LENGTH_TO_US);
convert(meters, centimeters, feet, inches, LENGTH_TO_US);
output_results(meters, centimeters, feet, inches, LENGTH_TO_US);
}
void weight_to_metric(ConversionType conversion)
{
int pounds, kilograms;
double ounces, grams;
cout << "You have chosen to convert Pounds/Ounces to Kilograms/Grams.\n";
prompt_user(pounds, ounces, WEIGHT_TO_METRIC);
convert(pounds, ounces, kilograms, grams, WEIGHT_TO_METRIC);
output_results(pounds, ounces, kilograms, grams, WEIGHT_TO_METRIC);
}
|