📄 complex.cpp
字号:
/*#include <iostream.h>
class complex2
{
public:
complex2() { real=imag=0; }
complex2(double r, double i)
{\
real = r, imag = i;
}
complex2 operator +(const complex2 &c);
complex2 operator -(const complex2 &c);
complex2 operator *(const complex2 &c);
complex2 operator /(const complex2 &c);
friend void print(const complex2 &c);
private:
double real, imag;
}; */
#include "complex.h"
//#include "stdafx.h"
//operations about one operator between two complex2s;
complex2 complex2::operator +(const complex2 &c)
{
return complex2(real + c.real, imag + c.imag);
}
complex2 complex2::operator -(const complex2 &c)
{
return complex2(real - c.real, imag - c.imag);
}
complex2 complex2::operator *(const complex2 &c)
{
return complex2(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
}
complex2 complex2::operator /(const complex2 &c)
{
return complex2((real * c.real + imag * c.imag) / (c.real * c.real + c.imag * c.imag),
(imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag));
}
complex2 complex2::operator =(const complex2 &c)
{
real=c.real;
imag=c.imag;
return *this;
}
//operations between a complex2 and a value;
complex2 complex2::operator =(const double _val)
{
real=_val;
imag=0;
return *this;
}
complex2 complex2::operator *=(const double _val)
{
real*=_val;
imag*=_val;
return *this;
}
complex2 complex2::operator *( const double _val)
{
complex2 c;
c.real=_val*real;
c.imag=_val*imag;
return c;
}
complex2 complex2::operator /(const double _val)
{
return(complex2(real/_val,imag/_val));
}
complex2 complex2::operator +(const double _val)
{
return(complex2(_val+real,imag));
}
complex2 complex2::operator +=(const double _val)
{
real+=_val;
imag=imag;
return *this;
}
// operations about two operators between two complex
const complex2 &complex2::operator +=(const complex2 &c)
{
real += c.real; imag += c.imag;
return *this;
}
const complex2 &complex2::operator -=(const complex2 &c)
{
real -= c.real; imag -= c.imag;
return *this;
}
const complex2 &complex2::operator *=(const complex2 &c)
{
complex2 temp=*this;
real=temp.real * c.real - temp.imag * c.imag;
imag=temp.real * c.imag + temp.imag * c.real;
return *this;
}
const complex2 &complex2::operator /=(const complex2 &c)
{
complex2 temp=*this;
real=(temp.real * c.real + temp.imag * c.imag) / (c.real * c.real + c.imag * c.imag);
imag=(temp.imag * c.real - temp.real * c.imag) / (c.real * c.real + c.imag * c.imag);
return *this;
}
const int complex2::operator ==(const complex2 &c)
{
if((real==c.real)&&(imag==c.imag))
return 1;
else return 0;
}
int complex2::IsZero()
{
if(imag==0&&real==0)
return 1;
else
return 0;
}
void print(const complex2 &c)
{
if(c.imag<0)
{
if(c.real==0) cout<<c.imag<<'i';
else cout<<c.real<<c.imag<<'i';
}
else if(c.imag==0)
{
if(c.real==0) cout<<0;
else cout<<c.real;
}
else
{
if(c.real==0) cout<<c.imag<<'i';
else cout<<c.real<<'+'<<c.imag<<'i';
}
}
complex2 complex2::conjugate(void)
{
return(complex2(real,0-imag));
}
double complex2::magnitude2(void)
{
return(real*real+imag*imag);
}
/*complex2 complex2::transfer(double _val)
{
return(complex2 (_val,0));
}*/
double complex2::get_real(void)
{
return real;
}
double complex2::get_image(void)
{
return imag;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -