331929879

复数运算

class Complex{
 double real;
    double image;

    public Complex add(Complex another){
  Complex res = new Complex();
  res.real = real + another.real;
  res.image = image + another.image;
  return res;
    }
    
 public Complex minus(Complex another){// 当前复数与方法参数another相减;
  Complex r = new Complex();
  r.real = real - another.real;
  r.image = image - another.image;
  return r;
 }
 
 public Complex multi(Complex another){ //当前复数与方法参数another相乘(注意复数相乘); 
  Complex r = new Complex();
  r.real = real * another.real - image * another.image;
  r.image = real * another.image + image * another.real ;
  return r;
 }
 
 public void disp(){
  if( image == 0 ){
   System.out.print(real);
  }
  else
  {
   System.out.print(real + " + " + image + "i");
  }
 }
 
 public Complex(double real , double image){
  this.real = real;
  this.image = image ;
 }
 public Complex(){
  this(0 , 0);
 }
 
 public static void main (String[] args) {
  Complex c1 = new Complex(2 , 3);
  Complex c2 = new Complex(5 , 4);
  Complex res;
  
  c1.disp();
  System.out.print("  +   ");
  c2.disp();
  System.out.print("  =   ");
  c1.add(c2).disp();
  System.out.println();
  
  c1.disp();
  System.out.print("  -   ");
  c2.disp();
  System.out.print("  =   ");
  c1.minus(c2).disp();
  System.out.println();
  
  
  c1.disp();
  System.out.print("  *   ");
  c2.disp();
  System.out.print("  =   ");
  c1.multi(c2).disp();
  System.out.println();
    }
}

posted on 2007-10-12 09:13 小飞 阅读(269) 评论(0)  编辑  收藏


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


网站导航: