Java实现复数Complex的加减乘除运算、取模、求幅角角度
生活随笔
收集整理的這篇文章主要介紹了
Java实现复数Complex的加减乘除运算、取模、求幅角角度
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*** @Author: Yeman* @Date: 2021-09-23-9:03* @Description:*/class Complex{private double realPart; //復數的實部private double imaginaryPart; //復數的虛部public Complex() { //空參構造器}public Complex(double realPart, double imaginaryPart) {this.realPart = realPart;this.imaginaryPart = imaginaryPart;}//屬性的get、set方法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;}//加法運算public Complex add(Complex otherComplex){if (otherComplex != null) {return new Complex(this.getRealPart() + otherComplex.getRealPart(),this.getImaginaryPart() + otherComplex.getImaginaryPart());}else throw new RuntimeException("參與運算的對象為空!");}//減法運算public Complex decrease(Complex otherComplex){if (otherComplex != null) {return new Complex(this.getRealPart() - otherComplex.getRealPart(),this.getImaginaryPart() - otherComplex.getImaginaryPart());}else throw new RuntimeException("參與運算的對象為空!");}//乘法運算public Complex multiply(Complex otherComplex){if (otherComplex != null) {double newReal = this.getRealPart() * otherComplex.getRealPart() - this.getImaginaryPart() * otherComplex.getImaginaryPart();double newImaginary = this.getImaginaryPart() * otherComplex.getRealPart() + this.getRealPart() * otherComplex.getImaginaryPart();return new Complex(newReal,newImaginary);}else throw new RuntimeException("參與運算的對象為空!");}//除法運算public Complex divide(Complex otherComplex){if (otherComplex != null) {if (otherComplex.getRealPart() != 0 && otherComplex.getImaginaryPart() != 0){double newReal = (this.getRealPart() * otherComplex.getRealPart() + this.getImaginaryPart() * otherComplex.getImaginaryPart()) / (otherComplex.getRealPart() * otherComplex.getRealPart() + otherComplex.getImaginaryPart() * otherComplex.getImaginaryPart());double newImaginary = (this.getImaginaryPart() * otherComplex.getRealPart() - this.getRealPart() * otherComplex.getImaginaryPart()) / (otherComplex.getRealPart() * otherComplex.getRealPart() + otherComplex.getImaginaryPart() * otherComplex.getImaginaryPart());return new Complex(newReal,newImaginary);}else throw new RuntimeException("除數不能為0!");}else throw new RuntimeException("參與運算的對象為空!");}//取模public double delivery(){return Math.sqrt(this.getRealPart() * this.getRealPart() + this.getImaginaryPart() * this.getImaginaryPart());}//幅度值(角度)public double angle(){double atan;if (this.getRealPart() != 0) { //注意,該處double型變量若有進行其他操作,則不能以此方式判斷其等于0,應該是其絕對值小于某個很小的數;而這當前情景下,其實精度問題并不影響,因此可以這樣寫atan = Math.atan(this.getImaginaryPart() / this.getRealPart());}else {if (this.getImaginaryPart() > 0) {atan = Math.PI / 2;}else if (this.getImaginaryPart() < 0){atan = -Math.PI / 2;}else atan = Math.atan(0);}return atan;}}//測試主類
public class ComplexTest {public static void main(String[] args) {Complex complex1 = new Complex(0, 5);Complex complex2 = new Complex(3, -3);//取模測試double delivery = complex1.delivery();System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "的模為:" + delivery);//求角度測試double angle = complex1.angle();System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "的角度為:" + Math.toDegrees(angle) + "°");//加運算Complex add = complex1.add(complex2);System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "+" + "(" + complex2.getRealPart() + complex2.getImaginaryPart() + "i" + ")" + "=" + "(" + add.getRealPart() + "+" + add.getImaginaryPart() + "i" + ")");//減運算Complex decrease = complex1.decrease(complex2);System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "-" + "(" + complex2.getRealPart() + complex2.getImaginaryPart() + "i" + ")" + "=" + "(" + decrease.getRealPart() + "+" + decrease.getImaginaryPart() + "i" + ")");//乘法運算Complex multiply = complex1.multiply(complex2);System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "x" + "(" + complex2.getRealPart() + complex2.getImaginaryPart() + "i" + ")" + "=" + "(" + multiply.getRealPart() + "+" + multiply.getImaginaryPart() + "i" + ")");//除法運算Complex divide = complex1.divide(complex2);System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "/" + "(" + complex2.getRealPart() + complex2.getImaginaryPart() + "i" + ")" + "=" + "(" + divide.getRealPart() + "+" + divide.getImaginaryPart() + "i" + ")");}
}
總結
以上是生活随笔為你收集整理的Java实现复数Complex的加减乘除运算、取模、求幅角角度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 玩玩csgo电脑卡死了(一玩csgo电脑
- 下一篇: Java集合(8)--集合工具类Coll