一共三个文件:
         ComplexException.java   :  Complex类所应的异常
         Complex.java   : Complex类实现
         TestComplex.java   : 测试Complex类

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

 package kb.complex;
package kb.complex;


 public class ComplexException extends Exception
public class ComplexException extends Exception  {
{

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

 public ComplexException()
    public ComplexException()  {
{
 super();
        super();
 }
    }
 
    

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

 

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

 package kb.complex;
package kb.complex;

 import java.util.StringTokenizer;
import java.util.StringTokenizer;


 public class Complex
public class Complex  {
{

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

 /**//**
    /**//**
 * -*- constructors -*-
     * -*- constructors -*-
 */
     */

 public Complex()
    public Complex()  {}
{}
 
    

 public Complex( double r )
    public Complex( double r )  { real = r; }
{ real = r; }
 
    

 public Complex( Complex c )
    public Complex( Complex c )  { real = c.real; imag = c.imag; }
{ real = c.real; imag = c.imag; }
 
    

 public Complex( double r, double i )
    public Complex( double r, double i )  { real = r; imag = i; }
{ real = r; imag = i; }
 
    

 public Complex( String c ) throws ComplexException
    public Complex( String c ) throws ComplexException  {
{
 
        
 if( c == null || c.length() == 0 )
        if( c == null || c.length() == 0 )

 
         {
{
 real = imag = 0.0;
            real = imag = 0.0;
 return;
            return;
 }
        }
 
        
 StringTokenizer t = new StringTokenizer( c, delimiters );
        StringTokenizer t = new StringTokenizer( c, delimiters );
 String[] data = new String[t.countTokens()];
        String[] data = new String[t.countTokens()];

 for( int i = 0; i < data.length; i++ )
        for( int i = 0; i < data.length; i++ )  {
{
 data[ i ] = t.nextToken();
            data[ i ] = t.nextToken();
 }
        }
 
        
 switch( data.length )
        switch( data.length )

 
         {
{
 case 1:        //form: ",imag" "real," "real" and "(,imag)" "(real,)" "(real)"
        case 1:        //form: ",imag" "real," "real" and "(,imag)" "(real,)" "(real)"
 double tmp;
            double tmp;
 tmp = Double.parseDouble( data[0] );
            tmp = Double.parseDouble( data[0] );
 
            
 String str = c.split(",")[0];
            String str = c.split(",")[0];

 if( str.indexOf( data[0] ) >= 0 )
            if( str.indexOf( data[0] ) >= 0 )  {
{
 real = tmp;
                real = tmp;

 } else
            } else  {
{
 imag = tmp;
                imag = tmp;
 }
            }
 
            
 break;
            break;
 case 2:        //form: "real,imag" and "(real,imag)"
        case 2:        //form: "real,imag" and "(real,imag)"
 real = Double.parseDouble( data[0] );
            real = Double.parseDouble( data[0] );
 imag = Double.parseDouble( data[1] );
            imag = Double.parseDouble( data[1] );
 break;
            break;
 default:
        default:
 throw new ComplexException();
            throw new ComplexException();
 }
        }
 
        
 }
    }
 
    

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

 public double getReal()
    public double getReal()  { return real; }
{ return real; }

 public double getImag()
    public double getImag()  { return imag; }
{ return imag; }
 
    

 public void setReal( double r )
    public void setReal( double r )  { real = r; }
{ real = r; }

 public void setImag( double i )
    public void setImag( double i )  { imag = i; }
{ imag = i; }
 
    

 public String toString()
    public String toString()  {
{
 return new String( "(" + real + "," + imag + ")" );
        return new String( "(" + real + "," + imag + ")" );
 }
    }
 
    

 /**//**
    /**//**
 * -*- 实例运算方法,对自身作运算。-*-
     * -*- 实例运算方法,对自身作运算。-*-
 * 还应该加入复数与double类型数据的实例运算方法,但
     * 还应该加入复数与double类型数据的实例运算方法,但
 * 考虑到代码长度,便省了.
     * 考虑到代码长度,便省了.
 */
     */

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

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

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

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


 /**//**
    /**//**
 * -*- 类运算方法,提供两个复数的运算。-*-
     * -*- 类运算方法,提供两个复数的运算。-*-
 * 还应该加入复数与double类型数据的静态运算方法,但
     * 还应该加入复数与double类型数据的静态运算方法,但
 * 考虑到代码长度,便省了.
     * 考虑到代码长度,便省了.
 */
     */

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

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

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

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

 /**//*
    /**//*
 * 复数虽没有大小比较算法,但比较复数模的大小却是经常的。
     * 复数虽没有大小比较算法,但比较复数模的大小却是经常的。
 * 本方法实现的就是比较两个复数模的大小。
     * 本方法实现的就是比较两个复数模的大小。
 */
     */

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

 }
}

 

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

 package kb.complex;
package kb.complex;


 public class TestComplex
public class TestComplex  {
{


 /**//**
    /**//**
 * 测试不是很细致,所以可能仍会留有Bugs
     * 测试不是很细致,所以可能仍会留有Bugs
 */
     */

 public static void main(String[] args) throws Exception
    public static void main(String[] args) throws Exception  {
{
 Complex c1;
        Complex c1;
 Complex c2;
        Complex c2;
 
        
 String d1 = "(1,2)";
        String d1 = "(1,2)";
 String d2 = "1,2";
        String d2 = "1,2";
 
        
 String dd1 = " (   1 , 2 ) ";
        String dd1 = " (   1 , 2 ) ";
 String dd2 = " 1 , 2 ";
        String dd2 = " 1 , 2 ";
 
        
 String ddd1 = "(2,)";    //"(,2)" "(2)" : also ok
        String ddd1 = "(2,)";    //"(,2)" "(2)" : also ok
 String ddd2 = "2,";        //",2" "2" : also ok
        String ddd2 = "2,";        //",2" "2" : also ok
 
        
 String d = "";            //null also ok
        String d = "";            //null also ok
 
        
 c1 = new Complex( d );
        c1 = new Complex( d );
 System.out.println( "c1 by d: " + c1 );
        System.out.println( "c1 by d: " + c1 );
 
        
 c1 = new Complex( d1 );
        c1 = new Complex( d1 );
 System.out.println( "c1 by d1: " + c1 );
        System.out.println( "c1 by d1: " + c1 );
 
        
 c2 = new Complex( d2 );
        c2 = new Complex( d2 );
 System.out.println( "c2 by d2: " + c2 );
        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(Complex.divide(c1, c2)): " + Complex.divide(c1, c2) );
 System.out.println( "c1 /= c2(c1.divide(c2)): " + c1.divide(c2) );
        System.out.println( "c1 /= c2(c1.divide(c2)): " + c1.divide(c2) );
 
        
 c1 = new Complex( dd1 );
        c1 = new Complex( dd1 );
 System.out.println( "c1 by dd1: " + c1 );
        System.out.println( "c1 by dd1: " + c1 );
 
        
 c2 = new Complex( dd2 );
        c2 = new Complex( dd2 );
 System.out.println( "c2 by dd2: " + c2 );
        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(Complex.multiply(c1, c2)): " + Complex.multiply(c1, c2) );
 System.out.println( "c1 *= c2(c1.multiply(c2)): " + c1.multiply(c2) );
        System.out.println( "c1 *= c2(c1.multiply(c2)): " + c1.multiply(c2) );
 
        
 c1 = new Complex( ddd1 );
        c1 = new Complex( ddd1 );
 System.out.println( "c1 by ddd1: " + c1 );
        System.out.println( "c1 by ddd1: " + c1 );
 
        
 c2 = new Complex( ddd2 );
        c2 = new Complex( ddd2 );
 System.out.println( "c2 by ddd2: " + c2 );
        System.out.println( "c2 by ddd2: " + c2 );
 
        
 System.out.println( "c1 compareTo c2: " + c1.compareTo(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(c1.add(c2)): " + c1.add(c2) );
 System.out.println( "c1 + c2(Complex.minus(c1,c2)): " + Complex.minus(c1,c2) );
        System.out.println( "c1 + c2(Complex.minus(c1,c2)): " + Complex.minus(c1,c2) );

 }
    }

 }
}

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