BUPT JAVA: 复数

复数

题目描述:

编写复数类, 该类至少包括计算复数的加法, 减法, 乘法, 除法的方法.
编写 ComplexDivException 类,该类继承 Exception 类, 且至少包含两个数据成员, 一个为错误代码, 一个为错误信息. 当复数类除数为 0 时要抛出该异常.
编写测试类, 测试以上两个类.

输入:

第一行为一个字符串,只可能为 add, sub, mul, div四个字符串之一. 依次代表本次测试执行加法, 减法, 乘法和除法.
第二行为一个整数 n(0<n<100)n ( 0 < n < 100 ), 代表共 nn 组测试用例。
后边为 nn 行, 每行均为 4 个用空格分隔的浮点数, 依次参与运算的两个复数 c1, c2. 第一个数代表 c1 的实部, 第二个数代表 c1 的虚部, 第三个数代表 c2 的实部, 第四个数代表c2的虚部.

输出:

若干行, 通常一行对应输入的一行, 为 c1 加或减或乘或除以 c2 的结果 (保留一位小数, 具体格式见样例), 如果除法的除数为 0, 则输出两行对应输入的一行, 这两行的内容为 (注意空格):
Error No : 1001
Error Message : Divide by zero.
前边的 1001 为错误代码,后边 Divide by zero. 为错误信息.
注意输出时建议 采用String的格式化, 或printf的格式化, 不建议采用 DecimalFormat.

输入样例:

1
2
3
4
5
6
7
8
add
6
1 1 -1 -1
90.9 68.8 60.3 79.3
4.3 65.4 69.2 -49.7
-8.33 -1.14 4.2 1.62
3.7 1.86 -5.46 9.22
15.1 -19.6 2.2 -3.73

输出样例:

1
2
3
4
5
6
0.0+0.0i
151.2+148.1i
73.5+15.7i
-4.1+0.5i
-1.8+11.1i
17.3-23.3i

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 2023/04/10
// 复数

import java.util.Scanner;

public class Main {
public static void main(String[] argc) {
var input = new Scanner(System.in);
var order = input.next();
var n = input.nextInt();

var complexNumbers = new ComplexNumber[n][2];
var arr = new double[4];

for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++)
arr[j] = input.nextDouble();
complexNumbers[i][0] = new ComplexNumber(arr[0], arr[1]);
complexNumbers[i][1] = new ComplexNumber(arr[2], arr[3]);
}

switch (order) {
case "add" -> {
for (int i = 0; i < n; i++)
System.out.println(ComplexNumber.add(complexNumbers[i][0], complexNumbers[i][1]));
}
case "sub" -> {
for (int i = 0; i < n; i++)
System.out.println(ComplexNumber.sub(complexNumbers[i][0], complexNumbers[i][1]));
}
case "mul" -> {
for (int i = 0; i < n; i++)
System.out.println(ComplexNumber.mul(complexNumbers[i][0], complexNumbers[i][1]));
}
case "div" -> {
for (int i = 0; i < n; i++) {
try {
System.out.println(ComplexNumber.div(complexNumbers[i][0], complexNumbers[i][1]));
} catch (ComplexDivException e) {
System.out.println("Error No : " + e.errNo);
System.out.println("Error Message : " + e.errMess);
}
}
}
}
}
}

class ComplexNumber {
private final double real;
private final double imaginary;

ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}

public static ComplexNumber add(ComplexNumber c1, ComplexNumber c2) {
return new ComplexNumber(c1.real + c2.real, c1.imaginary + c2.imaginary);
}

public static ComplexNumber sub(ComplexNumber c1, ComplexNumber c2) {
return new ComplexNumber(c1.real - c2.real, c1.imaginary - c2.imaginary);
}

public static ComplexNumber mul(ComplexNumber c1, ComplexNumber c2) {
return new ComplexNumber(c1.real * c2.real - c1.imaginary * c2.imaginary, c1.real * c2.imaginary + c2.real * c1.imaginary);
}

public static ComplexNumber div(ComplexNumber c1, ComplexNumber c2) throws ComplexDivException {
if (c2.real == 0 && c2.imaginary == 0) {
throw new ComplexDivException();
}
double temp = c2.real * c2.real + c2.imaginary * c2.imaginary;
double newReal = (c1.real * c2.real + c1.imaginary * c2.imaginary) / temp,
newIma = (c2.real * c1.imaginary - c1.real * c2.imaginary) / temp;
return new ComplexNumber(newReal, newIma);
}

@Override
public String toString() {
if (this.imaginary >= 0)
return String.format("%.1f+%.1fi", this.real, this.imaginary);
else
return String.format("%.1f-%.1fi", this.real, Math.abs(this.imaginary));
}
}

class ComplexDivException extends Exception {
public final int errNo = 1001;
public final String errMess = "Divide by zero.";
}
2023-04-10 
IP属地: 北京

BUPT JAVA: 复数
https://dengwuli.github.io/2023/04/10/BUPT_JAVA/复数/
作者
DengWuLi
发布于
2023年4月10日
更新于
2023年7月14日
许可协议