⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 complex1.java

📁 复数类,实现了两个复数的加,减,乘,除以及一个复数和一个实数的加,减,乘,除.
💻 JAVA
字号:
public class Complex
{
   private double real,im; //实部,虚部 
   public Complex(double a, double b) 
   { 
      this.real = a; 
      this.im = b; 
    } 

    public Complex add(Complex b) 
    { 
      Complex result = new Complex(this.real + b.real, this.im + b.im);
      return result; 
     } 

    public Complex decrease(Complex b) 
   { 
     Complex result = new Complex(this.real - b.real, this.im - b.im); 
     return result; 
    } 

    public Complex multiply(Complex b) 
   { 
      double newreal = this.real*b.real - this.im * b.im; 
      double newim = this.real*b.im + this.im * b.real; 
      Complex result = new Complex(newreal, newim); 
      return result; 
    } 

    public Complex divide(Complex b) 
   { 
     Complex conjugate = new Complex(this.real, -this.im); 
     Complex result = conjugate.multiply(b); 
     result.real /= this.real*this.real + this.im * this.im; 
     result.im /= this.real*this.real + this.im * this.im; 
     return result; 
    } 
  
    public Complex add(double a)
    {
      Complex result=new Complex(this.real+a,this.im);
      return result;
     }

    public Complex decrease(double a)
    {
       Complex result=new Complex(this.real-a,this.im);
       return result;
     }

    public Complex multiply(double a)
    {
        double newreal=this.real*a;
        double newim=this.im*a;
        Complex result=new Complex(newreal,newim);
        return result;
     }

     public Complex divide(double a)
     {
        double newreal=this.real/a;
        double newim=this.im/a;
        Complex result=new Complex(newreal,newim);
        return result;
      }


    public String toString()
    { 
       String str = this.real + " + " + this.im + "i"; 
       return str; 
     } 


     public static void main(String[] args)   
     { 
       Complex a = new Complex (3, 4); 
       Complex b = new Complex (1,2);   
       double c=5;     
       System.out.println((a.add(b)).toString()); 
       System.out.println((a.decrease(b)).toString()); 
       System.out.println((a.multiply(b)).toString()); 
       System.out.println((a.divide(b)).toString()); 
       System.out.println((a.add(c)).toString()); 
       System.out.println((a.decrease(c)).toString()); 
       System.out.println((a.multiply(c)).toString()); 
       System.out.println((a.divide(c)).toString());  
     }      
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -