主题:用高精度实现整数+—×/,并计算catalan数和大组合数3
接2
cata &cata::operator /(const cata &y)
{
int k=compare_cata(*this,y); //注意,以下假设x为除数,y为被除数 x>y k=1,x<y k=0,x==y k=2;
if(0==k) //当x<y时,so easy^_^
{
resi.p_resi=new int [length()];
if(NULL==resi.p_resi)
{
cout<<"the error take place in resi"<<endl;
exit(1);
}
for(int i=0;i<length();i++)
{
resi.p_resi[i]=data[i];
}
resi.len_resi=length();
delete [] data;
data=new int [1];
data[0]=0;
n=1;
return *this;
}
else if(2==k) //当x==y时,so easy too^_^
{
delete [] data;
data=new int [1];
if(NULL==data)
{
cout<<"the error occur in /"<<endl;
exit(1);
}
data[0]=1;
n=1;
resi.p_resi=new int [1];
resi.p_resi[0]=0;
resi.len_resi=1;
return *this;
}
else //一定x>y,分两种情况1.len_x==len_y 2.len_x>len_y
{
int len_x=length(),len_y=y.length(),cp;
if(len_x==len_y) //len_x==len_y,只要循环减,减的次数为商
{
for(int i3=0;;i3++)
{
*this-y;
if((cp=compare_cata(*this,y))==0)
{
break;
}
}
resi.p_resi=data;
resi.len_resi=length();
data=new int [1];
if(NULL==data)
{
exit(1);
}
data[0]=i3+1;
n=1;
return *this;
}
else //len_x>len_y,以下为除法的精髓
{
cata temp(len_x);
for(int i=0;i<len_x-len_y;i++)
{
temp.data[i]=0;
}
for(int j=len_x-len_y;j<len_x;j++)
{
temp.data[j]=y.data[j-len_x+len_y];
}
temp.n=len_x;
int *result=new int [len_x-len_y+1]; //result存放商
if(NULL==result)
{
cout<<"the error occur in /"<<endl;
exit(1);
}
for(j=0;j<=(len_x-len_y);j++)
{
cp=compare_cata(*this,temp);
if(1==cp||2==cp)
{
for(int i1=0;;i1++)
{
*this-temp;
if((cp=compare_cata(*this,temp))==0)
{
break;
}
}
result[len_x-len_y-j]=i1+1;
}
else //cp==0
{
result[len_x-len_y-j]=0;
}
temp.del_first(); //删去首单元
}
resi.p_resi=data; //余数最后在this->data中
resi.len_resi=n;
this->data=result;
this->n=len_x-len_y+1;
int cycle=1; //除法有可能在商的高位产生0,用cycle进行回溯,去除无用0
while(result[n-cycle]==0&&cycle<n)
{
cycle++;
}
n=n-cycle+1;
result=NULL;
return *this;
}
}
}///:p
inline cata &cata::operator =(const cata &y)
{
if(this->n!=y.n)
{
delete [] data;
data=new int [y.n];
if(NULL==data)
{
cout<<"the error take palce in operator ="<<endl;
exit(1);
}
}
for(int i=0;i<y.n;i++)
{
data[i]=y.data[i];
}
n=y.n;
return *this;
}///:p
cata &cata::operator /(const cata &y)
{
int k=compare_cata(*this,y); //注意,以下假设x为除数,y为被除数 x>y k=1,x<y k=0,x==y k=2;
if(0==k) //当x<y时,so easy^_^
{
resi.p_resi=new int [length()];
if(NULL==resi.p_resi)
{
cout<<"the error take place in resi"<<endl;
exit(1);
}
for(int i=0;i<length();i++)
{
resi.p_resi[i]=data[i];
}
resi.len_resi=length();
delete [] data;
data=new int [1];
data[0]=0;
n=1;
return *this;
}
else if(2==k) //当x==y时,so easy too^_^
{
delete [] data;
data=new int [1];
if(NULL==data)
{
cout<<"the error occur in /"<<endl;
exit(1);
}
data[0]=1;
n=1;
resi.p_resi=new int [1];
resi.p_resi[0]=0;
resi.len_resi=1;
return *this;
}
else //一定x>y,分两种情况1.len_x==len_y 2.len_x>len_y
{
int len_x=length(),len_y=y.length(),cp;
if(len_x==len_y) //len_x==len_y,只要循环减,减的次数为商
{
for(int i3=0;;i3++)
{
*this-y;
if((cp=compare_cata(*this,y))==0)
{
break;
}
}
resi.p_resi=data;
resi.len_resi=length();
data=new int [1];
if(NULL==data)
{
exit(1);
}
data[0]=i3+1;
n=1;
return *this;
}
else //len_x>len_y,以下为除法的精髓
{
cata temp(len_x);
for(int i=0;i<len_x-len_y;i++)
{
temp.data[i]=0;
}
for(int j=len_x-len_y;j<len_x;j++)
{
temp.data[j]=y.data[j-len_x+len_y];
}
temp.n=len_x;
int *result=new int [len_x-len_y+1]; //result存放商
if(NULL==result)
{
cout<<"the error occur in /"<<endl;
exit(1);
}
for(j=0;j<=(len_x-len_y);j++)
{
cp=compare_cata(*this,temp);
if(1==cp||2==cp)
{
for(int i1=0;;i1++)
{
*this-temp;
if((cp=compare_cata(*this,temp))==0)
{
break;
}
}
result[len_x-len_y-j]=i1+1;
}
else //cp==0
{
result[len_x-len_y-j]=0;
}
temp.del_first(); //删去首单元
}
resi.p_resi=data; //余数最后在this->data中
resi.len_resi=n;
this->data=result;
this->n=len_x-len_y+1;
int cycle=1; //除法有可能在商的高位产生0,用cycle进行回溯,去除无用0
while(result[n-cycle]==0&&cycle<n)
{
cycle++;
}
n=n-cycle+1;
result=NULL;
return *this;
}
}
}///:p
inline cata &cata::operator =(const cata &y)
{
if(this->n!=y.n)
{
delete [] data;
data=new int [y.n];
if(NULL==data)
{
cout<<"the error take palce in operator ="<<endl;
exit(1);
}
}
for(int i=0;i<y.n;i++)
{
data[i]=y.data[i];
}
n=y.n;
return *this;
}///:p