-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigInteger.cpp
More file actions
398 lines (340 loc) · 9.98 KB
/
Copy pathBigInteger.cpp
File metadata and controls
398 lines (340 loc) · 9.98 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "BigInteger.h"
BigInteger::BigInteger()
{
digit_vector.clear();
isNegative = false;
}
BigInteger::BigInteger(std::string really_big_number)
{
std::string tmp ="";
bool isValid = true, allZeros = false;
int counter = 0;
isNegative = false;
for (int i = 0; i < really_big_number.length(); i++)
{
try
{
if (i == 0)
{
if (!(really_big_number[0] == '-' || (really_big_number[0] >= 48 && really_big_number[0] <= 57))) //check to see if first digit is a - or a number
{
throw BigIntException();
}
}
else
{
if (really_big_number[i] < 48 || really_big_number[i] > 57) //check to see if the rest of the digits are numbers
{
throw BigIntException();
}
}
}
catch (BigIntException &e)
{
cout << "Error occurred: " << e.what() << endl;
i = really_big_number.length();
isValid = false;
}
}
if (really_big_number[0] != '-') //if number isn't negative, remove leading zeros
{
while (counter < really_big_number.length()-1 && really_big_number[counter] == '0')
counter++;
for (int i = counter; i < really_big_number.length(); i++)
tmp+=really_big_number[i];
if (counter != 0)
really_big_number = tmp;
if (tmp=="" && counter == really_big_number.length()-1)
really_big_number = "0";
}
else //if number is negative, remove leading zeros
{
really_big_number = really_big_number.substr(1);
while (counter < really_big_number.length()-1 && really_big_number[counter] == '0')
counter++;
tmp+='-';
for (int i = counter; i < really_big_number.length(); i++)
tmp+=really_big_number[i];
if (tmp!="-")
really_big_number = tmp;
if (tmp=="-" && counter == really_big_number.length()-1)
really_big_number = "0";
}
if (really_big_number == "-0")
really_big_number = "0";
if (!isValid)
really_big_number = "";
if (really_big_number[0] == '-')
isNegative = true;
if (isValid && isNegative)
for (int i = 0; i < really_big_number.length(); i++) //this block fills the digit vector with the string that was inputted
digit_vector.insert(i, really_big_number[i+1]);
else if (isValid)
for (int i = 0; i < really_big_number.length(); i++)
digit_vector.insert(i, really_big_number[i]);
}
std::string BigInteger::to_string()
{
std::string tmp="";
if(isNegative) //appends a negative if the number is supposed to be negative
tmp+='-';
for (int i = 0; i < digit_vector.size(); i++) //shoves the digit vector into a string to return
tmp += digit_vector[i];
return tmp;
}
BigInteger operator+(BigInteger &bi1, BigInteger &bi2)
{
bool negAdd = false;
std::string string1 = bi1.to_string();
std::string string2 = bi2.to_string();
std::string revAnswer = "";
std::string answer="";
int iCounter1, iCounter2;
int rmd=0;
if ((string1[0] == '-') == (string2[0] == '-'))
{
if (string1[0] == '-') //strips strings of negative sign if they are both negative to make addition easier
{
string1 = string1.substr(1);
string2 = string2.substr(1);
negAdd = true;
}
if (string1.length() < string2.length()) //swaps the two strings if the 2nd's length is larger than the first
{
std::string tmp = string1;
string1 = string2;
string2 = tmp;
}
if (negAdd)
{
iCounter1 = string1.length()-2; //get counter that keep track of where addition is happening in each string
iCounter2 = string2.length()-2;
}
else
{
iCounter1 = string1.length()-1;
iCounter2 = string2.length()-1;
}
while (iCounter1 >= 0 && iCounter2 >= 0) //add places that line up until one of the strings reach their front
{
int tmp = (string1[iCounter1] - '0') + (string2[iCounter2] - '0') + rmd;
rmd = 0;
if (tmp > 9)
{
rmd = 1;
tmp-=10;
}
revAnswer+=(tmp+'0');
iCounter1--;
iCounter2--;
}
while (iCounter1 >= 0) //add the rest of the longer string to the result
{
int tmp = (string1[iCounter1] - '0') + rmd;
rmd = 0;
if (tmp > 9)
{
rmd = 1;
tmp-=10;
}
revAnswer+=(tmp+'0');
iCounter1--;
}
if(rmd)
revAnswer+="1"; //if a remainder remains after the loop add one to the answer
if (negAdd)
revAnswer+='-'; //if answer should be negative, append the negative
for (int i = revAnswer.length()-1; i >=0; i--) //reverse the answer to get the real result
answer+=revAnswer[i];
BigInteger result(answer); //make a new BigInteger to return
return result;
}
else if ((string1[0] == '-') != (string2[0] == '-')) //if one of them is negative, send the probelm to operator-
{
BigInteger result;
if (string1[0] == '-')
{
BigInteger bi3(string1.substr(1,string1.length()-2));
result = bi2 - bi3;
}
else
{
BigInteger bi3(string2.substr(1,string2.length()-2));
result = bi1 - bi3;
}
return result;
}
}
BigInteger operator-(BigInteger &bi1, BigInteger &bi2)
{
std::string string1 = bi1.to_string();
std::string string2 = bi2.to_string();
std::string revAnswer="", answer="", temp = "";
bool negSub = false, lenSwap = false, str1IsLarger = true, //variables to hold many different quantities
str2IsLarger = false, removeZeros = true, isNegative=false, //regarding negatives, which is larger, etc.
digit2IsLonger = false;
int iCounter1, iCounter2, borrow=0, len1, len2, leadZeroCount = 0;
if ((string1[0] == '-') == (string2[0] == '-')) //check to see if both numbers are negative or both are positive
{
if (string1.length() == 3 && string2.length() == 3 && //check to see if strings are single digits and if they are negative
string1[0] == '-' && string2[0] == '-' &&
(string1[1]-'0') < (string2[1]-'0'))
{
digit2IsLonger = true;
}
if (string1[0] == '-') //take off the negative sign for easier substration
{
string1 = string1.substr(1);
string2 = string2.substr(1);
negSub = true;
}
if (string1.length() < string2.length()) //swap the two strings if the first is shorter
{
std::string tmp = string1;
string1 = string2;
string2 = tmp;
str2IsLarger = true;
if (!negSub)
lenSwap = true;
}
if (negSub)
{
iCounter1 = len1 = string1.length()-2; //counters to keep track of the position in the string
iCounter2 = len2 = string2.length()-2;
}
else
{
iCounter1 = len1 = string1.length()-1;
iCounter2 = len2 = string2.length()-1;
}
if (len1 == len2)
for (int i = 0; i < len1; i++)
{
if ((string1[i]-'0') > (string2[i]-'0')) //this block goes through same length string to see which is larger
{
i=len1;
}
else if ((string1[i]-'0') < (string2[i]-'0'))
{
std::string tmp = string1;
string1 = string2;
string2 = tmp;
i = len1;
lenSwap = true;
str2IsLarger = true;
}
}
if (len1 == 0 && len2 == 0 && (string1[0]-'0') < (string2[0]-'0'))
negSub = true;
while (iCounter1 >= 0 && iCounter2 >= 0) //subtract parts of string that line up with each other
{
int tmp = (string1[iCounter1]-'0') - (string2[iCounter2]-'0') - borrow;
borrow = 0;
if (tmp < 0 && ((len1 != 0) || (len2 != 0)))
{
borrow = 1;
tmp+=10;
}
else if (tmp < 0)
{
str1IsLarger = false;
tmp*=(-1);
}
revAnswer+=(tmp+'0');
iCounter1--;
iCounter2--;
}
while (iCounter1 >= 0) //append the rest of the longer string to the answer string
{
int tmp = (string1[iCounter1]-'0') - borrow;
borrow = 0;
if (tmp < 0)
{
borrow = 1;
tmp+=10;
}
revAnswer+=(tmp+'0');
iCounter1--;
}
//this block resolves any issues with negative signs that arise
if ((negSub && ((len1 == 0) && (len2 == 0)) && str1IsLarger) || lenSwap ||
(negSub && ((len1 != 0) && (len2 != 0))) || ((len1 == 0) && (len2 == 0) && !str1IsLarger))
{
revAnswer+='-';
isNegative = true;
}
if (negSub && str2IsLarger && isNegative) //remove negative sign if it was added based on conditions shown
{
revAnswer = revAnswer.substr(0,revAnswer.length()-1);
isNegative = false;
}
for (int i = revAnswer.length()-1; i>=0; i--) //reverse the answer to the right order
answer+=revAnswer[i];
if (len1 == len2)
for (int i = 0; i <= len1; i++) //check if zeros need to be removed
{
if ((string1[i]-'0') == (string2[i]-'0'))
{
removeZeros = false;
}
else
{
removeZeros = true;
i = len1;
}
}
if (!removeZeros) //if this is true answer was all zeros, so set answer to one zero
answer = '0';
if ((len1 != 0 || len2 != 0) && removeZeros) //remove zeros depending on whether or not the number is negative
{
if (!isNegative)
{
while (answer[leadZeroCount] == '0')
leadZeroCount++;
for (int i = leadZeroCount; i < answer.length(); i++)
temp+=answer[i];
answer = temp;
}
else
{
while (answer[leadZeroCount+1] == '0')
leadZeroCount++;
temp+='-';
for (int i = leadZeroCount+1; i < answer.length(); i++)
temp+=answer[i];
answer = temp;
}
}
if (negSub && !str2IsLarger && answer[0] != '-')
{
temp="-";
for (int i = 0; i < answer.length(); i++)
temp+=answer[i];
answer = temp;
}
if (answer == "-0")
answer = '0';
if (negSub && len1 == 0 && len2 == 0 && answer[0] == '-' && digit2IsLonger)
answer = answer.substr(1,answer.length()-1);
BigInteger result(answer);
return result;
}
else //string2 is negative and string1 is positive and this is sent to operator+ which handles it
{
std::string tmp="";
if (string1[0] == '-')
{
BigInteger bi4, bi3(string1.substr(1,string1.length()-2));
bi4 = bi2 + bi3;
tmp = '-' + bi4.to_string();
BigInteger result(tmp);
return result;
}
else
{
BigInteger bi3(string2.substr(1,string2.length()-2)), result;
result = bi1 + bi3;
return result;
}
}
}