解析: 题目不难,主要是考察复数的四则运算公式,这玩意高中学的早就忘记了,有点坑。另外题目要求自己封装四则运算的方法。
package _3_5_test;
import java.util.Scanner;
/*P1103
* */
public class SeventyOne {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
String sign = scanner.next();
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double d = scanner.nextDouble();
double re[] = null;
if (sign.equals("+")) {
re = add(a, b, c, d);
} else if (sign.equals("-")) {
re = sub(a, b, c, d);
} else if (sign.equals("*")) {
re = mul(a, b, c, d);
} else if (sign.equals("/")) {
re = div(a, b, c, d);
}
System.out.printf("%.2f+%.2fi", re[0], re[1]);
}
public static double[] add(Double a, Double b, Double c, Double d) {
double result[] = new double[2];
result[0] = a + c;
result[1] = b + d;
return result;
}
public static double[] sub(Double a, Double b, Double c, Double d) {
double result[] = new double[2];
result[0] = a - c;
result[1] = b - d;
return result;
}
// 复数的乘法公式:(a+bi)(c+di)=ac+adi+bci-bd
public static double[] mul(Double a, Double b, Double c, Double d) {
double result[] = new double[2];
result[0] = (a * c - b * d);
result[1] = (a * d + b * c);
return result;
}
// 复数的加法公式:(a+bi)/(c+di)=((ac+bd)+(bc-ad)i)/(c*c+d*d)
public static double[] div(Double a, Double b, Double c, Double d) {
double result[] = new double[2];
result[0] = (a * c + b * d) / (c * c + d * d);
result[1] = (b * c - a * d) / (c * c + d * d);
return result;
}
}