-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber.cpp
More file actions
335 lines (300 loc) · 7.72 KB
/
Number.cpp
File metadata and controls
335 lines (300 loc) · 7.72 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <gmpxx.h>
#include <iostream>
#include <stdexcept>
#include <cmath>
#include <regex>
#include <vector>
#include "Number.h"
void input_string(mpz_class& a, mpz_class& b, std::string c);
std::string output_string(mpq_class value);
struct Number::Impl
{
mpq_class value;
};
// (Oscar) Constructors/Deconstructor
Number::Number() : pimpl( new Impl())
{
}
Number::Number(std::string s) : pimpl( new Impl())
{
input_string(this->pimpl->value.get_num(), this->pimpl->value.get_den(), s);
this->pimpl->value.canonicalize();
}
Number::Number(const char* s) : pimpl (new Impl())
{
input_string(this->pimpl->value.get_num(), this->pimpl->value.get_den(), std::string(s));
this->pimpl->value.canonicalize();
}
Number::Number(const Number& n) : pimpl( new Impl())
{
this->pimpl->value = n.pimpl->value;
}
Number::Number(Number&& n)
{
this->pimpl = n.pimpl;
n.pimpl = nullptr;
}
Number::~Number()
{
delete pimpl;
}
// (Oscar) Arithmetic Operators
Number Number::operator+(const Number& n) const
{
Number sum;
sum.pimpl->value = this->pimpl->value + n.pimpl->value;
return sum;
}
Number Number::operator-(const Number& n) const
{
Number diff;
diff.pimpl->value = this->pimpl->value - n.pimpl->value;
return diff;
}
Number Number::operator*(const Number& n) const
{
Number product;
product.pimpl->value = this->pimpl->value * n.pimpl->value;
return product;
}
Number Number::operator/(const Number& n) const
{
Number quotient; // Currently zero, 0/1
if (quotient == n) // Is the denominator 0?
throw std::runtime_error("Error: Division by Zero\n");
quotient.pimpl->value = this->pimpl->value / n.pimpl->value;
return quotient;
}
// (Oscar) Comparison Operators
bool Number::operator<(const Number& n) const
{
if (cmp(this->pimpl->value, n.pimpl->value) < 0) // negative if less than
return true;
else
return false;
}
bool Number::operator>(const Number& n) const
{
if (cmp(this->pimpl->value, n.pimpl->value) > 0) // positive if greater than
return true;
else
return false;
}
bool Number::operator==(const Number& n) const
{
if (cmp(this->pimpl->value, n.pimpl->value) == 0) // 0 if equal to
return true;
else
return false;
}
bool Number::operator!=(const Number& n) const
{
return !(*this == n);
}
bool Number::operator>=(const Number& n) const
{
return !(*this < n);
}
bool Number::operator<=(const Number& n) const
{
return !(*this > n);
}
// (Oscar) Assignment Operators
Number& Number::operator=(std::string s)
{
input_string(this->pimpl->value.get_num(), this->pimpl->value.get_den(), s);
this->pimpl->value.canonicalize();
return *this;
}
Number& Number::operator=(const char* s)
{
input_string(this->pimpl->value.get_num(), this->pimpl->value.get_den(), std::string(s));
this->pimpl->value.canonicalize();
return *this;
}
Number& Number::operator=(Number&& n)
{
this->pimpl = n.pimpl;
n.pimpl = nullptr;
return *this;
}
Number& Number::operator=(const Number& n)
{
this->pimpl->value = n.pimpl->value;
return *this;
}
Number& Number::operator+=(const Number& n)
{
*this = *this + n;
return *this;
}
Number& Number::operator-=(const Number& n)
{
*this = *this - n;
return *this;
}
Number& Number::operator*=(const Number& n)
{
*this = *this * n;
return *this;
}
Number& Number::operator/=(const Number& n)
{
*this = *this / n;
return *this;
}
// (Oscar) Input Operator
std::istream& operator>>(std::istream& is, Number& n)
{
std::string input;
is >> input;
n = input;
return is;
}
// (Oscar) Output Operator
std::ostream& operator<<(std::ostream& os, const Number& n)
{
if (n.pimpl != nullptr)
os << output_string(n.pimpl->value);
return os;
}
// (Sam)
void input_string(mpz_class& a, mpz_class& b, std::string c)
{
std::regex numberFormat("^[-]?[0-9]*[.]?[0-9]*$"); // Regular Expression for a number
std::smatch isFormated;
if (!regex_search(c, isFormated, numberFormat))
throw std::runtime_error("Error: Invalid Number Input Format\n");
int powTen;
while(c.front() == '0' && c.length() > 1 && c.at(1) != '.')
{
c.erase(c.begin()); // Pop off zeros in the front
}
int indexP = c.find('.');
if (indexP == std::string::npos) // Case 1: No decimal point
{
powTen = 0;
b = 1;
a = c;
}
else // Case 2: There is a decimal point
{
while(c.back() == '0' && c.length() > 1 && c.at(c.length() - 2) != '.')
{
c.pop_back(); // Pop off zeros at the back
}
powTen = (c.length() - 1) - indexP; // How many decimal places to move
b = pow(10, powTen);
c.erase(indexP, 1);
a = c;
}
}
// (Sam)
std::string chopZeros(std::string input)
{
while(input.back() == '0')
{
input.pop_back(); // Pop off zeros at the back
}
return input;
}
std::string output_string(mpq_class value)
{
mpz_class numer = value.get_num();
mpz_class denom = value.get_den();
mpz_class div1 = numer;
mpz_class div2 = denom;
mpz_class remainder = 0;
mpz_class tempRemainder = 0;
std::string ans = "";
std::string dividendSTR = div1.get_str();
std::string divisorSTR = div2.get_str();
if (dividendSTR.front() == '-')
{
dividendSTR.erase(0,1);
ans.push_back('-');
}
if (divisorSTR.front() == '-')
{
divisorSTR.erase(0,1);
if (ans.front() == '-')
{
ans.erase(0,1);
}
else
{
ans.push_back('-');
}
}
std::string dividendSTRO = dividendSTR;
int decimal = -1;
for (int i = 0; i < dividendSTR.length(); i++)
{
if (dividendSTR.at(i) == '.')
{
decimal = i;
}
}
if (decimal = -1)
{
decimal = dividendSTR.length();
}
while(dividendSTRO.length() < (decimal + 6))
{
dividendSTRO.push_back('0');
}
// First stoi change
div1 = dividendSTRO.at(0) - '0';
div2 = mpz_class(divisorSTR, 10);
int when = decimal + 5;
for (int i = 0; i < when; i++)
{
if(i == decimal)
{
ans.push_back('.');
}
else
{
remainder = div1 / div2;
// second stoi change
tempRemainder = mpz_class(remainder.get_str(), 10);
ans.push_back('0' + tempRemainder.get_ui());
div1 = (div1 - (div2 * remainder)) * 10;
div1 = div1 + (dividendSTRO.at(i + 1) - '0');
}
}
while (ans.front() == '0' && ans.at(1) != '.')
{
ans.erase(ans.begin());
}
if (ans.front() == '-')
{
while (ans.at(1) == '0' && ans.at(2) != '.')
{
ans.erase(1,1);
}
}
return ans;
}
// (Sam)
Number sqrt(const Number& n)
{
Number squareRoot;
// Basically I create 3 floats to store the data I need:
// the sqrt(numerator), sqrt(denominator), and the old denominator
mpf_class numer = n.pimpl->value.get_num();
mpf_class denom = n.pimpl->value.get_den();
mpf_class oldDenom = denom;
if (sgn(numer) != sgn(denom))
throw std::runtime_error("Error: Negative Number Under Square Root\n");
numer = sqrt(numer);
denom = sqrt(denom);
// Here i set the numerator to sqrt(numer) * sqrt(denom),
// and I set the denominator to oldDenom
// This way the new value is squrt(numer) * sqrt(denom) / denom
// to preserve the precision of the denominator
squareRoot.pimpl->value.get_num() = numer * denom;
squareRoot.pimpl->value.get_den() = oldDenom;
squareRoot.pimpl->value.canonicalize();
return squareRoot;
}