Java练习 SDUT-4303_简单的复数运算(类和对象)

简单的复数运算(类和对象)
Time Limit: 2000 ms Memory Limit: 65536 KiB
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 cin = new Scanner(System.in);
		shu a,b;
		a = new shu(cin.nextInt(),cin.nextInt());
		int x,y,z;
		while(cin.hasNext())
		{
			x = cin.nextInt();
			y = cin.nextInt();
			z = cin.nextInt();
			if(x==0&&y==0&&z==0)
				break;
			b = new shu(x,y);
			if(z==1)
				a.jia(b);
			else if(z==2)
				a.jian(b);
			else if(z==3)
				a.cheng(b);
			//System.out.println(a.a+" "+a.b);
		}
		System.out.println(a.a+" "+a.b);
		cin.close();
	}
}

class shu
{
	int a,b;
	shu(int a,int b)
	{
		this.a = a;
		this.b = b;
	}
	void jia(shu b)
	{
		this.a += b.a;
		this.b += b.b;
	}
	void jian(shu b)
	{
		this.a -= b.a;
		this.b -= b.b;
	}
	void cheng(shu b)
	{
		int x,y;
		x = this.a * b.a - this.b * b.b;
		y = this.a * b.b + this.b * b.a;
		this.a = x;
		this.b = y;
	}
}

原文地址:https://www.cnblogs.com/luoxiaoyi/p/9950794.html