bignum 大数模板

  今天无意间看到一个很好的大数模板,能算加、减、乘、除等基本运算,但操作减法的时候只能大数减小数,也不支持负数,如果是两个负数的话去掉符号相加之后再取反就可以了,一正一负比较绝对值大小,然后相减。我借用了一下:(作过少许代码上的精简)

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = ; struct bign{
int len, s[maxn];
bign(){
memset(s,,sizeof(s));
len= ;
}
bign(int num) { *this = num; }
bign(const char *num) { *this = num; }
bign operator = (const int num){
char s[maxn];
sprintf(s,"%d",num);
return *this = s;
}
bign operator = (const char *num){
for(int i = ; num[i] && num[i] ==''; ++num) ;
len = strlen(num);
for(int i = ; i < len; ++i)
s[i] = num[len--i] -'';
return *this;
}
bign operator + (const bign &b) const {
bign c;
c.len = ;
int maxlen = max(len, b.len);
for(int i = , g = ; g || i < maxlen; ++i){ // g是进位
int x = g;
if(i < len) x += s[i];
if(i < b.len) x += b.s[i];
c.s[c.len++] = x % ;
g = x / ;
}
return c;
}
bign operator += (const bign &b){
return *this = *this + b;
}
void clean(){
while(len > && !s[len-]) --len;
}
bign operator * (const bign &b) const {
bign c;
c.len = len + b.len; for(int i = ; i < len; ++i)
for(int j = ; j < b.len; ++j)
c.s[i+j] += s[i] * b.s[j]; for(int i = ; i < c.len; ++i){
c.s[i+] += c.s[i]/;
c.s[i] %= ;
}
c.clean();
return c;
}
bign operator *= (const bign &b){
return *this = *this * b;
}
bign operator -(const bign &b) const {
bign c;
c.len = ;
for(int i = , g = ; i < len; ++i){ //此时的 g是退位
int x = s[i]- g;
if(i < b.len) x -= b.s[i];
if(x >= ) g = ;
else {
g = ;
x += ;
}
c.s[c.len++] = x;
}
c.clean();
return c;
}
bign operator -= (const bign &b){
return *this = *this - b;
}
bign operator / (const bign &b) const {
bign c, f = ;
for(int i = len-; i >= ; --i){
f *= ;
f.s[] = s[i];
while(f >= b){
f -= b;
++c.s[i];
}
}
c.len = len;
c.clean();
return c;
}
bign operator /= (const bign &b){
return *this = *this / b;
}
bign operator % (const bign &b) const {
bign r = *this / b;
return *this- r*b;
}
bign operator %= (const bign &b){
return *this = *this % b;
}
bool operator < (const bign &b) const {
if(len != b.len) return len < b.len;
for(int i= len-; i>=; --i)
if(s[i] != b.s[i]) return s[i] < b.s[i];
return ;
}
bool operator > (const bign &b) const {
if(len != b.len) return len > b.len;
for(int i = len-; i >= ; --i)
if(s[i] != b.s[i]) return s[i] > b.s[i];
return ;
}
bool operator == (const bign &b) const {
return !(*this < b || *this > b);
}
bool operator != (const bign &b) const {
return !(*this == b);
}
bool operator <= (const bign &b) const {
return *this < b || *this == b;
}
bool operator >= (const bign &b) const {
return *this > b || *this == b;
}
string str() const {
string res = "";
for(int i = ; i < len; ++i)
res = char(s[i]+'') + res; //这里一定不能掉乱 +的顺序!
return res;
}
}; istream& operator >> (istream &in, bign &x){
string s;
in >> s;
x = s.c_str();
return in;
} ostream& operator << (ostream &out, const bign &x){
out << x.str();
return out;
} int main()
{
bign a,b,c,d,e,f,g;
while(cin>>a>>b)
{
a.clean();
b.clean();
c = a+b;
d = a-b;
e = a*b;
f = a/b;
g = a%b;
cout<<"a+b = "<<c<<endl;
cout<<"a-b = "<<d<<endl;
cout<<"a*b = "<<e<<endl;
cout<<"a/b = "<<f<<endl;
cout<<"a%b = "<<g<<endl;
cout<<(a==b? "a = b":"a != b")<<endl;
}
return ;
}

  在此感谢原博客的模板提供: bign类C++高精度模板

上一篇:大数模板 poj3982


下一篇:XML详解:第三部分 XML解析