Java复数运算


描述

创建一个复数类Complex,用来执行复数的算数运算,并编写一个程序来测试该类。

(1)该类有两个成员变量,即实部(realPart)和虚部(imaginaryPart),均为实数类型

(2)定义一个构造函数,用来对类的对象进行初始化,构造函数的函数原理为:

       Complex(double realPart,double imaginaryPart);

(3)定义一个add成员函数,实现两个复数的加法

       Complex add(Complex);

(4)定义一个sub成员函数,实现两个复数的减法

       Complex sub(Complex);

(5)定义一个mul成员函数,实现两个复数的乘法

       Comple mul(Complex);

(6)定义一个div成员函数,实现两个复数的除法

       Complex div(Complex);

(7)定义一个print成员函数,打印当前复数运算结果,打印格式为:3.0+2.0i或-3.0-2.0i

测试用的main函数如下:

public static void main(String[] a) {
        Scanner input = new Scanner(System.in);
        int x1,y1,x2,y2;
        x1=input.nextInt();
        y1=input.nextInt();
        x2=input.nextInt();
        y2=input.nextInt();
        Complex c1 = new Complex(x1, y1);
        Complex c2 = new Complex(x2, y2);
        Complex c3=c1.add(c2);
        Complex c4=c1.sub(c2);
        Complex c5=c1.mul(c2);
        Complex c6=c1.div(c2);
        c3.print();
        c4.print();
        c5.print();
        c6.print();
    }

 

输入

输入占1行,分别是4个整数,用来构造两个复数

输出

输出占4行,分别为两个复数运算后的结果。

难度

入门

输入示例

3 4 1 -2

输出示例

4.0+2.0i
2.0+6.0i
11.0-2.0i
-1.0+2.0i
import java.util.Scanner;

public class Complex {
    double realPart;
    double imaginaryPart;
    public static void main(String[] a) {
        Scanner input = new Scanner(System.in);
        int x1,y1,x2,y2;
        x1=input.nextInt();
        y1=input.nextInt();
        x2=input.nextInt();
        y2=input.nextInt();
        Complex c1 = new Complex(x1, y1);
        Complex c2 = new Complex(x2, y2);
        Complex c3=c1.add(c2);
        Complex c4=c1.sub(c2);
        Complex c5=c1.mul(c2);
        Complex c6=c1.div(c2);
        c3.print();
        c4.print();
        c5.print();
        c6.print();
    }

    public double getRealPart() {
        return realPart;
    }

    public void setRealPart(double realPart) {
        this.realPart = realPart;
    }

    public double getImaginaryPart() {
        return imaginaryPart;
    }

    public void setImaginaryPart(double imaginaryPart) {
        this.imaginaryPart = imaginaryPart;
    }

    private void Complex(double realPart,double imaginaryPart){     // 供不带参数的构造方法调用
        this.realPart=realPart;
        this.imaginaryPart=imaginaryPart;
    }

    Complex(double realPart, double imaginaryPart){
        this.realPart=realPart;
        this.imaginaryPart=imaginaryPart;
    }

    Complex add(Complex a) {
        double real2 = a.getRealPart();
        double image2 = a.getImaginaryPart();
        double newReal = realPart + real2;
        double newImage = imaginaryPart + image2;
        Complex result = new Complex(newReal, newImage);
        return result;
    }

    Complex sub(Complex a){
        double real2 = a.getRealPart();
        double image2 = a.getImaginaryPart();
        double newReal = realPart - real2;
        double newImage = imaginaryPart - image2;
        Complex result = new Complex(newReal,newImage);
        return result;
    }

    Complex mul(Complex a) {
        double real2 = a.getRealPart();
        double image2 = a.getImaginaryPart();
        double newReal =  realPart*real2 - imaginaryPart*image2;
        double newImage = imaginaryPart*real2 + realPart*image2;
        Complex result = new Complex(newReal, newImage);
        return result;
    }

    Complex div(Complex a) {
        double real2 = a.getRealPart();
        double image2 = a.getImaginaryPart();
        double newReal = (realPart*real2 + imaginaryPart*image2)/(real2*real2 + image2*image2);
        double newImage = (imaginaryPart*real2 - realPart*image2)/(real2*real2 + image2*image2);
        Complex result = new Complex(newReal, newImage);
        return result;
    }

    public void print() {
        if (imaginaryPart > 0) {
            System.out.println(realPart + "+" + imaginaryPart + "i");
        } else if (imaginaryPart < 0) {
            System.out.println(realPart + "" + imaginaryPart + "i");//注意!
        } else {
            System.out.println(realPart);
        }
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM