/*****************************
 * 自定义复数类
 * add by yxb
 * 2021-12-14
 ****************************/
package com.njcn.utils.sagDomain;

public class Complex {
    public double realPart;//复数实部
    public double imaginaryPart;//复数虚部

    public Complex()//复数类的无参构造方法
    {
        this.imaginaryPart=0;
        this.realPart=0;
    }

    public Complex(double realPart,double imaginaryPart)//复数类的有参构造方法
    {
        this.imaginaryPart=imaginaryPart;
        this.realPart=realPart;
    }

    //复数实部和虚部的getter和setter方法
    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;
    }
    //复数加法(a+bi)+(c+di)=(a+c)+(b+d)i
    public Complex add(Complex a,Complex b)
    {
        //a复数的值
        double a_real = 0;
        double a_imaginary = 0;
        //b复数的值
        double b_real = 0;
        double b_imaginary = 0;
        if(a != null){
            a_real = a.realPart;
            a_imaginary =a.imaginaryPart;
        }
        if(b != null){
            b_real = b.realPart;
            b_imaginary =b.imaginaryPart;
        }
        return  new Complex(a_real + b_real,a_imaginary + b_imaginary);
    }

    //复数减法(a+bi)-(c+di)=(a-c)+(b-d)i
    public Complex subduction(Complex a,Complex b)
    {
        //a复数的值
        double a_real = 0;
        double a_imaginary = 0;
        //b复数的值
        double b_real = 0;
        double b_imaginary = 0;
        if(a != null){
            a_real = a.realPart;
            a_imaginary =a.imaginaryPart;
        }
        if(b != null){
            b_real = b.realPart;
            b_imaginary =b.imaginaryPart;
        }
        return new Complex(a_real - b_real,a_imaginary - b_imaginary);
    }
    //复数乘法(a+bi)*(c+di)=(ac-bd)+(ad+bc)i
    public Complex multiplication(Complex a,Complex b)
    {
        //a复数的值
        double a_real = 0;
        double a_imaginary = 0;
        //b复数的值
        double b_real = 0;
        double b_imaginary = 0;
        if(a != null){
            a_real = a.realPart;
            a_imaginary =a.imaginaryPart;
        }
        if(b != null){
            b_real = b.realPart;
            b_imaginary =b.imaginaryPart;
        }
        return new Complex((a_real*b_real-a_imaginary*b_imaginary),(a_real*b_imaginary+a_imaginary*b_real));
    }
    //复数除法(a+bi)/(c+di)=(ac+bd)/(c²+d²)+((bc-ad)/(c²+d²))i
    public Complex dividion(Complex a,Complex b)
    {
        //a复数的值
        double a_real = 0;
        double a_imaginary = 0;
        //b复数的值
        double b_real = 0;
        double b_imaginary = 0;
        if(a != null){
            a_real = a.realPart;
            a_imaginary =a.imaginaryPart;
        }
        if(b != null){
            b_real = b.realPart;
            b_imaginary =b.imaginaryPart;
        }

        Complex cmp_result = new Complex(0,0);
        if(b_real == 0 && b_imaginary == 0)//排除除数不能为零的情况
        {
            System.out.println("除数不能为零");
        }
        else{
            cmp_result = new Complex(((a_real*b_real+a_imaginary*b_imaginary)/(b_real*b_real+b_imaginary*b_imaginary)),((a_imaginary*b_real-a_real*b_imaginary)/(b_real*b_real+b_imaginary*b_imaginary)));
        }
        return cmp_result;
    }

    //复数的模
    public static double module(Complex a)
    {
        //a复数的值
        double a_real = 0;
        double a_imaginary = 0;
        if(a != null){
            a_real = a.realPart;
            a_imaginary =a.imaginaryPart;
        }

        double x=a_real*a_real+a_imaginary*a_imaginary;
        return Math.sqrt(x);
    }
    //打印输出
    public void print()
    {
        if(this.imaginaryPart<0)//当复数的虚部为负数时，避免多打印出一个"+"
        {
            System.out.println(this.realPart+""+this.imaginaryPart+"i");
        }
        else
        {
            System.out.println(this.realPart+"+"+this.imaginaryPart+"i");
        }
    }

    // 复数测试
    public static void main(String[] args)
    {
        Complex com1=new Complex(1,2);
        com1.print();
        Complex com2=new Complex(3,4);
        com2.print();
        Complex com3=new Complex(5,6);
        com3.print();
        Complex com4=new Complex(4,8);
        com4.print();
        Complex com5=new Complex(0,0);
        System.out.println("复数加法：");
        com5=com5.add(com1,com2);
        com5.print();//(1.0+2.0i)+(3.0+4.0i)=4.0+6.0i
        System.out.println("-----------------------");
        System.out.println("复数减法：");
        com5=com5.subduction(com2,com3);//(3.0+4.0i)+(5.0+6.0i)=-2.0-2.0i
        com5.print();
        System.out.println("-----------------------");
        System.out.println("复数乘法：");
        com5=com5.multiplication(com3,com4);//(5.0+6.0i)*(4.0+8.0i)=-28.0+64.0i
        com5.print();
        System.out.println("-----------------------");
        System.out.println("复数除法：");//(1.0+2.0i)/(4.0+8.0i)=0.25+0.0i
        com5=com5.dividion(com1,com4);
        com5.print();
        System.out.println("-----------------------");
        System.out.println("复数的模：");
        double x=module(com2);
        System.out.println(x);//|3.0+4.0i|=5.0
        System.out.println("-----------------------");
    }

    @Override
    public String toString() {
        return "Complex{" +
                "realPart=" + realPart +
                ", imaginaryPart=" + imaginaryPart +
                '}';
    }
}
