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

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

1. 题目内容

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

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

注意下图的2个化简公式





转自 https://blog.csdn.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



  • 8
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java 中,我们可以定义一个复数类来实现复数加减乘除运算。下面是一个简单的复数类的实现: ```java public class Complex { private double real; private double imag; public Complex(double real, double imag) { this.real = real; this.imag = imag; } public Complex add(Complex other) { double real = this.real + other.real; double imag = this.imag + other.imag; return new Complex(real, imag); } public Complex subtract(Complex other) { double real = this.real - other.real; double imag = this.imag - other.imag; return new Complex(real, imag); } public Complex multiply(Complex other) { double real = this.real * other.real - this.imag * other.imag; double imag = this.real * other.imag + this.imag * other.real; return new Complex(real, imag); } public Complex divide(Complex other) { double denominator = other.real * other.real + other.imag * other.imag; double real = (this.real * other.real + this.imag * other.imag) / denominator; double imag = (this.imag * other.real - this.real * other.imag) / denominator; return new Complex(real, imag); } public double getReal() { return real; } public double getImag() { return imag; } @Override public String toString() { return real + " + " + imag + "i"; } } ``` 在上面的代码中,我们定义了一个 Complex 类,它包含实部和虚部两个成员变量。这个类还包含了四个方法:add、subtract、multiply 和 divide,分别用于实现复数的加、减、乘和除运算。这个类还提供了 getter 方法来获取实部和虚部的值,以及一个 toString 方法来将复数转换成字符串形式。 下面是一个使用这个类的例子: ```java public class Main { public static void main(String[] args) { Complex a = new Complex(1, 2); Complex b = new Complex(3, 4); Complex sum = a.add(b); Complex diff = a.subtract(b); Complex product = a.multiply(b); Complex quotient = a.divide(b); System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("a + b = " + sum); System.out.println("a - b = " + diff); System.out.println("a * b = " + product); System.out.println("a / b = " + quotient); } } ``` 在上面的代码中,我们创建了两个复数对象 a 和 b,并分别进行了加、减、乘和除运算,并将结果输出到控制台上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值