等号重载的问题!
程序代码:
#include <iostream> #include <cstring> const int N=10; using namespace std; class HugeInt { private: int integer[N]; public: HugeInt(long val=0); HugeInt(const char* str); friend HugeInt MinusHugeInt(HugeInt& h1,HugeInt& h2); friend HugeInt& operator + (HugeInt& h1,HugeInt& h2); friend HugeInt& operator + (HugeInt& op1,int op2); friend HugeInt& operator + (HugeInt& op1,char* op2); friend ostream& operator << (ostream& os,const HugeInt& num); bool operator > (const HugeInt& rhs); HugeInt& operator = (const HugeInt& rhs); HugeInt& operator - ( HugeInt& rhs); HugeInt& Multiply(short n); HugeInt& Shiftleft(int s); friend HugeInt& operator * (HugeInt& op1,HugeInt& op2); friend class HugeDouble; }; HugeInt::HugeInt(long val) { for(int i=0;i<N;i++) integer[i]=0; for(int i=N-1;val!=0&&i>=0;i--) { integer[i]=val%10; val/=10; } } HugeInt::HugeInt(const char* str) { for(int i=0;i<N;i++) integer[i]=0; for(int i=N-strlen(str),j=0;i<N,j<strlen(str);i++,j++) if(isdigit(str[j])) integer[i]=str[j]-'0'; } HugeInt& HugeInt::operator = (const HugeInt& rhs) { if(this!=&rhs) { for(int i=N-1;i>=0;i--) this->integer[i]=rhs.integer[i]; } return *this; } HugeInt& operator + (HugeInt& h1,HugeInt& h2) { HugeInt h; int carry=0; for(int i=N-1;i>=0;i--) { h.integer[i]=h1.integer[i]+h2.integer[i]+carry; if(h.integer[i]>=10) { h.integer[i]%=10; carry=1; } else carry=0; } return h; } HugeInt& operator + (HugeInt& op1,int op2) { return op1+HugeInt(op2); } HugeInt& operator + (HugeInt& op1,char* op2) { return op1+HugeInt(op2); } ostream& operator << (ostream& os,const HugeInt& num) { int i; for(i=0;(num.integer[i]==0)&&(i<N);i++) {} if(i==N) os<<0; else for(;i<N;i++) os<<num.integer[i]; return os; } bool HugeInt:: operator > (const HugeInt& rhs) { int i,j; for(i=0;(this->integer[i]==0)&&(i<N);i++) {} for(j=0;(rhs.integer[j]==0)&&(j<N);j++) {} if(i<j) return true; else if(i>j) return false; else { for(int s=i;s<N;s++) { if(this->integer[i]>rhs.integer[i]) return true; else if(this->integer[i]<rhs.integer[i]) return false; } return false; } } HugeInt MinusHugeInt(HugeInt& h1,HugeInt& h2) { HugeInt temp; int carry=0; for(int i=N-1;i>=0;i--) { temp.integer[i]=h1.integer[i]-h2.integer[i]-carry; if(temp.integer[i]<0) { temp.integer[i]+=10; carry=1; } else carry=0; } return temp; } HugeInt& HugeInt:: operator - ( HugeInt& rhs) { HugeInt temp; if(*this>rhs) temp=MinusHugeInt(*this,rhs); else { temp=MinusHugeInt(rhs,*this); int i; for( i=0;(temp.integer[i]==0)&&i<N;i++) {} temp.integer[i]*=-1; } return temp; } HugeInt& HugeInt::Multiply(short n) { HugeInt temp; int carry=0; for(int i=N-1;i>=0;i--) { temp.integer[i]=n*this->integer[i]+carry; short m=temp.integer[i]; if(m>=10) { temp.integer[i]%=10; carry=m/10; } else carry=0; } return temp; } HugeInt& HugeInt::Shiftleft(int s) //左移s位 { HugeInt temp; if(s==0) return *this; for(int i=N-1;i>N-1-s;i--) temp.integer[i]=0; int m; for(m=0;(this->integer[m]==0)&&(m<N);m++) {} for(int j=N-1-s,i=N-1;j>=m-s;j--,i--) temp.integer[j]=this->integer[i]; return temp; } HugeInt& operator * (HugeInt& op1,HugeInt& op2) { HugeInt s,temp,temp1; int i,j; for(i=0;(op1.integer[i]==0)&&(i<N);i++) {} for(j=0;(op2.integer[j]==0)&&(j<N);j++) {} int m=0; for(int s1=N-1;s1>=j;s1--) { temp=op1.Multiply(op2.integer[s1]); temp1=temp.Shiftleft(m); m++; s=s+temp1; } return s; } int main() { char* s ="35"; char* s1= "55"; HugeInt a(s), b(s1), c,d,f; c=(a+b); d=a-b; f=a*b; cout<<c<<endl; cout<<d<<endl; cout<<f<<endl; }请教大虾,对于 c=a+b这句,我加了等号重载就是错的,删掉等号重载就会输出正确! 我分析知: 在计算a+b 后 返回的h 没有 作=重载的参数,而是乱给一个数,导致输出错误!
请教大侠指教!小弟一定十分感谢!!!
[ 本帖最后由 m21wo 于 2010-10-21 20:02 编辑 ]