📄 complex.h
字号:
#include"iostream.h"
template<class T>
class Complex
{
private:
T x,y;
public:
Complex(T a,T b)
{
x=a,y=b;
}
Complex(){} //default constructor
Complex(Complex& a) //copy construct
{
x=a.x,y=a.y;
}
~Complex()
{
//nothing shoud be done
}
Complex & operator=(const Complex &op)
{
x=op.x,y=op.y;
return *this;
}
friend const Complex operator+(const Complex &opa,const Complex &opb)
{
T a=opa.x+opb.x;
T b=opa.y+opb.y;
Complex c(a,b);
return c;
}
friend const Complex operator-(const Complex &opa,const Complex &opb)
{
T a=opa.x-opb.x;
T b=opa.y-opb.y;
Complex c(a,b);
return c;
}
friend const Complex operator*(const Complex &opa,const Complex &opb)
{
T a=opa.x*opb.x-opa.y*opb.y;
T b=opa.x*opb.y+opa.y*opb.x;
Complex c(a,b);
return c;
}
friend const Complex operator/(const Complex &opa,const Complex &opb)
{
if(opb.x==0&&opb.y==0)
{
cout<<"除法错误"<<"\n";
Complex f;
return f;
}
T a=(opa.x*opb.x+opa.y*opb.y)/(opb.x*opb.x+opb.y*opb.y);
T b=(opa.y*opb.x-opa.x*opb.y)/(opb.x*opb.x+opb.y*opb.y);
Complex c(a,b);
return c;
}
friend ostream& operator<<(ostream& out,Complex& op)
{
out<<op.x<<(op.y>0?"+":"")<<op.y<<"i\t";
return out;
}
friend istream& operator>>(istream& in,Complex& op)
{
in>>op.x;
in>>op.y;
return in;
}
}
;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -