现在的位置: 首页 > 综合 > 正文

用java写一个复数类Complex

2013年01月16日 ⁄ 综合 ⁄ 共 4993字 ⁄ 字号 评论关闭

由于java库中没有提供复数的操作,应同学的需求,写了一个复数的类。有两个构造方法不带参数和带参数的,不带参数时默认实部虚部都为0。还有这些基本的方法,看名字就知道了。

  public double getReal()      { return this.real; }  //得到实部
 public double getImage()     { return this.img; }  //得到虚部
 public double getReal(Complex c) { return c.real;  }  //得到复数c的实部
 public double getImage(Complex c) { return c.img;   }  //得到复数c的虚部
 public void setReal (double real) { this.real=real;  }  //设置实部
 public void setImage(double img) { this.img =img;  }  //设置虚部

 public Complex addComplex(Complex a,Complex b) //两个复数相加,结果返回

 public Complex decComplex(Complex a,Complex b) //两个复数相减,结果返回

 public Complex mulComplex(Complex a,Complex b) //两个复数相乘,结果返回

 public Complex divComplex(Complex a,Complex b) //两个复数相除,结果返回

 public void addComplex(Complex cplx)    //加上一个复数

 public void decComplex(Complex cplx)    //减去一个复数

 public void mulComplex(Complex cplx)    //乘与一个复数

 public void divComplex(Complex cplx)    //除去一个复数

除此之外还有一些测试的代码,就一个文件Complex.java,全部代码如下(可以直接编译运行):

 

/*
*        文件名:Complex.java
*        作者    :yangdk
*        E-mail:jidacun@163.com
*        主页    :
http://blog.csdn.net/yang_dk
*/


public class Complex
{
    
double real,img;    //实部和虚部
    
    
public Complex()        //默认构造方法
    {
        
this.real=0;
        
this.img =0;
    }

    
public Complex(double real,double img)    //带参数的构造方法
    {
        
this.real=real;
        
this.img =img;
    }

    
public double getReal()                        return this.real;    }        //得到实部
    public double getImage()                    return this.img;    }        //得到虚部
    public double getReal(Complex c)    {    return c.real;        }        //得到复数c的实部,这两个函数看起来好像有点多余,但在特殊的情况下会有用
    public double getImage(Complex c)    {    return c.img;            }        //得到复数c的虚部
    public void setReal    (double real)    {    this.real=real;        }        //设置实部
    public void setImage(double img)    {    this.img =img;        }        //设置虚部

    
public Complex addComplex(Complex a,Complex b)    //两个复数相加,结果返回
    {
        Complex temp 
=new Complex();
        temp.real
=a.real+b.real;
        temp.img 
=a.img +b.img;
        
return temp;
    }
    
    
public Complex decComplex(Complex a,Complex b)    //两个复数相减,结果返回
    {
        Complex temp 
= new Complex();
        temp.real 
= a.real - b.real;
        temp.img  
= a.img  - b.img;
        
return temp;
    }

    
public Complex mulComplex(Complex a,Complex b)    //两个复数相乘,结果返回
    {
        Complex temp 
= new Complex();
        temp.real 
= a.real*b.real-a.img*b.img;
        temp.img    
= a.real*b.img+a.img*b.real;
        
return temp;
    }

    
public Complex divComplex(Complex a,Complex b)    //两个复数相除,结果返回
    {
        Complex temp 
= new Complex();
        temp.real
=(a.real*b.real+a.img*b.img)/(b.real*b.real+b.img*b.img);
        temp.img 
=(a.img*b.real-a.real*b.img)/(b.real*b.real+b.img*b.img);
        
return temp;
    }

    
    
public void addComplex(Complex cplx)                //加上一个复数
    {
        
this.real=this.real+cplx.real;
        
this.img =this.img +cplx.img;
    }

    
public void decComplex(Complex cplx)                //减去一个复数
    {
        
this.real=this.real-cplx.real;
        
this.img =this.img -cplx.img;
    }

    
public void mulComplex(Complex cplx)                //乘与一个复数
    {
        
double temp=this.real;    //下一行代码会改变this.real的值,先用一个临时变量存起来
        this.real=this.real*cplx.real-this.img*cplx.img;
        
this.img =temp*cplx.img+this.img*cplx.real;
    }

    
public void divComplex(Complex cplx)                //除去一个复数
    {
        
double temp=this.real;    //下一行代码会改变this.real的值,先用一个临时变量存起来
        this.real=(this.real*cplx.real+this.img*cplx.img)/(cplx.real*cplx.real+cplx.img*cplx.img);
        
this.img =(this.img*cplx.real-temp*cplx.img)/(cplx.real*cplx.real+cplx.img*cplx.img);
    }


    
/****以上是这个复数类的所有函数,下面是一些测试的代码****/

    
public void printComplex()        //在console端输出这个复数,测试用
    {        
        System.out.println(
""+this.real+"+"+this.img+"i");//这里可以填加一点代码以判断虚部的正负,这个工作我没有做
    }

    
    
public static void main(String[] args)        //测试代码
    {
        Complex cc
=new Complex(4,8);
        cc.printComplex();
        Complex dd
=new Complex(2,2);
        dd.printComplex();
        System.out.println(
"-----------------");
        Complex ff
=new Complex();
        
        ff
=ff.addComplex(cc,dd);
        ff.printComplex();
        ff
=ff.decComplex(cc,dd);
        ff.printComplex();
        ff
=ff.mulComplex(cc,dd);
        ff.printComplex();
        ff
=ff.divComplex(cc,dd);
        ff.printComplex();
        System.out.println(
"-----------------");
         cc.addComplex(dd);
        cc.printComplex();        
        cc
=new Complex(4,8);
        cc.decComplex(dd);
        cc.printComplex();
        cc
=new Complex(4,8);
        cc.mulComplex(dd);
        cc.printComplex();
        cc
=new Complex(4,8);
        cc.divComplex(dd);
        cc.printComplex();
        System.out.println(
"-----------------");
        cc.setReal(
123);
        cc.setImage(
456);
        cc.printComplex();
        System.out.println(
""+cc.getReal()+"+"+cc.getImage()+"i");    
        System.out.println(
"-----------------");
    }

}

抱歉!评论已关闭.