Java - 复数 - 虚数单位问题
Java - complex numbers - problem with imaginary unit
我对虚数单位中的“i”有一些疑问。
当我有带数字的“i”时,我的程序就可以工作了。 4+4i 没问题。
但是当我只有“我”时不想工作。 4+i 不起作用。
我不知道如何更改代码来解决此错误。我知道下面几行会造成这个问题。
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));
}
}
修复
你用数组的长度作为条件
double a_img = x.length > 1 ? Double.parseDouble(x[1]) : 1;
double b_img = y.length > 1 ? Double.parseDouble(y[1]) : 1;
改善
目前您的代码不处理负数,因为破折号位于拆分模式中。您可以使用正则表达式来匹配您需要的部分:(-?\d+)\+?(-?\d*)\+?i
real
部分的解析很容易,对于 img
你可以检查该部分是否为空(案例 +i
)以及该部分是否只是破折号(案例-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,使用起来可能会像
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));
}
我对虚数单位中的“i”有一些疑问。 当我有带数字的“i”时,我的程序就可以工作了。 4+4i 没问题。 但是当我只有“我”时不想工作。 4+i 不起作用。
我不知道如何更改代码来解决此错误。我知道下面几行会造成这个问题。
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));
}
}
修复
你用数组的长度作为条件
double a_img = x.length > 1 ? Double.parseDouble(x[1]) : 1;
double b_img = y.length > 1 ? Double.parseDouble(y[1]) : 1;
改善
目前您的代码不处理负数,因为破折号位于拆分模式中。您可以使用正则表达式来匹配您需要的部分:(-?\d+)\+?(-?\d*)\+?i
real
部分的解析很容易,对于 img
你可以检查该部分是否为空(案例 +i
)以及该部分是否只是破折号(案例-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,使用起来可能会像
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));
}