#不知道怎么输出类
#【输入示例】
2 3.08 -2.04 5.06
【输出示例】
(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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double a1 = in.nextDouble();
double b1 = in.nextDouble();
double a2 = in.nextDouble();
double b2 = in.nextDouble();
Complex a = new Complex(a1, b1);
Complex b = new Complex(a2, b2);
Complex c = new Complex();
System.out.print(a + " + " + b + " = ");
c = a.add(b);
c.print();
System.out.print(a + " - " + b + " = ");
c = a.sub(b);
c.print();
System.out.print(a + " * " + b + " = ");
c = a.mul(b);
c.print();
System.out.print(a + " / " + b + " = ");
c = a.div(b);
c.print();
in.close();
}
}
```java
class Complex {
private double a,b;
public Complex() {
a=0;
b=0;
}
public Complex(double q,double p) {
a=q;
b=p;
}
public double geta() {
return a;
}
public double getb() {
return b;
}
public void print() {
if(a>0) {
if(b>0) {
System.out.printf("%.1f%.1fi/n",Math.abs(a),Math.abs(b));
}
else if(b==0) {
System.out.printf("%.1f/n",Math.abs(a));
}
else {
System.out.printf("%.1f-%.1fi/n",Math.abs(a),Math.abs(b));
}
}
else if(a==0) {
if(b>0) {
System.out.printf("%.1fi/n",Math.abs(b));
}
else if(b==0) {
System.out.printf("0.0/n");
}
else {
System.out.printf("-%.1fi/n",Math.abs(b));
}
}
else {
if(b>0) {
System.out.printf("-%.1f%.1fi/n",Math.abs(a),Math.abs(b));
}
else if(b==0) {
System.out.printf("-%.1f/n",Math.abs(a));
}
else {
System.out.printf("-%.1f-%.1fi/n",Math.abs(a),Math.abs(b));
}
}
}
public Complex add (Complex c) {
Complex d=new Complex(a+c.geta(),b+c.getb());
return d;
}
public Complex sub (Complex c) {
Complex d=new Complex(a-c.geta(),b-c.getb());
return d;
}
public Complex mul (Complex c) {
Complex d=new Complex(a*c.geta()-(b*c.getb()),a*c.getb()+(b*c.geta()));
return d;
}
public Complex div (Complex c) {
Complex d=new Complex((a*c.geta()+(b*c.getb()))/(c.geta()*c.geta()+c.getb()*c.getb()),(a*c.getb()-(b*c.geta()))/(c.geta()*c.geta()+c.getb()*c.getb()));
return d;
}
}
```