forked from AfikPeretz/NumberWithUnits.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberWithUnits.cpp
More file actions
214 lines (175 loc) · 7.12 KB
/
Copy pathNumberWithUnits.cpp
File metadata and controls
214 lines (175 loc) · 7.12 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "NumberWithUnits.hpp"
#include <sstream>
using namespace::std;
namespace ariel {
//constractor
NumberWithUnits::NumberWithUnits(double amount, const string &unit_type) {
if (unitsType.count(unit_type) > 0) {
_unit.first = amount;
_unit.second = unit_type;
}
else {
throw("It is not possible to connect different types of units of measure");
}
}
/*
This method in the first stage reads from a file and inserts into the variables and in the second stage
creates the conversion rate between the various units of measurement (such that can be converted)
*/
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;
unitsType[unit1][unit2] = amount;
unitsType[unit2][unit1] = 1/amount;
for (auto const &unit : unitsType[unit1]) {
unitsType[unit.first][unit2] = amount / unit.second;
unitsType[unit2][unit.first] = unit.second / amount;
}
for (auto const &unit : unitsType[unit2]) {
unitsType[unit.first][unit1] = (1/amount) / unit.second;
unitsType[unit1][unit.first] = unit.second / (1/amount);
}
}
}
}
//operators
NumberWithUnits NumberWithUnits::operator- () const {
NumberWithUnits result(_unit.first * (-1), _unit.second);
return result;
}
NumberWithUnits NumberWithUnits::operator- (const NumberWithUnits &other_num) const {
if (unitsType[_unit.second].count(other_num._unit.second) > 0 && unitsType[other_num._unit.second].count(_unit.second) > 0) {
double sum = _unit.first - (other_num._unit.first * unitsType[other_num._unit.second][_unit.second]);
return NumberWithUnits(sum, _unit.second);
}
throw("Different units of measure");
}
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++ () {
_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+ () const {
return *this;
}
NumberWithUnits NumberWithUnits::operator+ (const NumberWithUnits &other_num) const {
if (unitsType[_unit.second].count(other_num._unit.second) > 0) {
double sum = _unit.first + (other_num._unit.first * unitsType[other_num._unit.second][_unit.second]);
return NumberWithUnits(sum, _unit.second);
}
throw("Different units of measure");
}
bool NumberWithUnits::operator> (const NumberWithUnits &other_num) const {
if (unitsType[_unit.second].count(other_num._unit.second) > 0) {
return ((unitsType[_unit.second][other_num._unit.second] * _unit.first - other_num._unit.first) > epsilon);
}
throw("Different units of measure");
}
bool NumberWithUnits::operator>= (const NumberWithUnits &other_num) const {
if (unitsType[_unit.second].count(other_num._unit.second) > 0) {
return (this->_unit.first >= other_num._unit.first * unitsType[other_num._unit.second][_unit.second]);
}
throw("Different units of measure");
}
bool NumberWithUnits::operator< (const NumberWithUnits &other_num) const {
if (unitsType[_unit.second].count(other_num._unit.second) > 0) {
return (this->_unit.first < other_num._unit.first * unitsType[other_num._unit.second][_unit.second]);
}
throw("Different units of measure");
}
bool NumberWithUnits::operator<= (const NumberWithUnits &other_num) const {
return (*this < other_num || *this == other_num);
}
bool NumberWithUnits::operator== (const NumberWithUnits &other_num) const {
if (unitsType[_unit.second].count(other_num._unit.second) > 0 && unitsType[other_num._unit.second].count(_unit.second) > 0) {
return (abs(_unit.first - other_num._unit.first * unitsType[other_num._unit.second][_unit.second]) <= epsilon);
}
throw("Different units of measure");
}
bool NumberWithUnits::operator!= (const NumberWithUnits &other_num) const {
return !(*this == other_num);
}
NumberWithUnits& NumberWithUnits::operator+= (const NumberWithUnits &num) {
if (unitsType[_unit.second].count(num._unit.second) > 0) {
_unit.first += (num._unit.first * unitsType[num._unit.second][_unit.second]);
return *this;
}
throw("Different units of measure");
}
NumberWithUnits& NumberWithUnits::operator-= (const NumberWithUnits &num) {
if (unitsType[_unit.second].count(num._unit.second) > 0 ) {
_unit.first -= (num._unit.first * unitsType[num._unit.second][_unit.second]);
return *this;
}
throw("Different units of measure");
}
NumberWithUnits NumberWithUnits::operator* (double factor) const {
return NumberWithUnits(_unit.first * factor, _unit.second);
}
/**
* @brief
*
* @param factor
* @param other_num
* @return NumberWithUnits
*/
NumberWithUnits operator* (double factor, const NumberWithUnits &other_num) {
return other_num * factor;
}
/**
* @brief This method creates a string that represents the object for the output stream
* @param stream output sream
* @param num
* @return ostream&
*/
ostream& operator<< (ostream& stream, const NumberWithUnits &num) {
stream << num._unit.first << "[" << num._unit.second << "]";
return stream;
}
/**
* @brief This method produces an object from a text file and by reading agreed markings knows
how to put the information into the different types of variables
* @param stream
* @param num
* @return istream&
*/
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 (unitsType.count(result) > 0) {
num._unit.first = in_num;
num._unit.second = result;
return stream;
}
throw("Wrong Unit");
}
}