- BC20270080's blog
高精度运算库
- 2025-1-4 13:43:38 @
这个库支持100000位数的加(+)减(-)乘(*)运算
语言:C++11及以上
#include <bits/stdc++.h>
using namespace std;
const unsigned int SIZE = 1e5;
class large
{
public:
bool if_positive;
short value[SIZE];//value[0]表示位数,value[1]是个位,倒序录入
void reset()
{
if_positive = true;
memset(value,0,sizeof(value));
}
void get_abs() {if_positive = true;}
void get_back() {if_positive = (if_positive)?false:true;}
void times10()
{
int len = value[0];
value[0]++;
for(int i = len;i >= 1;i--)
{
value[i + 1] = value[i];
}
value[1] = 0;
}
void timek10(int k)
{
while(k--) times10();
}
};
large add(large a,large b)
{
large result;
result.reset();
int len = max(a.value[0],b.value[0]);
result.value[0] = len;
for(int i = 1;i <= len;i++)
{
int carry = a.value[i] + b.value[i];
result.value[i] = carry % 10;
carry /= 10;
result.value[i + 1] += carry;
}
if(result.value[len + 1] > 0) result.value[0]++;
return result;
}
large sub(large a,large b)
{
large result;
result.reset();
int len = max(a.value[0],b.value[0]);
for(int i = 1;i <= len;i++)
{
signed int carry = a.value[i] - b.value[i];
if(carry < 0)
{
carry += 10;
result.value[i + 1]--;
}
result.value[i] = carry;
}
if(result.value[len + 1] != 0) result.value[0]++;
if(result.value[result.value[0]] < 0) result.if_positive = false;
result.value[result.value[0]] = abs(result.value[result.value[0]]);
return result;
}
large operator+(large a,large b)
{
large result;
result.reset();
if(a.if_positive == b.if_positive)
{
result = add(a,b);
result.if_positive = a.if_positive;
}
else
{
if(a.if_positive == true) result = sub(a,b);
else result = sub(b,a);
}
return result;
}
large operator-(large a,large b)
{
b.get_back();
return a + b;
}
large string_to_large(string input)
{
large result;
if(input[0] == '-')
{
result.if_positive = false;
input.erase(0,1);
}
else if(input[0] == '+')
{
result.if_positive = true;
input.erase(0,1);
}
else if(input[0] >= '0' && input[0] <= '9') result.if_positive = true;
else
{
printf("Input Error");
exit(0);
}
int len = input.size();
for(int i = 0;i < len;i++)
{
result.value[len - i] = input[i];
}
return result;
}
string large_to_string(large output)
{
int len = output.value[0];
string result = "";
for(int i = len;i >= 1;i--)
{
result += (char)(output.value[i] + '0');
}
return result;
}
void output_large(large out,bool front_enter,bool end_enter)
{
string ans = large_to_string(out);
if(front_enter) printf("\n");
printf("%s",ans);
if(end_enter) printf("\n");
}
large int_mul(large x,int y)
{
large result;
int len = x.value[0];
int cur;
for(int i = 1;i <= len;i++)
{
int ans = x.value[i] * y;
cur = ans / 10; ans %= 10;
result.value[i] = ans;
result.value[i + 1] += cur;
}
result.value[0] = len;
if(cur > 0) result.value[0]++;
return result;
}
large mul(large a,large b)
{
large ans;ans.reset();
int len = a.value[0];
for(int i = 1;i <= len;i++)
{
large cur = int_mul(b,a.value[i]);
cur.timek10(i - 1);
ans = ans + cur;
}
if(ans.value[ans.value[0] + 1] > 0) ans.value[0]++;
return ans;
}
large operator*(large a,large b)
{
large result;
result = mul(a,b);
result.if_positive = (a.if_positive == b.if_positive);
return result;
}
large input_large()
{
string s0;
scanf("%s",&s0);
return string_to_large(s0);
}