#include <bits/stdc++.h>
using namespace std;
struct big_number {
bool is_postive;
string s;
big_number () {}
big_number (string ss) { if (ss[0] != '-') {this->s = ss; this->is_postive = true;} else {this->s = ss.substr(1, ss.size()); this->is_postive = false;}}
big_number (string ss, bool flag) {this->s = ss; this->is_postive = flag; }
void print() {if (is_postive == false) cout << "-"; cout << s;}
void println() {if (is_postive == false) cout << "-"; cout << s << "\n";}
int size() { return int(s.size()); }
bool operator < (const big_number &b) const {
if (int(s.size()) != int(b.s.size())) return int(s.size()) < int(b.s.size());
return s < b.s;
}
bool operator > (const big_number &b) const {
if (int(s.size()) != int(b.s.size())) return int(s.size()) > int(b.s.size());
return s > b.s;
}
bool operator == (const big_number &b) const {
return s == b.s;
}
bool operator != (const big_number &b) const {
return s != b.s;
}
friend inline big_number operator + (const big_number &p, const big_number &q) {
if (p.is_postive != q.is_postive) {
if (p.is_postive == false) {big_number np(p.s, true), nq(q.s, true); return nq - np;}
else {big_number np(p.s, true), nq(q.s, true); return np - nq;}
}
string a = p.s, b = q.s;
reverse(a.begin(), a.end()); reverse(b.begin(), b.end());
if (int(a.size()) < int(b.size())) swap(a, b); // len(a) > len(b)
int t = 0; big_number ans;
for (int i = 0; i < int(a.size()); i ++ ) {
if (i < int(a.size())) t += (a[i] - '0');
if (i < int(b.size())) t += (b[i] - '0');
ans.s += char('0' + (t % 10)); t /= 10;
}
if (t) ans.s += char('0' + (t % 10));
reverse(ans.s.begin(), ans.s.end());
if (p.is_postive == false and q.is_postive == false) ans.is_postive = false;
else ans.is_postive = true;
return ans;
}
friend inline big_number operator - (const big_number &p, const big_number &q) {
if (p.is_postive != q.is_postive) {
if (p.is_postive == false) {big_number np(p.s, false), nq(q.s, false); return nq + np;}
else {big_number np(p.s, true), nq(q.s, true); return np + nq;}
} else {
if (p.is_postive == false) {big_number np(p.s, true), nq(q.s, true); return nq - np;}
}
string a = p.s, b = q.s;
if (a == b) return big_number("0");
reverse(a.begin(), a.end()); reverse(b.begin(), b.end());
// bool postive = true;
int t = 0; big_number ans;
if (p < q) {swap(a, b); ans.is_postive = false; } // len(a) > len(b)
for (int i = 0; i < int(a.size()); i ++ ) {
if (i < int(a.size())) t += (a[i] - '0');
if (i < int(b.size())) t -= (b[i] - '0');
ans.s += char('0' + ((t + 10) % 10));
t = t < 0 ? -1 : 0;
}
assert(t == 0);
while (not ans.s.empty() and ans.s.back() == '0') ans.s.pop_back();
reverse(ans.s.begin(), ans.s.end());
return ans;
}
string MUL (string a, int b, int cnt) const {
string B = a, ans; int n = B.size();
int t = 0; reverse(B.begin(), B.end());
if (cnt) ans += string(cnt, '0');
for (int i = 0; i < n; i ++ ) {
t += b * int(B[i] - '0');
ans += char('0' + (t % 10));
t /= 10;
}
while (t) {ans += char('0' + (t % 10)); t /= 10; }
reverse(ans.begin(), ans.end());
return ans;
}
friend inline big_number operator * (const big_number &p, const big_number &q) {
string a = p.s, b = q.s;
if (int(a.size()) < int(b.size())) swap(a, b); // len(a) > len(b)
big_number ans("0"), mul(a);
reverse(b.begin(), b.end());
for (int i = 0; i < int(b.size()); i ++ ) {
string now = p.MUL(a, int(b[i] - '0'), i);
ans = ans + big_number(now);
}
if (p.is_postive != q.is_postive) ans.is_postive = false;
else ans.is_postive = true;
return ans;
}
};
int main() {
// big_number a("1231"), b("234");
string x, y; cin >> x >> y;
big_number a(x), b(y), ans;
ans = a + b; ans.println();
ans = a - b; ans.println();
ans = a * b; ans.println();
ans = b - a; ans.println();
ans = (b - a) * (a - b); ans.println();
return 0;
}
标签:Bignumber,int,big,number,整数,postive,ans,size
From: https://www.cnblogs.com/c972937/p/17437458.html