複數的四則運算法則:(c1:-7+5i,c2:5-3i)java
運行結果:算法
1 package test02; 2 import java.util.*; 3 public class Complex { 4 private double a; 5 private double b; 6 public double getA() { 7 return a; 8 } 9 public void setA(double a) { 10 this.a = a; 11 } 12 public double getB() { 13 return b; 14 } 15 public void setB(double b) { 16 this.b = b; 17 } 18 public Complex(double x,double y) 19 { 20 this.a=x; 21 this.b=y; 22 } 23 public Complex complexAdd(Complex c) 24 { 25 Complex add=new Complex(this.a+c.a, this.b+c.b); 26 return add; 27 } 28 public Complex complexMinus(Complex c) 29 { 30 Complex minus=new Complex(this.a-c.a, this.b-c.b); 31 return minus; 32 } 33 public Complex complexMultiplicatin(Complex c) 34 { 35 Complex multiplicatin=new Complex(this.a*c.a-this.b*c.b, this.b*c.a+this.a*c.b); 36 return multiplicatin; 37 } 38 public Complex complexDivision(Complex c) 39 { 40 Complex division=new Complex((this.a*c.a+this.b*c.b)/(c.a*c.a+c.b*c.b), (this.b*c.a-this.a*c.b)/(c.a*c.a+c.b*c.b)); 41 return division; 42 } 43 public String toString() 44 { 45 if(this.b<0) 46 return this.a+"-"+(-this.b)+"i"; 47 return this.a+"+"+this.b+"i"; 48 } 49 public static void main(String[] args) { 50 // TODO Auto-generated method stub 51 System.out.println("輸入c1的實部和虛部:"); 52 Scanner in=new Scanner(System.in); 53 double a1=in.nextDouble(); 54 double a2=in.nextDouble(); 55 System.out.println("輸入c2的實部和虛部:"); 56 double a11=in.nextDouble(); 57 double a22=in.nextDouble(); 58 System.out.println("c1:"+a1+"+("+a2+")i"); 59 System.out.println("c2:"+a11+"+("+a22+")i"); 60 Complex c1=new Complex(a1,a2); 61 Complex c2=new Complex(a11,a22); 62 System.out.println("c1和c2的運算結果:"); 63 System.out.println("c1+c2="+c1.complexAdd(c2)); 64 System.out.println("c1-c2="+c1.complexMinus(c2)); 65 System.out.println("c1*c2="+c1.complexMultiplicatin(c2)); 66 System.out.println("c1/c2="+c1.complexDivision(c2)); 67 } 68 69 }