一個複數是一個形式為a+bi的數,這裡的a和b都是實數,i是-1的平方根。數位a和b分別稱為複數的實部和虛部。 可以使用下面的公式完成複數的加、減、乘、除:
(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;
還可以使用下面的公式得到複數的絕對值: |a+bi|=sqrt(a^2+b^2);
設計一個名為Complex的複數類來表示複數以及完成複數運算的add、substract、multiply、divide和 calAbs(絕對值)方法,並且覆蓋toString方法以返回一個表示複數的字串。方法toString返回字串 a+bi。如果b是0,那麼它只返回a。
實現三個構造器:Complex(a, b)、Complex(a)和Complex()。Complex()建立數位0的Complex 物件,而Complex(a)建立一個b為0的Complex物件。 還提供getA()和getB()方法以返回複數的實部和虛部。
編寫一個測試程式,提示使用者輸入兩個複數,然後顯示它們做加、減、乘、除之後的結果。
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個構造方法//
public double getB() {
return this.imaginPart;
}//返回imagine的值//
public double getA() {
return this.realPart;
}//返回real的值//
public Complex complexAdd(Complex a) {//傳進來的complex a和b類似於c中的指標
Complex c = new Complex();//定義一個c的complex類給c初始化
c.realPart = this.realPart + a.realPart;
c.imaginPart = this.imaginPart + a.imaginPart;
return c;//返回c這個子類的值//
}
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";
else
return String.format("%.2fi", this.imaginPart);
}
else {
if (this.imaginPart >= 0 && this.imaginPart < 0.01)
return String.format("%.2f", this.realPart);
else
if (this.imaginPart > 0)
return String.format("%.2f+%.2fi", this.realPart, this.imaginPart);
else
return 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:%.2f\n",a.getA(),a.getB(),a.calAbs());
System.out.printf("Real:%.2f imaginary:%.2f Fabs:%.2f\n",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));
}
}
前前後後花了2天時間 看了幾十篇CSDN 問了大佬終於解決了 害 鐵廢物了
不足之處請大家指出改正!