自己的:
const int LEN=100;
typedef struct HighPrecision
{
short data[LEN],h;
HighPrecision()
{
CLEAR(data);
h=0;
}
HighPrecision(int n)
{
CLEAR(data);
h=0;
while(n>0) data[h++]=n%10,n/=10;
if(!data[h]) h--;
}
/*HighPrecision(const HighPrecision &a)
{
memcpy(data,a.data,sizeof(data));
h=a.h;
}*/
const HighPrecision &operator= (int n)
{
CLEAR(data);
h=0;
while(n>0) data[h++]=n%10,n/=10;
if(!data[h]) h--;
return *this;
}
const HighPrecision &operator= (const HighPrecision &a)
{
memcpy(data,a.data,sizeof(data));
h=a.h;
return *this;
}
bool odd()
{
return data[0]&1;
}
void output()
{
for(int i=h;i>=0;i--) printf("%d",data[i]);
}
} BigInteger;
bool operator< (const BigInteger &a,const BigInteger &b)
{
if (a.h<b.h) return 1;
else if (a.h>b.h) return 0;
else
{
int h=b.h,k=0;
for(int i=h;i>=0;i--)
if (a.data[i]!=b.data[i]) {k=i;break;}
return a.data[k]<b.data[k];
}
}
bool operator< (const BigInteger &a,int n)
{
return a<BigInteger(n);
}
bool operator> (const BigInteger &a,const BigInteger &b)
{
if (a.h<b.h) return 0;
else if (a.h>b.h) return 1;
else
{
int h=b.h,k=0;
for(int i=h;i>=0;i--)
if (a.data[i]!=b.data[i]) {k=i;break;}
return a.data[k]>b.data[k];
}
}
bool operator> (const BigInteger &a,int n)
{
return a>BigInteger(n);
}
bool operator== (const BigInteger &a,const BigInteger &b)
{
if (a.h!=b.h) return 0;
else
{
int h=b.h,k=0;
for(int i=h;i>=0;i--)
if (a.data[i]!=b.data[i]) {k=i;break;}
return a.data[k]==b.data[k];
}
}
bool operator== (const BigInteger &a,int n)
{
return a==BigInteger(n);
}
bool operator>= (const BigInteger &a,const BigInteger &b)
{
return (a>b)||(a==b);
}
BigInteger operator+(const BigInteger &a,const BigInteger &b)
{
BigInteger c;
int h=max(a.h,b.h);
for(int i=0;i<=h;i++)
{
c.data[i]+=a.data[i]+b.data[i];
c.data[i+1]=c.data[i]/10;
c.data[i]%=10;
}
while(c.data[c.h+1]>0) c.h++;
return c;
}
BigInteger operator+(const BigInteger &a,int n)
{
return a+BigInteger(n);
}
BigInteger operator+= (const BigInteger &a,const BigInteger &b)
{
return a+b;
}
BigInteger operator-(const BigInteger &a,const BigInteger &b)
{
BigInteger c(a);
int h=c.h;
for(int i=0;i<=c.h;i++)
{
c.data[i]-=b.data[i];
if (c.data[i]<0)
{
c.data[i]+=10;
c.data[i+1]--;
}
}
while(c.data[c.h]<1) c.h--;
return c;
}
BigInteger operator-(const BigInteger &a,int n)
{
return a-BigInteger(n);
}
BigInteger operator-= (const BigInteger &a,const BigInteger &b)
{
return a-b;
}
BigInteger operator* (const BigInteger &a,const BigInteger &b)
{
BigInteger c;
for(int i=0;i<=a.h;i++)
for(int j=0;j<=b.h;j++)
c.data[i+j-1]+=a.data[i]*b.data[j];
int k=0;
while(c.data[k++]>0)
{
c.data[k+1]+=c.data[k]/10;
c.data[k]%=10;
}
c.h=(k-1>=0)?k-1:0;
return c;
}
BigInteger operator*= (const BigInteger &a,const BigInteger &b)
{
return a*b;
}
BigInteger operator/ (const BigInteger &a,const BigInteger &b)
{
BigInteger c,tmp;
int h=max(a.h,b.h);
for( int i = h; i >= 0; i-- ){
tmp*= 10;
tmp+= a.data[i];
while( tmp >= b ) { tmp -= b; c.data[i]++; }
}
c.h=a.h;
while(c.data[c.h]<1) c.h--;
return c;
}
BigInteger operator/ (const BigInteger &a,int n)
{
return a/BigInteger(n);
}
BigInteger operator/= (const BigInteger &a,const BigInteger &b)
{
return a/b;
}
BigInteger operator% (const BigInteger &a,const BigInteger &b)
{
BigInteger c,tmp;
int h=max(a.h,b.h);
for( int i = h; i >= 0; i-- ){
tmp*= 10;
tmp+= a.data[i];
while( tmp >= b ) { tmp -= b; c.data[i]++; }
}
tmp.h=a.h;
while(tmp.data[tmp.h]<1) tmp.h--;
return tmp;
}
BigInteger operator>>(const BigInteger &a,int n)
{
BigInteger c(a);
for (int i=1;i<=n;i++) c=c/2;
return c;
}
别人的:
http://blog.csdn.net/wall_f/article/details/8373395