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

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));
	}

}

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值