复数的运算(类和对象)(写的不正规 单纯应付机考)

来源:互联网 发布:淘宝网客服电话是多少 编辑:程序博客网 时间:2024/05/14 05:47

复数的运算(类和对象)

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic Discuss

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——复数XY相加;2——复数XY相减;3——复数XY相乘。

Output

计算数据输出其简化复数形式,如:-2-2i、-4、-3i1+2i0

Example Input

1 13 4 25 2 12 -1 30 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

Author

zhouxq
import java.util.*;public class Main {public static void main(String[] args) { Scanner cin = new Scanner(System.in); int x = cin.nextInt(); int y = cin.nextInt(); while(cin.hasNext()){ int x1 = cin.nextInt(); int y1 = cin.nextInt(); int op = cin.nextInt(); if(op == 1){ x += x1; y += y1; } else if(op == 2){ x -= x1; y -= y1; } else if(op == 3){ int xx = x; x = x*x1 - y*y1; y = xx*y1 + x1*y; } //System.out.println(x + "  " + y); } if(x == 0 && y == 0) System.out.println(0); else if(x == 0 && y == 1) System.out.println("i"); else if(x == 0 && y == -1) System.out.println("-i");  else if(x==0 && y != 0) System.out.println(y+"i"); else if(y==0 && x != 0) System.out.println(x);  else if(x != 0 && y < -1) System.out.println(x+""+y+"i"); else if(x != 0 && y == -1) System.out.println(x+"-i"); else if(x != 0 && y == 1) System.out.println(x+"i"); else if(x != 0 && y > 0) System.out.println(x+"+"+y+"i"); }}


0 0