实验五运算符重载.docx
- 文档编号:9688689
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:16
- 大小:16.51KB
实验五运算符重载.docx
《实验五运算符重载.docx》由会员分享,可在线阅读,更多相关《实验五运算符重载.docx(16页珍藏版)》请在冰豆网上搜索。
实验五运算符重载
实验五、运算符重载
09计算机1白杨0929210028
1、设计一个简单的复数计算器,要求:
(1)设计一个复数类Complex,并通过重载运算符函数,
能够计算两个复数的加、减、乘、除,复数与实数的加、减、乘、除。
(2)重载输入、输出运算符函数,能够很方便的输入输出。
比如:
Complex c;
cin>>c;
cout< (3)在main.cpp中编写代码对其进行充分的测试。 2、定义类并重载运算符函数,完成指定的TOJ题目: 1548和3249 /*1548复数运算*/ #include #include usingnamespacestd; classComplex { private: doublereal,imag; public: Complex(): real(0),imag(0){} ~Complex(){} voidSet(doubler,doublei){real=r;imag=i;} doubleGet_real(){returnreal;} doubleGet_imag(){returnimag;} friendistream&operator>>(istream&is,constComplex&C); friendostream&operator<<(ostream&os,constComplex&C); Complexoperator+(constComplex&C); Complexoperator-(constComplex&C); Complexoperator*(constComplex&C); Complexoperator/(constComplex&C); }; istream&operator>>(istream&is,Complex&C) { doubler,i; cin>>r>>i; C.Set(r,i); returnis; } ostream&operator<<(ostream&os,constComplex&C) { cout.setf(ios: : fixed); if(C.real==0) { if(C.imag>-0.05&&C.imag<0.05) os<<"0.0"; elseif(C.imag==1) os<<"i"; elseif(C.imag==-1) os<<"-i"; elseos< (1)< } else { if(C.imag>-0.05&&C.imag<0.05) os< (1)< elseif(C.imag==1) os< (1)< elseif(C.imag==-1) os< (1)< elseif(C.imag>0) os< (1)< elseif(C.imag<0) os< (1)< } returnos; } ComplexComplex: : operator+(constComplex&C) { Complextmp; tmp.real=real+C.real; tmp.imag=imag+C.imag; returntmp; } ComplexComplex: : operator-(constComplex&C) { Complextmp; tmp.real=real-C.real; tmp.imag=imag-C.imag; returntmp; } ComplexComplex: : operator*(constComplex&C) { Complextmp; tmp.real=real*C.real-imag*C.imag; tmp.imag=real*C.imag+imag*C.real; returntmp; } ComplexComplex: : operator/(constComplex&C) { Complextmp; tmp.real=(real*C.real+imag*C.imag)/(C.real*C.real+C.imag*C.imag); tmp.imag=(imag*C.real-real*C.imag)/(C.real*C.real+C.imag*C.imag); returntmp; } intmain() { intcas; cin>>cas; while(cas--) { Complexc1,c2; charop; cin>>c1>>op>>c2; switch(op) { case'+': cout< case'-': cout< case'*': cout< case'/': cout< } } return0; } /*3249大数类*/ #include #include #include #include usingnamespacestd; classBigInteger { private: vector boolsign; public: BigInteger(constBigInteger&B){num=B.num;sign=B.sign;} BigInteger(intsize=1,bool_sign=true){while(size--)num.push_back(0);sign=_sign;} ~BigInteger(){} friendistream&operator>>(istream&is,BigInteger&B); friendostream&operator<<(ostream&os,constBigInteger&B); booloperator>(constBigInteger&B)const; booloperator<(constBigInteger&B)const; booloperator==(constBigInteger&B)const; BigIntegeroperator=(constBigInteger&B); BigIntegeroperator+(constBigInteger&B); BigIntegeroperator-(constBigInteger&B); BigIntegeroperator*(constBigInteger&B); BigIntegeroperator/(constBigInteger&B); }; istream&operator>>(istream&is,BigInteger&B) { B.num.clear(); stringstr; cin>>str; for(inti=0;i! =str.size();i++) B.num.push_back(str[i]-'0'); returnis; } ostream&operator<<(ostream&os,constBigInteger&B) { if(B.sign==false)os<<'-'; for(inti=0;i! =B.num.size();i++) os< returnos; } boolBigInteger: : operator>(constBigInteger&B)const { if(num.size()>B.num.size()) returntrue; if(num.size()==B.num.size()) { for(inti=0;i! =num.size();i++) if(num[i]>B.num[i]) returntrue; elseif(num[i] returnfalse; } returnfalse; } boolBigInteger: : operator<(constBigInteger&B)const { if(num.size() returntrue; if(num.size()==B.num.size()) { for(inti=0;i! =num.size();i++) if(num[i] returntrue; elseif(num[i]>B.num[i]) returnfalse; } returnfalse; } boolBigInteger: : operator==(constBigInteger&B)const { if(num==B.num&&sign==B.sign) returntrue; returnfalse; } BigIntegerBigInteger: : operator=(constBigInteger&B) { num=B.num; sign=B.sign; return*this; } BigIntegerBigInteger: : operator+(constBigInteger&B) { inti,j; intlen0=num.size(),len1=B.num.size(); intlen2=max(len0,len1); intt=len2; BigIntegerA(len2+1,true); for(i=len0-1,j=len1-1;i>=0&&j>=0;i--,j--) A.num[t--]=num[i]+B.num[j]; while(i>=0) A.num[t--]=num[i--]; while(j>=0) A.num[t--]=B.num[j--]; for(i=len2;i>=0;i--) { if(A.num[i]>=10) { A.num[i-1]+=A.num[i]/10; A.num[i]%=10; } } while(! A.num[0]&&A.num.size()>1) A.num.erase(A.num.begin()+0); returnA; } BigIntegerBigInteger: : operator-(constBigInteger&B) { boolsign=true; inti,j; intlen0=num.size(),len1=B.num.size(); BigIntegera(*this),b(B); if(a { swap(a,b); swap(len0,len1); sign=false; } BigIntegerA(len0,sign); for(i=len0-1,j=len1-1;j>=0;i--,j--) A.num[i]=a.num[i]-b.num[j]; while(i>=0) { A.num[i]=a.num[i]; i--; } for(i=len0-1;i>0;i--) { if(A.num[i]<0) { A.num[i]+=10; A.num[i-1]--; } } while(! A.num[0]&&A.num.size()>1) A.num.erase(A.num.begin()+0); returnA; } BigIntegerBigInteger: : operator*(constBigInteger&B) { inti,j; intlen0=num.size(),len1=B.num.size(); intlen2=len0+len1-1; BigIntegerA(len2,true); BigIntegera(*this),b(B); for(i=len0-1;i>=0;i--) for(j=len1-1;j>=0;j--) A.num[i+j]+=a.num[i]*b.num[j]; for(i=len2-1;i>0;i--) { if(A.num[i]>=10) { A.num[i-1]+=A.num[i]/10; A.num[i]%=10; } } while(! A.num[0]&&A.num.size()>1) A.num.erase(A.num.begin()+0); returnA; } BigIntegerBigInteger: : operator/(constBigInteger&B) { BigIntegerc; BigIntegerd(*this); intlen0=num.size(),len1=B.num.size(),lend=len1; inti1,i2,i,j,jj,carry,cf; j=0; for(i1=len1-1;i1 { if(lend { d.num[lend]=num[i1+1]; c.num.push_back(0);++j; ++lend; } elseif(lend==len1) { jj=1; for(i=0;i { if(d.num[i]>B.num[i]) break; elseif(d.num[i] { jj=0; break; } } if(jj==0) { d.num[lend]=num[i1+1]; c.num.push_back(0);++j; ++lend; continue; } } if(jj==1||lend>len1) { cf=jj=0; while(d.num[jj]<=0&&jj ++jj; if(lend-jj>len1)cf=1; elseif(lend-jj else { i2=0;cf=1; for(i=jj;i { if(d.num[i] { cf=0; break; } elseif(d.num[i]>B.num[i2]) { break; } ++i2; } }//else while(cf) { i2=len1-1;cf=0; for(i=lend-1;i>=lend-len1;--i) { d.num[i]=d.num[i]-B.num[i2]; if(d.num[i]<0) { d.num[i]=d.num[i]+10; carry=1; --d.num[i-1]; } elsecarry=0; --i2; } ++c.num[j];jj=0; while(d.num[jj]<=0&&jj ++jj; if(lend-jj>len1)cf=1; elseif(lend-jj else { i2=0;cf=1; for(i=jj;i { if(d.num[i] { cf=0; break; } elseif(d.num[i]>B.num[i2]) { break; } ++i2; } }//else }//while jj=0; while(d.num[jj]<=0&&jj ++jj; for(i=0;i d.num[i]=d.num[i+jj]; d.num[i]=num[i1+1]; lend=i+1; c.num.push_back(0); ++j; }//else }//for while(! c.num[0]&&c.num.size()>1) {c.num.erase(c.num.begin()+0);j--;} BigIntegerA(j,true); for(i=0;i A.num[i]=c.num[i]; returnA; } intmain() { BigIntegera,b; charop; while(cin>>a>>op>>b) { switch(op) { case'+': cout< case'-':
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 运算 重载