-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass_BigInt.cpp
243 lines (204 loc) · 5.18 KB
/
Class_BigInt.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
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
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int compareAbs(string a, string b){
if (a.length() > b.length())
return 1;
if (a.length() < b.length())
return -1;
return a.compare(b);
}
string add(string a, string b){
string c ;
long long n1 = a.length(), n2 = b.length() , nho = 0 , Tong;
if(n1 > n2) b.insert(0,n1-n2,'0');
if(n1 < n2) a.insert(0,n2-n1,'0');
c=a;
for(int i = a.length()-1; i>= 0; i--){
Tong = (a[i] - 48) + (b[i] - 48) + nho;
nho = Tong/10;
Tong = Tong%10;
c[i] = char(Tong+48);
}
if(nho > 0) c = char(nho + 48) + c;
return c;
}
// alwways a-b . Xet a luon luon > b
string subtract(string a, string b){
string c = "";
if (compareAbs(a, b) < 0) swap(a, b);
long long n1 = a.length(), n2 = b.length(), Muon = 0 ,Hieu;
if(n1 > n2) b.insert(0,n1-n2,'0');
for(int i = a.length()-1 ; i>= 0 ; i--){
Hieu = (a[i] - 48) -(b[i] - 48) - Muon;
if(Hieu < 0){
Hieu += 10;
Muon = 1;
}else{
Muon = 0;
}
c = char(Hieu +48) + c;
}
while(c.length() > 1 && c[0] == '0') c.erase(0,1);
return c;
}
class BigInt{
string value;
int sign;
public:
// Hàm khởi tạo số nguyên lớn
BigInt();
BigInt(const char *);
BigInt(int);
BigInt(const BigInt &);
// Toán tử in ra màn hình
friend ostream &operator<<(ostream &, const BigInt &);
// Toán tử gán
BigInt &operator=(int);
BigInt &operator=(const char *);
BigInt &operator=(const BigInt &);
// Toán tử cộng
friend BigInt operator+(const BigInt &, const BigInt &);
friend BigInt operator+(const BigInt &, int);
friend BigInt operator+(int, const BigInt &);
// Toán tử trừ
friend BigInt operator-(const BigInt &, const BigInt &);
friend BigInt operator-(const BigInt &, int);
friend BigInt operator-(int, const BigInt &);
// Toán tử cộng rồi gán
BigInt &operator+=(int);
BigInt &operator+=(const BigInt &);
// Toán tử trừ rồi gán
BigInt &operator-=(int);
BigInt &operator-=(const BigInt &);
};
BigInt::BigInt(){
value = "0";
sign = 1;
}
BigInt::BigInt(const char *x){
int index = 1;
if (x[0] == '-'){
index = 1;
sign = -1;
}else{
index = 0;
sign = 1;
}
while (*(x + index) != '\0'){
value += *(x + index);
index++;
}
}
BigInt::BigInt(int x){
if (x >= 0) sign = 1;
else sign = -1;
value = to_string(abs(x));
}
BigInt::BigInt(const BigInt &other){
value = other.value;
sign = other.sign;
}
ostream &operator<<(ostream &os, const BigInt &other){
if (other.sign == -1) os << '-';
os << other.value;
return os;
}
BigInt &BigInt ::operator=(int x){
if (x >= 0) sign = 1;
else sign = -1;
value = to_string(abs(x));
return *this;
}
BigInt &BigInt ::operator=(const char *x){
value.clear();
int index = 1;
if (x[0] == '-'){
index = 1;
sign = -1;
}else{
index = 0;
sign = 1;
}
while (*(x + index) != '\0'){
value += *(x + index);
index++;
}
return *this;
}
BigInt &BigInt ::operator=(const BigInt &other){
value = other.value;
sign = other.sign;
return *this;
}
BigInt operator+(const BigInt &a, const BigInt &b){
BigInt ans;
if (a.sign == b.sign){
ans.sign = a.sign;
ans.value = add(a.value, b.value);
}else{
if (compareAbs(a.value, b.value) >= 0){
ans.value = subtract(a.value, b.value);
ans.sign = a.sign;
}else{
ans.value = subtract(b.value, a.value);
ans.sign = b.sign;
}
}
return ans;
}
BigInt operator+(const BigInt &other, int num){
return other + BigInt(num);
}
BigInt operator+(int num, const BigInt &other){
return other + BigInt(num);
}
BigInt operator-(const BigInt &a, const BigInt &b){
BigInt res;
if (a.sign == b.sign){
if (compareAbs(a.value, b.value) >= 0){
if (a.sign == -1) res.sign = -1;
else res.sign = 1;
res.value = subtract(a.value, b.value);
}else{
if (a.sign == 1) res.sign = -1;
else res.sign = -1;
res.value = subtract(a.value, b.value);
}
}else{
res.sign = a.sign;
res.value = add(a.value, b.value);
}
return res;
}
BigInt operator-(const BigInt &other, int b){
return other - BigInt(b);
}
BigInt operator-(int a, const BigInt &other){
return BigInt(a) - other;
}
BigInt &BigInt::operator+=(int num){
*this = *this + num;
return *this;
}
BigInt &BigInt::operator+=(const BigInt &other){
*this = *this + other;
return *this;
}
BigInt &BigInt::operator-=(int num){
*this = *this - num;
return *this;
}
BigInt &BigInt::operator-=(const BigInt &other){
*this = *this - other;
return *this;
}
int main(){
BigInt a = BigInt("7412391231723192399991239");
BigInt b = BigInt(21348123);
cout << a - b << endl;
cout << 43 - a << endl;
cout << b - 71 << endl;
return 0;
}