- 这篇文章就纯属自娱自乐啦,受时间以及技术原因的限制,这里面的重载,减法支持的功能仅限于减一次(因为没有同是负号的判断),除法仅限于高精除单精(高精除高精的一个个的减或者二分实在是不想写了)……
- 结构体片段以及输出操作如下:
struct bignum
{
int len,s[maxn];
char flag;
bignum()
{
len=1;
flag=‘+‘;
memset(s,0,sizeof(s));
}
bignum (int num)
{
*this=num;
}
bignum (const char *num)
{
*this=num;
}
bignum operator = (const char *a)
{
len=strlen(a);
for (int i=1;i<=len;++i)
s[i]=a[len-i]-‘0‘;
return *this;
}
bignum operator = (const int num)
{
char a[maxn];
sprintf(a,"%d",num);
*this=a;
return *this;
}
bignum operator + (const bignum &a)
{
bignum c;
c.len=max(len,a.len)+1;
for (int i=1;i<c.len;++i)
{
c.s[i]+=(s[i]+a.s[i]);
c.s[i+1]+=c.s[i]/10;
c.s[i]%=10;
}
if (c.s[c.len]==0)
c.len--;
return c;
}
bignum operator += (const bignum &a)
{
*this=*this+a;
return *this;
}
bignum operator * (const bignum &a)
{
bignum c;
c.len+=(len+a.len);
for (int i=1;i<=len;++i)
for (int j=1;j<=a.len;++j)
{
c.s[i+j-1]+=(s[i]*a.s[j]);
c.s[i+j]+=(c.s[i+j-1]/10);
c.s[i+j-1]%=10;
}
while (c.s[c.len]==0)
c.len--;
return c;
}
bignum operator *= (const bignum &a)
{
*this=(*this) * a;
return *this;
}
bool operator < (const bignum &a) const
{
if (len!=a.len)
return len<a.len;
for (int i=len;i>=1;--i)
if (s[i]!=a.s[i])
return s[i]<a.s[i];
return false;
}
bool operator > (const bignum &a) const
{
return a<*this;
}
bool operator <= (const bignum &a) const
{
return !(*this>a);
}
bool operator >= (const bignum &a) const
{
return !(*this<a);
}
bool operator == (const bignum &a) const
{
return !((*this<a) || (*this>a));
}
bool operator != (const bignum &a) const
{
return !(*this==a);
}
void change (bignum &a,bignum &b)
{
bignum tmp=a;
a=b;
b=tmp;
}
bignum operator - (const bignum &a) const
{
bignum b=*this,c;
if (b<a)
{
c.flag=‘-‘;
c.len=a.len;
for (int i=1;i<=c.len;++i)
{
c.s[i]+=(a.s[i]-b.s[i]);
if (c.s[i]<0)
{
c.s[i]+=10;
c.s[i+1]-=1;
}
}
while (c.len==0)
c.len--;
return c;
}
c.len=b.len;
for (int i=1;i<=c.len;++i)
{
c.s[i]+=(b.s[i]-a.s[i]);
if (c.s[i]<0)
{
c.s[i]+=10;
c.s[i+1]-=1;
}
}
while (c.len==0)
c.len--;
return c;
}
bignum operator -= (const bignum &a)
{
*this=(*this)-a;
return *this;
}
bignum operator / (const int n)
{
bignum c,b=*this;
c.len=b.len;
int x=0;
for (int i=1;i<=n;++i)
{
c.s[i]=(x*10+b.s[i])/n;
x=(x*10+b.s[i])%n;
}
while (c.s[c.len]==0)
c.len--;
return c;
}
bignum operator /= (const int a)
{
*this=*this/a;
return *this;
}
};
ostream& operator << (ostream &out,const bignum &x)
{
for (int i=x.len;i>=1;--i)
printf("%d",x.s[i]);
return out;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-17 23:26:44