PTA 7-15 复数四则运算 题解
PTA 7-15 复数四则运算 题解
这是一道没有什么思维含量的典型模拟题,不过对于此题我封装了一个 \(\texttt{java}\) 的复数四则运算方式方便调用。
有一点非常坑的就是,纯实数与纯虚数运算的数与结果输出处理。
题面
本题要求编写程序,计算2个复数的和、差、积、商。
输入格式:
输入在一行中按照a1 b1 a2 b2的格式给出2个复数C1=a1+b1i和C2=a2+b2i的实部和虚部。题目保证C2不为0。
输出格式:
分别在4行中按照(a1+b1i) 运算符 (a2+b2i) = 结果的格式顺序输出2个复数的和、差、积、商,数字精确到小数点后1位。如果结果的实部或者虚部为0,则不输出。如果结果为0,则输出0.0。
输入样例1:
2 3.08 -2.04 5.06
输出样例1:
(2.0+3.1i) + (-2.0+5.1i) = 8.1i
(2.0+3.1i) - (-2.0+5.1i) = 4.0-2.0i
(2.0+3.1i) * (-2.0+5.1i) = -19.7+3.8i
(2.0+3.1i) / (-2.0+5.1i) = 0.4-0.6i
输入样例2:
1 1 -1 -1.01
输出样例2:
(1.0+1.0i) + (-1.0-1.0i) = 0.0
(1.0+1.0i) - (-1.0-1.0i) = 2.0+2.0i
(1.0+1.0i) * (-1.0-1.0i) = -2.0i
(1.0+1.0i) / (-1.0-1.0i) = -1.0
代码
//javac
import java.util.Scanner;
public class Main {
// 类似c++结构体,我们定义一个 num 类来封装两复数加减乘除的做法
public static class num{
double real;//非静态变量
double ima;
public num(double real, double ima){ // 构造函数
this.real = real;
this.ima = ima;
}
// this.* 是第一个数的成员变量, other.* 是第二个数的成员变量
public num add(num other) { //addition 处理加法
return new num(this.real + other.real, this.ima + other.ima);
}
public num subt(num other){ // subtraction 处理减法
return new num(this.real - other.real, this.ima - other.ima);
}
public num mul(num other){ // 处理乘法
return new num(this.real* other.real - this.ima* other.ima, this.ima*other.real + this.real* other.ima);
}
public num div(num other){ // 处理除法
num tmp = new num(other.real, -1.0*other.ima); // 我们要先使分母有理化,为此需要声明一个新的 tmp 构造为分母的共轭复数
other = tmp.mul(other); // 分母有理化过程,此条语句结束后,分母有理
return new num((this.real* tmp.real - this.ima* tmp.ima)/other.real, (this.ima*tmp.real + this.real* tmp.ima)/other.real);
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
num a = new num(scan.nextDouble(), scan.nextDouble());
num b = new num(scan.nextDouble(), scan.nextDouble());
scan.close();
add(a, b);
subt(a, b);
mul(a, b); //
div(a, b);
}
// 根据题目要求精度,四舍五入
public static num toOne(num x){
return new num(Math.round(x.real * 10.0) / 10.0, Math.round(x.ima * 10.0) / 10.0);
}
public static void add(num a, num b){
num ans = a.add(b);
//System.out.printf("(%f", a.real, a.ima, b.real, b.ima);
printNum(toOne(a));
System.out.print(" + ");
printNum(toOne(b));
System.out.print(" = ");
printAns(toOne(ans));
//printTest(ans);
}
public static void subt(num a, num b){
num ans = a.subt(b);
printNum(toOne(a));
System.out.print(" - ");
printNum(toOne(b));
System.out.print(" = ");
printAns(toOne(ans));
//System.out.printf("%f %f \n", ans.real, ans.ima);
}
public static void mul(num a, num b){//Multiplication
num ans = a.mul(b);
printNum(toOne(a));
System.out.print(" * ");
printNum(toOne(b));
System.out.print(" = ");
printAns(toOne(ans));
}
public static void div(num a, num b){
num ans = a.div(b);
printNum(toOne(a));
System.out.print(" / ");
printNum(toOne(b));
System.out.print(" = ");
printAns(toOne(ans));
}
public static void printTest(num x){
System.out.printf("%f %f \n", x.real, x.ima);
}
//考虑有0
public static void printNum(num x){
final double eps = 1e-10;
System.out.printf("(%.1f", x.real);
if(x.ima >= 0) System.out.print("+");
System.out.printf("%.1fi)", x.ima);
}
public static void printAns(num ans){
boolean flag = false;
final double eps = 1e-1;
if(Math.abs(ans.real) > eps) {
System.out.printf("%.1f", ans.real);
flag = true;
}
if(ans.ima >= eps && flag) System.out.print("+");
if(Math.abs(ans.ima) > eps) {System.out.printf("%.1fi", ans.ima); flag = true;}
if(!flag){System.out.print("0.0");}
System.out.println();
}
}

浙公网安备 33010602011771号