复数的运算(类和对象)
Time Limit: 1000MS
Memory Limit: 65536KB
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相乘。
Output
计算数据输出其简化复数形式,如:-2-2i、-4、-3i、1+2i、0。
Example Input
1 1 3 4 2 5 2 1 2 -1 3 0 2 2
Example Output
5-7i
Hint
输入与输出形式示例:
如果输入:
2 3
-2 1 1
则输出: 4i
如果输入:
1 2
-1 -2 1
则输出: 0
复数的输出形式示例:
实部 虚部 输出形式
0 0 0
-4 0 -4
0 4 4i
3 2 3+2i
3 -2 3-2i
如果输入:
2 3
-2 1 1
则输出: 4i
如果输入:
1 2
-1 -2 1
则输出: 0
复数的输出形式示例:
实部 虚部 输出形式
0 0 0
-4 0 -4
0 4 4i
3 2 3+2i
3 -2 3-2i
import java.util.Scanner;
class Complex{
int real;
int image;
Complex(){
}
Complex(int r,int i){
real = r;
image = i;
}
int getReal(){
return real;
}
int getImage(){
return image;
}
}
public class Main {
public static void main(String args[]){
Scanner reader = new Scanner(System.in);
int r,ima;
int x1,x2;
int n;
x1 = reader.nextInt();
x2 = reader.nextInt();
while(reader.hasNext()){
r = reader.nextInt();
ima = reader.nextInt();
n = reader.nextInt();
if(n == 1){
Complex c = new Complex(r,ima);
x1 = c.getReal()+x1;
x2 = c.getImage()+x2;
}
else if(n == 2){
Complex c = new Complex(r,ima);
x1 = x1 - c.getReal();
x2 = x2 - c.getImage();
}
else if(n == 3){
Complex c = new Complex(r,ima);
int y1 = x1*c.getReal();
int y2 = x1 * c.getImage();
int y3 = x2*c.getReal();
int y4 = x2 * c.getImage();
x1 = y1 - y4;
x2 = y2 + y3;
}
}
if(x1 == 0 && x2 == 1)
System.out.println("i");
else if(x1 == 0 && x2 == -1){
System.out.println("-i");
}
else if(x1 == 0&&x2 != 0){
System.out.println(x2+"i");
}
else if(x1 == 0&&x2 == 0){
System.out.println("0");
}
else if(x1 != 0&&x2 == 0)
System.out.println(x1);
else if(x1 != 0&&x2 == 1)
System.out.println(x1+"+i");
else if(x1 != 0&&x2 == -1){
System.out.println(x1+"-i");
}
else if(x1 != 0&&x2 > 0) {
System.out.println(x1+"+"+x2+"i");
}
else if(x1 != 0 && x2 < 0){
System.out.println(x1+""+x2+"i");
}
}
}
class Complex{
int real;
int image;
Complex(){
}
Complex(int r,int i){
real = r;
image = i;
}
int getReal(){
return real;
}
int getImage(){
return image;
}
}
public class Main {
public static void main(String args[]){
Scanner reader = new Scanner(System.in);
int r,ima;
int x1,x2;
int n;
x1 = reader.nextInt();
x2 = reader.nextInt();
while(reader.hasNext()){
r = reader.nextInt();
ima = reader.nextInt();
n = reader.nextInt();
if(n == 1){
Complex c = new Complex(r,ima);
x1 = c.getReal()+x1;
x2 = c.getImage()+x2;
}
else if(n == 2){
Complex c = new Complex(r,ima);
x1 = x1 - c.getReal();
x2 = x2 - c.getImage();
}
else if(n == 3){
Complex c = new Complex(r,ima);
int y1 = x1*c.getReal();
int y2 = x1 * c.getImage();
int y3 = x2*c.getReal();
int y4 = x2 * c.getImage();
x1 = y1 - y4;
x2 = y2 + y3;
}
}
if(x1 == 0 && x2 == 1)
System.out.println("i");
else if(x1 == 0 && x2 == -1){
System.out.println("-i");
}
else if(x1 == 0&&x2 != 0){
System.out.println(x2+"i");
}
else if(x1 == 0&&x2 == 0){
System.out.println("0");
}
else if(x1 != 0&&x2 == 0)
System.out.println(x1);
else if(x1 != 0&&x2 == 1)
System.out.println(x1+"+i");
else if(x1 != 0&&x2 == -1){
System.out.println(x1+"-i");
}
else if(x1 != 0&&x2 > 0) {
System.out.println(x1+"+"+x2+"i");
}
else if(x1 != 0 && x2 < 0){
System.out.println(x1+""+x2+"i");
}
}
}
import java.util.*;
import java.math.*;
class S{
public int x;
public int y;
S(int n, int m){
x = n;
y = m;
}
public void add(int n, int m){
x = x+n;
y = y+m;
}
public void sub(int n, int m){
x = x-n;
y = y-m;
}
public void cheng(int n, int m){
int h = x*n-y*m;
y = x*m+y*n;
x = h;
}
}
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner( System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
S s = new S(n, m);
while ( scanner.hasNext() ){
n = scanner.nextInt();
m = scanner.nextInt();
int h = scanner.nextInt();
if ( h == 1 ){
s.add(n, m);
}
else if ( h == 2 ){
s.sub(n, m);
}
else if ( h == 3 ){
s.cheng(n, m);
}
}
if ( s.x == 0 && s.y == 1 ){
System.out.println("i");
}
else if ( s.x == 0 && s.y == -1 ){
System.out.println("-i");
}
else if ( s.x == 0 && s.y != 0 ){
System.out.println(s.y+"i");
}
else if ( s.x == 0 && s.y == 0 ){
System.out.println("0");
}
else if ( s.x != 0&&s.y == 0 ){
System.out.println(s.x);
}
else if ( s.x != 0 && s.y == 1 ){
System.out.println(s.x+"+i");
}
else if ( s.x != 0 && s.y == -1 ){
System.out.println(s.x+"-i");
}
else if ( s.x != 0 && s.y > 0 ){
System.out.println(s.x+"+"+s.y+"i");
}
else if ( s.x != 0 && s.y < 0 ){
System.out.println(s.x+""+s.y+"i");
}
}
}
import java.math.*;
class S{
public int x;
public int y;
S(int n, int m){
x = n;
y = m;
}
public void add(int n, int m){
x = x+n;
y = y+m;
}
public void sub(int n, int m){
x = x-n;
y = y-m;
}
public void cheng(int n, int m){
int h = x*n-y*m;
y = x*m+y*n;
x = h;
}
}
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner( System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
S s = new S(n, m);
while ( scanner.hasNext() ){
n = scanner.nextInt();
m = scanner.nextInt();
int h = scanner.nextInt();
if ( h == 1 ){
s.add(n, m);
}
else if ( h == 2 ){
s.sub(n, m);
}
else if ( h == 3 ){
s.cheng(n, m);
}
}
if ( s.x == 0 && s.y == 1 ){
System.out.println("i");
}
else if ( s.x == 0 && s.y == -1 ){
System.out.println("-i");
}
else if ( s.x == 0 && s.y != 0 ){
System.out.println(s.y+"i");
}
else if ( s.x == 0 && s.y == 0 ){
System.out.println("0");
}
else if ( s.x != 0&&s.y == 0 ){
System.out.println(s.x);
}
else if ( s.x != 0 && s.y == 1 ){
System.out.println(s.x+"+i");
}
else if ( s.x != 0 && s.y == -1 ){
System.out.println(s.x+"-i");
}
else if ( s.x != 0 && s.y > 0 ){
System.out.println(s.x+"+"+s.y+"i");
}
else if ( s.x != 0 && s.y < 0 ){
System.out.println(s.x+""+s.y+"i");
}
}
}