forked from matanb1238/NumberWithUnits-A
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberWithUnits.cpp
162 lines (147 loc) · 5.08 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <map>
#include <string.h>
#include "graph.cpp"
#include "NumberWithUnits.hpp"
using namespace std;
namespace ariel
{
static graph g;
double NumberWithUnits::convert(const string &from, const string &to, double fromVal){
if(from == to){
return fromVal;
}
double retVal = g.getConv(from, to);
if(retVal > -1){
return fromVal*retVal;
}
__throw_invalid_argument("Not valid convertion");
}
void NumberWithUnits::read_units(ifstream &f)
{
string num1;
string num2;
string n;
string l;
double value1 = 0;
double value2 = 0;
while (getline(f, l))
{
istringstream Sstream(l);
if (!(Sstream >> value1 >> num1 >> n >> value2 >> num2))
{
break;
}
g.addEdge(num1 ,num2, (value2/value1));
}
}
NumberWithUnits::NumberWithUnits(const double& value, const string &unit){
if (!g.checkExist(unit))
{
__throw_invalid_argument("Invalid unit");
}
num=value;
the_unit=unit;
}
int comparison(const NumberWithUnits& num1, const NumberWithUnits& num2)
{
double x = num1.num - NumberWithUnits::convert(num2.the_unit, num1.the_unit, num2.num);
double y = NumberWithUnits::convert(num2.the_unit, num1.the_unit, num2.num) - num1.num;
const double epsilon = 0.00001;
if (x > epsilon)
{
return 1;
}
if (y > epsilon)
{
return -1;
}
return 0;
}
// in/out
ostream &operator<<(ostream &os, const NumberWithUnits &number) {
return (os << number.num << '[' << number.the_unit << ']');
}
istream &operator>>(std::istream &is, NumberWithUnits &unit)
{
string s;
double number = 0;
char c = ']';
is >> number >> c;
while(c != ']'){
if(c != '['){
s.insert(s.end(), c);
}
is >> c;
}
unit.num = number;
unit.the_unit = s;
if(!g.checkExist(unit.the_unit)){
__throw_invalid_argument("Unit is not exist");
}
return is;
}
NumberWithUnits operator+(const NumberWithUnits &num1, const NumberWithUnits &num2)
{
double converted = NumberWithUnits::convert(num2.the_unit, num1.the_unit, num2.num);
return NumberWithUnits(num1.num + converted, num1.the_unit);
}
NumberWithUnits operator+(const NumberWithUnits& num1, double num) {
return NumberWithUnits(num1.num + num, num1.the_unit);
}
NumberWithUnits& NumberWithUnits::operator+=(const NumberWithUnits &num2)
{
double con = convert(num2.the_unit, this->the_unit, num2.num);
this->num += con;
return *this;
}
NumberWithUnits operator-(const NumberWithUnits &num1, const NumberWithUnits &num2)
{
double converted = NumberWithUnits::convert(num2.the_unit, num1.the_unit, num2.num);
return NumberWithUnits(num1.num - converted, num1.the_unit);
}
NumberWithUnits operator-(const NumberWithUnits& num1, double num){
return NumberWithUnits(num1.num - num, num1.the_unit);
}
NumberWithUnits& NumberWithUnits::operator-=(const NumberWithUnits &num2)
{
double con = convert(num2.the_unit, this->the_unit, num2.num);
this->num -= con;
return *this;
}
// bool operators
bool operator>(const NumberWithUnits &num1, const NumberWithUnits &num2) { return comparison(num1, num2) > 0; }
bool operator>=(const NumberWithUnits &num1, const NumberWithUnits &num2) { return comparison(num1, num2) >= 0; }
bool operator<(const NumberWithUnits &num1, const NumberWithUnits &num2) { return comparison(num1, num2) < 0; }
bool operator<=(const NumberWithUnits &num1, const NumberWithUnits &num2) { return comparison(num1, num2) <= 0; }
bool operator==(const NumberWithUnits &num1, const NumberWithUnits &num2) { return comparison(num1, num2) == 0; }
bool operator!=(const NumberWithUnits &num1, const NumberWithUnits &num2) { return comparison(num1, num2) != 0; }
// increment/decrement
NumberWithUnits& operator++(NumberWithUnits &number) {
++number.num;
return number;
}
NumberWithUnits operator++(NumberWithUnits &number, int) {
NumberWithUnits temp = number;
number.num++;
return temp;
}
NumberWithUnits& operator--(NumberWithUnits &number) {
--number.num;
return number;
}
NumberWithUnits operator--(NumberWithUnits &number, int) {
NumberWithUnits temp = number;
number.num--;
return temp;
}
NumberWithUnits operator*(const NumberWithUnits &unit, double n) {
return NumberWithUnits{unit.num*n, unit.the_unit};
}
NumberWithUnits operator*(double n, const NumberWithUnits &unit) {
return NumberWithUnits{unit.num*n, unit.the_unit};
}
}