forked from Dev-XYS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
High-Precision(Integer).cpp
191 lines (175 loc) · 2.57 KB
/
High-Precision(Integer).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
#include <cstdio>
#include <cstring>
using namespace std;
int _global_length_;
class lint
{
private:
int s_length, n_length, *num;
char *temp;
int max(int x, int y);
void clear();
int nextshort(int offset);
lint mul1(int x, int offset);
public:
lint(int len = _global_length_);
void read();
void write();
lint operator + (lint x);
lint operator - (lint x);
lint operator * (lint x);
bool operator < (lint x);
};
int lint::max(int x, int y)
{
return x > y ? x : y;
}
void lint::clear()
{
memset(num, 0, n_length * sizeof(num[0]));
}
int lint::nextshort(int offset)
{
int res = 0;
for (int i = max(offset - 3, 0); i <= offset; i++)
{
res = res * 10 + (temp[i] - '0');
}
return res;
}
lint lint::mul1(int x, int offset)
{
lint res(s_length);
int d = 0;
for (int i = 0; i + offset < n_length; i++)
{
d = num[i] * x + d;
res.num[i + offset] = d % 10000;
d /= 10000;
}
return res;
}
lint::lint(int len)
{
s_length = len;
n_length = len / 4;
temp = new char[s_length];
num = new int[n_length];
clear();
}
void lint::read()
{
scanf("%s", temp);
clear();
int l = strlen(temp), k = 0;
for (int i = l - 1; i >= 0; i -= 4)
{
num[k++] = nextshort(i);
}
}
void lint::write()
{
int i;
for (i = n_length - 1; i >= 0; i--)
{
if (num[i] != 0)
{
break;
}
}
if (i == -1)
{
printf("0");
return;
}
printf("%d", num[i]);
for (i--; i >= 0; i--)
{
printf("%04d", num[i]);
}
}
lint lint::operator + (lint x)
{
lint res(s_length);
int d = 0;
for (int i = 0; i < n_length; i++)
{
d += num[i] + x.num[i];
res.num[i] = d % 10000;
d /= 10000;
}
return res;
}
lint lint::operator - (lint x)
{
lint res(s_length);
int d = 0;
for (int i = 0; i < n_length; i++)
{
d += num[i] - x.num[i];
res.num[i] = (d + 10000) % 10000;
d = d < 0 ? -1 : 0;
}
return res;
}
lint lint::operator * (lint x)
{
lint res(s_length), t(s_length);
for (int i = 0; i < s_length; i++)
{
t = mul1(x.num[i], i);
res = res + t;
}
return res;
}
bool lint::operator < (lint x)
{
for (int i = n_length - 1; i >= 0; i--)
{
if (num[i] < x.num[i])
{
return true;
}
else if (num[i] > x.num[i])
{
return false;
}
}
return false;
}
int main()
{
lint a(100), b(100);
char op[10];
while (true)
{
a.read();
scanf("%s", op);
b.read();
switch (op[0])
{
case '+':
(a + b).write();
break;
case '-':
if (a < b)
{
printf("-");
(b - a).write();
}
else
{
(a - b).write();
}
break;
case '*':
(a * b).write();
break;
default:
printf("Invalid operator.");
break;
}
printf("\n");
}
return 0;
}