高精度封装类(重载运算符版)
2026-08-07 22:17:05
发布于:广东
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const ll MAXN = 1e6 + 5;
struct Big {
int a[MAXN];
bool neg;
Big() {
memset(a, 0, sizeof(a));
a[0] = 1;
neg = false;
}
Big(const Big &o) {
memcpy(a, o.a, sizeof(a));
neg = o.neg;
}
Big(const string &s) {
memset(a, 0, sizeof(a));
a[0] = 1;
neg = false;
ll st = 0;
if (s[0] == '-') {
neg = true;
st = 1;
} else if (s[0] == '+') {
st = 1;
}
a[0] = s.size() - st;
if (a[0] == 0) {
a[0] = 1;
a[1] = 0;
neg = false;
return;
}
for (ll i = 0; i < a[0]; i++) {
a[a[0] - i] = s[st + i] - '0';
}
while (a[0] > 1 && !a[a[0]]) {
a[0]--;
}
if (a[0] == 1 && a[1] == 0) {
neg = false;
}
}
Big(ll num) {
memset(a, 0, sizeof(a));
neg = false;
if (num < 0) {
neg = true;
num = -num;
}
if (num == 0) {
a[0] = 1;
a[1] = 0;
return;
}
a[0] = 0;
while (num) {
a[++a[0]] = num % 10;
num /= 10;
}
}
inline Big& operator = (const Big &o) {
if (this != &o) {
memcpy(a, o.a, sizeof(a));
neg = o.neg;
}
return *this;
}
static inline ll abscmp(const Big &x, const Big &y) {
if (x.a[0] != y.a[0]) {
return x.a[0] > y.a[0] ? 1 : -1;
}
for (ll i = x.a[0]; i >= 1; i--) {
if (x.a[i] != y.a[i]) {
return x.a[i] > y.a[i] ? 1 : -1;
}
}
return 0;
}
inline Big abs() const {
Big res = *this;
res.neg = false;
return res;
}
static inline Big abs_add(const Big &x, const Big &y) {
Big res;
res.a[0] = max(x.a[0], y.a[0]);
ll c = 0;
for (ll i = 1; i <= res.a[0]; i++) {
ll sum = c + x.a[i] + y.a[i];
c = sum / 10;
res.a[i] = sum % 10;
}
while (c) {
res.a[++res.a[0]] = c % 10;
c /= 10;
}
return res;
}
static inline Big abs_sub(const Big &x, const Big &y) {
Big res;
if (abscmp(x, y) < 0) {
res.a[0] = y.a[0];
ll b = 0;
for (ll i = 1; i <= res.a[0]; i++) {
ll d = y.a[i] - b - x.a[i];
if (d < 0) {
d += 10;
b = 1;
} else {
b = 0;
}
res.a[i] = d;
}
} else {
res.a[0] = x.a[0];
ll b = 0;
for (ll i = 1; i <= res.a[0]; i++) {
ll d = x.a[i] - b - y.a[i];
if (d < 0) {
d += 10;
b = 1;
} else {
b = 0;
}
res.a[i] = d;
}
}
while (res.a[0] > 1 && !res.a[res.a[0]]) {
res.a[0]--;
}
if (res.a[0] == 1 && res.a[1] == 0) {
res.neg = false;
}
return res;
}
inline Big operator + (const Big &o) const {
if (neg == o.neg) {
Big res = abs_add(*this, o);
res.neg = neg;
return res;
} else {
Big res = abs_sub(*this, o);
if (abscmp(*this, o) < 0) {
res.neg = o.neg;
} else {
res.neg = neg;
}
return res;
}
}
inline Big& operator += (const Big &o) {
*this = *this + o;
return *this;
}
inline Big operator - (const Big &o) const {
Big tmp = o;
tmp.neg = !tmp.neg;
return *this + tmp;
}
inline Big& operator -= (const Big &o) {
*this = *this - o;
return *this;
}
inline Big abs_mul(const Big &o) const {
Big res;
res.a[0] = a[0] + o.a[0] + 1;
for (ll i = 1; i <= a[0]; i++) {
for (ll j = 1; j <= o.a[0]; j++) {
res.a[i + j - 1] += a[i] * o.a[j];
}
}
for (ll i = 1; i < res.a[0]; i++) {
if (res.a[i] >= 10) {
res.a[i + 1] += res.a[i] / 10;
res.a[i] %= 10;
}
}
while (res.a[0] > 1 && !res.a[res.a[0]]) {
res.a[0]--;
}
return res;
}
inline Big operator * (const Big &o) const {
Big res = abs_mul(o);
res.neg = (neg != o.neg);
if (res.a[0] == 1 && res.a[1] == 0) {
res.neg = false;
}
return res;
}
inline Big& operator *= (const Big &o) {
*this = *this * o;
return *this;
}
inline Big operator * (const ll &k) const {
Big res;
if (k == 0) {
res.a[0] = 1;
res.a[1] = 0;
res.neg = false;
return res;
}
ll kk = k;
if (kk < 0) {
kk = -kk;
res.neg = !neg;
} else {
res.neg = neg;
}
res.a[0] = a[0];
ll g = 0;
for (ll i = 1; i <= a[0]; i++) {
ll sum = g + a[i] * kk;
res.a[i] = sum % 10;
g = sum / 10;
}
while (g) {
res.a[++res.a[0]] = g % 10;
g /= 10;
}
if (res.a[0] == 1 && res.a[1] == 0) {
res.neg = false;
}
return res;
}
inline Big& operator *= (const ll &k) {
*this = *this * k;
return *this;
}
inline Big operator / (const ll &k) const {
if (k == 0) {
throw "Division by zero";
}
Big res;
ll kk = k;
if (kk < 0) {
kk = -kk;
res.neg = !neg;
} else {
res.neg = neg;
}
res.a[0] = a[0];
ll r = 0;
for (ll i = a[0]; i >= 1; i--) {
ll cur = r * 10 + a[i];
res.a[i] = cur / kk;
r = cur % kk;
}
while (res.a[0] > 1 && !res.a[res.a[0]]) {
res.a[0]--;
}
if (res.a[0] == 1 && res.a[1] == 0) {
res.neg = false;
}
return res;
}
inline Big& operator /= (const ll &k) {
*this = *this / k;
return *this;
}
inline ll operator % (const ll &k) const {
if (k == 0) {
throw "Modulo by zero";
}
ll kk = k < 0 ? -k : k;
ll r = 0;
for (ll i = a[0]; i >= 1; i--) {
r = (r * 10 + a[i]) % kk;
}
return neg ? -r : r;
}
inline Big operator / (const Big &o) const {
if (o.a[0] == 1 && o.a[1] == 0) {
throw "Division by zero";
}
if (abscmp(*this, o) < 0) {
return Big();
}
Big res;
res.neg = (neg != o.neg);
Big rem;
for (ll i = a[0]; i >= 1; i--) {
for (ll j = rem.a[0]; j >= 1; j--) {
rem.a[j + 1] = rem.a[j];
}
rem.a[1] = a[i];
if (rem.a[rem.a[0] + 1]) {
rem.a[0]++;
}
ll l = 0, r = 9, mid, ans = 0;
while (l <= r) {
mid = (l + r) >> 1;
Big tmp = o.abs() * mid;
if (abscmp(tmp, rem.abs()) <= 0) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
res.a[i] = ans;
Big tmp = o.abs() * ans;
rem = rem.abs() - tmp;
}
while (res.a[0] > 1 && !res.a[res.a[0]]) {
res.a[0]--;
}
if (res.a[0] == 1 && res.a[1] == 0) {
res.neg = false;
}
return res;
}
inline Big& operator /= (const Big &o) {
*this = *this / o;
return *this;
}
inline Big operator % (const Big &o) const {
if (o.a[0] == 1 && o.a[1] == 0) {
throw "Modulo by zero";
}
if (abscmp(*this, o) < 0) {
return *this;
}
Big rem;
for (ll i = a[0]; i >= 1; i--) {
for (ll j = rem.a[0]; j >= 1; j--) {
rem.a[j + 1] = rem.a[j];
}
rem.a[1] = a[i];
if (rem.a[rem.a[0] + 1]) {
rem.a[0]++;
}
ll l = 0, r = 9, mid, ans = 0;
while (l <= r) {
mid = (l + r) >> 1;
Big tmp = o.abs() * mid;
if (abscmp(tmp, rem.abs()) <= 0) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
Big tmp = o.abs() * ans;
rem = rem.abs() - tmp;
}
rem.neg = neg;
if (rem.a[0] == 1 && rem.a[1] == 0) {
rem.neg = false;
}
return rem;
}
inline Big& operator %= (const Big &o) {
*this = *this % o;
return *this;
}
inline Big pow(ll n) const {
if (n == 0) {
return Big("1");
}
if (n < 0) {
throw "Negative exponent not supported";
}
Big base = this->abs();
Big res("1");
while (n) {
if (n & 1) {
res = res * base;
}
base = base * base;
n >>= 1;
}
if (neg && (n & 1)) {
res.neg = true;
}
return res;
}
inline Big sqrt(ll n = 2) const {
if (n < 1) {
throw "Root index must be positive";
}
if (neg) {
throw "Cannot take root of negative number";
}
if (n == 1) {
return *this;
}
Big l("1"), r = *this, mid, pow_mid;
while (abscmp(l, r) <= 0) {
mid = (l + r) / 2;
pow_mid = mid.pow(n);
if (abscmp(pow_mid, *this) == 0) {
return mid;
} else if (abscmp(pow_mid, *this) < 0) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return r;
}
inline Big shl(ll k) const {
if (k < 0) return shr(-k);
if (a[0] == 1 && a[1] == 0) return *this;
Big res = *this;
for (ll i = res.a[0]; i >= 1; i--) {
res.a[i + k] = res.a[i];
}
for (ll i = 1; i <= k; i++) {
res.a[i] = 0;
}
res.a[0] += k;
return res;
}
inline Big shr(ll k) const {
if (k < 0) return shl(-k);
if (k >= a[0]) return Big("0");
Big res = *this;
for (ll i = 1; i <= res.a[0] - k; i++) {
res.a[i] = res.a[i + k];
}
res.a[0] -= k;
while (res.a[0] > 1 && !res.a[res.a[0]]) {
res.a[0]--;
}
return res;
}
inline bool operator == (const Big &o) const {
if (neg != o.neg) return false;
if (a[0] != o.a[0]) return false;
for (ll i = 1; i <= a[0]; i++) {
if (a[i] != o.a[i]) return false;
}
return true;
}
inline bool operator != (const Big &o) const {
return !(*this == o);
}
inline bool operator < (const Big &o) const {
if (neg != o.neg) return neg && !o.neg;
if (neg && o.neg) return abscmp(*this, o) > 0;
return abscmp(*this, o) < 0;
}
inline bool operator > (const Big &o) const {
return o < *this;
}
inline bool operator <= (const Big &o) const {
return !(*this > o);
}
inline bool operator >= (const Big &o) const {
return !(*this < o);
}
inline void print() const {
if (neg && !(a[0] == 1 && a[1] == 0)) {
cout << "-";
}
for (ll i = a[0]; i >= 1; i--) {
cout << a[i];
}
cout << endl;
}
inline string to_string() const {
string res;
if (neg && !(a[0] == 1 && a[1] == 0)) {
res += '-';
}
for (ll i = a[0]; i >= 1; i--) {
res += char('0' + a[i]);
}
return res;
}
};
inline istream& operator >> (istream &in, Big &x) {
string s;
in >> s;
x = Big(s);
return in;
}
inline ostream& operator << (ostream &out, const Big &x) {
if (x.neg && !(x.a[0] == 1 && x.a[1] == 0)) {
out << '-';
}
for (ll i = x.a[0]; i >= 1; i--) {
out << x.a[i];
}
return out;
}
inline Big operator << (const Big &x, const ll &k) {
return x.shl(k);
}
inline Big& operator <<= (Big &x, const ll &k) {
x = x.shl(k);
return x;
}
inline Big operator >> (const Big &x, const ll &k) {
return x.shr(k);
}
inline Big& operator >>= (Big &x, const ll &k) {
x = x.shr(k);
return x;
}
inline bool operator && (const Big &x, const Big &y) {
return !(x.a[0] == 1 && x.a[1] == 0) && !(y.a[0] == 1 && y.a[1] == 0);
}
inline bool operator || (const Big &x, const Big &y) {
return !(x.a[0] == 1 && x.a[1] == 0) || !(y.a[0] == 1 && y.a[1] == 0);
}
inline bool operator ! (const Big &x) {
return x.a[0] == 1 && x.a[1] == 0;
}
inline Big operator + (const ll &k, const Big &o) {
return o + k;
}
inline Big operator - (const ll &k, const Big &o) {
return Big(k) - o;
}
inline Big operator * (const ll &k, const Big &o) {
return o * k;
}
inline Big operator / (const ll &k, const Big &o) {
return Big(k) / o;
}
int main() {
// 函数: abs() pow() sqrt() to_string()
// 用法:a.abs() a.pow(10) a.sqrt(2) a.to_string()
// 说明:绝对值 幂 开n次方根 转为字符串
// 实现了 = + += - -= * *= / /= % %= == != < > <= >= << <<= >> >>=(左移右移) && || ! >> <<(流输入输出)
// 作者: wangyanxi 部!分!代码借助DeepSeek 原创内容超过80%
// 求测试代码
return 0;
}
- Big 高精度类 - 完整函数与运算符说明
- 一、构造函数 (4个)
-
Big()默认构造,初始化为0Big(const Big &o)拷贝构造Big(const string &s)从字符串构造,支持正负号Big(ll num)从整数构造,支持负数- 二、赋值运算符 (1个)
-
operator=赋值- 三、比较运算符 (6个)
-
operator==等于operator!=不等于operator<小于operator>大于operator<=小于等于operator>=大于等于- 四、算术运算符 - 高精度 ±*/% 高精度 (10个)
-
operator+高精度 + 高精度operator+=高精度 += 高精度operator-高精度 - 高精度operator-=高精度 -= 高精度operator*高精度 × 高精度operator*=高精度 ×= 高精度operator/高精度 ÷ 高精度operator/=高精度 ÷= 高精度operator%高精度 % 高精度operator%=高精度 %= 高精度- 五、算术运算符 - 高精度 ±*/% 低精度 (5个)
-
operator*高精度 × 低精度operator*=高精度 ×= 低精度operator/高精度 ÷ 低精度operator/=高精度 ÷= 低精度operator%高精度 % 低精度 (返回ll)- 六、算术运算符 - 低精度 ±*/% 高精度 (4个)
-
operator+低精度 + 高精度operator-低精度 - 高精度operator*低精度 × 高精度operator/低精度 ÷ 高精度- 七、位运算运算符 (6个)
-
operator<<左移k位 (×10^k)operator<<=左移赋值operator>>右移k位 (÷10^k)operator>>=右移赋值shl(k)成员函数左移k位shr(k)成员函数右移k位- 八、逻辑运算符 (3个)
-
operator&&逻辑与 (非零为真)operator||逻辑或operator!逻辑非 (零为真)- 九、流输入输出 (2个)
-
operator>>流输入operator<<流输出- 十、工具函数 (5个)
-
abs()返回绝对值pow(ll n)乘方 (快速幂)sqrt(ll n=2)开n次根 (默认平方根)print()打印到控制台to_string()转为string- 十一、静态辅助函数 (4个)
-
abscmp(x,y)比较绝对值abs_add(x,y)绝对值加法 (内部用)abs_sub(x,y)绝对值减法 (内部用)abs_mul(x,y)绝对值乘法 (内部用)-
- 总计: 50个函数/运算符
-
此表格使用DeepSeek生成
未经允许请勿搬运
求点赞
这里空空如也















有帮助,赞一个