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 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
// Osman Zakir
// 12 / 19 / 2016
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 4 Drill
// Drill specs:
/**
* 1. Write a program that consists of a while -loop that (each time around the
* loop) reads in two int s 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 con-
* version 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).
*/
#include "../../std_lib_facilities.h"
int main()
{
double value = 0;
double smaller = std::numeric_limits<double>::max();
double larger = std::numeric_limits<double>::min();
double meters = 0;
constexpr double cm_to_m = 100.0, in_to_m = 0.0254, ft_to_m = 0.3048;
const char exit_ch = '|';
double sum = 0;
string unit = "";
char exit_test = ' ';
vector<double> numbers_entered;
cout << "Enter values with units; enter '|' to stop\n"
<< "(accepted units are: cm, m, in, ft\n"
<< "all values will be converted to meters):\n";
while (cin.get(exit_test))
{
if (exit_test == exit_ch)
{
break;
}
else if (isdigit(exit_test) || exit_test == '.')
{
cin.putback(exit_test);
cin >> value >> unit;
cin.ignore();
if (unit == "cm")
{
meters = value / cm_to_m;
numbers_entered.push_back(meters);
sum += meters;
value = meters;
}
else if (unit == "ft")
{
meters = value * ft_to_m;
numbers_entered.push_back(meters);
sum += meters;
value = meters;
}
else if (unit == "in")
{
meters = value * in_to_m;
numbers_entered.push_back(meters);
sum += meters;
value = meters;
}
else if (unit == "m")
{
meters = value;
numbers_entered.push_back(meters);
sum += meters;
value = meters;
}
else
{
cout << "Error: invalid unit. Please try again.\n";
}
if (value > larger)
{
larger = value;
cout << value << ": the largest so far\n";
}
if (value < smaller)
{
smaller = value;
cout << value << ": the smallest so far\n";
}
}
}
sort(numbers_entered);
cout << fixed;
cout << setprecision(2);
cout << "The sum of all values entered is: " << sum << "\n"
<< "The number of values entered is: " << numbers_entered.size() << "\n"
<< "The smallest value entered is: " << smaller << "\n"
<< "The largest value entered is: " << larger << "\n\n";
cout << "All of the numbers entered are:\n";
for (auto const &x : numbers_entered)
{
cout << x << "\n";
}
keep_window_open();
}
|