-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargeIntegar.cpp
178 lines (174 loc) · 5.99 KB
/
LargeIntegar.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 <algorithm>
#include <iostream>
#include <string.h>
#include <sstream>
#include <istream>
#include <stdio.h>
#include <string>
using namespace std;
#define InitSize 30
#define MaxOfFBit 9999
#define CalcBit(X) (X > 999 ? 4 : (X > 99 ? 3 : (X > 9 ? 2 : (X > 0 ? 1 : 0))))
class LargeIntegar {
int *Nums, *BegPos, *EndPos, size, bit; bool Sign;
public:
LargeIntegar(int sz = InitSize) {
Sign = false, BegPos = NULL, bit = 0;
Nums = new int[sz], size = sz, EndPos = Nums + sz;
std:: fill(Nums, Nums + sz, false);
}
template <typename Int>
LargeIntegar Convert_Int_to_LargeIntegar(Int integar) {
string Str = to_string(integar);
return Convert_String_to_LargeIntegar(Str);
}
void Redistribute_Memory(int sz) {
int* NewInt = new int[sz]; EndPos = NewInt + sz;
std:: fill(NewInt, NewInt + sz, false);
memcpy(Nums, NewInt + sz - size, size * sizeof(int));
delete Nums; Nums = NewInt, size = sz;
}
static LargeIntegar Convert_String_to_LargeIntegar(string& Str) {
int sz = max(int(Str.size() + 5), InitSize);
LargeIntegar Res = LargeIntegar(sz);
int *p = Res.EndPos - 1;
while (Str[0] == '0' and Str.size() > 1) Str.erase(0, 1);
for (int i = Str.size() - 1; i >= 0; i -= 4) {
Res.BegPos = p;
if (i < 3) {
*p = Str[i] - '0', Res.bit += 1;
if (i > 0) *p += 10 * (Str[i - 1] - '0'), Res.bit += 1;
if (i > 1) *p += 100 * (Str[i - 2] - '0'), Res.bit += 1;
break;
}
*p = Str[i] - '0' + 10 * (Str[i - 1] - '0') +
(Str[i - 2] - '0') * 100 + 1000 * (Str[i - 3] - '0');
p -= 1, Res.bit += 4;
}
return Res;
}
template <typename Int>
LargeIntegar operator = (const Int& o) {
return *this = Convert_Int_to_LargeIntegar(o);
}
friend istream& operator >> (istream &in, LargeIntegar &Int) {
string str; in >> str;
Int = Convert_String_to_LargeIntegar(str);
return in;
}
friend ostream& operator << (ostream &out, LargeIntegar &lInt) {
if (not lInt.BegPos) {
out << 0; return out;
}
int* num = lInt.BegPos;
if (lInt.Sign) cout << '-';
out << *num; num += 1;
int tmp;
while (num != lInt.EndPos) {
if (*num == 0) out << "000";
else if ((tmp = CalcBit(*num)) < 4) {
tmp = 4 - tmp;
while (tmp --) putchar('0');
}
out << *num;
num += 1;
}
return out;
}
int operator ! () const {
return not(*BegPos);
}
bool operator < (const LargeIntegar& o) const {
if (not Sign and o.Sign) return false;
if (Sign and not o.Sign) return true;
if (Sign and o.Sign) return not((-(*this)) < (-o));
if (bit != o.bit) return bit < o.bit;
int *p = this->BegPos, *q = o.BegPos;
while (p != this->EndPos and q != o.EndPos) {
if (*p != *q) return *p < *q;
p = p + 1, q = q + 1;
}
return false;
}
bool operator >= (const LargeIntegar& o) const {
return not (*this < o);
}
bool operator == (const LargeIntegar& o) const {
if (not Sign and o.Sign) return false;
if (Sign and not o.Sign) return false;
if (Sign and o.Sign) return false;
if (bit != o.bit) return false;
int *p = this->BegPos, *q = o.BegPos;
while (p != this->EndPos and q != o.EndPos) {
if (*p != *q) return false;
p = p + 1, q = q + 1;
}
return true;
}
bool operator != (const LargeIntegar& o) const {
return not (*this == o);
}
bool operator > (const LargeIntegar& o) const {
return (*this >= o and not (*this == o));
}
bool operator <= (const LargeIntegar& o) const {
return not (*this > o);
}
LargeIntegar operator + (const LargeIntegar& o) const {
bool flag = false;
if (o.Sign and Sign) flag = true;
else if (o.Sign) return *this - (-o);
else if (Sign) return o - (-(*this));
if (*this < o) return o + *this;
LargeIntegar Res = LargeIntegar(max(max(bit, o.bit) + 5, InitSize));
int *p = EndPos - 1, *q = o.EndPos - 1, *r = Res.EndPos - 1;
while (p != BegPos - 1) {
*r += *p + (q == o.BegPos - 1 ? 0 : *q);
if (*r > MaxOfFBit) {
*(r - 1) += 1, *r %= MaxOfFBit + 1,
Res.BegPos = r - 1, Res.bit += 4;
}
else Res.BegPos = r, Res.bit += (p == BegPos ? CalcBit(*r) : 4);
p -= 1, q -= (q == o.BegPos - 1 ? 0 : 1), r -= 1;
}
if (flag) Res.Sign = true;
return Res;
}
LargeIntegar operator - () const {
LargeIntegar Res = *this;
Res.Sign ^= 1;
return Res;
}
LargeIntegar operator - (const LargeIntegar& o) const {
if (not Sign and o.Sign) return *this + (-o);
if (Sign and not o.Sign) return -((-(*this) + o));
if (Sign and Sign) return ((-o) - (-(*this)));
if (*this < o) return -(o - *this);
LargeIntegar Res = LargeIntegar(max(max(bit, o.bit) + 5, InitSize));
int *p = EndPos - 1, *q = o.EndPos - 1, *r = Res.EndPos - 1;
while (p != BegPos - 1) {
*r += *p - (q == o.BegPos - 1 ? 0 : *q);
if (*r < 0) *(r - 1) -= 1, *r += MaxOfFBit + 1, Res.BegPos = r - 1;
else Res.BegPos = r;
Res.bit += (p == BegPos ? CalcBit(*r) : 4);
p -= 1, q -= (q == o.BegPos - 1 ? 0 : 1), r -= 1;
}
while (*Res.BegPos == 0 and Res.BegPos < Res.EndPos - 1)
Res.BegPos += 1;
return Res;
}
};
int Test() {
LargeIntegar o, b;
cin >> o >> b;
cout << o << endl;
cout << b << endl;
printf("If A > B ? %d\n", o > b);
// o = o + b;
// long long a = 123456789123456789ll;
cout << (o == b) << endl;
return 0;
}
int main () {
return Test();
}