Problem Description
设计一个类Complex,用于封装对复数的下列操作:
成员变量:实部real,虚部image,均为整数变量;
构造方法:无参构造方法、有参构造方法(参数2个)
成员方法:含两个复数的加、减、乘操作。
复数相加举例: (1+2i)+(3+4i)= 4 + 6i
复数相减举例: (1+2i)-(3+4i)= -2 - 2i
复数相乘举例: (1+2i)*(3+4i)= -5 + 10i
要求:对复数进行连环算术运算。
Input
输入有多行。
第一行有两个整数,代表复数X的实部和虚部。
后续各行的第一个和第二个数表示复数Y的实部和虚部,第三个数表示操作符op: 1——复数X和Y相加;2——复数X和Y相减;3——复数X和Y相乘。
当输入0 0 0时,结束运算,输出结果。
Output
输出一行。
第一行有两个整数,代表复数的实部和虚部。
Sample Input
1 1 3 4 2 5 2 1 2 -1 3 0 2 2 0 0 0
Sample Output
5 -7
Hint
Source
zhouxq
package test;
import java.util.Scanner;
class Complex{
int real;
int image;
public Complex() {
real = 0;
image = 0;
}
public Complex(int real, int image) {
this.real = real;
this.image = image;
}
public Complex addd(Complex b) {
Complex c = new Complex();
c.real = this.real+b.real;
c.image = this.image + b.image;
return c;
}
public Complex subtract(Complex b) {
Complex c = new Complex();
c.real = this.real-b.real;
c.image = this.image - b.image;
return c;
}
public Complex Multiple(Complex b) {
Complex c = new Complex();
c.real = this.real*b.real-this.image*b.image;
c.image = this.real*b.image + this.image*b.real;
return c;
}
}
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int b = reader.nextInt();
int c = reader.nextInt();
Complex x = new Complex(b,c);
while(reader.hasNext()) {
b = reader.nextInt();
c = reader.nextInt();
int d = reader.nextInt();
if(b==0&&c==0&&d==0) {
System.out.printf("%d %d\n", x.real,x.image);
break;
}
Complex y = new Complex(b,c);
if(d==1) {
x = x.addd(y);
}
else if(d== 2) {
x = x.subtract(y);
}
else if(d==3) {
x = x.Multiple(y);
}
}
}
}