复数-虚单位问题

oyjwcjzk  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(252)

我对想象单位中的“我”有一些问题。当我有“我”这个数字时,我的程序就工作了。4+4i没关系。但当我只有“我”不想工作的时候。4+我不工作。
我不知道如何修改代码来解决这个错误。我知道下面的几行造成了这个问题。

String x[] = str1.split("\\+|i|-");
        String y[] = str2.split("\\+|i|-");

它是一种计算以字符串形式键入的复数的乘法(*)、除法(/)、加法(+)和减法(-)的程序。

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
    private static DecimalFormat df = new DecimalFormat("0.0");
    public static String Addition(double a_r, double a_i, double b_r, double b_i)
    {
        double x = a_r + b_r;
        double y = a_i + b_i;
        return df.format(x) + "+" + df.format(y) + "i";
    }
    public static String Subtraction(double a_r, double a_i, double b_r, double b_i)
    {
        double x = a_r - b_r;
        double y = a_i - b_i;
        return df.format(x) + "-" + df.format(y) + "i";
    }
    public static String Multiplication(double a_r, double a_i, double b_r, double b_i)
    {
        double x = a_r * b_r - a_i * b_i;
        double y = a_r * b_i + a_i * b_r;
        return df.format(x) + "+" + df.format(y) + "i";
    }
    public static String Division(double a_r, double a_i, double b_r, double b_i)
    {
        double x = a_r*b_r + a_i*b_i / b_r + b_i;
        double y = a_r*b_i - a_i*b_r / b_r + b_i;
        return df.format(x) + "+" + df.format(y) + "i";
    }

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Num1");
        String str1 = scan.nextLine();

        System.out.println("Num2");
        String str2 = scan.nextLine();

        String x[] = str1.split("\\+|i|-");
        String y[] = str2.split("\\+|i|-");

        double a_real = Double.parseDouble(x[0]);
        double a_img = Double.parseDouble(x[1]);
        double b_real = Double.parseDouble(y[0]);
        double b_img = Double.parseDouble(y[1]);

        System.out.println(a_real);
        System.out.println(a_img);
        System.out.println(b_real);
        System.out.println(b_img);

       System.out.println(Addition(a_real, a_img, b_real, b_img));
       System.out.println(Subtraction(a_real, a_img, b_real, b_img));
       System.out.println(Multiplication(a_real, a_img, b_real, b_img));
       System.out.println(Division(a_real, a_img, b_real, b_img));
    }
}
af7jpaap

af7jpaap1#

修复

使用数组的长度作为条件

double a_img = x.length > 1 ? Double.parseDouble(x[1]) : 1;
double b_img = y.length > 1 ? Double.parseDouble(y[1]) : 1;

改善

现在你的代码不处理负数,因为破折号是在分割模式。您可以使用正则表达式来匹配所需的部分: (-?\\d+)\\+?(-?\\d*)\\+?ireal 部分是容易的,因为 img 您可以检查零件是否为空(箱 +i )如果这部分只是一个破折号(case -i )

Matcher m = Pattern.compile("(-?\\d+)\\+?(-?\\d*)\\+?i").matcher(value);
if (m.find()) {
    System.out.println(m.group(1) + "<>" + m.group(2) + " ==>" + m.groupCount());
    real = Double.parseDouble(m.group(1));
    img = m.group(2).isEmpty() ? 1 : m.group(2).equals("-") ? -1 : Double.parseDouble(m.group(2));
} else {
   throw new InvalidParameterException(value);
}

面向对象程序设计

设计一个小班,用途可能是

class Complex {

    private static final DecimalFormat df = new DecimalFormat("0.0");
    private final double real, img;

    public Complex(String value) {
        Matcher m = Pattern.compile("(-?\\d+)\\+?(-?\\d*)\\+?i").matcher(value);
        if (m.find()) {
            System.out.println(m.group(1) + "<>" + m.group(2) + " ==>" + m.groupCount());
            real = Double.parseDouble(m.group(1));
            img = m.group(2).isEmpty() ? 1 : m.group(2).equals("-") ? -1 : Double.parseDouble(m.group(2));
        } else {
            throw new InvalidParameterException(value);
        }
    }

    public Complex(double r, double i) {
        real = r;
        img = i;
    }    
    public Complex add(Complex other) {
        return new Complex(real + other.real, img + other.img);
    }    
    public Complex substract(Complex other) {
        return new Complex(real - other.real, img - other.img);
    }    
    public Complex multiply(Complex other) {
        return new Complex(real * other.real - img * other.img, real * other.img + img * other.real);
    }

    public Complex divide(Complex other) {
        double denominator = Math.pow(other.real, 2) + Math.pow(other.img, 2);
        return new Complex((real * other.real + img * other.img) / denominator,
            (real * other.img - img * other.real) / denominator);
    }    
    @Override
    public String toString() { return df.format(real) + "+" + df.format(img) + "i";}
}

使用

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("Input num1: ");
    String str1 = scan.nextLine();

    System.out.println("Input num2: ");
    String str2 = scan.nextLine();

    Complex c1 = new Complex(str1);
    Complex c2 = new Complex(str2);
    System.out.println(c1.add(c2));
    System.out.println(c1.substract(c2));
    System.out.println(c1.multiply(c2));
    System.out.println(c1.divide(c2));
}

相关问题