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

 Statistic

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

Hint

Source

zhouxq

 

package test;
import java.util.Scanner;

class Complex{
    int real;
    int image;
    public Complex() {
        real = 0;
        image = 0;
    }
    public Complex(int real, int image) {
        this.real = real;
        this.image = image;
    }
    public Complex addd(Complex b) {
        Complex c = new Complex();
        c.real = this.real+b.real;
        c.image = this.image + b.image;
        return c;
    }
    public Complex subtract(Complex b) {
        Complex c = new Complex();
        c.real = this.real-b.real;
        c.image = this.image - b.image;
        return c;
    }
    public Complex Multiple(Complex b) {
        Complex c = new Complex();
        c.real = this.real*b.real-this.image*b.image;
        c.image = this.real*b.image + this.image*b.real;
        return c;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int b = reader.nextInt();
        int c = reader.nextInt();
        Complex x = new Complex(b,c);
        while(reader.hasNext()) {
            b = reader.nextInt();
            c = reader.nextInt();
            int d = reader.nextInt();
            if(b==0&&c==0&&d==0) {
                System.out.printf("%d %d\n", x.real,x.image);
                break;
            }
            Complex y = new Complex(b,c);
            if(d==1) {
                x = x.addd(y);
            }
            else if(d== 2) {
                x = x.subtract(y);
            }
            else if(d==3) {
                x = x.Multiple(y);
            }
        }
        
    }
}
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值