# 复数的四则运算

1.复数必须包含两个变量，实部（real）虚部（vir）,为方便外界调用，给予对应的Getter和Setter方法，同时，给予四种构造方法，无参，单参，双参，对象参

	private double real;
private double vir;

public Complex() {
}

public Complex(double real) {
this.real = real;
}

public Complex(double real, double vir) {
this.real = real;
this.vir = vir;
}

public Complex(Complex complex) {
this(complex.real,complex.vir);
}

public double getReal() {
return real;
}

public void setReal(double real) {
this.real = real;
}

public double getVir() {
return vir;
}

public void setVir(double vir) {
this.vir = vir;
}


2.为了实现减法用加法表示，除法用乘法表示，给出取反和取倒

	//取反
private static  Complex invert(Complex a) {
return new Complex(-a.real,-a.vir);
}

/*取倒数
*  1/(a+bi)
* (a-bi)/((a+bi)(a-bi))
* (a-bi)/(a*a + b*b)
* 实部:a/(a*a + b*b)；虚部:-b/(a*a + b*b)
*/
private static Complex reciprocal(Complex a) {
if(a.real*a.real+a.vir*a.vir - 0 < 1e-6)
throw new DivZeroException("除0错");
//此处可直接抛RunTimeException()异常，DivZeroException()异常为我自行定义，继承了RunTimeException()
return new Complex(a.real/(a.real*a.real+a.vir*a.vir),-(a.vir/(a.real*a.real+a.vir*a.vir)));
}


3.每种四则运算皆有两种表示方式，类似a+=b,c=a+b,下面以加法为例

	//a+=b
this.real+=a.real;
this.vir+=a.vir;
return this;
}

public static Complex add(Complex a,Complex b) {
}


4.重写equals方法和toString方法，输出格式为(real,vir)

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (obj.getClass() != this.getClass()) {
return false;
}

Complex c = (Complex) obj;

return Math.abs(c.real - this.real) < 1e-6 && Math.abs(c.vir - this.vir) < 1e-6;
}

@Override
public String toString() {
return "("+real+","+vir+")";//用（real,vir）格式输出复数
}



public class Complex {
private double real;
private double vir;

public Complex() {
}

public Complex(double real) {
this.real = real;
}

public Complex(double real, double vir) {
this.real = real;
this.vir = vir;
}

public Complex(Complex complex) {
this(complex.real,complex.vir);
}

public double getReal() {
return real;
}

public void setReal(double real) {
this.real = real;
}

public double getVir() {
return vir;
}

public void setVir(double vir) {
this.vir = vir;
}

//取反
private static  Complex invert(Complex a) {
return new Complex(-a.real,-a.vir);
}

/*取倒数
*  1/(a+bi)
* (a-bi)/((a+bi)(a-bi))
* (a-bi)/(a*a + b*b)
* 实部:a/(a*a + b*b)；虚部:-b/(a*a + b*b)
*/
private static Complex reciprocal(Complex a) {
if(a.real*a.real+a.vir*a.vir - 0 < 1e-6)
throw new DivZeroException("除0错");
//此处可直接抛RunTimeException()异常，DivZeroException()异常为我自行定义，继承了RunTimeException()
return new Complex(a.real/(a.real*a.real+a.vir*a.vir),-(a.vir/(a.real*a.real+a.vir*a.vir)));
}

//加法
//a+=b
this.real+=a.real;
this.vir+=a.vir;
return this;
}

public static Complex add(Complex a,Complex b) {
}

//减法
//a-=b
public Complex sub(Complex a) {
}

//c=a-b
public static  Complex sub(Complex a,Complex b) {
}

//乘法
//a*=b
public Complex mul(Complex a) {
double real1 = this.real;
double real2 = a.real;
double vir1 = this.vir;
double vir2=a.vir;
this.real = real1*real2-vir1*vir2;
this.vir = real1*vir2+real2*vir1;
return this;
}

//c=a*b
public static Complex mul(Complex a,Complex b) {
return new Complex(a).mul(b);
}

//a/=b
public Complex div(Complex a) {
return mul(reciprocal(a));
}

//c=a/b
public static Complex div(Complex a,Complex b) {
return new Complex(a).mul(reciprocal(b));
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (obj.getClass() != this.getClass()) {
return false;
}

Complex c = (Complex) obj;

return Math.abs(c.real - this.real) < 1e-6 && Math.abs(c.vir - this.vir) < 1e-6;
}

//输出
@Override
public String toString() {
return "("+real+","+vir+")";//用（real,vir）格式输出复数
}

}



public class Test {

public static void main(String[] args) {
Complex c1 = new Complex(1, -6);
Complex c2 = new Complex(2, 6);

System.out.println("加法");
System.out.println(c3);

System.out.println("减法");
Complex c4 =Complex.sub(c1, c2);
System.out.println(c4);

System.out.println("乘法");
Complex c5 = Complex.mul(c1, c2);
System.out.println(c5);

System.out.println("除法");
Complex c6 = Complex.div(c1, c2);
System.out.println(c6);

}

}



	System.out.println("加法");
Complex c3 = new Complex();