-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
601 lines (517 loc) · 13.5 KB
/
Copy pathmain.cpp
File metadata and controls
601 lines (517 loc) · 13.5 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// Either include the necessary headers or use a single header file
// #include <iostream>
// #include <vector>
// #include <string>
// #include <algorithm>
// #include <sstream>
#include <bits/stdc++.h>
using namespace std;
class BigInteger
{
private:
vector<int> digits;
bool isNegative;
// Function to remove leading zeros
void removeLeadingZeros()
{
while (digits.size() > 1 && digits.back() == 0)
{
digits.pop_back();
}
// If the number becomes zero, ensure it's positive
if (digits.size() == 1 && digits[0] == 0)
{
isNegative = false;
}
}
// Function to compare absolute values
int compareAbsolute(const BigInteger &other) const
{
if (digits.size() > other.digits.size())
{
return 1;
}
if (digits.size() < other.digits.size())
{
return -1;
}
for (int i = digits.size() - 1; i >= 0; i--)
{
if (digits[i] > other.digits[i])
{
return 1;
}
if (digits[i] < other.digits[i])
{
return -1;
}
}
return 0;
}
// Function for addition
BigInteger addAbsolute(const BigInteger &other) const
{
BigInteger result;
result.digits.clear();
result.isNegative = false;
int carry = 0;
size_t maxSize = max(digits.size(), other.digits.size());
for (size_t i = 0; i < maxSize || carry; i++)
{
int sum = carry;
if (i < digits.size())
{
sum += digits[i];
}
if (i < other.digits.size())
{
sum += other.digits[i];
}
result.digits.push_back(sum % 10);
carry = sum / 10;
}
return result;
}
// Function for subtraction (assumes this >= other)
BigInteger subtractAbsolute(const BigInteger &other) const
{
BigInteger result;
result.digits.clear();
result.isNegative = false;
int borrow = 0;
for (size_t i = 0; i < digits.size(); i++)
{
int diff = digits[i] - borrow;
if (i < other.digits.size())
{
diff -= other.digits[i];
}
if (diff < 0)
{
diff += 10;
borrow = 1;
}
else
{
borrow = 0;
}
result.digits.push_back(diff);
}
result.removeLeadingZeros();
return result;
}
public:
// Default constructor
BigInteger() : isNegative(false)
{
digits.push_back(0);
}
// Constructor from integer
BigInteger(long long num) : isNegative(num < 0)
{
if (num == 0)
{
digits.push_back(0);
isNegative = false;
return;
}
if (num < 0)
{
num = -num;
}
while (num > 0)
{
digits.push_back(num % 10);
num /= 10;
}
}
// Constructor from string
BigInteger(const string &str)
{
if (str.empty())
{
digits.push_back(0);
isNegative = false;
return;
}
size_t start = 0;
isNegative = false;
if (str[0] == '-')
{
isNegative = true;
start = 1;
}
else if (str[0] == '+')
{
start = 1;
}
if (start >= str.length())
{
digits.push_back(0);
isNegative = false;
return;
}
// Store digits in reverse order (least significant first)
for (int i = str.length() - 1; i >= (int)start; i--)
{
if (str[i] >= '0' && str[i] <= '9')
{
digits.push_back(str[i] - '0');
}
}
if (digits.empty())
{
digits.push_back(0);
isNegative = false;
}
else
{
removeLeadingZeros();
}
}
// Copy constructor
BigInteger(const BigInteger &other) : digits(other.digits), isNegative(other.isNegative) {}
// Assignment operator
BigInteger &operator=(const BigInteger &other)
{
if (this != &other)
{
digits = other.digits;
isNegative = other.isNegative;
}
return *this;
}
// Addition operator
BigInteger operator+(const BigInteger &other) const
{
if (isNegative == other.isNegative)
{
BigInteger result = addAbsolute(other);
result.isNegative = isNegative;
return result;
}
else
{
int cmp = compareAbsolute(other);
if (cmp == 0)
{
return BigInteger(0);
}
else if (cmp > 0)
{
BigInteger result = subtractAbsolute(other);
result.isNegative = isNegative;
return result;
}
else
{
BigInteger result = other.subtractAbsolute(*this);
result.isNegative = other.isNegative;
return result;
}
}
}
// Subtraction operator
BigInteger operator-(const BigInteger &other) const
{
if (isNegative != other.isNegative)
{
BigInteger result = addAbsolute(other);
result.isNegative = isNegative;
return result;
}
else
{
int cmp = compareAbsolute(other);
if (cmp == 0)
{
return BigInteger(0);
}
else if (cmp > 0)
{
BigInteger result = subtractAbsolute(other);
result.isNegative = isNegative;
return result;
}
else
{
BigInteger result = other.subtractAbsolute(*this);
result.isNegative = !isNegative;
return result;
}
}
}
// Multiplication operator
BigInteger operator*(const BigInteger &other) const
{
BigInteger result;
result.digits.assign(digits.size() + other.digits.size(), 0);
result.isNegative = (isNegative != other.isNegative);
for (size_t i = 0; i < digits.size(); i++)
{
int carry = 0;
for (size_t j = 0; j < other.digits.size() || carry; j++)
{
long long prod = result.digits[i + j] + carry;
if (j < other.digits.size())
{
prod += (long long)digits[i] * other.digits[j];
}
result.digits[i + j] = prod % 10;
carry = prod / 10;
}
}
result.removeLeadingZeros();
return result;
}
// Division operator (integer division)
BigInteger operator/(const BigInteger &other) const
{
if (other.isZero())
{
throw runtime_error("Division by zero");
}
if (compareAbsolute(other) < 0)
{
return BigInteger(0);
}
BigInteger dividend = *this;
dividend.isNegative = false;
BigInteger divisor = other;
divisor.isNegative = false;
BigInteger result;
result.digits.assign(digits.size(), 0);
BigInteger current;
current.digits.clear();
for (int i = digits.size() - 1; i >= 0; i--)
{
current.digits.insert(current.digits.begin(), digits[i]);
current.removeLeadingZeros();
int count = 0;
while (current.compareAbsolute(divisor) >= 0)
{
current = current.subtractAbsolute(divisor);
count++;
}
result.digits[i] = count;
}
result.isNegative = (isNegative != other.isNegative);
result.removeLeadingZeros();
return result;
}
// Modulo operator
BigInteger operator%(const BigInteger &other) const
{
return *this - (*this / other) * other;
}
// Comparison operators
bool operator==(const BigInteger &other) const
{
return isNegative == other.isNegative && digits == other.digits;
}
bool operator!=(const BigInteger &other) const
{
return !(*this == other);
}
bool operator<(const BigInteger &other) const
{
if (isNegative != other.isNegative)
{
return isNegative;
}
int cmp = compareAbsolute(other);
return isNegative ? cmp > 0 : cmp < 0;
}
bool operator>(const BigInteger &other) const
{
return other < *this;
}
bool operator<=(const BigInteger &other) const
{
return !(*this > other);
}
bool operator>=(const BigInteger &other) const
{
return !(*this < other);
}
// Unary operators
BigInteger operator-() const
{
BigInteger result = *this;
if (!isZero())
{
result.isNegative = !result.isNegative;
}
return result;
}
BigInteger operator+() const
{
return *this;
}
// Compound assignment operators
BigInteger &operator+=(const BigInteger &other)
{
*this = *this + other;
return *this;
}
BigInteger &operator-=(const BigInteger &other)
{
*this = *this - other;
return *this;
}
BigInteger &operator*=(const BigInteger &other)
{
*this = *this * other;
return *this;
}
BigInteger &operator/=(const BigInteger &other)
{
*this = *this / other;
return *this;
}
BigInteger &operator%=(const BigInteger &other)
{
*this = *this % other;
return *this;
}
// Utility functions
bool isZero() const
{
return digits.size() == 1 && digits[0] == 0;
}
bool isPositive() const
{
return !isNegative && !isZero();
}
bool isNegativeValue() const
{
return isNegative;
}
string toString() const
{
if (isZero())
{
return "0";
}
string result;
if (isNegative)
{
result += "-";
}
for (int i = digits.size() - 1; i >= 0; i--)
{
result += (char)('0' + digits[i]);
}
return result;
}
// Power function (for positive exponents)
BigInteger power(int exp) const
{
if (exp < 0)
{
throw runtime_error("Negative exponent not supported");
}
if (exp == 0)
{
return BigInteger(1);
}
BigInteger result(1);
BigInteger base = *this;
while (exp > 0)
{
if (exp % 2 == 1)
{
result *= base;
}
base *= base;
exp /= 2;
}
return result;
}
// GCD function
BigInteger gcd(const BigInteger &other) const
{
BigInteger a = *this;
BigInteger b = other;
a.isNegative = false;
b.isNegative = false;
while (!b.isZero())
{
BigInteger temp = b;
b = a % b;
a = temp;
}
return a;
}
// Stream output operator
friend ostream &operator<<(ostream &os, const BigInteger &num)
{
os << num.toString();
return os;
}
// Stream input operator
friend istream &operator>>(istream &is, BigInteger &num)
{
string str;
is >> str;
num = BigInteger(str);
return is;
}
};
// main function with some default examples
int main()
{
cout << "=== BigInteger Class Demo ===" << endl;
// Test basic operations
BigInteger a("123456789012345678901234567890");
BigInteger b("987654321098765432109876543210");
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << endl;
// Addition
cout << "Addition:" << endl;
cout << "a + b = " << a + b << endl;
cout << endl;
// Subtraction
cout << "Subtraction:" << endl;
cout << "b - a = " << b - a << endl;
cout << "a - b = " << a - b << endl;
cout << endl;
// Multiplication
cout << "Multiplication:" << endl;
// BigInteger c("123456789");
// BigInteger d("987654321");
BigInteger c("864197532086419753208641975320");
BigInteger d("864197532086419753208641975320");
cout << c << " * " << d << " = " << c * d << endl;
cout << endl;
// Division
cout << "Division:" << endl;
cout << b << " / " << a << " = " << b / a << endl;
cout << b << " % " << a << " = " << b % a << endl;
cout << endl;
// Power
cout << "Power:" << endl;
BigInteger e("12345");
cout << e << "^3 = " << e.power(3) << endl;
cout << endl;
// Comparison
cout << "Comparison:" << endl;
cout << "a == b: " << (a == b) << endl;
cout << "a < b: " << (a < b) << endl;
cout << "a > b: " << (a > b) << endl;
cout << endl;
// Negative numbers
cout << "Negative numbers:" << endl;
BigInteger f("-123456789");
BigInteger g("987654321");
cout << f << " + " << g << " = " << f + g << endl;
cout << f << " * " << g << " = " << f * g << endl;
cout << endl;
// GCD
cout << "GCD:" << endl;
BigInteger h("48");
BigInteger i("18");
cout << "gcd(" << h << ", " << i << ") = " << h.gcd(i) << endl;
return 0;
}