用java实现的复数类
/**
* 在这里给出对类 Complex 的描述。
* 这是一个复数类,本来想用重载运算符来设计类的,上网查了一下资料发现java没有运算符重载,只好作罢.
* 复数类
* @作者(你的名字)
* @版本(一个版本号或者一个日期)
*/
public class Complex
{
private double real;//实部
private double imayinary;//虚部
//构造函数
public Complex()
{
this.real = 0;
this.imayinary = 0;
}
public Complex(double Real,double Imayinary)
{
this.real = Real;
this.imayinary = Imayinary;
}
//属性方法
//获得实步
public double getReal()
{
return real;
}
//获得虚部
public double getImayinary()
{
return imayinary;
}
//设置实部
public double setReal(double Real)
{
this.real = Real;
return real;
}
//设置虚部
public double setImayinary(double Imayinary)
{
this.imayinary = Imayinary;
return imayinary;
}
//加法运算
//a-b
public static Complex Add(Complex a,Complex b)
{
Complex result = new Complex();
double c = a.getReal() + b.getReal();
result.setReal(c);
double d = a.getImayinary() + b.getImayinary();
result.setImayinary(d);
return result;
}
//a为加数
public Complex Add(Complex a)
{
double c = a.getReal() + this.getReal();
this.setReal(c);
double d = a.getImayinary() + this.getImayinary();
this.setImayinary(d);
return this;
}
//减法运算
//静态减法
public static Complex Reduce(Complex a,Complex b)
{
Complex result = new Complex();
double c = a.getReal() - b.getReal();
result.setReal(c);
double d = a.getImayinary() - b.getImayinary();
result.setImayinary(d);
return result;
}
//a为减数
public Complex ReduceFirst(Complex a)
{
double c = this.getReal() - a.getReal();
this.setReal(c);
double d = this.getImayinary() - a.getImayinary();
this.setImayinary(d);
return this;
}
//a为被减数
public Complex ReduceLast(Complex a)
{
double c = this.getReal() - a.getReal();
this.setReal(c);
double d = this.getImayinary() - a.getImayinary();
this.setImayinary(d);
return this;
}
//乘法运算
public static Complex multiply(Complex a,Complex b)
{
Complex result = new Complex();
double c = a.getReal() * b.getReal() - a.getImayinary() * b.getImayinary();
result.setReal(c);
double d = a.getReal() * b.getImayinary() + a.getImayinary() * b.getReal();
result.setImayinary(d);
return result;
}
//a为乘数
public Complex multiply(Complex a)
{
double c = a.getReal() * this.getReal() - a.getImayinary() * this.getImayinary();
this.setReal(c);
double d = a.getReal() * this.getImayinary() + a.getImayinary() * this.getReal();
this.setImayinary(d);
return this;
}
//除法运算
//静态除法
public static Complex divide(Complex a,Complex b)
{
Complex result = new Complex();
double c = (a.getReal() * b.getReal() + a.getImayinary() * b.getImayinary()) / ( b.getReal() * b.getReal() + b.getImayinary() * b.getImayinary());
result.setReal(c);
double d = (a.getImayinary() * b.getReal() - a.getReal() * b.getImayinary()) / (b.getReal() * b.getReal() + b.getImayinary() * b.getImayinary());
result.setImayinary(d);
return result;
}
//b为除数
public Complex divideFirst(Complex b)
{
double c = (this.getReal() * b.getReal() + this.getImayinary() * b.getImayinary()) / ( b.getReal() * b.getReal() + b.getImayinary() * b.getImayinary());
this.setReal(c);
double d = (this.getImayinary() * b.getReal() - this.getReal() * b.getImayinary()) / (b.getReal() * b.getReal() + b.getImayinary() * b.getImayinary());
this.setImayinary(d);
return this;
}
//a为被除数
public Complex divideLast(Complex a)
{
double c = (a.getReal() * this.getReal() + a.getImayinary() * this.getImayinary()) / ( this.getReal() * this.getReal() + this.getImayinary() * this.getImayinary());
this.setReal(c);
double d = (a.getImayinary() * this.getReal() - a.getReal() * this.getImayinary()) / (this.getReal() * this.getReal() + this.getImayinary() * this.getImayinary());
this.setImayinary(d);
return this;
}
//显示复数
public String outString()
{
return this.getReal() + "+" + this.getImayinary() + "i";
}
}