-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter4-drill.cpp
More file actions
85 lines (78 loc) · 1.91 KB
/
Copy pathchapter4-drill.cpp
File metadata and controls
85 lines (78 loc) · 1.91 KB
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
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
void print_almost_equal(double val_1,double val_2)
{
const double almost_equal = 1.0/10000000;
if(fabs(val_1 - val_2) < almost_equal)
{
std::cout << "the numbers are almost equal" << std::endl;
}
}
void print_vector(std::vector<double> & v)
{
sort(v.begin(),v.end());
for(unsigned int i = 0; i < v.size();i++)
{
std::cout << v[i] << std::endl;
}
}
int main()
{
double value = 0.0;
const double m_to_cm =100 , inch_to_cm = 2.54, feet_to_inches = 12;
double smallest = 0.0, largest = 0.0,sum = 0.0;
bool is_set = false;
int counter = 0;
std::vector<double> store;
std::string unit = "";
while(std::cin >> value >> unit)//takes in a number followed by a unit, example 10 cm, value = 10 , unit = cm
{
if(unit.compare("m") == 0)
{
sum+=value;
}
else if (unit.compare("cm") == 0)
{
sum = sum + (value/100);
}
else if(unit.compare("in") == 0)
{
sum = sum + ((value * inch_to_cm)/100);
}
else if(unit.compare("ft") == 0)
{
sum = sum + ((value * feet_to_inches * inch_to_cm)/100);
}
else
{
std::cout << "Unrecognized unit: " << unit << std::endl;
return 0;
}
if(smallest == 0.0 && largest == 0.0 && is_set == false)
{
smallest = value;
largest = value;
is_set = true;
}
else if(value > largest)
{
largest = value;
std::cout << largest << " largest so far " << std::endl
<< "but the smallest is still " << smallest << std::endl;
}
else if (value < smallest)
{
smallest = value;
std::cout << smallest << " smallest so far " << std::endl
<< "but the largest is still " << largest << std::endl;
}
counter++;
store.push_back(value);
}
print_vector(store);
std::cout << "sum is: " << sum << "m" << " number of values entered is: " << counter << std::endl;
return 0;
}