大整数除法Word格式.docx
《大整数除法Word格式.docx》由会员分享,可在线阅读,更多相关《大整数除法Word格式.docx(15页珍藏版)》请在冰豆网上搜索。
//返回绝对值
friendostream&
operator<
<
(ostream&
dirt,Cbigint&
iv);
//输出
friendistream&
operator>
>
(istream&
//输入
CbigintCbigint:
operator=(Cbigintn);
//重载等号
booloperator>
(constCbigint&
n);
booloperator<
booloperator==(constCbigint&
Cbigintoperator-(constCbigint&
Cbigintoperator-(longn);
Cbigintoperator-()const;
//重载负号
Cbigintoperator+(constCbigint&
Cbigintoperator+(longn);
Cbigintoperator*(longn);
Cbigintoperator*(constCbigint&
Cbigintoperator/(longn);
Cbigintoperator/(constCbigint&
stringtoString();
//转化为字条串
voidinit(char*p);
//用字串初始化
};
//bigint.cpp
#include"
bigint.h"
Cbigint:
Cbigint(char*p)
if(p==NULL)
...{
for(inti=1;
i<
maxsize-1;
i++)
m_int[i]=0;
m_int[0]='
+'
;
length=1;
}
else
length=strlen(p);
inti=0;
if(p[0]!
='
-'
)
for(i=1;
=length;
m_int[i]=p[i-1]-48;
length;
m_int[i]=p[i]-48;
length--;
for(i;
maxsize;
}
Cbigint(longn)
longj=0,m=n;
while((m/10)!
=0)
j++;
m=m/10;
length=j+1;
while((n/10)!
m_int[j+1]=n%10;
j--;
n=n/10;
m_int[1]=n;
if(n>
voidCbigint:
init(char*p)
inti=1;
Cbigint(constCbigint&
c)
for(inti=0;
=c.length;
m_int[i]=c.m_int[i];
length=c.length;
stringCbigint:
toString()
{
stringstr(length+1,'
\0'
);
if(m_int[0]=='
{
for(intj=0;
j<
j++)
str[j]=m_int[j+1]+48;
for(intj=1;
str[j]=m_int[j]+48;
str[0]='
returnstr;
sinmult(intoffset,charstr1,constchar*str2,intn,char*dest)//一位的整数str1(0-9)与str2相乘把结果把到dest的offset位上
intinc=0,i,x;
i=n-1;
while(i>
x=str2[i]*str1+inc+dest[offset];
dest[offset]=x%10;
inc=x/10;
i--;
offset++;
while(inc)
x=dest[offset]+inc;
mult(constchar*str1,constchar*str2,intn1,intn2,char*dest)
inti=n1-1;
sinmult(n1-i-1,str1[i],str2,n2,dest);
CbigintCbigint:
operator*(constCbigint&
n)
Cbiginttemp;
chardest[maxsize]={0};
inti=0,j=0;
mult(m_int+1,n.m_int+1,length,n.length,dest);
for(i=maxsize-1;
i>
=0;
i--)
if(dest[i]!
break;
temp.length=i+1;
for(j=0;
=i;
temp.m_int[j+1]=dest[i-j];
if(m_int[0]!
=n.m_int[0])
temp.m_int[0]='
returntemp;
operator/(constCbigint&
Cbigintdiv1=abs(),div2=n.abs(),result;
intflag=0;
if(m_int[0]==n.m_int[0])
flag='
if(div1<
div2)
result.m_int[0]='
result.m_int[1]=0;
result.length=1;
returnresult;
result.m_int[0]=flag;
if(div1==div2)
result.m_int[1]=1;
intlen1=div1.length,len2=div2.length,len3,temp=0;
=len2;
if(div1.m_int[i]>
div2.m_int[i])
result.length=len1-len2+1;
if(div1.m_int[i]<
result.length=len1-len2;
if(i>
len2)
len3=result.length;
while(div1>
div2||div1==div2)
temp=len1-len2+1;
temp=len1-len2;
result.m_int[len3-temp+1]=result.m_int[len3-temp+1]+1;
div2.length=div2.length+temp-1;
div1=div1-div2;
div2.length=len2;
len1=div1.length;
operator/(longn)
return(*this)/Cbigint(n);
operator=(constCbigintn)
=n.length;
m_int[i]=n.m_int[i];
for(inti=n.length+1;
length=n.length;
returnn;
operator*(longn)
return(*this)*Cbigint(n);
operator+(longn)
return*this+Cbigint(n);
boolCbigint:
if(m_int[0]>
n.m_int[0])
returntrue;
if(m_int[0]<
returnfalse;
if(length>
n.length)
if(length<
if(m_int[i]>
n.m_int[i])
if(m_int[i]<
operator==(constCbigint&
if(length!
=n.length)
if(m_int[i]!
=n.m_int[i])
if(*this>
n||*this==n)
operator+(constCbigint&
charflag='
flag=m_int[0];
if(*this<
sum(m_int+1,length,n.m_int+1,n.length,dest);
sum(n.m_int+1,n.length,m_int+1,length,dest);
if(abs()==n.abs())
temp.length=1;
temp.m_int[1]=0;
if(abs()>
n.abs())
sub(m_int+1,length,n.m_int+1,n.length,dest);
flag=n.m_int[0];
sub(n.m_int+1,n.length,m_int+1,length,dest);
temp.m_int[0]=flag;
operator-(constCbigint&
return*this+(-n);
operator-(longn)
return*this+Cbigint(-n);
sum(constchar*str1,intn1,constchar*str2,intn2,char*dest)
intinc=0,x=0;
intm=0;
while(n1-->
0)
n2--;
x=str1[n1]+str2[n2]+inc;
dest[m++]=x%10;
while(n2-->
x=str2[n2]+inc;
dest[m]=inc;
operator-()const
Cbiginttemp=*this;
if(temp.m_int[0]=='
sub(constchar*str1,intn1,constchar*str2,intn2,char*dest)
intm=0,x=0,inc=0;
m=0;
n1--;
x=str1[n1]-str2[n2]-inc;
if(x<
x+=10;
inc=1;
inc=0;
x=str1[n1]-inc;
dest[m]=x%10;
m++;
abs()const
Cbiginttemp(*this);
ostream&
iv)
returndirt<
iv.toString();
istream&
chartemp[1000];
dirt>
temp;
iv.init(temp);
returndirt;