【模板】高精度
作者:互联网
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct bign
{
int d[200010], len;
bign() { memset(d, 0, sizeof d); len = 1; }
bign(int num) { *this = num; }
bign(char* num) { *this = num; }
bign operator=(const char* num)
{
len = strlen(num);
for (int i = 0; i < len; i++) d[i] = num[len - i - 1] - '0';
return *this;
}
bign operator=(int num)
{
char c[1010];
sprintf(c, "%d", num);
*this = c;
return *this;
}
void clear()
{
while (len > 1 && !d[len - 1]) len--;
}
bign operator+(const bign& b)
{
bign c; c.len = 0;
for (int i = 0, t = 0; t || i < len || i < b.len; i++)
{
if (i < len) t += d[i];
if (i < b.len) t += b.d[i];
c.d[c.len++] = t % 10;
t /= 10;
}
return c;
}
bign operator-(const bign& b)
{
bign c; c.len = 0;
for (int i = 0, t = 0; i < len; i++)
{
t += d[i];
if (i < b.len) t -= b.d[i];
c.d[c.len++] = (t + 10) % 10;
if (t >= 0) t = 0;
else t = -1;
}
c.clear();
return c;
}
bign operator*(const bign& b)
{
bign c; c.len = len + b.len;
for (int i = 0; i < len; i++) for (int j = 0; j < b.len; j++) c.d[i + j] += d[i] * b.d[j];
for (int i = 0; i < c.len - 1; i++) c.d[i + 1] += c.d[i] / 10, c.d[i] %= 10;
c.clear();
return c;
}
bool operator < (const bign& b)
{
if (len != b.len) return len < b.len;
for (int i = len - 1; i >= 0; i--)
if (d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bool operator <= (const bign& b)
{
if (len != b.len) return len < b.len;
for (int i = len - 1; i >= 0; i--)
if (d[i] != b.d[i]) return d[i] < b.d[i];
return true;
}
bool operator > (const bign& b)
{
if (len != b.len) return len > b.len;
for (int i = len - 1; i >= 0; i--)
if (d[i] != b.d[i]) return d[i] > b.d[i];
return false;
}
bool operator >= (const bign& b)
{
if (len != b.len) return len > b.len;
for (int i = len - 1; i >= 0; i--)
if (d[i] != b.d[i]) return d[i] > b.d[i];
return true;
}
bign operator+=(const bign& b)
{
*this = *this + b;
return *this;
}
bign operator-=(const bign& b)
{
*this = *this - b;
return *this;
}
bign operator*=(const bign& b)
{
*this = *this * b;
return *this;
}
void print()
{
for (int i = len - 1; i >= 0; i--) std::cout << d[i];
cout << '\n';
}
string str()
{
string res = "";
for (int i = 0; i < len; i++) res = (char)(d[i] + '0') + res;
return res;
}
};
istream& operator >>(istream& in, bign& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator <<(ostream& out, bign& x)
{
out << x.str();
return out;
}
int main()
{
bign a,b,c;
cin>>a>>b;
c=a+b;
cout<<c<<'\n';
}
标签:return,高精度,int,len,bign,operator,const,模板 来源: https://blog.csdn.net/Fighting_Peter/article/details/113826132