Complex类实现复数的加减乘除运算

本文详细介绍了如何使用Java实现复数类(Complex),包括多种构造方法、基本属性访问、四则运算、复数比较及除零错误处理。通过具体代码示例,展示了复数加、减、乘、除的两种实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java实现Complex类(复数工具)

复数工具实现复数的四则运算,即加减乘除,和简单的输出。
进一步明确需求 :
1.应该至少提供4种构造方法,分别是:无参,单参,双参和其他Complex类对象作为参数;
2.提供对实部虚部的基本访问(Getters和Setters);
3.简单输出格式为“(实部,虚部)” (ToString方法的覆盖);
4.四则运算的每一个运算,应提供两种实现方案;以加法为例:
a += b;
c = a + b;
5.提供对复数相等判断的操作(即实部和虚部均对应相等)(equals方法的覆盖);
6.复数除法操作应对“除0 错”进行有效性判断和操作;

下面是完整的代码:

package com.mec.complex.core;

/**
 * 这是一个教学工具:复数<br>
 * 复数拥有<font size=2 color="red">实部</font>(real)和虚部(vir) ;<bir>
 * 本类提供复数的四则运算 ;<br>
 * 关于加法,有两种:
 * c = a+b;和a += b;
 * 在代码中均已实现。
 * @author Ailsa
 *
 */

public class Complex {
	private double real;
	private double vir;
	
	public Complex() {
		this(0.0, 0.0);
	}
	
	public Complex(double real) {
		this(real, 0.0);
	}
	
	public Complex(double real, double vir) {
		this.real = real;
		this.vir = vir;
	}
	
	public Complex(Complex complex) {
		this(complex.real, complex.vir);
	}

	public double getReal() {
		return real;
	}

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

	public double getVir() {
		return vir;
	}

	public void setVir(double vir) {
		this.vir = vir;
	}

	//四则运算;
	//先实现加法运算:根据需求分析,加法运算有两种形式;
	//a += b 和 c = a + b;
	//先实现a += b;
	
	public Complex add(Complex other) {
		this.real += other.real;
		this.vir += other.vir;
		
		return this;
	}
	//c = a + b;
	
	//一般方法;
/*	public static Complex add(Complex one, Complex other) {
		Complex result = new Complex();
		
		result.real = one.real + other.real;
		result.vir = one.vir + other.vir;
		
		return result;
	}
*/	
	
	//优化代码;
	public static Complex add(Complex one, Complex other) {
		return new Complex(one).add(other);
	}
	
	//实现减法运算;
	//先实现a -= b;
	
	//一般方法;
/*	public Complex sub(Complex other) {
		this.real -= other.real;
		this.vir -= other.vir;
		
		return this;
	}
	//c = a - b;
	public static Complex sub(Complex one, Complex other) {
		Complex result = new Complex();
		
		result.real = one.real - other.real;
		result.vir = one.vir - other.vir;
		
		return result;
	}
*/
	//优化代码;
	//用加法实现减法;
	private static Complex opposite(Complex one) {
		return new Complex(-one.real, -one.vir);
	}
	
	public Complex sub(Complex one) {
		return this.add(opposite(one));
	}
	
	public static Complex sub(Complex one, Complex other) {
		return new Complex(one).sub(other);
		//或者 return new Complex(one).add(opposite(other));
	}
	
	//实现乘法运算;
	//先实现 a *= b;
	
	public Complex mul(Complex one) {
		double real = this.real;
		
		this.real = real * one.real - vir * one.vir;
		this.vir = real * one.vir + vir * one.real;
		
		return this;
	}
	
	//c = a * b;
	//一般方法;
/*	public static Complex mul(Complex one, Complex other) {
		Complex result = new Complex();
		
		result.real = one.real * other.real - one.vir * other.vir;
		result.vir = one.real * other.vir + one.vir * other.real;
		
		return result;
	}
*/
	//代码优化;
	public static Complex mul(Complex one, Complex other) {
		return new Complex(one).mul(other);
	}
	
	//实现除法运算;
	//先实现 a/=b;
	
	//一般方法;
/*	public void div(Complex one) {
		Complex zero = new Complex();
		if (one.equals(zero)) {
			System.out.println("除0错!");
			return;
		}
		double model = one.real * one.real + one.vir * one.vir;
		this.real /= model;
		this.vir /= model;
	}
	
	public static Complex div(Complex one, Complex other) {
		Complex zero = new Complex();
		if (other.equals(zero)) {
			System.out.println("除0错!");
			return null;
		}
		double model = other.real * other.real + other.vir * other.vir;
		Complex result = new Complex();
		result.real = (one.real * other.real + one.vir * other.vir) / model;
		result.vir = (one.real * other.vir - one.vir * other.real) / model;
		
		return result;
	}
*/
	
	//代码优化;
	private static Complex reciprocal(Complex one) {
		double model = one.real * one.real + one.vir * one.vir;
		
		if (Math.abs(model) < 1e-6) {
			return null;
		}
		
		return new Complex(one.real / model, -one.vir / model);
	}
	
	public Complex div(Complex one) {
		Complex res = reciprocal(one);
		return res == null ? null : this.mul(res);
	}
	
	public static Complex div(Complex one, Complex other) {
		Complex res = reciprocal(other);
		return res == null ? null : new Complex(one).mul(res);
	}
	
	@Override
	public String toString() {
		return "(" + real + "," + vir + ")";
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		if (obj.getClass() != this.getClass()) {
			return false;
		}
		
		Complex tmp = (Complex) obj;
		
		return Math.abs(tmp.real - this.real) < 1e-6 && Math.abs(tmp.vir - this.vir) < 1e-6;
	}
	
}

下面是Test类:

package com.mec.complex.core;

public class Test {

	public static void main(String[] args) {
		Complex someOne = new Complex(1.0, 2.0);
		Complex someTwo = new Complex(2.0, -1.0);
		
		System.out.println(someOne);
		System.out.println(someTwo);
		System.out.println(someOne == someTwo);
		System.out.println(someOne.equals(someTwo));
		
		//System.out.println(someOne.add(someTwo));
		System.out.println(Complex.add(someOne, someTwo));
		System.out.println(Complex.sub(someOne, someTwo));
		System.out.println(Complex.mul(someOne, someTwo));
		//System.out.println(someOne.mul(someTwo));
		System.out.println(Complex.div(someOne, someTwo));
	}

}

复数是由实数和虚数相加得到的数,其中虚数是有一个实数与i相乘而得到的数。因此,我们可以定义一个复数实现复数加减乘除运算。 下面是一个Python实现复数: ```python class Complex: def __init__(self, real=0, imag=0): self.real = real self.imag = imag def __add__(self, other): return Complex(self.real + other.real, self.imag + other.imag) def __sub__(self, other): return Complex(self.real - other.real, self.imag - other.imag) def __mul__(self, other): return Complex(self.real * other.real - self.imag * other.imag, self.real * other.imag + self.imag * other.real) def __truediv__(self, other): denom = other.real**2 + other.imag**2 return Complex((self.real * other.real + self.imag * other.imag)/denom, (self.imag * other.real - self.real * other.imag)/denom) def __str__(self): return f"{self.real} + {self.imag}i" ``` 在这个复数中,我们需要定义两个属性:`real`和`imag`,它们分别表示复数的实部和虚部。接下来,我们可以定义四个基本的运算操作: - 加法:重载`__add__`运算符,实现两个复数的加法。 - 减法:重载`__sub__`运算符,实现两个复数的减法。 - 乘法:重载`__mul__`运算符,实现两个复数的乘法。 - 除法:重载`__truediv__`运算符,实现两个复数除法。 最后,我们还可以定义`__str__`方法,返回复数的字符串表示,方便输出和调试。 这个复数可以用来实现复杂的计算和数据处理任务,比如信号处理、图像处理、机器学习等领域。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值