欢迎来到天天文库
浏览记录
ID:41420263
大小:59.92 KB
页数:8页
时间:2019-08-24
《作业_复数类的参考代码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#includeusingstd::ostream;classComplex{private:doublereal,image;public:Complex(doublerl=0,doubleimg=O):real(rl),image(img){}Complex(constComplex&c):real(c.real),image(c.image)}}doubleReal()const{returnreal;)doubleImage()const{returnimage;}voidReal(doubleii){real=r
2、l;}voidImage(doubleimg){image=img;}voidSet(doublex,doubley){real=x;image=y;}Complexoperator+(constComplex&c2)const;Complexoperator-(constComplex&c2)const;Complexoperator*(constComplex&c2)const;Complexoperator/(constComplex&c2)const;voidPrint()const;friendostream&opcrat(”vv(
3、ostrcam&out,constComplex&c);#endiflllllllllllllllllllllllllllllllllllllllll//Complex.CPPlllllllllllllllllllllllllllllllllllllllll#include#include"Complex.h”usingnamespacestd;ostream&openitoKvCostream&out,constComplex&c){c.Print();returnout;}voidComplex::Print()con
4、st{if(image==0){cout«real;}elseif(real==0){cout«image«T;}elseif(image>0){coinvv(vvrcalvv'+NvimagcvvTvv'):elseif(image<0)coutvvY'vvrealvvimagevvTvv')';ComplexComplex::operator+(constComplex&c2)const{Complexret;ret.Set(c2.real+reahc2.image+image);returnret;}ComplexComplex::op
5、crator-(constComplex&c2)constComplexComplex::operator*(constComplex&c2)const{Complexret;ret.real=this->real*c2.real-this->image*c2.image;ret.Image(this->Real()*c2.Image()+this->image*c2.Real());returnret;}ComplexComplex::operator/(constComplex&c2)const{doublem=c2.real*c2.re
6、al+c2.image*c2.image;doublex=(real*c2・real+image*c2・image)/m;doubley=(image*c2.real-real*c2.image)/m;returnComplex(x,y);lllllllllllllllllllllllllllllllllllllllllIITcst.hIllllllllllllllllllllllllllllllllllllllll#ifndefTEST_H#defineTEST_H//=====================voidTest_l();vo
7、idTest_l_l();voidTest_l_2();voidTest_2();voidTest_3();voidTest_4();voidTcst_4_l();voidTest_5();//=====================#endiflllllllllllllllllllllllllllllllllllllllll//Test.CPPlllllllllllllllllllllllllllllllllllllllll#include#include#include"Complex.h"usin
8、gnamespacestd;voidTest_l(){//测试当复数退化为实数时的四则运算coutvvT.O测试当复数退化为实数时的四则运算”vvendl;Comp
此文档下载收益归作者所有