第二小组

复数类实现 by freeyun

 

class  complex
{
      
double  real,imag;
      complex ()    
{}
      complex(
double  r,double  i)
           
{   real=r;
               imag
=i;
           }
 
      complex     addition(complex x,complex   y)
           
{   this.real=x.real+y.real;
               
this.imag=x.imag+y.imag;
               
return(this) ;
            }


     complex      subtraction(complex x,complex   y)       
          
{   this.real=x.real-y.real;
               
this.imag=x.imag-y.imag;
               
return(this) ;              
           }


     complex     multiplication(complex x,complex   y)
          
{    this.real=(x.real)*(y.real)-(x.imag)*(y.imag);
               
this.imag=(x.real)*(y.imag)+(x.imag)*(y.real);
               
return(this) ;             
           }


      complex     division(complex x,complex   y)
          
{    this.real=((x.real)*(y.real)+(x.imag)*(y.imag))/((y.real)*(y.real)+(y.imag)*(y.imag));
               
this.imag=((x.imag)*(y.real)-(x.real)*(y.imag))/((y.real)*(y.real)+(y.imag)*(y.imag));
               
return(this) ;
          }


}
               

public    class   C
{  
    
public  static  void  main(String    args[])
     
{   
        complex   c1
=new  complex(2,4);
        complex   c2
=new  complex(4,8);
        complex   c
=new   complex();
         c.addition(c1,c2);
         System.
out.println("c=c1(2,4)+c2(4,8)"+" "+c.real+","+c.imag);
         c.subtraction(c1,c2);
         System.
out.println("c=c1(2,4)-c2(4,8)"+" "+c.real+","+c.imag);
         c.multiplication(c1,c2);
         System.
out.println("c=c1(2,4)*c2(4,8)"+" "+c.real+","+c.imag);
         c.division(c1,c2);
         System.
out.println("c=c1(2,4)/c2(4,8)"+" "+c.real+","+c.imag);
      }

}
 




name     C.java
运行结果:c=c1(2,4)+c2(4,8)     6.0 ,12.0
                    c=c1(2,4)-c2(4,8)    -2.0 ,-4.0
                    c=c1(2,4)*c2(4,8)    -24.0,32.0
                    c=c1(2,4)/c2(4,8)      0.5,0.0

求助:为什么我在函数中用getReal,getImag分别返回各函数的实部和虚部实现不了?

posted on 2005-07-29 00:16 第二小组 阅读(920) 评论(2)  编辑  收藏 所属分类: 小组交流

Feedback

# re: 复数类实现 by freeyun 2005-07-29 00:27 第二小组

什么问题,不明白.
为什么实现不了?  回复  更多评论   

# re: 复数类实现 by freeyun 2005-11-02 14:34 hh

我也是学java的怎么就入不了门那 请高人指点 谢谢  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航: