-
Notifications
You must be signed in to change notification settings - Fork 0
/
scalarUnits.cpp
234 lines (207 loc) · 6.86 KB
/
scalarUnits.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
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
#include "scalarUnits.h"
#include <cstdio>
#include <QTextStream>
#include "precision.h"
#include "toString.h"
scalarUnits::scalarUnits(long double newValue, QString newUnit)
{
value = newValue;
QList<QString> empty;
unit.append(empty); // numerators
unit.append(empty); // denominators
unit[numerator].append(newUnit);
unit[denominator].append("");
}
scalarUnits::scalarUnits(long double newValue, QList<QList<QString> > newUnit)
{
value = newValue;
unit = newUnit;
}
QList<QList<QString> > scalarUnits::simplifyUnits()
{
for (int indexNumer = 0; indexNumer < unit[numerator].length(); indexNumer++)
{
for (int indexDenom = 0; indexDenom < unit[denominator].length(); indexDenom++)
{
if (unit[denominator][indexDenom] == unit[denominator][indexNumer])
{
unit[numerator].removeAt(indexNumer);
unit[denominator].removeAt(indexDenom);
indexNumer--;
indexDenom--;
}
}
}
return unit;
}
bool scalarUnits::unitsEqual(QList<QList<QString> > newUnit) const
{
if ((unit[numerator][0] == "") || (unit[denominator][0] == "")) return false;
if (newUnit.length() != unit.length()) return false;
if (newUnit[numerator].length() != unit[numerator].length()) return false;
if (newUnit[denominator].length() != unit[denominator].length()) return false;
for (int index = 0; index < newUnit[numerator].length(); index++) {
if (newUnit[numerator][index] != unit[numerator][index]) return false;
}
for (int index = 0; index < newUnit[denominator].length(); index++) {
if (newUnit[denominator][index] != unit[denominator][index]) return false;
}
return true;
}
long double scalarUnits::round(long double number, int digits)
{
long double multiplier = 1;
if (digits > 0)
for (int index = 0; index < digits; index ++)
{
multiplier *= 10;
}
else {
for (int index = 0; index > digits; index --)
{
multiplier /= 10;
}
}
return roundl(number * multiplier) / multiplier;
}
QString toString(scalarUnits scalar)
{
QString unitsFinal = scalar.unit[scalarUnits::numerator][0];
for (int index = 1; index < scalar.unit[scalarUnits::numerator].length(); index++)
{
unitsFinal += " * " + scalar.unit[scalarUnits::numerator][index];
}
if (scalar.unit[scalarUnits::denominator][0] != "")
{
unitsFinal += " / (";
for (int index = 0; index < scalar.unit[scalarUnits::denominator].length(); index++)
{
unitsFinal += " * " + scalar.unit[scalarUnits::denominator][index];
}
unitsFinal += ")";
}
return toStringD(qRound(static_cast<double>(scalar.getValue()))) + unitsFinal;
}
scalarUnits operator+(const scalarUnits& left, const scalarUnits& right) {
scalarUnits temp;
if ( left.unitsEqual(right.getUnit())) {
temp.unit = left.unit;
temp.value = left.value + right.value;
}
else {
temp.unit = left.unit;
temp.value = scalarUnits::errorValue;
QTextStream stream(stderr);
stream << "Tried to add " + toString(left) + " and " + toString(right) + ", units do not match";
}
return temp;
}
scalarUnits operator-(const scalarUnits& left, const scalarUnits& right) {
return left+(-right);
}
scalarUnits operator*(const scalarUnits& left, const scalarUnits& right) {
scalarUnits temp;
if( left.unitsEqual(right.getUnit())) {
temp.unit = left.unit;
temp.value = left.value * right.value;
}
else {
temp.unit = left.unit;
temp.value = scalarUnits::errorValue;
QTextStream stream(stderr);
stream << "Tried to multiply " + toString(left) + " and " + toString(right) + ", units do not match";
}
return temp;
}
scalarUnits operator/(const scalarUnits& left, const scalarUnits& right) {
scalarUnits temp;
if( left.unitsEqual(right.unit)) {
temp.unit = left.unit;
temp.value = left.value / right.value;
}
else {
temp.unit = left.unit;
temp.value = scalarUnits::errorValue;
QTextStream stream(stderr);
stream << "Tried to divide" + toString(left) + " and " + toString(right) + ", units do not match";
}
return temp;
}
bool operator==(const scalarUnits& left, const scalarUnits& right) {
return ((std::abs(left.value - right.value) < zeroL) && left.unitsEqual(right.unit));
}
bool operator!=(const scalarUnits& left, const scalarUnits& right) {
return !(left == right);
}
bool operator<(const scalarUnits& left, const scalarUnits& right) {
if( left.unitsEqual(right.unit)) {
return left.value < right.value;
}
else {
QTextStream stream(stderr);
stream << "Tried to compare (<) " + toString(left) + " and " + toString(right) + ", units do not match";
}
return false;
}
bool operator>(const scalarUnits& left, const scalarUnits& right) {
if( left.unitsEqual(right.unit)) {
return left.value > right.value;
}
else {
QTextStream stream(stderr);
stream << "Tried to compare (>) " + toString(left) + " and " + toString(right) + ", units do not match";
}
return false;
}
bool operator<=(const scalarUnits& left, const scalarUnits& right) {
if( left.unitsEqual(right.unit)) {
return left.value <= right.value;
}
else {
QTextStream stream(stderr);
stream << "Tried to compare (<=) " + toString(left) + " and " + toString(right) + ", units do not match";
}
return false;
}
bool operator>=(const scalarUnits& left, const scalarUnits& right) {
if( left.unitsEqual(right.unit)) {
return left.value >= right.value;
}
else {
QTextStream stream(stderr);
stream << "Tried to compare (>=) " + toString(left) + " and " + toString(right) + ", units do not match";
}
return false;
}
scalarUnits& scalarUnits::operator+=(const scalarUnits& obj) {
*this = *this+obj;
return *this;
}
scalarUnits& scalarUnits::operator-=(const scalarUnits& obj) {
*this = *this-obj;
return *this;
}
scalarUnits& scalarUnits::operator*=(const scalarUnits& obj) {
*this = *this*obj;
return *this;
}
scalarUnits& scalarUnits::operator/=(const scalarUnits& obj) {
*this = *this/obj;
return *this;
}
scalarUnits& scalarUnits::operator++() {
*this = *this+1;
return *this;
}
scalarUnits& scalarUnits::operator--() {
*this = *this-1;
return *this;
}
scalarUnits scalarUnits::operator+() const {
return *this;
}
scalarUnits scalarUnits::operator-() const {
scalarUnits temp;
temp.value = -temp.value;
return temp;
}