高精度真的是我见过的比较恶心的模拟之一了。
class HugeInt { friend ostream& operator<<(ostream&, const HugeInt&); public: static const int digits = 30; HugeInt(long = 0); HugeInt(const char*); HugeInt operator+(const HugeInt&) const; HugeInt operator+(int) const; HugeInt operator+(const char*) const; HugeInt operator-(const HugeInt&) const; HugeInt operator*(const HugeInt&) const; HugeInt operator/(const HugeInt&) const; bool operator<(const HugeInt&) const; bool operator<=(const HugeInt&) const; bool operator>(const HugeInt&) const; bool operator>=(const HugeInt&) const; bool operator==(const HugeInt&) const; bool operator!=(const HugeInt&) const; int getLength() const; private: vector<int> integer=vector<int>(digits,0); }; void add(const vector<int> a, const vector<int> b,vector<int>& c) { for (int i = 0; i < HugeInt::digits; ++i) { c[i] += a[i] + b[i]; if (c[i] / 10) { c[i + 1] += 1; c[i] -= 10; } } } void sub(const vector<int> a, const vector<int> b, vector<int>& c) { for (int i = 0; i < HugeInt::digits; ++i) { c[i] += a[i] - b[i]; if (c[i] < 0) { c[i + 1] -= 1; c[i] += 10; } } } void mul(const vector<int> a, const vector<int> b, vector<int>& c) { for (int i = 0; i < HugeInt::digits; ++i) { for (int j = 0; j <= i; ++j) c[i] += a[j] * b[i - j]; if (c[i] / 10) { c[i + 1] += c[i] / 10; c[i] %= 10; } } } inline bool greater_eq(const vector<int> a,const vector<int> b, int last_dg, int len) { if (a[last_dg + len] != 0) return true; for (int i = len - 1; i >= 0; --i) { if (a[last_dg + i] > b[i]) return true; if (a[last_dg + i] < b[i]) return false; } return true; } void div(const vector<int> a,const vector<int> b,vector<int>& c) { vector<int> d(HugeInt::digits, 0); int a_len, b_len; for (a_len = HugeInt::digits-1; a_len > 0; --a_len) if (a[a_len - 1] != 0) break; for (b_len = HugeInt::digits - 1; b_len > 0; --b_len) if (b[b_len - 1] != 0) break; for (int i = 0; i < a_len; ++i) d[i] = a[i]; for (int i = a_len - b_len; i >= 0; --i) { while (greater_eq(d, b, i, b_len)) { for (int j = 0; j < b_len; ++j) { d[i + j] -= b[j]; if (d[i + j] < 0) { d[i + j + 1] -= 1; d[i + j] += 10; } } c[i] += 1; } } } ostream& operator<<(ostream& out, const HugeInt& num) { bool flag = 0; for (int i = HugeInt::digits - 1; i >= 0; i--) { if (num.integer[i])flag = 1; if (flag) { out << num.integer[i]; } } if (!flag)out << 0; return out; } HugeInt::HugeInt(long a) { int cnt = 0; while (a) { integer[cnt++] = (short)(a % 10); a /= 10; } } HugeInt::HugeInt(const char* s) { int len = strlen(s); for (int i = 0; i < len; ++i) { integer[len - i - 1] = (short)(s[i] - '0'); } } bool HugeInt::operator<(const HugeInt& num) const { int len_1 = 0, len_2 = 0; for (int i = HugeInt::digits - 1; i >= 0; i--) { if (this->integer[i]) { len_1 = i + 1; break; } } for (int i = HugeInt::digits - 1; i >= 0; i--) { if (this->integer[i]) { len_2 = i + 1; break; } } if (len_1 < len_2)return 1; else if (len_1 > len_2)return 0; else { for (int i = len_1 - 1; i >= 0; i--) { if (this->integer[i] < num.integer[i])return 1; else if (this->integer[i] < num.integer[i])return 0; } return 0; } } bool HugeInt::operator<=(const HugeInt& num) const { int len_1 = 0, len_2 = 0; for (int i = HugeInt::digits - 1; i >= 0; i--) { if (this->integer[i]) { len_1 = i + 1; break; } } for (int i = HugeInt::digits - 1; i >= 0; i--) { if (this->integer[i]) { len_2 = i + 1; break; } } if (len_1 < len_2)return 1; else if (len_1 > len_2)return 0; else { for (int i = len_1 - 1; i >= 0; i--) { if (this->integer[i] < num.integer[i])return 1; else if (this->integer[i] < num.integer[i])return 0; } return 1; } } bool HugeInt::operator>(const HugeInt& num) const { return !(*this <= num); } bool HugeInt::operator>=(const HugeInt& num) const { return !(*this < num); } bool HugeInt::operator==(const HugeInt& num) const { for (int i = 0; i < HugeInt::digits; i++) { if (this->integer[i] != num.integer[i])return 0; } } bool HugeInt::operator!=(const HugeInt& num) const { return !(*this == num); } HugeInt HugeInt::operator-(const HugeInt& num) const { HugeInt tmp; sub(this->integer, num.integer, tmp.integer); return tmp; } HugeInt HugeInt::operator+(const HugeInt& num) const{ HugeInt tmp; add(this->integer, num.integer, tmp.integer); return tmp; } HugeInt HugeInt::operator+(int num)const { HugeInt tmp; HugeInt n((long)num); add(this->integer, n.integer, tmp.integer); return tmp; } HugeInt HugeInt::operator+(const char* num)const { HugeInt tmp; HugeInt n(num); add(this->integer, n.integer, tmp.integer); return tmp; } HugeInt HugeInt::operator*(const HugeInt& num) const { HugeInt tmp; mul(this->integer, num.integer, tmp.integer); return tmp; } HugeInt HugeInt::operator/(const HugeInt & num) const { HugeInt tmp; div(this->integer, num.integer, tmp.integer); return tmp; } int HugeInt::getLength() const { for (int i = this->digits - 1; i >= 0; i--) { if (this->integer[i])return i + 1; } }