-
Notifications
You must be signed in to change notification settings - Fork 0
/
big_integer_library.cpp
178 lines (157 loc) · 4.99 KB
/
big_integer_library.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
#include <bits/stdc++.h>
using namespace std;
struct Bigint {
string a; // to store the digits
int sign; // sign = -1 for negative numbers, sign = 1 otherwise
Bigint() {} // default constructor
Bigint( string b ) { (*this) = b; } // constructor for string
Bigint( long long num ) {
if(num<0) sign=-1;
else sign=1;
if(num==0) a.push_back('0');
while(num)
{
a.push_back( num%10 + '0');
num/=10;
}
} // constructor for string
int size() { // returns number of digits
return a.size();
}
Bigint inverseSign() { // changes the sign
sign *= -1;
return (*this);
}
Bigint normalize( int newSign ) { // removes leading 0, fixes sign
for( int i = a.size() - 1; i > 0 && a[i] == '0'; i-- )
a.erase(a.begin() + i);
sign = ( a.size() == 1 && a[0] == '0' ) ? 1 : newSign;
return (*this);
}
void operator = ( string b ) { // assigns a string to Bigint
a = b[0] == '-' ? b.substr(1) : b;
reverse( a.begin(), a.end() );
this->normalize( b[0] == '-' ? -1 : 1 );
}
bool operator < ( const Bigint &b ) const { // less than operator
if( sign != b.sign ) return sign < b.sign;
if( a.size() != b.a.size() )
return sign == 1 ? a.size() < b.a.size() : a.size() > b.a.size();
for( int i = a.size() - 1; i >= 0; i-- ) if( a[i] != b.a[i] )
return sign == 1 ? a[i] < b.a[i] : a[i] > b.a[i];
return false;
}
bool operator == ( const Bigint &b ) const { // operator for equality
return a == b.a && sign == b.sign;
}
Bigint operator + ( Bigint b ) { // addition operator overloading
if( sign != b.sign ) return (*this) - b.inverseSign();
Bigint c;
for(int i = 0, carry = 0; i<a.size() || i<b.size() || carry; i++ ) {
carry+=(i<a.size() ? a[i]-48 : 0)+(i<b.a.size() ? b.a[i]-48 : 0);
c.a += (carry % 10 + 48);
carry /= 10;
}
return c.normalize(sign);
}
Bigint operator - ( Bigint b ) { // subtraction operator overloading
if( sign != b.sign ) return (*this) + b.inverseSign();
int s = sign; sign = b.sign = 1;
if( (*this) < b ) return ((b - (*this)).inverseSign()).normalize(-s);
Bigint c;
for( int i = 0, borrow = 0; i < a.size(); i++ ) {
borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48);
c.a += borrow >= 0 ? borrow + 48 : borrow + 58;
borrow = borrow >= 0 ? 0 : 1;
}
return c.normalize(s);
}
Bigint operator * ( Bigint b ) { // multiplication operator overloading
int MAXN=a.size()+b.size()+5;
int tmp[MAXN];
memset(tmp,0,sizeof(tmp));
for(int i=0; i<a.size(); i++)
for(int j=0, p=i; j<b.size(); j++){
tmp[p++] += (a[i]-'0')*(b.a[j]-'0');
}
Bigint c;
for(int i=0; i<MAXN-1; i++){
tmp[i+1] += tmp[i]/10;
tmp[i] %= 10;
c.a.push_back(tmp[i]+'0');
}
return c.normalize(sign*b.sign);
}
Bigint operator / ( Bigint b ) { // division operator overloading
if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
Bigint c("0"), d;
for( int j = 0; j < a.size(); j++ ) d.a += "0";
int dSign = sign * b.sign; b.sign = 1;
for( int i = a.size() - 1; i >= 0; i-- ) {
c.a.insert( c.a.begin(), '0');
c = c + a.substr( i, 1 );
while( !( c < b ) ) c = c - b, d.a[i]++;
}
return d.normalize(dSign);
}
Bigint operator % ( Bigint b ) { // modulo operator overloading
if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
Bigint c("0");
b.sign = 1;
for( int i = a.size() - 1; i >= 0; i-- ) {
c.a.insert( c.a.begin(), '0');
c = c + a.substr( i, 1 );
while( !( c < b ) ) c = c - b;
}
return c.normalize(sign);
}
void print() {
if( sign == -1 ) putchar('-');
// added by shahriar
reverse( a.begin(), a.end() );
cout << a << endl;
reverse( a.begin(), a.end() );
// added by shahriar
// for( int i = a.size() - 1; i >= 0; i-- ) putchar(a[i]);
// puts("");
}
};
int main(){
// Bigint first("550");
// Bigint second("550");
// Bigint thr("550");
//
// Bigint sum = first + second ;
// sum = sum + thr;
//
// first.print();
//
// sum.print();
//
// Bigint tmp("200");
//
// first = tmp;
//
// Bigint tmp3("200");
// second = tmp3;
//
// sum = first + second;
//
// sum.print();
Bigint arr[501];
Bigint f(1);
arr[1] = f;
Bigint s(1);
arr[2] = s;
arr[1] = f * s;
arr[1].print();
// for( int i = 3; i<500; i++ ){
//// Bigint tmp;
// arr[i] = arr[i-2] + arr[i-1];
// }
//
// for( int i = 1; i<500; i++ ){
// arr[i].print();
// }
return 0;
}