说明:没有测试,仅仅用于复习。
复数的运算(类和对象)
Problem Description
设计一个类Complex,用于封装对复数的下列操作:
成员变量:实部real,虚部image,均为整数变量;
构造方法:无参构造方法、有参构造方法(参数2个)
成员方法:含两个复数的加、减、乘操作。
复数相加举例: (1 2i) (3 4i)= 4 6i
复数相减举例: (1 2i)-(3 4i)= -2 - 2i
复数相乘举例: (1 2i)*(3 4i)= -5 10i
要求:对复数进行连环运算。
代码语言:javascript复制class Complex{
int real,image;
Complex(){
}
Complex(int real, int image){
this.real = real;
this.image = image;
}
public int getReal(){
return real;
}
public int getImage(){
return image;
}
public Complex getSum(Complex a){
int R = a.getReal();
int I = a.getImage();
int newR = real R;
int newI = image I;
Complex res = new Complex(newR, newI);
return res;
}
public Complex getSub(Complex a){
int R = a.getReal();
int I = a.getImage();
int newR = real R;
int newI = real I;
Complex res = new Complex(newR,newI);
return res;
}
public Complex getMul(Complex a){
int R = a.getReal();
int I = a.getImage();
int newR = real * R - I * image;
int newI = image * R real * I;
Complex res = new Complex(newR,newI);
return res;
}
public String toString(){
DecimalFormat ze = new DecimalFormat("#0");
String str;
if(image > 0){
str = ze.format(real) " " ze.format(image) "i";
}
else if(image < 0) {
str = ze.format(real) "" ze.format(image) "i";
}
else {
str = ze.format(real);
}
return str;
}
}
Input
输入有多行。 第一行有两个整数,代表复数X的实部和虚部。 后续各行的第一个和第二个数表示复数Y的实部和虚部,第三个数表示操作符op: 1——复数X和Y相加;2——复数X和Y相减;3——复数X和Y相乘。
当输入0 0 0时,结束运算,输出结果。
Output
输出一行。
第一行有两个整数,代表复数的实部和虚部。
Sample Input
代码语言:javascript复制1 1
3 4 2
5 2 1
2 -1 3
0 2 2
0 0 0
Sample Output
代码语言:javascript复制5 -7
代码语言:javascript复制package aa;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int x,y,z;
x = sc.nextInt();
y = sc.nextInt();
Complex com1 = new Complex(x,y);
while(sc.hasNext()){
x = sc.nextInt();
y = sc.nextInt();
z = sc.nextInt();
if(x == 0 && y == 0 && z == 0){
String str = com1.toString();
System.out.println(str);
}
Complex com2 = new Complex(x,y);
if(z == 1){
com1 = com1.getSum(com2);
}else if(z==2){
com1 = com1.getSub(com2);
}else if(z==3){
com1 = com1.getMul(com2);
}
}
}
}
class Complex{
int real,image;
Complex(){
}
Complex(int real, int image){
this.real = real;
this.image = image;
}
public int getReal(){
return real;
}
public int getImage(){
return image;
}
public Complex getSum(Complex a){
int R = a.getReal();
int I = a.getImage();
int newR = real R;
int newI = image I;
Complex res = new Complex(newR, newI);
return res;
}
public Complex getSub(Complex a){
int R = a.getReal();
int I = a.getImage();
int newR = real R;
int newI = real I;
Complex res = new Complex(newR,newI);
return res;
}
public Complex getMul(Complex a){
int R = a.getReal();
int I = a.getImage();
int newR = real * R - I * image;
int newI = image * R real * I;
Complex res = new Complex(newR,newI);
return res;
}
public String toString(){
DecimalFormat ze = new DecimalFormat("#0");
String str;
if(image > 0){
str = ze.format(real) " " ze.format(image) "i";
}
else if(image < 0) {
str = ze.format(real) "" ze.format(image) "i";
}
else {
str = ze.format(real);
}
return str;
}
}