class complex
{
private:
int real = 0;
int imag = 0;
public:
complex(int a, int b)
{
real = a;
imag = b;
}
int get_real()
{
return real;
}
int get_imag()
{
return imag;
}
friend complex operator + (complex first, complex second);
friend complex operator - (complex first, complex second);
friend complex operator * (complex first, complex second);
friend complex operator / (complex first, complex second);
};
complex operator + (complex first, complex second)
{
int r = first.real + second.real;
int i = first.imag + second.imag;
return complex(r, i);
}
complex operator - (complex first, complex second)
{
int r = first.real - second.real;
int i = first.imag - second.imag;
return complex(r, i);
}
complex operator * (complex first, complex second)
{
//r=ac-bd
int r = first.real*second.real - first.imag*second.imag;
//i=bc+ad
int i = first.real*second.imag + first.imag*second.real;
return complex(r, i);
}
complex operator / (complex first, complex second)
{
//r=(ac+bd)/(c*c+d*d)
int r = (first.real*second.real + first.imag*second.imag) / (second.real*second.real + second.imag*second.imag);
//i=(bc-ad)/(c*c+d*d)
int i = (first.imag*second.real - first.real*second.imag) / (second.real*second.real + second.imag*second.imag);
return complex(r, i);
}
A simple C++ complex class
最新推荐文章于 2022-05-05 18:53:20 发布