高精度加、减、乘、除模板

描述

高精度的加、减、乘、除模板。

代码

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
//  Created by ZJYelizaveta on Wednesday, October 25, 2017 PM03:14:56 CST
// Copyright (c) 2017年 ZJYelizaveta. All rights reserved.

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
template<typename T> T readIn() {
T x(0), f(1);
char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
const int MAX_N = 1000 + 3;
const int INF = 0x3f3f3f3f;

struct BigInt {
vector<int> v;
static const int BASE = 10;

BigInt(ll x) {
do {
v.push_back(x % 10);
} while (x /= 10);
}

BigInt(const string &str) {
v.reserve(str.length());

for (int i = str.length() - 1; i >= 0; i--) {
v.push_back(str[i] - '0');
}
}

BigInt() {}

void removePreZero() {
while(v.size() >= 1 && v.back() == 0) v.pop_back();
}

bool operator<(const BigInt &a) const {
if (v.size() != a.v.size()) {
return v.size() < a.v.size();
}

for (int i = v.size() - 1; i >= 0; i--) {
if (v[i] != a.v[i]) {
return v[i] < a.v[i];
}
}
return false;
}

bool operator>(const BigInt &a) const { return a < *this; }

bool operator<=(const BigInt &a) const { return !(a < *this); }

bool operator>=(const BigInt &a) const { return !(*this < a); }

bool operator!=(const BigInt &a) const { return a < *this || a > *this; }

bool operator==(const BigInt &a) const { return !(a < *this) && !(a > *this); }

BigInt operator+(const BigInt &a) const {
BigInt res;
int sum = 0;

for (int i = 0; i < max(a.v.size(), v.size()); i++) {
if (i < a.v.size()) sum += a.v[i];
if (i < v.size()) sum += v[i];

res.v.push_back(sum % BASE);
sum /= BASE;
}
if (sum) res.v.push_back(sum);
res.removePreZero();

return res;
}

BigInt operator-(const BigInt &a) const {
BigInt res;
int dif = 0;

for (int i = 0; i < max(a.v.size(), v.size()); i++) {
if (i < v.size()) dif += v[i];
if (i < a.v.size()) dif -= a.v[i];

if (dif >= 0) {
res.v.push_back(dif);
dif = 0;
}
else {
res.v.push_back((dif + BASE) % BASE);
dif = -1;
}
}

res.removePreZero();
return res;
}

BigInt operator*(const BigInt &a) const {
BigInt res;

res.v.resize(v.size() + a.v.size(), 0);
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < a.v.size(); j++) {
res.v[i + j] += v[i] * a.v[j];
res.v[i + j + 1] += res.v[i + j] / BASE;
res.v[i + j] %= BASE;
}
}

res.removePreZero();
return res;
}

BigInt operator/(const BigInt &a) const {
BigInt res, ret(0);

res.v.resize(v.size(), 0);
ret = 0;
for (int i = v.size() - 1; i >= 0; i--) {
ret = ret * 10 + v[i];

while (ret >= a) {
ret = ret - a;
res.v[i]++;
}
}

res.removePreZero();
return res;
}
};

ostream& operator<<(ostream &out, const BigInt &x) {
for (int i = x.v.size() - 1; i >= 0; i--) {
out << x.v[i];
}
return out;
}

istream& operator>>(istream &in, BigInt &x) {
string str;
in >> str;

x = BigInt(str);
return in;
}

int main()
{
BigInt a, b;
cin >> a >> b;

// 加
BigInt c = a + b;
cout << c << endl;

// 减
BigInt c; bool flag = false;
if (a < b) {
flag = true;
c = a;
a = b;
b = c;
}

// cout << a << ' ' << b << endl;
BigInt ans = a - b;

if (a == b) cout << 0 << endl;
else if (flag) cout << '-' << ans << endl;
else cout << ans << endl;

// 乘
BigInt ans = a * b;
cout << ans << endl;

// 除
BigInt ans = a / b;
cout << ans << endl;

return 0;
}
Share