设计一个名为complex的类来表示复数_complex类java解决
生活随笔
收集整理的這篇文章主要介紹了
设计一个名为complex的类来表示复数_complex类java解决
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
作者:wzu_cza123
出自:CSDN
原文:blog.csdn.net/wzu_cza123/article/details/108857953?utm_medium=distribute.pc_category.none-task-blog-hot-5.nonecase&depth_1-utm_source=distribute.pc_category.none-task-blog-hot-5.nonecase&request_id=
complex類java解決
一個(gè)復(fù)數(shù)是一個(gè)形式為a+bi的數(shù),這里的a和b都是實(shí)數(shù),i是-1的平方根。數(shù)字a和b分別稱為復(fù)數(shù)的實(shí)部和虛部。 可以使用下面的公式完成復(fù)數(shù)的加、減、乘、除:(a+bi)+(c+di)=(a+c)+(b+d)i;(a+bi)-(c+di)=(a-c)+(b-d)i;(a+bi)*(c+di)=(ac-bd)+(ad+bc)i;(a+bi)/(c+di)=(ac+bd)/(c^2+d^2)+ (bc-ad)/(c^2+d^2)i;還可以使用下面的公式得到復(fù)數(shù)的絕對(duì)值: |a+bi|=sqrt(a^2+b^2);設(shè)計(jì)一個(gè)名為Complex的復(fù)數(shù)類來表示復(fù)數(shù)以及完成復(fù)數(shù)運(yùn)算的add、substract、multiply、divide和 calAbs(絕對(duì)值)方法,并且覆蓋toString方法以返回一個(gè)表示復(fù)數(shù)的字符串。方法toString返回字符串 a+bi。如果b是0,那么它只返回a。實(shí)現(xiàn)三個(gè)構(gòu)造器:Complex(a, b)、Complex(a)和Complex()。Complex()創(chuàng)建數(shù)字0的Complex 對(duì)象,而Complex(a)創(chuàng)建一個(gè)b為0的Complex對(duì)象。 還提供getA()和getB()方法以返回復(fù)數(shù)的實(shí)部和虛部。編寫一個(gè)測試程序,提示用戶輸入兩個(gè)復(fù)數(shù),然后顯示它們做加、減、乘、除之后的結(jié)果。 import java.util.Scanner; class Complex {private double realPart;private double imaginPart;Complex(double r, double i) {this.realPart = r;this.imaginPart = i;}Complex(double real){this.realPart=real;this.imaginPart=0;}Complex() {this.realPart = 0;this.imaginPart = 0;}//3個(gè)構(gòu)造方法//public double getB() {return this.imaginPart;}//返回imagine的值//public double getA() {return this.realPart;}//返回real的值//public Complex complexAdd(Complex a) {//傳進(jìn)來的complex a和b類似于c中的指針Complex c = new Complex();//定義一個(gè)c的complex類給c初始化c.realPart = this.realPart + a.realPart;c.imaginPart = this.imaginPart + a.imaginPart;return c;//返回c這個(gè)子類的值//}public Complex complexSubtract(Complex a) {Complex c = new Complex();c.realPart = this.realPart - a.realPart;c.imaginPart = this.imaginPart - a.imaginPart;return c;}//減法//public Complex mutiply(Complex a) {Complex c = new Complex();c.realPart = this.realPart * a.realPart - this.imaginPart * a.imaginPart;c.imaginPart = this.realPart * a.imaginPart + this.imaginPart * a.realPart;return c;}public Complex divide(Complex a) {Complex c = new Complex();c.realPart = (a.realPart * this.realPart + a.imaginPart * this.imaginPart) / (a.realPart * a.realPart + a.imaginPart * a.imaginPart);c.imaginPart = (a.realPart * this.imaginPart - this.realPart * a.imaginPart) / (a.realPart * a.realPart + a.imaginPart * a.imaginPart);return c;}public double calAbs() {double temp = Math.sqrt(this.realPart * this.realPart +this.imaginPart * this.imaginPart);return temp;}//求模//public String toString() {if (this.realPart >= 0 && this.realPart < 0.01) {if (this.imaginPart >= 0 && this.imaginPart < 0.01)return "0.00";elsereturn String.format("%.2fi", this.imaginPart);}else {if (this.imaginPart >= 0 && this.imaginPart < 0.01)return String.format("%.2f", this.realPart);elseif (this.imaginPart > 0)return String.format("%.2f+%.2fi", this.realPart, this.imaginPart);elsereturn String.format("%.2f%.2fi", this.realPart, this.imaginPart);}} }class Main{public static void main(String[] args) {Scanner in = new Scanner(System.in);double real1=in.nextDouble();double image1=in.nextDouble();double real2=in.nextDouble();double image2=in.nextDouble();Complex a=new Complex(real1,image1);Complex b=new Complex(real2,image2);System.out.printf("Real:%.2f imaginary:%.2f Fabs:%.2fn",a.getA(),a.getB(),a.calAbs());System.out.printf("Real:%.2f imaginary:%.2f Fabs:%.2fn",b.getA(),b.getB(),b.calAbs());System.out.println(a.complexAdd(b));System.out.println(a.complexSubtract(b));System.out.println(a.mutiply(b));System.out.println(a.divide(b));} } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的设计一个名为complex的类来表示复数_complex类java解决的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 昆仑通态触摸屏如何把参数由触摸屏传递到P
- 下一篇: Thread.getContextCla