第二小組

          復(fù)數(shù)類(lèi)實(shí)現(xiàn) - kb

          一共三個(gè)文件:
                   ComplexException.java   :  Complex類(lèi)所應(yīng)的異常
                   Complex.java   : Complex類(lèi)實(shí)現(xiàn)
                   TestComplex.java   : 測(cè)試Complex類(lèi)

          /**
           * 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 + ")"
           );
              }

              
              
          /**
               * -*- 實(shí)例運(yùn)算方法,對(duì)自身作運(yùn)算。-*-
               * 還應(yīng)該加入復(fù)數(shù)與double類(lèi)型數(shù)據(jù)的實(shí)例運(yùn)算方法,但
               * 考慮到代碼長(zhǎng)度,便省了.
               
          */

              
          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
          ;
              }


              
          /**
               * -*- 類(lèi)運(yùn)算方法,提供兩個(gè)復(fù)數(shù)的運(yùn)算。-*-
               * 還應(yīng)該加入復(fù)數(shù)與double類(lèi)型數(shù)據(jù)的靜態(tài)運(yùn)算方法,但
               * 考慮到代碼長(zhǎng)度,便省了.
               
          */

              
          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;
              }

              
              
          /*
               * 復(fù)數(shù)雖沒(méi)有大小比較算法,但比較復(fù)數(shù)模的大小卻是經(jīng)常的。
               * 本方法實(shí)現(xiàn)的就是比較兩個(gè)復(fù)數(shù)模的大小。
               
          */

              
          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 {

              
          /**
               * 測(cè)試不是很細(xì)致,所以可能仍會(huì)留有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) );

              }


          }



          輸出結(jié)果如下:
          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) 評(píng)論(6)  編輯  收藏 所屬分類(lèi): 小組交流

          Feedback

          # re: 復(fù)數(shù)類(lèi)實(shí)現(xiàn) - 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  回復(fù)  更多評(píng)論   

          # re: 復(fù)數(shù)類(lèi)實(shí)現(xiàn) - kb 2005-07-29 00:51 第二小組

          .......so many hidden bugs......?
          Really?
          看來(lái)我的代碼還是很爛啊.
          可不可以指點(diǎn)一下?讓我也長(zhǎng)長(zhǎng)記性.  回復(fù)  更多評(píng)論   

          # re: 復(fù)數(shù)類(lèi)實(shí)現(xiàn) - kb 2005-07-29 01:06 第二小組

          剛才看了一下org.apache.commons.math.complex.Complex ,
          確實(shí)感覺(jué)出差距來(lái)了,但不知道它內(nèi)部是如何實(shí)現(xiàn)的,那樣的話會(huì)更好的理解.其實(shí)很多的復(fù)數(shù)算法我都不知道是怎么算的,比如pow,sqrt,tan,etc......  回復(fù)  更多評(píng)論   

          # re: 復(fù)數(shù)類(lèi)實(shí)現(xiàn) - 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  回復(fù)  更多評(píng)論   

          # 復(fù)數(shù)類(lèi)實(shí)現(xiàn) 2006-10-19 17:19 堅(jiān)持到底[匿名]

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


          //方法部分定義
          //構(gòu)造函數(shù)實(shí)現(xiàn)部分
          public ComplexNumber (double r,double I){
          this.m_dRealpart=r;
          this.m_dImaginpart=I;
          }

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

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

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

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

          //兩個(gè)復(fù)數(shù)對(duì)象加法運(yùn)算
          ComplexNumber complexAdd(ComplexNumber c){
          this.m_dRealpart=this.m_dRealpart+c.m_dRealpart;
          this.m_dImaginpart=this.m_dImaginpart+c.m_dImaginpart;
          return this;
          }

          //兩個(gè)復(fù)數(shù)對(duì)象的減法運(yùn)算
          ComplexNumber complexMinus(ComplexNumber c){
          this.m_dRealpart=this.m_dRealpart-c.m_dRealpart;
          this.m_dImaginpart=this.m_dImaginpart-c.m_dImaginpart;
          return this;
          }


          //兩個(gè)復(fù)數(shù)對(duì)象的乘法運(yùn)算
          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;
          }

          //兩個(gè)復(fù)數(shù)對(duì)象的除法運(yùn)算
          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;
          }


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


          public static void main(String[] args) {
          //輸出兩個(gè)復(fù)數(shù)相加結(jié)果
          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());

          //輸出兩個(gè)復(fù)數(shù)相減結(jié)果
          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());

          //輸出兩個(gè)復(fù)數(shù)相乘結(jié)果
          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());


          //輸出兩個(gè)復(fù)數(shù)相除結(jié)果
          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());

          }

          }
            回復(fù)  更多評(píng)論   

          # 復(fù)數(shù)類(lèi)實(shí)現(xiàn) 2006-10-19 17:22 堅(jiān)持到底[匿名]

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


          //方法部分定義
          //構(gòu)造函數(shù)實(shí)現(xiàn)部分
          public ComplexNumber (double r,double I){
          this.m_dRealpart=r;
          this.m_dImaginpart=I;
          }

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

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

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

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

          //兩個(gè)復(fù)數(shù)對(duì)象加法運(yùn)算
          ComplexNumber complexAdd(ComplexNumber c){
          this.m_dRealpart=this.m_dRealpart+c.m_dRealpart;
          this.m_dImaginpart=this.m_dImaginpart+c.m_dImaginpart;
          return this;
          }

          //兩個(gè)復(fù)數(shù)對(duì)象的減法運(yùn)算
          ComplexNumber complexMinus(ComplexNumber c){
          this.m_dRealpart=this.m_dRealpart-c.m_dRealpart;
          this.m_dImaginpart=this.m_dImaginpart-c.m_dImaginpart;
          return this;
          }


          //兩個(gè)復(fù)數(shù)對(duì)象的乘法運(yùn)算
          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;
          }

          //兩個(gè)復(fù)數(shù)對(duì)象的除法運(yùn)算
          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;
          }


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


          public static void main(String[] args) {
          //輸出兩個(gè)復(fù)數(shù)相加結(jié)果
          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());

          //輸出兩個(gè)復(fù)數(shù)相減結(jié)果
          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());

          //輸出兩個(gè)復(fù)數(shù)相乘結(jié)果
          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());


          //輸出兩個(gè)復(fù)數(shù)相除結(jié)果
          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());

          }

          }
            回復(fù)  更多評(píng)論   


          主站蜘蛛池模板: 静宁县| 肃北| 姚安县| 抚顺市| 铜山县| 南川市| 锦州市| 沙坪坝区| 颍上县| 双牌县| 定州市| 玉田县| 井冈山市| 昌都县| 江北区| 福清市| 祁东县| 株洲县| 得荣县| 惠东县| 鄂托克旗| 广州市| 勃利县| 图们市| 锡林郭勒盟| 苏州市| 邯郸市| 开化县| 江达县| 长阳| 江川县| 延寿县| 乐平市| 施秉县| 丁青县| 宕昌县| 竹北市| 通许县| 邛崃市| 安多县| 元阳县|