📄 ex5_7_1.cpp
字号:
#include <iostream.h>
#include <math.h>
class Complex{
double Real,Image ;
public :
Complex(double r=0.0, double i=0.0):Real(r),Image(i){}//定义构造函数
Complex(Complex &com){
Real=com.Real ; Image=com.Image ;
}//定义拷贝构造函数
void Print(){
cout<<"Real="<<Real<<'\t'<<"Image="<<Image<<'\n';
}
friend Complex operator+(const Complex &,const Complex &);
void operator+=(Complex);
double abs(void);
friend Complex operator*(const Complex &,const Complex &);
friend Complex operator/(const Complex &,const Complex &);
};
Complex operator+(const Complex & c1,const Complex & c2){
return Complex(c1.Real+c2.Real,c1.Image+c2.Image);
}//隐式说明局部对象
void Complex::operator+=(Complex c){
Real=Real+c.Real ;
Image=Image+c.Image ;
}
double Complex::abs(void){
return sqrt(Real*Real+Image*Image);
}
Complex operator*(const Complex & c1,const Complex & c2){
return Complex(c1.Real*c2.Real-c1.Image*c2.Image ,c1.Real*c2.Image+c2.Real*c1.Image);
}
Complex operator/(const Complex & c1,const Complex & c2){
double d=c2.Real*c2.Real+c2.Image*c2.Image ;
return Complex((c1.Real*c2.Real+c1.Image*c2.Image)/d , (c1.Image*c2.Real-c1.Real*c2.Image)/d) ;
}
void main(void){
Complex c1(1.0,1.0) , c2(2.0,2.0) , c3(4.0,4.0) , c;
double d=0.5 ;
c1.Print();
c=c2+c3 ;
c.Print() ;
c+=c1 ;
c.Print() ;
c=c+d ;
c.Print() ;
c=d+c ;
c.Print() ;
c=c3*c2 ;
c.Print() ;
c=c3/c1 ;
c.Print() ;
c=c3*d;
c.Print() ;
c=c3/d;
c.Print() ;
cout<<"c3的模为:"<<c3.abs()<<endl ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -