-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysicalNumber.cpp
More file actions
245 lines (236 loc) · 6.41 KB
/
Copy pathPhysicalNumber.cpp
File metadata and controls
245 lines (236 loc) · 6.41 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//
// PhysicalNumber.cpp
// PhysicalNumber
//
// Created by Jeme Jbareen on 4/2/19.
// Copyright © 2019 Jeme Jbareen. All rights reserved.
//
#include "PhysicalNumber.h"
#include "Unit.h"
#include <string>
#include <sstream>
#include<vector>
#include <map>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace ariel;
bool PhysicalNumber::checkType(const PhysicalNumber &a) const{
if(this->_type>=0 && this->_type<=2 && a._type>=0 && a._type<=2) return true;
else if(this->_type>=3 && this->_type<=5 && a._type>=3 && a._type<=5) return true;
else if(this->_type>=6 && this->_type<=8 && a._type>=6 && a._type<=8) return true;
return false;
}
double PhysicalNumber::get_num() const{return _num;}
Unit PhysicalNumber::get_type() const{return _type;}
double PhysicalNumber::Diff(const PhysicalNumber &a) const{
switch (a.get_type()) {
case KM:
return 100000;
break;
case M:
return 100;
break;
case CM:
break;
case HOUR:
return 3600;
break;
case MIN:
return 60;
break;
case SEC:
break;
case TON:
return 1000000;
break;
case KG:
return 1000;
break;
case G:
break;
default:
return 1;
break;
}
return 1;
}
const PhysicalNumber PhysicalNumber::operator+(const PhysicalNumber& a) const{
if(!checkType(a))
throw "you're trying to add two different types!";
double val=Diff(a)*a._num + Diff(*(this))*_num;
val/=Diff(*(this));
return PhysicalNumber(val,_type);
}
const PhysicalNumber PhysicalNumber::operator-(const PhysicalNumber& a) const{
if(!checkType(a))
throw "you're trying to add two different types!";
double val=Diff(*(this))*_num-Diff(a)*a._num;
val/=Diff(*(this));
return PhysicalNumber(val,_type);
}
PhysicalNumber& PhysicalNumber::operator+=(const PhysicalNumber& a){
if(!checkType(a))
throw "you're trying to add two different types!";
double val=Diff(*(this))*_num +Diff(a)*a._num;
val/=Diff(*(this));
_num=val;
return *this;
}
PhysicalNumber& PhysicalNumber::operator-=(const PhysicalNumber& a){
if(!checkType(a))
throw "you're trying to add two different types!";
double val=Diff(*(this))*_num - Diff(a)*a._num;
val/=Diff(*(this));
_num=val;
return *this;
}
PhysicalNumber& PhysicalNumber::operator=(const PhysicalNumber& a){
_num=a._num;
_type=a._type;
return *this;
}
const PhysicalNumber PhysicalNumber::operator+() const {
return *(this);
}
const PhysicalNumber PhysicalNumber::operator-() const {
return PhysicalNumber(-_num,_type);
}
const bool PhysicalNumber::operator==(const PhysicalNumber &a) const{
if(!checkType(a))
throw "you're trying to comparee two different types!";
if(Diff(*(this))*_num==(Diff(a)*a._num))
return true;
return false;
}
const bool PhysicalNumber::operator<(const PhysicalNumber &a) const{
if(!checkType(a))
throw "you're trying to comparee two different types!";
if(Diff(*(this))*_num<(Diff(a)*a._num))
return true;
return false;
}
const bool PhysicalNumber::operator>(const PhysicalNumber &a) const{
if(!checkType(a))
throw "you're trying to comparee two different types!";
if(Diff(*(this))*_num>(Diff(a)*a._num))
return true;
return false;
}
const bool PhysicalNumber::operator<=(const PhysicalNumber &a) const{
if(!checkType(a))
throw "you're trying to comparee two different types!";
if(Diff(*(this))*_num<=(Diff(a)*a._num))
return true;
return false;
}
const bool PhysicalNumber::operator>=(const PhysicalNumber &a) const{
if(!checkType(a))
throw "you're trying to comparee two different types!";
if(Diff(*(this))*_num>=(Diff(a)*a._num))
return true;
return false;
}
const bool PhysicalNumber::operator!=(const PhysicalNumber &a) const{return !(*(this)==a);}
PhysicalNumber& PhysicalNumber::operator++(){
_num++;
return *this;
}
PhysicalNumber& PhysicalNumber::operator--(){
_num--;
return *this;
}
std::ostream& ariel::operator<<(std::ostream& os, const PhysicalNumber& a){
switch (a._type) {
case KM:
os<<a._num<<"[km]";
break;
case M:
os<<a._num<<"[m]";
break;
case CM:
os<<a._num<<"[cm]";
break;
case HOUR:
os<<a._num<<"[hour]";
break;
case MIN:
os<<a._num<<"[min]";
break;
case SEC:
os<<a._num<<"[sec]";
break;
case TON:
os<<a._num<<"[ton]";
break;
case KG:
os<<a._num<<"[kg]";
break;
default:
os<<a._num<<"[g]";
break;
}
return os;
}
Unit get_type(const std::string s){
static std::map<std::string, Unit> string_type{
{"CM", ariel::CM},
{"M", ariel::M},
{"KM", ariel::KM},
{"SEC", ariel::SEC},
{"MIN", ariel::MIN},
{"HOUR", ariel::HOUR},
{"G", ariel::G},
{"KG", ariel::KG},
{"TON", ariel::TON},
};
auto x = string_type.find(s);
if (x != end(string_type)){
return x->second;
}
throw std::invalid_argument(s);
}
istream &ariel::operator>>(istream &is, PhysicalNumber &a)
{
std::string str;
is >> str;
if (str.find("]") == -1 || str.find("[") == -1 || str.find("[]")!=-1){
return is;
}
str = str.substr(0, str.length() - 1);
std::stringstream ss(str);
std::vector<std::string> result;
while (ss.good()){
std::string substr;
getline(ss, substr, '[');
result.push_back(substr);
}
double val=0;
try{
val = stod(result[0]);
std::transform(result[1].begin(), result[1].end(), result[1].begin(), ::toupper);
}
catch(...){
return is;
}
Unit un =Unit::CM;
const std::string type = result[1];
try{
un = get_type(type);
}
catch(...){
return is;
}
a = PhysicalNumber(val, un);
return is;
}
const PhysicalNumber PhysicalNumber::operator++ (int){
PhysicalNumber a(*(this));
++*(this);
return a;
}
const PhysicalNumber PhysicalNumber::operator--(int){
PhysicalNumber a(*(this));
--*(this);
return a;
}