第二小組

          復數類實現 - kb

          一共三個文件:
                   ComplexException.java   :  Complex類所應的異常
                   Complex.java   : Complex類實現
                   TestComplex.java   : 測試Complex類

          /**
           * File: ComplexException.java
           * author: kb @ 2005-7-28
           
          */


          package kb.complex;

          public class ComplexException extends Exception {
              
          public ComplexException( Exception e ) 
          {
                  super( e );
              }

              
              
          public ComplexException() {
                  super();
              }

              
              
          public String toString() {
                  
          return "ComplexException: Maybe init string's form uncorrect."
          ;
              }

          }



          /**
           * File: Complex.java
           * author: kb @ 2005-7-28
           
          */


          package kb.complex;

          import java.util.StringTokenizer;

          public class Complex {

              
          private double
           real;
              
          private double
           imag;
              
              
          private static final String delimiters = " (),\t"
          ;
              
              
          /**
               * -*- constructors -*-
               
          */

              
          public Complex() {}
              
              
          public Complex( double r ) { real = r; }
              
              
          public Complex( Complex c ) { real = c.real; imag = c.imag; }
              
              
          public Complex( double r, double i ) { real = r; imag = i; }
              
              
          public Complex( String c ) throws ComplexException {
                  
                  
          if( c == null || c.length() == 0
           )
                  
          {
                      real 
          = imag = 0.0
          ;
                      
          return
          ;
                  }

                  
                  StringTokenizer t 
          = new StringTokenizer( c, delimiters );
                  String[] data 
          = new
           String[t.countTokens()];
                  
          forint i = 0; i < data.length; i++ ) 
          {
                      data[ i ] 
          =
           t.nextToken();
                  }

                  
                  
          switch( data.length )
                  
          {
                  
          case 1:        //form: ",imag" "real," "real" and "(,imag)" "(real,)" "(real)"

                      double tmp;
                      tmp 
          = Double.parseDouble( data[0
          ] );
                      
                      String str 
          = c.split(",")[0
          ];
                      
          if( str.indexOf( data[0] ) >= 0 ) 
          {
                          real 
          =
           tmp;
                      }
           else {
                          imag 
          =
           tmp;
                      }

                      
                      
          break;
                  
          case 2:        //form: "real,imag" and "(real,imag)"

                      real = Double.parseDouble( data[0] );
                      imag 
          = Double.parseDouble( data[1
          ] );
                      
          break
          ;
                  
          default
          :
                      
          throw new
           ComplexException();
                  }

                  
              }

              
              
          /**
               * -*- accessor definition -*- 
               
          */

              
          public double getReal() return real; }
              
          public double getImag() return imag; }
              
              
          public void setReal( double r ) { real = r; }
              
          public void setImag( double i ) { imag = i; }
              
              
          public String toString() {
                  
          return new String( "(" + real + "," + imag + ")"
           );
              }

              
              
          /**
               * -*- 實例運算方法,對自身作運算。-*-
               * 還應該加入復數與double類型數據的實例運算方法,但
               * 考慮到代碼長度,便省了.
               
          */

              
          public Complex add( Complex c ) {
                  real 
          +=
           c.real;
                  imag 
          +=
           c.imag;
                  
          return this
          ;
              }

              
              
          public Complex minus( Complex c ) {
                  real 
          -=
           c.real;
                  imag 
          -=
           c.imag;
                  
          return this
          ;
              }

              
              
          public Complex multiply( Complex c ) {
                  
          double tmp =
           real;
                  real 
          = real * c.real - imag *
           c.imag;
                  imag 
          = tmp * c.imag + imag *
           c.real;
                  
          return this
          ;
              }

              
              
          public Complex divide( Complex c ) {
                  
          this.multiply( new Complex( c.real, -
           c.imag ) );
                  
                  
          double tmp = c.real * c.real + c.imag *
           c.imag;
                  real 
          /=
           tmp;
                  imag 
          /=
           tmp;
                  
          return this
          ;
              }


              
          /**
               * -*- 類運算方法,提供兩個復數的運算。-*-
               * 還應該加入復數與double類型數據的靜態運算方法,但
               * 考慮到代碼長度,便省了.
               
          */

              
          public static Complex add( Complex c1, Complex c2 ) {
                  
          return new Complex( c1.real + c2.real, c1.imag +
           c2.imag );
              }

              
              
          public static Complex minus( Complex c1, Complex c2 ) {
                  
          return new Complex( c1.real - c2.real, c1.imag -
           c2.imag );
              }

              
              
          public static Complex multiply( Complex c1, Complex c2 ) {
                  
          double real = c1.real * c2.real - c1.imag *
           c2.imag;
                  
          double imag = c1.real * c2.imag + c1.imag *
           c2.real;
                  
          return new
           Complex( real, imag );
              }

              
              
          public static Complex divide( Complex c1, Complex c2 ) {
                  Complex c 
          = new Complex( c2.real, -
           c2.imag );
                  
          double tmp = c2.real * c2.real + c2.imag *
           c2.imag;
                  
                  c.multiply( c1 );
                  
                  c.real 
          /=
           tmp;
                  c.imag 
          /=
           tmp;
                  
          return
           c;
              }

              
              
          /*
               * 復數雖沒有大小比較算法,但比較復數模的大小卻是經常的。
               * 本方法實現的就是比較兩個復數模的大小。
               
          */

              
          public int compareTo( Complex c ) {
                  
          double a = Math.pow( real, 2 ) + Math.pow( imag, 2
           );
                  
          double b = Math.pow( c.real, 2 ) + Math.pow( c.imag, 2
           );
                  
          return ( a > b ? 1 : ( a < b ? -1 : 0
           ) );
              }


          }



          /**
           * File: TestComplex.java
           * author: kb @ 2005-7-28
           
          */


          package kb.complex;

          public class TestComplex {

              
          /**
               * 測試不是很細致,所以可能仍會留有Bugs
               
          */

              
          public static void main(String[] args) throws Exception {
                  Complex c1;
                  Complex c2;
                  
                  String d1 
          = "(1,2)"
          ;
                  String d2 
          = "1,2"
          ;
                  
                  String dd1 
          = " (   1 , 2 ) "
          ;
                  String dd2 
          = " 1 , 2 "
          ;
                  
                  String ddd1 
          = "(2,)";    //"(,2)" "(2)" : also ok

                  String ddd2 = "2,";        //",2" "2" : also ok
                  
                  String d 
          = "";            //null also ok

                  
                  c1 
          = new
           Complex( d );
                  System.
          out.println( "c1 by d: " +
           c1 );
                  
                  c1 
          = new
           Complex( d1 );
                  System.
          out.println( "c1 by d1: " +
           c1 );
                  
                  c2 
          = new
           Complex( d2 );
                  System.
          out.println( "c2 by d2: " +
           c2 );

                  System.
          out.println( "c1 / c2(Complex.divide(c1, c2)): " +
           Complex.divide(c1, c2) );
                  System.
          out.println( "c1 /= c2(c1.divide(c2)): " +
           c1.divide(c2) );
                  
                  c1 
          = new
           Complex( dd1 );
                  System.
          out.println( "c1 by dd1: " +
           c1 );
                  
                  c2 
          = new
           Complex( dd2 );
                  System.
          out.println( "c2 by dd2: " +
           c2 );

                  System.
          out.println( "c1 * c2(Complex.multiply(c1, c2)): " +
           Complex.multiply(c1, c2) );
                  System.
          out.println( "c1 *= c2(c1.multiply(c2)): " +
           c1.multiply(c2) );
                  
                  c1 
          = new
           Complex( ddd1 );
                  System.
          out.println( "c1 by ddd1: " +
           c1 );
                  
                  c2 
          = new
           Complex( ddd2 );
                  System.
          out.println( "c2 by ddd2: " +
           c2 );
                  
                  System.
          out.println( "c1 compareTo c2: " +
           c1.compareTo(c2) );
                  
                  System.
          out.println( "c1 += c2(c1.add(c2)): " +
           c1.add(c2) );
                  System.
          out.println( "c1 + c2(Complex.minus(c1,c2)): " +
           Complex.minus(c1,c2) );

              }


          }



          輸出結果如下:
          c1 by d: (0.0,0.0)
          c1 by d1: (
          1.0,2.0)
          c2 by d2: (
          1.0,2.0)
          c1 
          / c2(Complex.divide(c1, c2)): (1.0,0.0)
          c1 
          /= c2(c1.divide(c2)): (1.0,0.0)
          c1 by dd1: (
          1.0,2.0)
          c2 by dd2: (
          1.0,2.0)
          c1 
          * c2(Complex.multiply(c1, c2)): (-3.0,4.0)
          c1 
          *= c2(c1.multiply(c2)): (-3.0,4.0)
          c1 by ddd1: (
          2.0,0.0)
          c2 by ddd2: (
          2.0,0.0)
          c1 compareTo c2: 
          0
          c1 
          += c2(c1.add(c2)): (4.0,0.0)
          c1 
          + c2(Complex.minus(c1,c2)): (2.0,0.0)

          posted on 2005-07-28 22:44 第二小組 閱讀(2057) 評論(6)  編輯  收藏 所屬分類: 小組交流

          Feedback

          # re: 復數類實現 - kb 2005-07-29 00:08 Flair@ZJU

          :p, so many hidden bugs... anyway, it's nice to have a try.
          Here is a good reference: org.apache.commons.math.complex.Complex  回復  更多評論   

          # re: 復數類實現 - kb 2005-07-29 00:51 第二小組

          .......so many hidden bugs......?
          Really?
          看來我的代碼還是很爛啊.
          可不可以指點一下?讓我也長長記性.  回復  更多評論   

          # re: 復數類實現 - kb 2005-07-29 01:06 第二小組

          剛才看了一下org.apache.commons.math.complex.Complex ,
          確實感覺出差距來了,但不知道它內部是如何實現的,那樣的話會更好的理解.其實很多的復數算法我都不知道是怎么算的,比如pow,sqrt,tan,etc......  回復  更多評論   

          # re: 復數類實現 - kb 2005-07-29 01:54 Flair@ZJU

          U cannot see the src code..?
          Just download it from http://jakarta.apache.org/site/downloads/downloads_commons-math.cgi  回復  更多評論   

          # 復數類實現 2006-10-19 17:19 堅持到底[匿名]

          /*復數類的實現:湖南農業大學信息科學技術學院計算機科學與技術八班(堅持到底) 2006-10-19 */
          /*復數加法公式:(a+bi)+(c+di)=(a+c)+(b+d)i*/
          /*復數減法公式:(a+bi)-(c+di)=(a-c)+(b-d)i*/
          /*復數乘法公式:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i*/
          /*復數除法公式:(a+bi)/(c+di)=[(ac+bd)/(c*c+d*d)]+[(bc-ad)/(c*c+d*d)]i*/
          public class ComplexNumber {
          //屬性部分定義
          //定義復數部分實部
          double m_dRealpart;
          //定義復數部分虛部
          double m_dImaginpart;
          //定義一個中間變量
          double R_dRealpart;
          //定義一個除法實部
          double Realpart;


          //方法部分定義
          //構造函數實現部分
          public ComplexNumber (double r,double I){
          this.m_dRealpart=r;
          this.m_dImaginpart=I;
          }

          //獲取復數實部值
          double getRealpart(){
          return this.m_dRealpart;
          }

          //獲取復數虛部值
          double getImaginpart(){
          return this.m_dImaginpart;
          }

          //設置復數實部值
          void setRealpart(double temp){
          this.m_dRealpart=temp;
          }

          //設置復數虛部值
          void setImaginpart(double temp){
          this.m_dImaginpart=temp;
          }

          //兩個復數對象加法運算
          ComplexNumber complexAdd(ComplexNumber c){
          this.m_dRealpart=this.m_dRealpart+c.m_dRealpart;
          this.m_dImaginpart=this.m_dImaginpart+c.m_dImaginpart;
          return this;
          }

          //兩個復數對象的減法運算
          ComplexNumber complexMinus(ComplexNumber c){
          this.m_dRealpart=this.m_dRealpart-c.m_dRealpart;
          this.m_dImaginpart=this.m_dImaginpart-c.m_dImaginpart;
          return this;
          }


          //兩個復數對象的乘法運算
          ComplexNumber complexChen(ComplexNumber c){
          R_dRealpart=this.m_dRealpart;
          this.m_dRealpart=this.m_dRealpart*c.m_dRealpart-this.m_dImaginpart*c.m_dImaginpart;
          this.m_dImaginpart=R_dRealpart*c.m_dImaginpart+c.m_dRealpart*this.m_dImaginpart;
          return this;
          }

          //兩個復數對象的除法運算
          ComplexNumber complexChu(ComplexNumber c){
          Realpart=this.m_dRealpart;
          this.m_dRealpart=(Realpart*c.m_dRealpart+this.m_dImaginpart*c.m_dImaginpart)/(c.m_dRealpart*c.m_dRealpart+c.m_dImaginpart*c.m_dImaginpart);
          this.m_dImaginpart=(this.m_dImaginpart*c.m_dRealpart-Realpart*c.m_dImaginpart)/(c.m_dRealpart*c.m_dRealpart+c.m_dImaginpart*c.m_dImaginpart);
          return this;
          }


          //構造一個a+bi的字符串形式
          public String toString(){
          String strTemp="";
          strTemp=this.m_dRealpart+"+"+
          this.m_dImaginpart+"i";
          return strTemp;
          }


          public static void main(String[] args) {
          //輸出兩個復數相加結果
          ComplexNumber complex1 = new ComplexNumber(10.0,10.0);
          System.out.println("m_dRealPart:"+complex1.m_dRealpart);
          ComplexNumber complex2 = new ComplexNumber(3.0,5.0);
          System.out.println("\n\ncomplex1:"+complex1.toString());
          System.out.println("complex2:"+complex2.toString());
          complex1.complexAdd(complex2);
          System.out.println("complex1+complex2=" + complex1.toString());

          //輸出兩個復數相減結果
          ComplexNumber complex3 = new ComplexNumber(10.0,10.0);
          System.out.println("\n\ncomplex3:"+complex3.toString());
          System.out.println("complex2:"+complex2.toString());
          complex3.complexMinus(complex2);
          System.out.println("complex3-complex2="+complex3.toString());

          //輸出兩個復數相乘結果
          ComplexNumber complex4 = new ComplexNumber(-1.0,2.0);
          System.out.println("\n\ncomplex4:"+complex4.toString());
          ComplexNumber complex5 = new ComplexNumber(2.0,-3.0);
          System.out.println("complex5:"+complex5.toString());
          complex4.complexChen(complex5);
          System.out.println("complex4*complex5="+complex4.toString());


          //輸出兩個復數相除結果
          ComplexNumber complex6 = new ComplexNumber(1.0,2.0);
          System.out.println("\n\ncomplex6:"+complex6.toString());
          ComplexNumber complex7 = new ComplexNumber(2.0,3.0);
          System.out.println("complex7:"+complex7.toString());
          complex6.complexChu(complex7);
          System.out.println("complex6/complex7="+complex6.toString());

          }

          }
            回復  更多評論   

          # 復數類實現 2006-10-19 17:22 堅持到底[匿名]

          //file: ComplexNumber.java
          /*復數類的實現:湖南農業大學信息科學技術學院計算機科學與技術八班(堅持到底) 2006-10-19 */
          /*復數加法公式:(a+bi)+(c+di)=(a+c)+(b+d)i*/
          /*復數減法公式:(a+bi)-(c+di)=(a-c)+(b-d)i*/
          /*復數乘法公式:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i*/
          /*復數除法公式:(a+bi)/(c+di)=[(ac+bd)/(c*c+d*d)]+[(bc-ad)/(c*c+d*d)]i*/
          public class ComplexNumber {
          //屬性部分定義
          //定義復數部分實部
          double m_dRealpart;
          //定義復數部分虛部
          double m_dImaginpart;
          //定義一個中間變量
          double R_dRealpart;
          //定義一個除法實部
          double Realpart;


          //方法部分定義
          //構造函數實現部分
          public ComplexNumber (double r,double I){
          this.m_dRealpart=r;
          this.m_dImaginpart=I;
          }

          //獲取復數實部值
          double getRealpart(){
          return this.m_dRealpart;
          }

          //獲取復數虛部值
          double getImaginpart(){
          return this.m_dImaginpart;
          }

          //設置復數實部值
          void setRealpart(double temp){
          this.m_dRealpart=temp;
          }

          //設置復數虛部值
          void setImaginpart(double temp){
          this.m_dImaginpart=temp;
          }

          //兩個復數對象加法運算
          ComplexNumber complexAdd(ComplexNumber c){
          this.m_dRealpart=this.m_dRealpart+c.m_dRealpart;
          this.m_dImaginpart=this.m_dImaginpart+c.m_dImaginpart;
          return this;
          }

          //兩個復數對象的減法運算
          ComplexNumber complexMinus(ComplexNumber c){
          this.m_dRealpart=this.m_dRealpart-c.m_dRealpart;
          this.m_dImaginpart=this.m_dImaginpart-c.m_dImaginpart;
          return this;
          }


          //兩個復數對象的乘法運算
          ComplexNumber complexChen(ComplexNumber c){
          R_dRealpart=this.m_dRealpart;
          this.m_dRealpart=this.m_dRealpart*c.m_dRealpart-this.m_dImaginpart*c.m_dImaginpart;
          this.m_dImaginpart=R_dRealpart*c.m_dImaginpart+c.m_dRealpart*this.m_dImaginpart;
          return this;
          }

          //兩個復數對象的除法運算
          ComplexNumber complexChu(ComplexNumber c){
          Realpart=this.m_dRealpart;
          this.m_dRealpart=(Realpart*c.m_dRealpart+this.m_dImaginpart*c.m_dImaginpart)/(c.m_dRealpart*c.m_dRealpart+c.m_dImaginpart*c.m_dImaginpart);
          this.m_dImaginpart=(this.m_dImaginpart*c.m_dRealpart-Realpart*c.m_dImaginpart)/(c.m_dRealpart*c.m_dRealpart+c.m_dImaginpart*c.m_dImaginpart);
          return this;
          }


          //構造一個a+bi的字符串形式
          public String toString(){
          String strTemp="";
          strTemp=this.m_dRealpart+"+"+
          this.m_dImaginpart+"i";
          return strTemp;
          }


          public static void main(String[] args) {
          //輸出兩個復數相加結果
          ComplexNumber complex1 = new ComplexNumber(10.0,10.0);
          System.out.println("m_dRealPart:"+complex1.m_dRealpart);
          ComplexNumber complex2 = new ComplexNumber(3.0,5.0);
          System.out.println("\n\ncomplex1:"+complex1.toString());
          System.out.println("complex2:"+complex2.toString());
          complex1.complexAdd(complex2);
          System.out.println("complex1+complex2=" + complex1.toString());

          //輸出兩個復數相減結果
          ComplexNumber complex3 = new ComplexNumber(10.0,10.0);
          System.out.println("\n\ncomplex3:"+complex3.toString());
          System.out.println("complex2:"+complex2.toString());
          complex3.complexMinus(complex2);
          System.out.println("complex3-complex2="+complex3.toString());

          //輸出兩個復數相乘結果
          ComplexNumber complex4 = new ComplexNumber(-1.0,2.0);
          System.out.println("\n\ncomplex4:"+complex4.toString());
          ComplexNumber complex5 = new ComplexNumber(2.0,-3.0);
          System.out.println("complex5:"+complex5.toString());
          complex4.complexChen(complex5);
          System.out.println("complex4*complex5="+complex4.toString());


          //輸出兩個復數相除結果
          ComplexNumber complex6 = new ComplexNumber(1.0,2.0);
          System.out.println("\n\ncomplex6:"+complex6.toString());
          ComplexNumber complex7 = new ComplexNumber(2.0,3.0);
          System.out.println("complex7:"+complex7.toString());
          complex6.complexChu(complex7);
          System.out.println("complex6/complex7="+complex6.toString());

          }

          }
            回復  更多評論   


          主站蜘蛛池模板: 玉环县| 蛟河市| 正阳县| 平和县| 巴中市| 随州市| 葫芦岛市| 青阳县| 澎湖县| 晴隆县| 陆河县| 张北县| 宣城市| 五华县| 聂荣县| 海伦市| 丹江口市| 宣恩县| 平阳县| 永安市| 登封市| 定远县| 寿光市| 辽阳县| 磐石市| 中西区| 曲阳县| 浦北县| 临颍县| 绥滨县| 阿合奇县| 横峰县| 安平县| 兴海县| 忻城县| 安西县| 盐山县| 南丹县| 锡林浩特市| 外汇| 巢湖市|