复数的四则运算
下面的代码是用Java编写的,主要是Complex的四则运算
该类主要负责实现复数的四则运算,加减乘除
每种运算都使用了两种方式,类似a+=b,c=a+b
减法使用加法表示,除法使用乘法表示,
包括取反,和取倒数
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
//取反
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
public Complex add(Complex a) {
this.real+=a.real;
this.vir+=a.vir;
return this;
}
//c=a+b,调用add(Complex a)
public static Complex add(Complex a,Complex b) {
return new Complex(a).add(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
public Complex add(Complex a) {
this.real+=a.real;
this.vir+=a.vir;
return this;
}
//c=a+b,调用add(Complex a)
public static Complex add(Complex a,Complex b) {
return new Complex(a).add(b);
}
//减法
//a-=b
public Complex sub(Complex a) {
return add(invert(a));
}
//c=a-b
public static Complex sub(Complex a,Complex b) {
return new Complex(a).add(invert(a));
}
//乘法
//a*=b
//实部:ac-bd 虚部:(ad+bc)i
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)格式输出复数
}
}
下面为测试类,为了便于计算,只调用了双参的四则运算,即类似于c=a+b
在下面的测试类中,我使用Complex类直接调用了四则运算的方法,这种写法是可以的,但有一个前提,直接使用类名调用方法时,要求该方法为静态,即使用static修饰
public class Test {
public static void main(String[] args) {
Complex c1 = new Complex(1, -6);
Complex c2 = new Complex(2, 6);
System.out.println("加法");
Complex c3 = Complex.add(c1, c2);
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);
}
}
不使用类名调用,add(Complex a,Complex b)就可以不使用“static”修饰。下面以加法为例,代码如下:
System.out.println("加法");
Complex c3 = new Complex();
c3.add(c1, c2);
System.out.println(c3);
运行结果如下: