/// Geoff Hagopian -- PPP4 Drill
#include
#include
#include
using namespace std;
/*
1. Write a program that consists of a while-loop that (each time around the loop) reads in two
ints and then prints them. Exit the program when a terminating '|' is entered.
2. Change the program to write out the smaller value is: followed by the smaller of the
numbers and the larger value is: followed by the larger value.
3. Augment the program so that it writes the line the numbers are equal (only) if they are
equal.
4. Change the program so that it uses doubles instead of ints.
5. Change the program so that it writes out the numbers are almost equal after writing out
which is the larger and the smaller if the two numbers differ by less than 1.0/100.
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.
7. 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).
8. Reject values without units or with “illegal” representations of units, such as y, yard, meter,
km, and gallons.
9. 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.
10. 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).
*/
void part1() {
int integer1, integer2;
while (cin >> integer1 >> integer2)
cout << integer1 << " " << integer2 << endl;
}
void part2_3() {
int integer1, integer2;
cin.clear();
cin.ignore(1000, '\n');
while (cin >> integer1 >> integer2)
if (integer1 < integer2)
cout << "The smaller is " << integer1 << endl;
else if (integer1 == integer2)
cout << "The numbers are equal!" << endl;
else cout << "The smaller is " << integer2 << endl;
}
void part4() {
double d1, d2;
cin.clear();
cin.ignore(1000, '\n');
while (cin >> d1 >> d2)
if (d1 < d2)
cout << "The smaller is " << d1 << endl;
else if (d1 == d2)
cout << "The numbers are equal!" << endl;
else cout << "The smaller is " << d2 << endl;
}
void part5() {
double d1, d2;
cin.clear();
cin.ignore(1000, '\n');
while (cin >> d1 >> d2) {
if (abs(d1 - d2) < 0.01)
cout << "Those numbers are approximately equal.";
if (d1 < d2)
cout << "The smaller is " << d1 << endl;
else if (d1 == d2)
cout << "The numbers are equal!" << endl;
else cout << "The smaller is " << d2 << endl;
}
}
void part6() {
double temp;
vector numbers;
cin.clear();
cin.ignore(1000, '\n');
cout << "Enter a double ('|' to quit): ";
while (cin >> temp) {
numbers.push_back(temp);
sort(numbers.begin(), numbers.end());
if (temp == numbers[0])
cout << " That's the smallest so far." << endl;
else if (temp == numbers.back())
cout << " That's the largest so far." << endl;
cout << "Enter a double ('|' to quit): ";
}
}
void part7() {
//cm, m, in, ft. Assume conversion factors 1m == 100cm, 1in ==
//2.54cm, 1ft == 12in.
const double mTocm = 100, inTocm = 2.54, ftToin = 12;
string units;
double number;
cin.clear();
cin.ignore(1000, '\n');
cout << "Enter a number with its units (cm, m, in, ft): ";
while (cin >> number >> units) {
if (units == "in") {
cout << number << " inches is " << number * inTocm << " cm" << endl;
}
else if (units == "m") {
cout << number << " m is " << number * mTocm << " cm" << endl;
}
else if (units == "cm") {
cout << number << " cm is " << number / mTocm << " m" << endl;
}
else if (units == "ft") {
cout << number << " ft is " << number * ftToin << " inches" << endl;
}
else cout << "I don't know those units!" << endl;
}
}
void part9() {
//cm, m, in, ft. Assume conversion factors 1m == 100cm, 1in ==
//2.54cm, 1ft == 12in.
const double mTocm = 100, inTocm = 2.54, ftToin = 12, inTom = 0.0254, ftTom = 0.3048;
string units;
double number, sum = 0, least = 1e300, most = 1e-300, count = 0;
cin.clear();
cin.ignore(1000, '\n');
cout << "Enter a number with its units (cm, m, in, ft): ";
while (cin >> number >> units) {
if (units == "in") {
cout << number << " inches is " << number * inTom << " m" << endl;
sum += number * inTom;
if (number * inTom > most) most = number * inTom;
if (number * inTom < least) least = number * inTom;
}
else if (units == "m") {
cout << number << " m is " << number << " m" << endl;
sum += number;
if (number > most) most = number;
if (number < least) least = number;
}
else if (units == "cm") {
cout << number << " cm is " << number / mTocm << " m" << endl;
sum += number / mTocm;
if (number / mTocm > most) most = number / mTocm;
if (number / mTocm < least) least = number / mTocm;
}
else if (units == "ft") {
cout << number << " ft is " << number * ftTom << " m" << endl;
sum += number * ftTom;
if (number * ftTom > most) most = number * ftTom;
if (number * ftTom < least) least = number * ftTom;
}
else cout << "I don't know those units!" << endl;
cout << "The smallest so far is " << least << endl
<< "The largest so far is " << most << endl
<< "The total so far is " << sum << endl;
}
}
int main() {
//part1();
//part2_3();
//part4();
//part5();
//part6();
//part7();
part9();
}