土钉墙施工:用JAVA实现复数的混合运算的源程序

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/07 07:08:43
要求是实现一个复数类,这个类本身提供四则运算,提示一下,该类结构是:
class Complex {
定义实部、虚部;

构造方法定义;

四则运算方法定义,如加法可定义为:
public Complex add(Complex oper){....}

其它的成员
}

使用该类时,可以
Complex a = new Complex(3.0, 4.), b = new Complex(3.5, 4.5), c;
c = a.add(b);
System.out.println("c="+c);
尽量简单一点~~~~~~~~

public class Complex
{
private double realPart;
private double imaginaryPart;

public Complex(double a, double b)
{
this.realPart = a;
this.imaginaryPart = b;
}

public Complex add(Complex a)
{
Complex result = new Complex(this.realPart + a.realPart, this.imaginaryPart + a.imaginaryPart);//(why?)
return result;
}

public Complex decrease(Complex a)
{
Complex result = new Complex(this.realPart - a.realPart, this.imaginaryPart - a.imaginaryPart);//(why?)
return result;
}

public Complex multiply(Complex a)
{
double newReal = this.realPart*a.realPart - this.imaginaryPart * a.imaginaryPart;
double newImaginary = this.realPart*a.imaginaryPart + this.imaginaryPart * a.realPart;
Complex result = new Complex(newReal, newImaginary);
return result;
}

public Complex divide(Complex a)
{
Complex conjugate = new Complex(this.realPart, -this.imaginaryPart);
Complex multiplication = conjugate.multiply(a);
multiplication.realPart /= this.realPart*this.realPart + this.imaginaryPart * this.imaginaryPart;
multiplication.imaginaryPart /= this.realPart*this.realPart + this.imaginaryPart * this.imaginaryPart;
return multiplication;
}

public String toString()
{
String show = this.realPart + " + " + this.imaginaryPart + "i";
return show;
}

public static void main(String [] args)
{
Complex a = new Complex (2, 3);
Complex b = new Complex (1,1);
System.out.println((a.add(b)).toString());
System.out.println((a.decrease(b)).toString());
System.out.println((a.multiply(b)).toString());
System.out.println((a.divide(b)).toString());

}

}

没人回答我来试试,按自己的理解写了个程序,请楼主看对不对了。
public class 复数
{
double 实部;

double 虚部;

public 复数(double 实部, double 虚部)
{
this.实部 = 实部;
this.虚部 = 虚部;
}

public static 复数 加法(复数 a, 复数 b)
{
复数 c = new 复数(0, 0);
c.实部 = a.实部 + b.实部;
c.虚部 = a.虚部 + b.虚部;
return c;
}

public static 复数 减法(复数 a, 复数 b)
{
复数 c = new 复数(0, 0);
c.实部 = a.实部 - b.实部;
c.虚部 = a.虚部 - b.虚部;
return c;
}

public static 复数 乘法(复数 a, 复数 b)
{
复数 c = new 复数(0, 0);
c.实部 = a.实部 * b.实部 - a.虚部 * b.虚部;
c.虚部 = a.虚部 * b.实部 + a.实部 * b.虚部;
return c;
}

public static 复数 除法(复数 a, 复数 b)
{

复数 c = new 复数(0, 0);

// 判断复数b的实部和虚部是否同时为0
if ((b.实部 <= -0.00000000001 || b.实部 >= 0.00000000001)
&& (b.虚部 <= -0.00000000001 || b.虚部 >= 0.00000000001))
{
c.实部 = (a.实部 * b.实部 + a.虚部 * b.虚部) / (b.实部 * b.实部 + b.虚部 * b.虚部);
c.虚部 = (a.虚部 * b.实部 - a.实部 * b.虚部) / (b.实部 * b.实部 + b.虚部 * b.虚部);
}

return c;
}

@Override
public String toString()
{
String s = this.实部 + "+" + this.虚部 + "i";

return s;
}

}
//=============================
//下面这个类是用来测试的
public class ComplexTest
{

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
复数 a = new 复数(2, 3);
复数 b = new 复数(4, 5);
a = 复数.加法(a, b);
System.out.println(a);
}

}
//=========================
//运行结果如下
6.0+8.0i