一共三个文件:
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()];
for( int 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)