此程序实现了对复数的加减乘除,输入的是实部和虚部。我分别用了c=a+b和a+=b;两个方法共同实现,代码如下,如果逻辑及代码有遗漏,欢迎随时评论,如果输入3,1则进行3+i和3+i的和为6+2i
public class Complex {
//定义实部和虚部
private double a;
private double b;
public Complex(){
this(0.0,0.0);
}
public Complex(double a){
this(a,0.0);
}
public Complex(double a,double b){
this.a=a;
this.b=b;
}
public Complex(Complex c){
this(c.a,c.b);
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
//a+=b;
public Complex add(Complex one){
this.a=this.a+one.a;
this.b=this.b+one.b;
return this;
//或用这种方法返回return this.a+one.a
}
//c=a+b;
public Complex add1(Complex one,Complex another){
this.a=one.a+another.a;
this.b=one.b+another.b;
return this;
}
//a-=b;
//先构造一个复数的相反数方法
private Complex xiangfan(Complex one){
return new Complex(-one.a, -one.b);
}
//直接进行调用
public Complex jian(Complex one){
return this.add(xiangfan(one));
}
//c=a-b;
public Complex jian1(Complex one,Complex another){
return this.add1(xiangfan(one),xiangfan(another));
}
//a*=b;
public Complex cheng(Complex one){
double c=0.0;
double d=0.0;
c=this.a*one.a-this.b*one.b;
d=this.a*one.b+this.b*one.a;
this.a=c;
this.b=d;
return this;
}
//c=a*b;
public Complex cheng1(Complex one,Complex another){
return new Complex(one).cheng(another);
}
// a /= b;
// public static DIYcomplex mul(DIYcomplex one, DIYcomplex other) {
// return new DIYcomplex(one).cheng(other);
// }
public Complex div(Complex c) {
double mod = c.a * c.a - c.b * c.b;
double mod1=0.0;
double mod2=0.0;
mod1=(this.a*c.a - this.b*c.b)/mod;
mod2=(this.b*c.a - this.a*c.b)/mod;
this.a=mod1;
this.b=mod2;
//对除数进行判断是否等于0
if(Math.abs(mod) < 1e-6) {
return null;
}
return this;
}
@Override
public String toString() {
return "DIYcomplex [a=" + a + "+" + b + "i ]";
}
public static void main(String[] args) {
Complex c1 = new Complex(3.0,1.0);//此刻输入的是一个复数的实部与虚部,产生一个对象就是一个复数
System.out.println(c1);
Complex c2 = new Complex(3.0,1.0);
c2.add(c2);
System.out.println(c2);
Complex c3 = new Complex(3.0,1.0);
c3.jian(c3);
System.out.println(c3);
Complex c4 = new Complex(3,1);
c4.cheng(c4);
System.out.println(c4);
Complex c5 = new Complex(2.0,1.0);
c5.div(c5);
System.out.println(c5);
}
}
运行结果为
DIYcomplex [a=3.0+1.0i ]
DIYcomplex [a=6.0+2.0i ]
DIYcomplex [a=0.0+0.0i ]
DIYcomplex [a=8.0+6.0i ]
DIYcomplex [a=1.0+0.0i ]
最近又写了个复数的四则运算,此次采用了static静态方法,不仅使代码更加简洁,逻辑上更有了深的认识。而对于static的使用规则,我总结到只要是有一个新类出现就会使用它。代码如下:
public class Complex {
private double real;
private double virtual;
public Complex() {
}
public Complex(double real) {
this(real, 0.0);// 若只输入一个数就是虚部为0
}
public Complex(double real, double virtual) {
this.real = real;
this.virtual = virtual;
}
public Complex(Complex one) {
this.real = one.real;
this.virtual = one.virtual;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getVirtual() {
return virtual;
}
public void setVirtual(double virtual) {
this.virtual = virtual;
}
@Override
public String toString() {
return "Complex [real=" + real + ", virtual=" + virtual + "]";
}
// a=a+b;
public Complex add(Complex one) {
this.real = one.real + this.real;
this.virtual = one.virtual + this.virtual;
return this;
}
public static Complex xiangfan(Complex one) {
return new Complex(-one.real, -one.virtual);
}
// c=a+b
public static Complex add1(Complex one, Complex other) {
return new Complex(one).add(other);
}
// a=a-b
public Complex jian(Complex one) {
return this.add(xiangfan(one));
}
// c=a-b
public static Complex jian1(Complex one, Complex other) {
return new Complex(one).jian(other);
}
// a=a*b
public Complex cheng(Complex one) {
double c = 0.0;
double d = 0.0;
c = one.real * this.real - one.virtual * this.virtual;
d = this.real * one.virtual + one.real * this.virtual;
this.real = c;
this.virtual = d;
return this;
}
// c=a*b
public static Complex cheng1(Complex one, Complex other) {
return new Complex(one).cheng(other);
}
// 倒数
public static Complex daoshu(Complex one) {
double mod = one.real * one.real - one.virtual * one.virtual;
if (Math.abs(mod) < 1e-6) {
return null;
}
double tt = one.real / mod;
double dd = one.virtual / mod;
return new Complex(tt, -dd);
}
// c=a/b
public static Complex div1(Complex one, Complex other) {
return new Complex(one).cheng(daoshu(other));
}
// a=a/b
public Complex div11(Complex c) {
double mod = c.real * c.real - c.virtual * c.virtual;
double mod1 = 0.0;
double mod2 = 0.0;
mod1 = (this.real * c.real - this.virtual * c.virtual) / mod;
mod2 = (this.virtual * c.real - this.real * c.virtual) / mod;
this.real = mod1;
this.virtual = mod2;
if (Math.abs(mod) < 1e-6) {
return null;
}
return this;
}
}
测试类
package com.mec.Complex.core;
public class Text {
public static void main(String[] args) {
Complex complex=new Complex(3.0,1.0);
complex.add(complex);
System.out.println(complex);
Complex complex1=new Complex(3.0,1.0);
complex1.jian(complex1);
System.out.println(complex1);
Complex complex2=new Complex(3.0,1.0);
complex2.cheng(complex2);
System.out.println(complex2);
Complex complex3=new Complex(10,1);
complex3.div11(complex3);
System.out.println(complex3);
}
}
这是结果:
Complex [real=6.0, virtual=2.0]
Complex [real=0.0, virtual=0.0]
Complex [real=8.0, virtual=6.0]
Complex [real=1.0, virtual=0.0]