【Pta】7-2 sdut-oop-1 简单的复数运算

7-2 sdut-oop-1 简单的复数运算

分数 20
作者 周雪芹
单位 山东理工大学
设计一个类Complex,用于封装对复数的下列操作:
成员变量:实部real,虚部image,均为整数变量;
构造方法:无参构造方法、有参构造方法(参数2个)
成员方法:含两个复数的加、减、乘操作。
复数相加举例: (1+2i)+(3+4i)= 4 + 6i
复数相减举例: (1+2i)-(3+4i)= -2 - 2i
复数相乘举例: (1+2i)*(3+4i)= -5 + 10i
要求:对复数进行连环算术运算。

提示:如果用PYTHON语言实现,不必设计Complex类,可以使用内置的复数数据类型,完成复数的算术运算。

输入格式:
输入有多行。
第一行有两个整数,代表复数X的实部和虚部。
后续各行的第一个和第二个数表示复数Y的实部和虚部,第三个数表示操作符op: 1——复数X和Y相加;2——复数X和Y相减;3——复数X和Y相乘。
当输入0 0 0时,结束运算,输出结果。
输出格式:
输出一行。
第一行有两个整数,代表复数的实部和虚部,实部和虚部之间用1个空格分开。
输入样例:
1 1
3 4 2
5 2 1
2 -1 3
0 2 2
0 0 0

输出样例:
5 -7
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB

pta版

import java.util.Scanner;

class Complex {
    public int real;
    public int image;

    public Complex() {

    }

    public Complex(int real, int image) {
        this.real = real;
        this.image = image;
    }

    public int getReal() {
        return real;
    }

    public void setReal(int real) {
        this.real = real;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public void jia(int real, int image, int x2, int y2) {
        this.real = real + x2;
        this.image = image + y2;
    }

    public void jian(int real, int image, int x2, int y2) {
        this.real = real - x2;
        this.image = image - y2;
    }
    public void cheng(int real,int image,int x2,int y2){
        int x1 = real;int y1 = image;
        this.real = (x1*x2-y1*y2);
        this.image = (x2*y1+x1*y2);
    }
}

 class Main {
    public static void main(String []args){
        int choice = 5;
        Complex c1 = new Complex();
        Scanner sc = new Scanner(System.in);
        int real = sc.nextInt();
        c1.setReal(real);
        int image = sc.nextInt();
        c1.setImage(image);
        while(choice!=0){
            int x2 = sc.nextInt();
            int y2 = sc.nextInt();
            choice = sc.nextInt();
            switch(choice){
                case 1:
                    c1.jia(c1.getReal(),c1.getImage(),x2,y2);
                    break;
                case 2:
                    c1.jian(c1.getReal(),c1.getImage(),x2,y2);
                    break;
                case 3:
                    c1.cheng(c1.getReal(),c1.getImage(),x2,y2);
                    break;
                case 0:
                    break;

            }
        }
        System.out.println(c1.getReal() + " " + c1.getImage());
    }
}

请添加图片描述

总结:这个题收获挺大的,一是学会了在idea中单步调试 f7和f8。二是对this关键字有了全新的认识,以及同一个变量在Java的不同方法、类中究竟是怎么样来回调用和变化的。

idea可调式版

package fushu1;

public class Complex {
    public int real;
    public int image;

    public Complex() {

    }

    public Complex(int real, int image) {
        this.real = real;
        this.image = image;
    }

    public int getReal() {
        return real;
    }

    public void setReal(int real) {
        this.real = real;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public void jia(int real, int image, int x2, int y2) {
        this.real = real + x2;
        this.image = image + y2;
    }

    public void jian(int real, int image, int x2, int y2) {
        this.real = real - x2;
        this.image = image - y2;
    }
    public void cheng(int real,int image,int x2,int y2){
        int x1 = real;int y1 = image;
        this.real = (x1*x2-y1*y2);
        this.image = (x2*y1+x1*y2);
    }
}
package fushu1;

import java.util.Scanner;

public class test {
    public static void main(String []args){
        int choice = 5;
        Complex c1 = new Complex();
        Scanner sc = new Scanner(System.in);
        int real = sc.nextInt();
        c1.setReal(real);
        int image = sc.nextInt();
        c1.setImage(image);
        while(choice!=0){
            int x2 = sc.nextInt();
            int y2 = sc.nextInt();
            choice = sc.nextInt();
            switch(choice){
                case 1:
                    c1.jia(c1.getReal(),c1.getImage(),x2,y2);
                    break;
                case 2:
                    c1.jian(c1.getReal(),c1.getImage(),x2,y2);
                    break;
                case 3:
                    c1.cheng(c1.getReal(),c1.getImage(),x2,y2);
                    break;
                case 0:
                    break;

            }
        }
        System.out.println(c1.getReal() + " " + c1.getImage());
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值