// G. Hagopian doing more PPP4 Drill
#include
#include
#include
#include
using namespace std;
/*
6. Now change the body of the loop so that it reads just one double each
time around. Define two variables to keep track of which is the smallest
and which is the largest value you have seen so far. Each time through the
loop write out the value entered. If it’s the smallest so far, write the
smallest so far after the number. If it is the largest so far, write the
largest so far after the number.
Add a unit to each double entered; that is, enter values such as 10cm,
2.5in, 5ft, or 3.33m. Accept the four units: cm, m, in, ft. Assume
conversion factors 1m == 100cm, 1in == 2.54cm, 1ft == 12in. Read the
unit indicator into a string. You may consider 12 m (with a space between
the number and the unit) equivalent to 12m (without a space).
Keep track of the sum of values entered (as well as the smallest and the
largest) and the number of values entered. When the loop ends, print the
smallest, the largest, the number of values, and the sum of values. Note
that to keep the sum, you have to decide on a unit to use for that sum;
use meters.
Keep all the values entered (converted into meters) in a vector. At the end,
write out those values.
11. Before writing out the values from the vector, sort them (that’ll make
them come out in increasing order).
*/
const double m2cm{ 100 }, in2cm{ 2.54 }, ft2in{ 12 };
double convert2m(double x, string unit) {
if (unit == "cm") return x / m2cm;
else if (unit == "in") return x * in2cm / 100;
else if (unit == "ft") return x * ft2in * in2cm / 100;
}
int main() {
vector vd;
string unit;
int counter{ 0 };
cout << "The maximum double is " << DBL_MAX
<< " and the min is " << DBL_MIN << endl;
double entry{}, smallest{ DBL_MAX }, largest{ DBL_MIN }, sum{0};
while (cin >> entry >> unit && unit != "quit") {
if (!(unit == "m" || unit == "cm" || unit == "in" || unit == "ft")) {
cout << "I don't know those units.\n";
continue;
}
entry = convert2m(entry, unit);
sum += entry;
++counter;
vd.push_back(entry);
if (entry >= smallest && entry <= largest)
cout << entry << unit << " = " << convert2m(entry,unit)
<< "m." << endl;
if (entry < smallest) {
cout << entry << "m, the smallest so far.\n";
smallest = entry;
}
if (entry > largest) {
cout << entry << "m, the largest so far.\n";
largest = entry;
}
}
cout << "The sum of the " << vd.size() << " entries is " << sum
<< "m, the smallest entry was " << smallest
<< "m, and the largest was " << largest;
sort(vd.begin(), vd.end());
cout << "\nThe values entered were, in order from smallest to largest:\n";
for (double d : vd) cout << d << " ";
}