欢迎来到天天文库
浏览记录
ID:17909320
大小:45.50 KB
页数:4页
时间:2018-09-09
《作业·复数类的参考代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、复数类的参考代码(功能不完整,有待补充
2、
3、VC++2005编译测试)///////////////////////////////////////////Complex.h/////////////////////////////////////////#ifndefCOMPLEX_H#defineCOMPLEX_H//============================================#includeusingstd::ostream;classComplex{private:doublereal,image;
4、public:Complex(doublerl=0,doubleimg=0):real(rl),image(img){}Complex(constComplex&c):real(c.real),image(c.image){}doubleReal()const{returnreal;}doubleImage()const{returnimage;}voidReal(doublerl){real=rl;}voidImage(doubleimg){image=img;}voidSet(doublex,doubley){real=x;image=y;}Com
5、plexoperator+(constComplex&c2)const;Complexoperator-(constComplex&c2)const;Complexoperator*(constComplex&c2)const;Complexoperator/(constComplex&c2)const;voidPrint()const;friendostream&operator<<(ostream&out,constComplex&c);};//============================================#endif//
6、/////////////////////////////////////////Complex.CPP/////////////////////////////////////////#include#include"Complex.h"usingnamespacestd;ostream&operator<<(ostream&out,constComplex&c){c.Print();returnout;}voidComplex::Print()const{if(image==0){cout<7、=0){cout<0){cout<<'('<8、9、VC++2005编译测试){cout<<'('<10、Complex::operator-(constComplex&c2)const{returnComplex(real-c2.real,image-c2.image);}ComplexComplex::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::op11、erator/(constComplex&c2)const{doublem=c2.real*c2.real+c2.image*c2.image;doublex=(real*c2.real+image*c2.image)/m;doubley=(image*c2.real-real*c2.image)/m;returnComplex(x,y);}///////////////////////////////////////////Test.h/////////////////////////////////////////#ifndefTEST_H#def12、ineTEST_H//=====================voidTest_1();vo
7、=0){cout<0){cout<<'('<8、9、VC++2005编译测试){cout<<'('<10、Complex::operator-(constComplex&c2)const{returnComplex(real-c2.real,image-c2.image);}ComplexComplex::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::op11、erator/(constComplex&c2)const{doublem=c2.real*c2.real+c2.image*c2.image;doublex=(real*c2.real+image*c2.image)/m;doubley=(image*c2.real-real*c2.image)/m;returnComplex(x,y);}///////////////////////////////////////////Test.h/////////////////////////////////////////#ifndefTEST_H#def12、ineTEST_H//=====================voidTest_1();vo
8、
9、VC++2005编译测试){cout<<'('<10、Complex::operator-(constComplex&c2)const{returnComplex(real-c2.real,image-c2.image);}ComplexComplex::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::op11、erator/(constComplex&c2)const{doublem=c2.real*c2.real+c2.image*c2.image;doublex=(real*c2.real+image*c2.image)/m;doubley=(image*c2.real-real*c2.image)/m;returnComplex(x,y);}///////////////////////////////////////////Test.h/////////////////////////////////////////#ifndefTEST_H#def12、ineTEST_H//=====================voidTest_1();vo
10、Complex::operator-(constComplex&c2)const{returnComplex(real-c2.real,image-c2.image);}ComplexComplex::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::op
11、erator/(constComplex&c2)const{doublem=c2.real*c2.real+c2.image*c2.image;doublex=(real*c2.real+image*c2.image)/m;doubley=(image*c2.real-real*c2.image)/m;returnComplex(x,y);}///////////////////////////////////////////Test.h/////////////////////////////////////////#ifndefTEST_H#def
12、ineTEST_H//=====================voidTest_1();vo
此文档下载收益归作者所有