forked from ShmuelLa/number_with_units
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberWithUnits.cpp
173 lines (150 loc) · 6.63 KB
/
NumberWithUnits.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "NumberWithUnits.hpp"
#include <sstream>
using namespace::std;
namespace ariel {
NumberWithUnits::NumberWithUnits(double amount, const string &unit_type) {
if (units_map.count(unit_type) > 0) {
_unit.first = amount;
_unit.second = unit_type;
}
else {
throw("This unit does not exist!");
}
}
void NumberWithUnits::read_units(ifstream &file_name) {
string unit1, unit2;
double amount = 0;
while (!file_name.fail() && !file_name.eof() ) {
file_name >> unit1;
if (unit1 == "1") {
file_name >> unit1;
file_name >> unit2;
file_name >> amount;
file_name >> unit2;
units_map[unit1][unit2] = amount;
units_map[unit2][unit1] = 1/amount;
for (auto const &unit : units_map[unit1]) {
units_map[unit.first][unit2] = amount / unit.second;
units_map[unit2][unit.first] = unit.second / amount;
}
for (auto const &unit : units_map[unit2]) {
units_map[unit.first][unit1] = (1/amount) / unit.second;
units_map[unit1][unit.first] = unit.second / (1/amount);
}
}
}
}
NumberWithUnits NumberWithUnits::operator- () const {
NumberWithUnits result(_unit.first * (-1), _unit.second);
return result;
}
NumberWithUnits & NumberWithUnits::operator-- () {
_unit.first = _unit.first -1;
return *this;
}
NumberWithUnits & NumberWithUnits::operator++ () {
_unit.first = _unit.first +1;
return *this;
}
NumberWithUnits NumberWithUnits::operator-- (int) {
NumberWithUnits result(_unit.first, _unit.second);
_unit.first = _unit.first -1;
return result;
}
NumberWithUnits NumberWithUnits::operator++ (int) {
NumberWithUnits result(_unit.first, _unit.second);
_unit.first = _unit.first +1;
return result;
}
NumberWithUnits NumberWithUnits::operator- (const NumberWithUnits &other_num) const {
if (units_map[_unit.second].count(other_num._unit.second) > 0 && units_map[other_num._unit.second].count(_unit.second) > 0) {
double sum = _unit.first - (other_num._unit.first * units_map[other_num._unit.second][_unit.second]);
return NumberWithUnits(sum, _unit.second);
}
throw("Units does not match");
}
NumberWithUnits NumberWithUnits::operator+ () const {
return *this;
}
NumberWithUnits NumberWithUnits::operator+ (const NumberWithUnits &other_num) const {
if (units_map[_unit.second].count(other_num._unit.second) > 0 && units_map[other_num._unit.second].count(_unit.second) > 0) {
double sum = _unit.first + (other_num._unit.first * units_map[other_num._unit.second][_unit.second]);
return NumberWithUnits(sum, _unit.second);
}
throw("Units does not match");
}
bool NumberWithUnits::operator> (const NumberWithUnits &other_num) const {
if (units_map[_unit.second].count(other_num._unit.second) > 0 && units_map[other_num._unit.second].count(_unit.second) > 0) {
return (units_map[_unit.second][other_num._unit.second] * _unit.first > other_num._unit.first
|| (units_map[_unit.second][other_num._unit.second] * _unit.first - other_num._unit.first) > epsilon);
}
throw("Units does not match");
}
bool NumberWithUnits::operator< (const NumberWithUnits &other_num) const {
if (units_map[_unit.second].count(other_num._unit.second) > 0 && units_map[other_num._unit.second].count(_unit.second) > 0) {
return (this->_unit.first < other_num._unit.first * units_map[other_num._unit.second][_unit.second]);
}
throw("Units does not match");
}
bool NumberWithUnits::operator== (const NumberWithUnits &other_num) const {
if (units_map[_unit.second].count(other_num._unit.second) > 0 && units_map[other_num._unit.second].count(_unit.second) > 0) {
return (abs(_unit.first - other_num._unit.first * units_map[other_num._unit.second][_unit.second]) <= epsilon);
}
throw("Wrong Unit");
}
bool NumberWithUnits::operator<= (const NumberWithUnits &other_num) const {
return (*this < other_num || *this == other_num);
}
bool NumberWithUnits::operator>= (const NumberWithUnits &other_num) const {
if (units_map[_unit.second].count(other_num._unit.second) > 0 && units_map[other_num._unit.second].count(_unit.second) > 0) {
return (this->_unit.first >= other_num._unit.first * units_map[other_num._unit.second][_unit.second]);
}
throw("Units does not match");
}
bool NumberWithUnits::operator!= (const NumberWithUnits &other_num) const {
return !(*this == other_num);
}
NumberWithUnits& NumberWithUnits::operator+= (const NumberWithUnits &num) {
if (units_map[_unit.second].count(num._unit.second) > 0 && units_map[num._unit.second].count(_unit.second) > 0) {
_unit.first += (num._unit.first * units_map[num._unit.second][_unit.second]);
return *this;
}
throw("Units does not match");
}
NumberWithUnits& NumberWithUnits::operator-= (const NumberWithUnits &num) {
if (units_map[_unit.second].count(num._unit.second) > 0 && units_map[num._unit.second].count(_unit.second) > 0) {
_unit.first -= (num._unit.first * units_map[num._unit.second][_unit.second]);
return *this;
}
throw("Units does not match");
}
NumberWithUnits NumberWithUnits::operator* (double factor) const {
return NumberWithUnits(_unit.first * factor, this->_unit.second);
}
NumberWithUnits operator* (double factor, const NumberWithUnits &other_num) {
return other_num * factor;
}
ostream& operator<< (ostream& stream, const NumberWithUnits &num) {
stream << num._unit.first << "[" << num._unit.second << "]";
return stream;
}
istream& operator>> (istream& stream, NumberWithUnits &num) {
double in_num = 0;
string result;
char tmp_char = ' ';
stream >> in_num;
stream >> tmp_char ;
while (tmp_char != ']') {
if (tmp_char != '[') {
result.push_back(tmp_char);
}
stream >> tmp_char;
}
if (units_map.count(result) > 0) {
num._unit.first = in_num;
num._unit.second = result;
return stream;
}
throw("Wrong Unit");
}
}