简单的复数运算(类和对象)

版权声明:禁止转载,翻版必究 https://blog.csdn.net/qq_41341757/article/details/83624963

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

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner reader=new Scanner(System.in);
		int a=reader.nextInt();
		int b=reader.nextInt();
			while(reader.hasNext())
			{
				int c=reader.nextInt();
			    int d=reader.nextInt();
			    int e=reader.nextInt();
				if(e==1)
				{
					a=a+c;
					b=b+d;
				}
				else  if(e==2)
				{
					a=a-c;
					b=b-d;
				}
				else if(e==3)
				{
					int x=a*c-b*d;
					int y=c*b+a*d;
					a=x;
					b=y;
				}
				else if(c==0&&d==0&&e==0)
				{
					System.out.println(a+" "+b);
					break;
				}
			}
		reader.close();
	}
}

在e 等于3的时候,如果不用x,y进行替代传出,将会是错的,即:a=ac-bd; b=cb+ad;原因后一个a在用前一个a的时候已经改变啦!!!

猜你喜欢

转载自blog.csdn.net/qq_41341757/article/details/83624963