用java实现复数的加减乘除运算

                                                       用java实现复数的加减乘除运算

1. 题目内容

设计一个类Complex,用于封装对复数的下列操作:
(1)一个带参数的构造函数,用于初始化复数成员
(2)一个不带参数的构造函数,调用代参数的构造函数完成对复数成员的初始化。
(3)实现两个复数的加法,减法的静态方法和实例方法。
(4)以复数的标准形式:x+yi 输出此复数
(5) 写两个函数,分别获得复数的实部getReal(),getImage()和虚部。

老师原题如上,自己做了两个复数的加减乘除运算,使用的是实例方法。如果要写静态方法,即类方法,要加static,再根据相应变化修改。区别是:实例方法既可调用实例变量和实例方法,又可调用类变量和类方法。类方法只可调用类变量和类方法。因时间关系,明天还有课,自己就暂且写了实例。用java实现复数的加减乘除运算

注意下图的2个化简公式

用java实现复数的加减乘除运算

用java实现复数的加减乘除运算



转自 https://blog.****.net/sunkun2013/article/details/12765527


2. 具体代码与解释

  1. import java.util.Scanner;  
  2.   
  3. public class Complex { // 复数类  
  4.     double real;  // 实部  
  5.     double image; // 虚部  
  6.       
  7.     Complex(){  // 不带参数的构造方法  
  8.         Scanner input = new Scanner(System.in);  
  9.         double real = input.nextDouble();  
  10.         double image = input.nextDouble();  
  11.         Complex(real,image);  
  12.     }  
  13.   
  14.     private void Complex(double real, double image) { // 供不带参数的构造方法调用  
  15.         // TODO Auto-generated method stub  
  16.         this.real = real;  
  17.         this.image = image;  
  18.     }  
  19.   
  20.     Complex(double real,double image){ // 带参数的构造方法  
  21.         this.real = real;  
  22.         this.image = image;  
  23.     }  
  24.   
  25.     public double getReal() {  
  26.         return real;  
  27.     }  
  28.   
  29.     public void setReal(double real) {  
  30.         this.real = real;  
  31.     }  
  32.   
  33.     public double getImage() {  
  34.         return image;  
  35.     }  
  36.   
  37.     public void setImage(double image) {  
  38.         this.image = image;  
  39.     }  
  40.       
  41.     Complex add(Complex a){ // 复数相加  
  42.         double real2 = a.getReal();  
  43.         double image2 = a.getImage();  
  44.         double newReal = real + real2;  
  45.         double newImage = image + image2;  
  46.         Complex result = new Complex(newReal,newImage);  
  47.         return result;  
  48.     }  
  49.       
  50.     Complex sub(Complex a){ // 复数相减  
  51.         double real2 = a.getReal();  
  52.         double image2 = a.getImage();  
  53.         double newReal = real - real2;  
  54.         double newImage = image - image2;  
  55.         Complex result = new Complex(newReal,newImage);  
  56.         return result;  
  57.     }  
  58.       
  59.     Complex mul(Complex a){ // 复数相乘  
  60.         double real2 = a.getReal();  
  61.         double image2 = a.getImage();  
  62.         double newReal = real*real2 - image*image2;  
  63.         double newImage = image*real2 + real*image2;  
  64.         Complex result = new Complex(newReal,newImage);  
  65.         return result;  
  66.     }  
  67.       
  68.     Complex div(Complex a){ // 复数相除  
  69.         double real2 = a.getReal();  
  70.         double image2 = a.getImage();  
  71.         double newReal = (real*real2 + image*image2)/(real2*real2 + image2*image2);  
  72.         double newImage = (image*real2 - real*image2)/(real2*real2 + image2*image2);  
  73.         Complex result = new Complex(newReal,newImage);  
  74.         return result;  
  75.     }  
  76.       
  77.     public void print(){ // 输出  
  78.         if(image > 0){  
  79.             System.out.println(real + " + " + image + "i");  
  80.         }else if(image < 0){  
  81.             System.out.println(real + "" + image + "i");  
  82.         }else{  
  83.             System.out.println(real);  
  84.         }  
  85.     }  
  86. }  


  1. public class MainClass { // 用于测试复数类  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         System.out.println("请用户输入第一个复数的实部和虚部:");  
  9.         Complex data1 = new Complex();  
  10.         System.out.println("请用户输入第二个复数的实部和虚部:");  
  11.         Complex data2 = new Complex();  
  12.          
  13.         // 以下分别为加减乘除  
  14.         Complex result_add = data1.add(data2);  
  15.         Complex result_sub = data1.sub(data2);  
  16.         Complex result_mul = data1.mul(data2);  
  17.         Complex result_div = data1.div(data2);  
  18.           
  19.         result_add.print();  
  20.         result_sub.print();  
  21.         result_mul.print();  
  22.         result_div.print();  
  23.     }  
  24. }  
请用户输入第一个复数的实部和虚部:
5 4
请用户输入第二个复数的实部和虚部:
3 6
8.0 + 10.0i
2.0-2.0i
-9.0 + 42.0i

0.8666666666666667-0.4i