【java面对对象】复数类型加减乘除的实现

class Complex {
	private double Realpart;
	private double Imagepart;

	Complex(double x, double y) {
		this.Realpart = x;
		this.Imagepart = y;
	}

	Complex() {
		this.Realpart = 0;
		this.Imagepart = 0;
	}

	public double GetReal(Complex c) {
		return c.Realpart;
	}

	public double GetImage(Complex c) {
		return c.Imagepart;
	}

	public Complex Add(Complex c2)// 加法运算
	{
		Complex sum = new Complex();
		sum.Realpart = this.Realpart + c2.Realpart;
		sum.Imagepart = this.Imagepart + c2.Imagepart;
		return sum;
	}

	public Complex Sub(Complex c2)// 减法运算
	{
		Complex dif = new Complex();
		dif.Realpart = this.Realpart - c2.Realpart;
		dif.Imagepart = this.Imagepart - c2.Imagepart;
		return dif;
	}

	public Complex Mul(Complex c2)// 乘法运算
	{
		Complex cheng = new Complex();
		cheng.Realpart = this.Realpart * c2.Realpart - this.Imagepart
				* c2.Imagepart;
		cheng.Imagepart = this.Realpart * c2.Imagepart + this.Imagepart
				* c2.Realpart;
		return cheng;
	}

	public Complex Div(Complex c2)// 除法运算
	{
		Complex Null = new Complex();
		Complex chu = new Complex();
		chu.Realpart = (this.Realpart * c2.Realpart + this.Imagepart
				* c2.Imagepart)
				/ (c2.Realpart * c2.Realpart + c2.Imagepart * c2.Imagepart);
		chu.Imagepart = (this.Imagepart * c2.Realpart - this.Realpart
				* c2.Imagepart)
				/ (c2.Realpart * c2.Realpart + c2.Imagepart * c2.Imagepart);
		if ((c2.Realpart * c2.Realpart + c2.Imagepart * c2.Imagepart) == 0) {
			System.out.println("the num doesnt exist");
			return Null;
		} else
			return chu;
	}

	public void Show(Complex c)// 复数类型的表示方法
	{
		if (c.Realpart != 0 && c.Imagepart != 0)
			System.out.printf("%.2f+%.2fi", c.Realpart, c.Imagepart);
		else if (c.Realpart != 0 && c.Imagepart == 0)
			System.out.printf("%.2f", c.Realpart);
		else if (c.Realpart == 0 && c.Imagepart != 0)
			System.out.printf("%.2fi", c.Imagepart);
		else if (c.Realpart == 0 && c.Imagepart == 0)
			System.out.printf("%d", 0);
	}

	public void helpTestShow(Complex C2) {
		Show(this);
		System.out.println();
		Show(C2);
		System.out.println();
		System.out.printf("he:");
		Show(this.Add(C2));
		System.out.println();
		System.out.printf("cha:");
		Show(this.Sub(C2));
		System.out.println();
		System.out.printf("ji:");
		Show(this.Mul(C2));
		System.out.println();
		System.out.printf("shang:");
		Show(this.Div(C2));
		System.out.println();
	}

}

public class Complex_ {
	public static void TestComplex()// 测试代码
	{
		Complex C1 = new Complex(520, 0);
		Complex C2 = new Complex(1314, 0);
		C1.helpTestShow(C2);
		Complex C3 = new Complex(0.5, 0.7);
		Complex C4 = new Complex(0.5, -0.7);
		C3.helpTestShow(C4);
		Complex C5 = new Complex(-8, 0);
		Complex C6 = new Complex(0, 10);
		C5.helpTestShow(C6);
		Complex C7 = new Complex(0, 0);
		Complex C8 = new Complex(0, -90);
		C7.helpTestShow(C8);
		Complex C9 = new Complex(2, 3);
		Complex C10 = new Complex(4, -9);
		C9.helpTestShow(C10);
	}

	public static void main(String[] args) {
		TestComplex();

	}

}



/*
测试数据:
520.00
1314.00
he:1834.00
cha:-794.00
ji:683280.00
shang:0.40
0.50+0.70i
0.50+-0.70i
he:1.00
cha:1.40i
ji:0.74
shang:-0.32+0.95i
-8.00
10.00i
he:-8.00+10.00i
cha:-8.00+-10.00i
ji:-80.00i
shang:0.80i
0
-90.00i
he:-90.00i
cha:90.00i
ji:0
shang:0
2.00+3.00i
4.00+-9.00i
he:6.00+-6.00i
cha:-2.00+12.00i
ji:35.00+-6.00i
shang:-0.20+0.31i

*/
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值