SDUT - 3336 复数的运算(类和对象) (Java)

复数的运算(类和对象)

Time Limit: 1000 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相乘。

Output

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

Sample Input
1 1
3 4 2
5 2 1
2 -1 3
0 2 2
Sample 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

Code

import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Complex x = new Complex(sc.nextInt(), sc.nextInt());
		while (sc.hasNext()) {
			Complex y = new Complex(sc.nextInt(), sc.nextInt());
			int op = sc.nextInt();
			if (op == 1) {
				x = x.add(y);
			} else if (op == 2) {
				x = x.sub(y);
			} else if (op == 3) {
				x = x.multi(y);
			}
		}
		System.out.println(x);
		sc.close();
	}
}

class Complex {
	int real;
	int imag;

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

	public Complex add(Complex c2) {
		real += c2.real;
		imag += c2.imag;
		return new Complex(real, imag);
	}

	public Complex sub(Complex c2) {
		real -= c2.real;
		imag -= c2.imag;
		return new Complex(real, imag);
	}

	public Complex multi(Complex c2) {
		int real_t = real;
		real = real * c2.real - imag * c2.imag;
		imag = imag * c2.real + real_t * c2.imag;
		return new Complex(real, imag);
	}

	@Override
	public String toString() {
		if (real != 0 && imag == 1) {
			return real + "+i";
		} else if (real != 0 && imag == -1) {
			return real + "-i";
		} else if (real != 0 && imag > 0) {
			return real + "+" + imag + "i";
		} else if (real != 0 && imag < 0) {
			return real + "" + imag + "i";
		} else if (real == 0 && imag == 1) {
			return "i";
		} else if (real == 0 && imag == -1) {
			return "-i";
		} else if (real == 0 && imag != 0) {
			return imag + "i";
		} else if (real != 0 && imag == 0) {
			return real + "";
		} else if (real == 0 && imag == 0) {
			return "0";
		}
		return null;
	}
}

反思:

Java类的练习,输出格式很坑,Output Limit Exceed无数次,发现是没判-1的情况……

1. 编写一个复数运算复数ComplexNumber的属性: m_dRealPart:,代表复数分。 m_dImaginPart:,代表复数分。 复数ComplexNumber的方法: ComplexNumber():构造函数,将都置为0。 ComplexNumber(double r,double i):构造函数,创建复数对象的同时完成复数的初始化,r为的初值,i为的初值。 getRealPart():获得复数对象。 getImaginPart():获得复数对象。 setRealPart(double d):把当前复数对象设置为给定的形式参数的数字。 setImaginaryPart(double d):把当前复数对象设置为给定的形式参数的数字。 complexAdd(ComplexNumber c):当前复数对象与形式参数复数对象相加,所得的结果也是复数值,返回给此方法的调用者。 complexAdd(double c):当前复数对象与形式参数对象相加,所得的结果仍是复数值,返回给此方法的调用者。 complexMinus(ComplexNumber c) :当前复数对象与形式参数复数对象相减,所得的结果也是复数值,返回给此方法的调用者。 complexMinus(double c) :当前复数对象与形式参数对象相减,所得的结果仍是复数值,返回给此方法的调用者。 complexMulti(ComplexNumber c):当前复数对象与形式参数复数对象相乘,所得的结果也是复数值,返回给此方法的调用者。 complexMulti(double c):当前复数对象与形式参数对象相乘,所得的结果仍是复数值,返回给此方法的调用者。 toString():把当前复数对象组合成a+bi的字符串形式,其中分别为的数据。 2. 编写Java Application程序使用上题定义的,检查定义是否正确。
### Python复数运算 以下是基于Python的复数运算现代码,利用Python内置的`complex`型来简化复数操作[^1]: ```python def complex_operations(): results = [] x_real, x_imaginary = map(int, input().split()) x = complex(x_real, x_imaginary) while True: line = input() if line.strip() == "0 0 0": break y_real, y_imaginary, op = line.split() y = complex(int(y_real), int(y_imaginary)) if int(op) == 1: # 加法 result = x + y elif int(op) == 2: # 减法 result = x - y elif int(op) == 3: # 乘法 result = x * y else: continue results.append(result) for res in results: print(f"{int(res.real)} {int(res.imag)}" .replace("j", "").replace("+", "")) if __name__ == "__main__": complex_operations() ``` 上述代码现了读取输入并执行复数加减乘操作的功能。通过循环不断接收输入直到遇到终止条件 `0 0 0`。 --- ### Java复数运算 对于Java中的复数运算,则需要手动定义一个`Complex`来进行封装[^2]。下面是一个完整的示例代码: ```java class Complex { private double real; private double imaginary; public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public Complex add(Complex other) { return new Complex(this.real + other.real, this.imaginary + other.imaginary); } public Complex subtract(Complex other) { return new Complex(this.real - other.real, this.imaginary - other.imaginary); } public Complex multiply(Complex other) { double r = this.real * other.real - this.imaginary * other.imaginary; double i = this.real * other.imaginary + this.imaginary * other.real; return new Complex(r, i); } @Override public String toString() { return (real != 0 ? real + " " : "") + (imaginary >= 0 && real != 0 ? "+" : "") + (imaginary != 0 ? Math.abs(imaginary) + "i" : ""); } } public class Main { public static void main(String[] args) { java.util.Scanner scanner = new Scanner(System.in); // 初始化 X System.out.println("Enter the first complex number:"); double xr = scanner.nextDouble(); double xi = scanner.nextDouble(); Complex x = new Complex(xr, xi); List<Complex> results = new ArrayList<>(); while (true) { System.out.println("Enter Y and operation code or '0 0 0' to stop:"); double yr = scanner.nextDouble(); double yi = scanner.nextDouble(); int opCode = scanner.nextInt(); if (yr == 0 && yi == 0 && opCode == 0) { break; } Complex y = new Complex(yr, yi); switch (opCode) { case 1 -> results.add(x.add(y)); case 2 -> results.add(x.subtract(y)); case 3 -> results.add(x.multiply(y)); default -> {} } } for (Complex c : results) { System.out.println(c.toString()); } } } ``` 此代码创建了一个名为`Complex`的用于处理复数的相关计算,并提供了加法、减法以及乘法的方法。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值