欢迎来到天天文库
浏览记录
ID:39547366
大小:51.00 KB
页数:4页
时间:2019-07-06
《c++作业习题答案》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、作业六一,要求:编写一个复数类CComplex,能够满足以下主函数正常运行并产生正确结果,并验证其运算顺序:voidmain(){CComplexa(1,1),b(2),c(3,1),d(a),e;e+=(a+b)*(a+c++)-(b/c)+(d==c);cout<#includeusingnamespacestd;classCComplex//定义一个CComplex类{public:doubl
2、eReal,Image;CComplex(doublereal=0,doubleimage=0){//构造函数Real=real;Image=image;}CComplex(CComplex&p){Real=p.Real;Image=p.Image;}friendostream&operator<<(ostream&os,CComplex&com)//重载输出{if(fabs(com.Image)<0.0000000001)//如果虚部为0os<3、om.Image<<"j"<0)os<4、;//除法重载CComplexoperator+=(CComplex&com);//加法赋值重载CComplexoperator-=(CComplex&com);//减法赋值重载CComplexoperator*=(CComplex&com);//乘法赋值重载CComplexoperator/=(CComplex&com);//除法赋值重载CComplexoperator++(int);//自增赋值重载intoperator==(CComplex&com);};CComplexCComplex::operator+(CComplex&com)//加法重载{CComplexsum;sum.5、Real=Real+com.Real;//实部相加sum.Image=Image+com.Image;//虚部相加returnsum;}CComplexoperator+(constCComplex&com,constinti){CComplexsum;sum.Real=+icom.Real;//实部加ireturnsum;}CComplexCComplex::operator-(CComplex&com)//减法重载{CComplexsub;sub.Real=Real-com.Real;//实部相减sub.Image=Image-com.Image;//虚部相减returnsub;}C6、ComplexCComplex::operator*(CComplex&com)//乘法重载{CComplexmulti;multi.Real=Real*com.Real-Image*com.Image;//实部乘积multi.Image=Real*com.Image+Image*com.Real;//虚部乘积returnmulti;}CComplexCComplex::operator/(CComplex&com)//除法重载{CComplexdiv;div.Real=(Real*com.Real+Image*com.Image)/(com.Real*com.Real+com.Imag7、e*com.Image);//实部除积div.Image=(Image*com.Real-Real*com.Image)/(com.Real*com.Real+com.Image*com.Image);//虚部除积returndiv;}CComplexCComplex::operator+=(CComplex&com)//重载加法赋值{Real=Real+com.Real;//实部相加Image=Image+com.Image;//
3、om.Image<<"j"<0)os<4、;//除法重载CComplexoperator+=(CComplex&com);//加法赋值重载CComplexoperator-=(CComplex&com);//减法赋值重载CComplexoperator*=(CComplex&com);//乘法赋值重载CComplexoperator/=(CComplex&com);//除法赋值重载CComplexoperator++(int);//自增赋值重载intoperator==(CComplex&com);};CComplexCComplex::operator+(CComplex&com)//加法重载{CComplexsum;sum.5、Real=Real+com.Real;//实部相加sum.Image=Image+com.Image;//虚部相加returnsum;}CComplexoperator+(constCComplex&com,constinti){CComplexsum;sum.Real=+icom.Real;//实部加ireturnsum;}CComplexCComplex::operator-(CComplex&com)//减法重载{CComplexsub;sub.Real=Real-com.Real;//实部相减sub.Image=Image-com.Image;//虚部相减returnsub;}C6、ComplexCComplex::operator*(CComplex&com)//乘法重载{CComplexmulti;multi.Real=Real*com.Real-Image*com.Image;//实部乘积multi.Image=Real*com.Image+Image*com.Real;//虚部乘积returnmulti;}CComplexCComplex::operator/(CComplex&com)//除法重载{CComplexdiv;div.Real=(Real*com.Real+Image*com.Image)/(com.Real*com.Real+com.Imag7、e*com.Image);//实部除积div.Image=(Image*com.Real-Real*com.Image)/(com.Real*com.Real+com.Image*com.Image);//虚部除积returndiv;}CComplexCComplex::operator+=(CComplex&com)//重载加法赋值{Real=Real+com.Real;//实部相加Image=Image+com.Image;//
4、;//除法重载CComplexoperator+=(CComplex&com);//加法赋值重载CComplexoperator-=(CComplex&com);//减法赋值重载CComplexoperator*=(CComplex&com);//乘法赋值重载CComplexoperator/=(CComplex&com);//除法赋值重载CComplexoperator++(int);//自增赋值重载intoperator==(CComplex&com);};CComplexCComplex::operator+(CComplex&com)//加法重载{CComplexsum;sum.
5、Real=Real+com.Real;//实部相加sum.Image=Image+com.Image;//虚部相加returnsum;}CComplexoperator+(constCComplex&com,constinti){CComplexsum;sum.Real=+icom.Real;//实部加ireturnsum;}CComplexCComplex::operator-(CComplex&com)//减法重载{CComplexsub;sub.Real=Real-com.Real;//实部相减sub.Image=Image-com.Image;//虚部相减returnsub;}C
6、ComplexCComplex::operator*(CComplex&com)//乘法重载{CComplexmulti;multi.Real=Real*com.Real-Image*com.Image;//实部乘积multi.Image=Real*com.Image+Image*com.Real;//虚部乘积returnmulti;}CComplexCComplex::operator/(CComplex&com)//除法重载{CComplexdiv;div.Real=(Real*com.Real+Image*com.Image)/(com.Real*com.Real+com.Imag
7、e*com.Image);//实部除积div.Image=(Image*com.Real-Real*com.Image)/(com.Real*com.Real+com.Image*com.Image);//虚部除积returndiv;}CComplexCComplex::operator+=(CComplex&com)//重载加法赋值{Real=Real+com.Real;//实部相加Image=Image+com.Image;//
此文档下载收益归作者所有