自定义数据类型 复数的Java实现
**复数**
数据:实部 虚部
ComplexNum类,私有(private)变量,用getter和setter访问与修改,用toString函数转为字符串
有参构造函数,传入实部和虚部创建复数ComplexNumber对象。
无参构造函数,创建复数0+0i,通过调用有参构造函数创建。
成员函数:模长,共轭,开方,加减乘除
类函数:加减乘除
成员函数需要通过ComplexNumber对象来调用,表示得到某个复数的模长、共轭、开方值,或者这个复数对象加上另一个复数得到的值,没有返回值,是对自身的修改。
类函数由static关键字修饰,则可以直接调用,或者在另一个类中通过ComplexNumber类名就可以调用,表示两个复数相加的值,返回值就是ComplexNumber对象,表示运算结果。
(类函数又叫静态函数,不能调用类中的其他成员变量和函数,只能调用类变量和类函数)
(成员函数可以调用类中的其他函数,也可以调用类函数)
代码:
package complexNumber; public class ComplexNumber { // 成员变量 private double realPart; private double imaginaryPart; public ComplexNumber() { // TODO Auto-generated constructor stub this(0,0); } public ComplexNumber(double real, double imaginary) { this.realPart = real; this.imaginaryPart = imaginary; } public void add(ComplexNumber number) { this.realPart += number.realPart; this.imaginaryPart += number.imaginaryPart; } public static ComplexNumber add(ComplexNumber number1, ComplexNumber number2) { double real = number1.realPart + number2.realPart; double imag = number1.imaginaryPart + number2.imaginaryPart; return new ComplexNumber(real, imag); } public void subtract(ComplexNumber number) { this.realPart -= number.realPart; this.imaginaryPart -= number.imaginaryPart; } public static ComplexNumber subtract(ComplexNumber number1, ComplexNumber number2) { double real = number1.realPart - number2.realPart; double imag = number1.imaginaryPart - number2.imaginaryPart; return new ComplexNumber(real, imag); } // (a+bi)*(c+di) = (ac-bd) + (bc+ad)i public void multiply(ComplexNumber number) { this.realPart = this.realPart * number.realPart - this.imaginaryPart * number.imaginaryPart; this.imaginaryPart = this.imaginaryPart * number.realPart + this.realPart * number.imaginaryPart; } public static ComplexNumber multiply(ComplexNumber number1, ComplexNumber number2) { double real = number1.realPart * number2.realPart - number1.imaginaryPart * number2.imaginaryPart; double imag = number1.imaginaryPart * number2.realPart - number1.realPart * number2.imaginaryPart; return new ComplexNumber(real, imag); } public void divide(ComplexNumber number) { // 分母 double denominator = number.magnitude(); this.realPart = this.realPart * number.realPart + this.imaginaryPart * number.imaginaryPart; this.realPart /= denominator; this.imaginaryPart = (this.imaginaryPart * number.realPart - this.realPart * number.imaginaryPart); this.imaginaryPart /= denominator; } public static ComplexNumber divide(ComplexNumber number1, ComplexNumber number2) { double denominator = number2.magnitude(); double real = number1.realPart * number2.realPart + number1.imaginaryPart * number2.imaginaryPart; double imag = (number1.imaginaryPart * number2.realPart - number1.realPart * number2.imaginaryPart); return new ComplexNumber(real / denominator, imag / denominator); } public double magnitude() { return Math.sqrt(this.realPart*this.realPart + this.imaginaryPart*this.imaginaryPart); } public ComplexNumber conjugate() { return new ComplexNumber(this.realPart, - this.imaginaryPart); } public double getRealPart() { return realPart; } public void setRealPart(double realPart) { this.realPart = realPart; } public double getImaginaryPart() { return imaginaryPart; } public void setImaginaryPart(double imaginaryPart) { this.imaginaryPart = imaginaryPart; } @Override public String toString() { if(this.imaginaryPart < 0) return "ComplexNumber " + realPart + "i" + imaginaryPart; return "ComplexNumber " + realPart + "i + " + imaginaryPart; }
// 主函数,测试一下这个类 public static void main(String[] args) { ComplexNumber num1 = new ComplexNumber(3, 4); ComplexNumber num2 = new ComplexNumber(5, 12); System.out.println("num1 : " + num1.toString()); System.out.println("num1的模长为:"+num1.magnitude()); System.out.println("num1的共轭为:"+num1.conjugate()); ComplexNumber num3 = ComplexNumber.add(num1, num2); System.out.println("num3 = num1 + num2 = " + num3.toString()); num1.add(num2); System.out.println("num1 <- num1 + num2 = " + num1.toString()); ComplexNumber num4 = ComplexNumber.subtract(num1, num2); System.out.println("num4 = num1 - num2 = " + num4.toString()); System.out.println("num2 : " + num2.toString()); System.out.println("num2的模长为:"+num2.magnitude()); System.out.println("num2的共轭为:"+num2.conjugate()); } }
运行结果:
num1 : ComplexNumber 3.0i + 4.0
num1的模长为:5.0
num1的共轭为:ComplexNumber 3.0i-4.0
num3 = num1 + num2 = ComplexNumber 8.0i + 16.0
num1 <- num1 + num2 = ComplexNumber 8.0i + 16.0
num4 = num1 - num2 = ComplexNumber 3.0i + 4.0
num2 : ComplexNumber 5.0i + 12.0
num2的模长为:13.0
num2的共轭为:ComplexNumber 5.0i-12.0