1、java 编写的复数加减乘除secondComplex.javapackage secondComplex;public class secondComplex private double real; private double vir; public secondComplex() this(0.0,0.0); public secondComplex(double real, double vir) this.real = real; this.vir = vir; public secondComplex(secondComplex c) this.real = c.real; th
2、is.vir = c.vir; /求共轭复数 public static secondComplex Conjugate(secondComplex a) return new secondComplex(a.real, -a.vir); /求复数的相反数 public secondComplex oppositeComplex(secondComplex a) return new secondComplex(-a.real, -a.vir); /求复数的倒数 public static secondComplex Reciprocal(secondComplex a) secondComp
3、lex b = secondComplex.Conjugate(a); b.Multiplication(a); b.real = b.real +b.vir; a = secondComplex.Conjugate(a); if(Math.abs(b.real) 1e-6) b.real = 0; return null; return new secondComplex(a.real/b.real, a.vir/b.real); /* * this+=a * return * */ public void Add(secondComplex a) this.real += a.real;
4、this.vir += a.vir; /* * this-=a; */ public void Sub(secondComplex a) a = a.oppositeComplex(a); this.Add(a); /* * this*=a; */ public void Multiplication(secondComplex a) double tmp; tmp = this.real; this.real = this.real * a.real - this.vir * a.vir; this.vir = tmp * a.vir + this.vir * a.real; /* *thi
5、s /= b * return */ public void Division(secondComplex a) secondComplex b = Multiplication(this, secondComplex.Reciprocal(a); if(b = null) return; this.real = b.real; this.vir = b.vir; /* * * c = this + a * return */ public secondComplex Add(secondComplex a, int type) return Add(this, a); /* * c = th
6、is - a */ public secondComplex Sub(secondComplex a, int type) a = a.oppositeComplex(a); return Add(this, a); /* * c = this * a */ public secondComplex Multiplication(secondComplex a, int type) return Multiplication(this, a); /* *c = this / b * return */ public secondComplex Division(secondComplex a,
7、int type) return Division(this,a); /* * c = a + b * param a * param b * return */ public static secondComplex Add(secondComplex a, secondComplex b) return new secondComplex(a.real + b.real, a.vir + b.vir); /* * c = a - b * return */ public secondComplex Sub(secondComplex a, secondComplex b) b = b.op
8、positeComplex(b); return Add(a, b); /* * c = a * b * return */ public static secondComplex Multiplication(secondComplex a, secondComplex b) if(b = null) return null; return new secondComplex(a.real * b.real - a.vir * b.vir , a.real * b.vir + a.vir * b.real); /* *c = a / b * return */ public secondCo
9、mplex Division(secondComplex a, secondComplex b) if(secondComplex.Reciprocal(b) = null) return null; return Multiplication(a, secondComplex.Reciprocal(b); public double getReal() if(Math.abs(real) 1e-6) real = 0; return real; public void setReal(double real) this.real = real; public double getVir()
10、if(Math.abs(vir) 1e-6) vir = 0; return vir; public void setVir(double vir) this.vir = vir; public String toString() if(Math.abs(this.getReal() 1e-6 & Math.abs(this.getVir() 1e-6) System.out.println(0); else if(Math.abs(this.getReal() 1e-6) if(Math.abs(this.getVir() 0) System.out.println(i); else Sys
11、tem.out.println(-i); else System.out.println(this.getVir()+i); else if(Math.abs(this.getVir() 0) System.out.println(+ i); else System.out.println(- i); else if(Math.abs(this.getVir() 1e-6) System.out.println( + this.getVir()+i); else if(this.getVir() 0) System.out.println(this.getReal() + + + this.g
12、etVir() + i); else System.out.println(this.getReal() + - + Math.abs(this.getVir() + i); return ; Complex.javapackage secondComplex;public class Complex extends secondComplex private double mo; private double jiao; public Complex() this(1.0,Math.PI/4); public Complex(double mo, double jiao) super(Com
13、plex.returnX(mo, jiao), Complex.returnY(mo, jiao); public static double returnX(double mo, double jiao) return mo * Math.cos(jiao); public static double returnY(double mo, double jiao) return mo * Math.sin(jiao); public static double returnMo(double x, double y) x = Math.abs(x); y = Math.abs(y); if(
14、x 1e-6) x = 0; if(y 1e-6) y = 0; return Math.sqrt(x*x + y*y); public static double returnJiao(double x, double y) double tmp1; int st1; int st2; double tmp2; /横轴正向:1;反向:-1;原点:0 /纵轴正向:1;反向:-1;原点:0 if(Math.abs(x) 0) tmp1 = x; st1 = 1; else tmp1 = x; st1 = -1; if(Math.abs(y) 0) tmp2 = y; st2 = 1; else
15、tmp2 = y; st2 = -1; if(0 = st1 & 0 = st2) System.out.println(复数无辐角); return -1000; else if(0 = st1 & 0 != st2) if(st2 0) return Math.PI/2; else return -Math.PI/2; else if(st1 0 & tmp2 != 0.0) return Math.atan(tmp2/tmp1); else if(st1 0 & tmp2 != 0.0) return Math.atan(tmp2/tmp1)-Math.PI; return -1000;
16、 public String toString() if(Complex.returnJiao(this.getReal(), this.getVir() = -900) return ; return 计算结果模值为: + (double)(int)(Complex.returnMo(this.getReal(), this.getVir()*1000)/1000 + 角度为: + (double)(int)(Complex.returnJiao(this.getReal(), this.getVir()*1000)/1000; public double getMo() return mo
17、; public void setMo(double mo) this.mo = mo; public double getJiao() return jiao; public void setJiao(double jiao) this.jiao = jiao; /* * this+=a */ public void Add(Complex a) this.setReal(this.getReal() + a.getReal(); this.setVir(this.getVir() + a.getVir(); public void Sub(Complex a) secondComplex
18、tmp = a.oppositeComplex(new secondComplex(a.getReal(), a.getVir(); Complex d = new Complex(); d.setReal(tmp.getReal(); d.setVir(tmp.getVir(); this.Add(d); public void Multiplication(Complex a) this.setReal(this.getReal() * a.getReal() - this.getVir() * a.getVir(); this.setVir( this.getReal() * a.get
19、Vir() + this.getVir() * a.getReal(); public void Division(Complex a) secondComplex tmp = new secondComplex(a.getReal(), a.getVir(); /Complex b = Multiplication(this, secondComplex.Reciprocal(tmp); secondComplex b = secondComplex.Reciprocal(tmp); if(b = null) return; Complex c = new Complex(Complex.r
20、eturnMo(b.getReal(), b.getVir(), Complex.returnJiao(b.getReal(), b.getVir(); Complex d = Multiplication(this, c); this.setReal(d.getReal(); this.setVir(d.getVir(); /* * c = this + a * return */ public Complex Add(Complex a, int type) return Add(this, a); public Complex Sub(Complex c, int type) secon
21、dComplex a = new secondComplex(c.getReal(),c.getVir(); a = a.oppositeComplex(a); Complex d = new Complex(Complex.returnMo(a.getReal(), a.getVir(), Complex.returnJiao(a.getReal(), a.getVir(); return Add(this, d); public Complex Multiplication(Complex a, int type) return Multiplication(this, a); publi
22、c Complex Division(Complex a,int type) return Division(this,a); /* * c = a + b * return */ public static Complex Add(Complex a, Complex b) secondComplex c = new secondComplex(a.getReal() + b.getReal(), a.getVir() + b.getVir(); Complex d = new Complex(Complex.returnMo(c.getReal(), c.getVir(), Complex
23、.returnJiao(c.getReal(), c.getVir(); return d; public Complex Sub(Complex a, Complex d) secondComplex b = new secondComplex(d.getReal(),d.getVir(); b = b.oppositeComplex(b); Complex c = new Complex(Complex.returnMo(b.getReal(), b.getVir(), Complex.returnJiao(b.getReal(), b.getVir(); return Add(a, c)
24、; public static Complex Multiplication(Complex a, Complex b) if(b = null) return null; secondComplex bb = new secondComplex(a.getReal() * b.getReal() - a.getVir() * b.getVir() , a.getReal() * b.getVir() + a.getVir() * b.getReal(); Complex c = new Complex(Complex.returnMo(bb.getReal(), bb.getVir(), C
25、omplex.returnJiao(bb.getReal(), bb.getVir(); return c; public Complex Division(Complex a, Complex c) secondComplex b = new secondComplex(c.getReal(), c.getVir(); secondComplex f = secondComplex.Reciprocal(b); if(f = null) return null; Complex d = new Complex(Complex.returnMo(f.getReal(), f.getVir(),
26、 Complex.returnJiao(f.getReal(), f.getVir(); return Multiplication(a, d); demoSecondComplex.javapackage secondComplex;public class demoSecondComplex public static void main(String args) System.out.println(-mo jiao-); Complex a = new Complex(1.0, 0.5*Math.PI); Complex b = new Complex(2.0, 0.5*Math.PI); a.Add(b); System.out.println(a); a = b.Add(a, 0); System.out.println(a); Complex d = a.Add(b, a); System.out.println(d); a.Sub(b); System.out.print
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2