forked from technophilis/BigIntegerCPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigIntegerSingleFile.cpp
196 lines (157 loc) · 5.42 KB
/
BigIntegerSingleFile.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
/*
* BigInteger Class, performs basic arithmetic operations of very large integers.
* Copyright (C) 2011 Mahmoud Mechehoul
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <algorithm>
using namespace std;
/* This is a minimized version of the BigInteger class meant to be
* used for single file development purposes (such as programming contests).
* For a complete documentation on what every method does please refer to
* the class header file 'BigInteger.h'.
*/
class BigInteger {
private:
string integer;
public:
BigInteger(unsigned int integer);
BigInteger(string integer);
void setInteger(unsigned int integer);
void setInteger(string integer);
unsigned int getIntValue() const;
string toString() const;
BigInteger addInteger(const BigInteger& integer_to_add) const;
BigInteger addInteger(const string& integer_to_add) const;
BigInteger multiplyInteger(const BigInteger& integer_to_multiply) const;
BigInteger multiplyInteger(const string& integer_to_multiply) const;
static size_t getTrimIndex(const string& integer);
bool operator==(const BigInteger& integer) const;
BigInteger operator+(const BigInteger& integer) const;
BigInteger operator*(const BigInteger& integer) const;
friend ostream& operator<<(ostream& in, BigInteger& integer);
};
int main() {
// INSERT YOUR CODE HERE
/* This is a sample code that demonstrates how to use the
* Big Integer arithmetic library.
*
* BigInteger a("35742549198872617291353508656626642567");
* BigInteger b("1298074214633706835075030044377087");
*
* BigInteger c = a + b;
* BigInteger d = a * b;
*
* cout << a << " + " << b << " = " << c << endl;
* cout << a << " * " << b << " = " << d << endl;
*/
return 0;
}
BigInteger::BigInteger(unsigned int integer) {
setInteger(integer);
}
BigInteger::BigInteger(string integer) {
for (int i = 0; i < (int)integer.size() && integer[i] >= '0' && integer[i] <= '9'; i++) {
this->integer += integer[i];
}
if (this->integer.size() == 0) {
this->integer = "0";
} else {
this->integer = integer.substr(getTrimIndex(integer));
}
}
void BigInteger::setInteger(unsigned int integer) {
if (integer == 0) this->integer = "0";
while (integer) {
this->integer = (char)((integer % 10) + '0') + this->integer;
integer /= 10;
}
}
void BigInteger::setInteger(string integer) {
this->integer = integer;
}
unsigned int BigInteger::getIntValue() const {
unsigned int ret = 0;
unsigned int biggest = 0xFFFFFFFF;
for (int i = 0; i < (int)integer.size(); i++) {
int unit = integer[i] - '0';
if (ret > (biggest - unit) / 10.0) return 0;
ret = ret * 10 + unit;
}
return ret;
}
string BigInteger::toString() const {
return integer;
}
BigInteger BigInteger::addInteger(const BigInteger& integer_to_add) const {
int a_n = max((int)(integer_to_add.toString().size() - toString().size()), 0);
int b_n = max((int)(toString().size() - integer_to_add.toString().size()), 0);
string a = string(a_n, '0') + toString();
string b = string(b_n, '0') + integer_to_add.toString();
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
string result; int carry = 0;
for (int i = 0; i < (int)a.size(); i++) {
int sum = (a[i] - '0') + (b[i] - '0') + carry;
result += ((char)(sum % 10 + '0'));
carry = sum / 10;
}
if (carry != 0) result += ((char)(carry + '0'));
reverse(result.begin(), result.end());
return BigInteger(result.substr(getTrimIndex(result)));
}
BigInteger BigInteger::addInteger(const string& integer_to_add) const {
return addInteger(BigInteger(integer_to_add));
}
BigInteger BigInteger::multiplyInteger(const BigInteger& integer_to_multiply) const {
string a = integer_to_multiply.toString();
string b = toString();
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
BigInteger ret("0");
for (int i = 0; i < (int)a.size(); i++) {
int carry = 0; string tmp = string(i, '0');
for (int j = 0; j < (int)b.size(); j++) {
int mul = (a[i] - '0') * (b[j] - '0') + carry;
tmp += ((char)(mul % 10 + '0'));
carry = mul / 10;
}
if (carry != 0) tmp += (carry + '0');
reverse(tmp.begin(), tmp.end());
ret = ret.addInteger(tmp.substr(getTrimIndex(tmp)));
}
return ret;
}
BigInteger BigInteger::multiplyInteger(const string& integer_to_multiply) const {
return multiplyInteger(BigInteger(integer_to_multiply));
}
size_t BigInteger::getTrimIndex(const string& integer) {
size_t index = 0;
while (integer[index] == '0' && index < integer.size() - 1) index++;
return index;
}
bool BigInteger::operator==(const BigInteger& integer) const {
return this->integer == integer.toString();
}
BigInteger BigInteger::operator+(const BigInteger& integer) const {
return addInteger(integer);
}
BigInteger BigInteger::operator*(const BigInteger& integer) const {
return multiplyInteger(integer);
}
ostream& operator<<(ostream& in, BigInteger& integer) {
in << integer.toString();
return in;
}