1、/返回绝对值 friend ostream&operator(istream &/输入 Cbigint Cbigint:operator =(Cbigint n);/重载等号 bool operator(const Cbigint &n); bool operator bool operator=(const Cbigint & Cbigint operator-(const Cbigint & Cbigint operator-(long n); Cbigint operator-()const;/重载负号 Cbigint operator+(const Cbigint & Cbigint
2、operator+(long n); Cbigint operator*(long n); Cbigint operator*(const Cbigint & Cbigint operator/(long n); Cbigint operator/(const Cbigint & string toString();/转化为字条串 void init(char *p);/用字串初始化;/bigint.cpp#include bigint.hCbigint:Cbigint(char *p) if(p=NULL) . for(int i=1;ivoid Cbigint:init(char *p)
3、int i=1;Cbigint(const Cbigint &c) for(int i=0;=c.length; m_inti=c.m_inti; length=c.length;string Cbigint:toString() string str(length+1,0); if(m_int0= for(int j=0;j x=str2i*str1+inc+destoffset; destoffset=x%10; inc=x/10; i-; offset+; while(inc) x=destoffset+inc;mult(const char *str1,const char *str2
4、,int n1,int n2,char *dest) int i=n1-1; sinmult(n1-i-1,str1i,str2,n2,dest);Cbigint Cbigint:operator *(const Cbigint &n) Cbigint temp; char destmaxsize=0; int i=0,j=0; mult(m_int+1,n.m_int+1,length,n.length,dest); for(i=maxsize-1;i=0;i-) if(desti! break; temp.length=i+1; for(j=0;=i; temp.m_intj+1=dest
5、i-j; if(m_int0!=n.m_int0) temp.m_int0= return temp;operator /(const Cbigint & Cbigint div1=abs(),div2=n.abs(),result; int flag=0; if(m_int0=n.m_int0) flag= if(div1div2.m_inti) result.length=len1-len2+1; if(div1.m_intilen2) len3=result.length; while(div1div2|div1=div2) temp=len1-len2+1; temp=len1-len
6、2; result.m_intlen3-temp+1=result.m_intlen3-temp+1+1; div2.length=div2.length+temp-1; div1=div1-div2; div2.length=len2; len1=div1.length;operator /(long n) return (*this)/Cbigint(n);operator =(const Cbigint n)=n.length; m_inti=n.m_inti; for(int i=n.length+1; length=n.length; return n;operator *(long
7、 n) return (*this)*Cbigint(n);operator +(long n) return *this+Cbigint(n);bool Cbigint: if(m_int0n.m_int0) return true; if(m_int0n.length) if(lengthn.m_inti) if(m_intin|*this=n)operator +(const Cbigint & char flag= flag=m_int0; if(*thisn.abs() sub(m_int+1,length,n.m_int+1,n.length,dest); flag=n.m_int
8、0; sub(n.m_int+1,n.length,m_int+1,length,dest); temp.m_int0=flag;operator -(const Cbigint & return *this+(-n);operator -(long n) return *this+Cbigint(-n);sum(const char *str1,int n1,const char *str2,int n2,char *dest) int inc=0,x=0; int m=0; while(n1-0) n2-; x=str1n1+str2n2+inc; destm+=x%10; while(n
9、2- x=str2n2+inc; destm=inc; operator -()const Cbigint temp=*this; if(temp.m_int0=sub(const char *str1,int n1,const char *str2,int n2,char *dest) int m=0,x=0,inc=0; m=0; n1-; x=str1n1-str2n2-inc; if(x x+=10; inc=1; inc=0; x=str1n1-inc; destm=x%10; m+;abs() const Cbigint temp(*this);ostream&iv) return dirttemp; iv.init(temp); return dirt;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1