复数类

                                   用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";
    }
   
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值