當前位置:首頁 » 編程語言 » java復數

java復數

發布時間: 2023-08-08 07:31:07

java 中實現復數的加減

(1):具體代碼(附註釋)

復數類:

publicclassComplex{

privatefloatshibu;

privatefloatxubu;

Complex()

{this(0,0);

}

Complex(floatshibu,floatxubu){

this.shibu=shibu;

this.xubu=xubu;

}


publicvoidAdd(Complexp)

{

Complexresult=newComplex();

result.shibu=this.shibu+p.shibu;

result.xubu=this.xubu+p.xubu;

System.out.print("加法結果為:"+result.shibu+"+"+result.xubu+"i");

}


publicvoidSub(Complexp)

{

Complexresult=newComplex();

result.shibu=this.shibu-p.shibu;

result.xubu=this.xubu-p.xubu;

System.out.print("加法結果為:"+result.shibu+"+"+result.xubu+"i");

}

publicvoidMul(Complexp)

{

Complexresult=newComplex();

result.shibu=this.shibu*p.shibu-this.xubu*p.xubu;

result.xubu=this.shibu*p.xubu+p.shibu*this.xubu;

System.out.print("乘法結果為:"+result.shibu+"+"+result.xubu+"i");

}

publicstaticvoidmain(String[]args){


Complexfushu1=newComplex(1,2);

Complexfushu2=newComplex(3,4);

fushu1.Add(fushu2);

fushu1.Sub(fushu2);

fushu1.Mul(fushu2);


}


}

(2):提供一個例子:

源代碼:

importjava.io.*;

publicclassBook{

doublesb;

doublexb;

Book(doublex,doubley){

this.sb=x;

this.xb=y;

}

Book(){

}


publicstaticvoidmain(Stringargs[]){


System.out.println("請輸入數據:");

doublea=0;

doubleb=0;

doublec=0;

doubled=0;

Strings;


BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));

System.out.println("請輸入第一個復述的實部:");

try{

s=in.readLine();

a=Double.parseDouble(s);

}

catch(IOExceptione)

{System.out.println("拋擲異常");}

System.out.println("請輸入第一個復述的虛部:");

try{

s=in.readLine();

b=Double.parseDouble(s);

}

catch(IOExceptione)

{System.out.println("拋擲異常");}


System.out.println("請輸入第二個復述的實部:");

try{

s=in.readLine();

c=Double.parseDouble(s);

}

catch(IOExceptione)

{System.out.println("拋擲異常");}

System.out.println("請輸入第二個復述的虛部:");

try{

s=in.readLine();

d=Double.parseDouble(s);

}

catch(IOExceptione)

{System.out.println("拋擲異常");}

Bookh;

h=newBook(a,b);

Bookj;

j=newBook(c,d);

System.out.println("您輸入的一個數為:");

toString(h);

System.out.println("您輸入的二個數為:");

toString(j);


Bookk;

k=newBook();


charz='y';

do{


System.out.println("請選擇您要進行的計算:");

System.out.println("1:進行加法運算");

System.out.println("2:進行減法運算");

System.out.println("3:進行修改");

System.out.println("4:進行乘法運算");

System.out.println("5:進行除法運算");

System.out.println("6:查看修改結果");

inti=0;

try{

i=Integer.parseInt(in.readLine());

}

catch(IOExceptione)

{System.out.println("拋擲異常");}

switch(i)

{

case1:

k.sb=jia(h.sb,j.sb);

k.xb=jia(h.xb,j.xb);

System.out.println("計算結果的實部為:"+k.sb);

System.out.println("計算結果的虛部為:"+k.xb);

toString(k);

break;

case2:

k.sb=jian(h.sb,j.sb);

k.xb=jian(h.xb,j.xb);

System.out.println("計算結果的實部為:"+k.sb);

System.out.println("計算結果的虛部為:"+k.xb);

toString(k);

break;

case3:

System.out.println("請輸入您要修改哪個實數:");

intl=0;

try{

l=Integer.parseInt(in.readLine());

}

catch(IOExceptione)

{System.out.println("拋擲異常");}

if(l==1)

{

h.xiugais(h);

h.xiugaix(h);

}

else

{

xiugais(j);

xiugaix(j);

}

break;

case4:

doublef=0;

doublee=0;

f=cheng(h.sb,j.sb)+cheng(h.xb,j.xb);

e=cheng(h.sb,j.xb)+cheng(h.xb,j.sb);

k.sb=(double)(Math.round(f*100)/100.0);

k.xb=(double)(Math.round(e*100)/100.0);

System.out.println("計算結果的實部為:"+k.sb);

System.out.println("計算結果的虛部為:"+k.xb);

toString(k);

break;

case5:

doublechushu=cheng(j.sb,j.sb)-cheng(j.xb,-j.xb);

doublebeichushus=jian(cheng(h.sb,j.sb),cheng(h.xb,-j.xb));

doublebeichushux=jia(cheng(h.sb,-j.xb),cheng(h.xb,j.sb));

k.sb=chu(beichushus,chushu);

k.xb=chu(beichushux,chushu);

System.out.println("計算結果的實部為:"+k.sb);

System.out.println("計算結果的虛部為:"+k.xb);

toString(k);

break;

case6:

System.out.println("修改後的結果為:");

System.out.println("第一個復數:"+toString(h));

System.out.println("第二個復數:"+toString(j));

break;


}

System.out.println("請問您是否還要繼續y/n:");

try{

z=(char)System.in.read();

System.in.skip(2);//忽略回車換行

}

catch(IOExceptione){}


}while(z=='y');

}


publicstaticdoublegets(Booka){

returna.sb;

}

publicstaticdoublegetx(Bookb){

returnb.xb;

}


publicstaticdoublexiugais(Booka)

{

BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));

System.out.println("請輸入您要修改的實部:");

doublem=0;

try{

m=Double.parseDouble(in.readLine());

}

catch(IOExceptione)

{System.out.println("拋擲異常");}

a.sb=m;

System.out.println("修改成功:");

return0;

}

publicstaticdoublexiugaix(Bookb)

{

BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));

System.out.println("請輸入您要修改的虛部:");

doublen=0;

try{

n=Double.parseDouble(in.readLine());

}

catch(IOExceptione)

{System.out.println("拋擲異常");}

b.xb=n;

System.out.println("修改成功:");

return0;

}

publicstaticdoublejia(doublea,doubleb)//

{

doublec=0;

c=a+b;

System.out.println("加法成功:");

returnc;

}


publicstaticdoublejian(doublea,doubleb)

{

doublec=0;

c=a-b;

System.out.println("減法成功:");

returnc;

}


publicstaticdoublecheng(doublea,doubleb)

{

doublec=0;

c=a*b;

System.out.println("乘法成功:");

returnc;

}


publicstaticdoublechu(doublea,doubleb)

{

doubled=0;

doublec=0;

d=a/b;

c=(double)(Math.round(d*100)/100.0);

System.out.println("除法成功:");

returnc;

}

publicstaticdoubletoString(Booka){

System.out.println("結果為:"+a.sb+"+"+a.xb+"*i");

return0;

}

}

(3)測試結果截圖:

❷ 用Java語言定義復數類Complex,必須滿足如下要求:

public class Complex{
public int RealPart;
public int ImaginPart;
public Complex(){
this.RealPart = 0;
this.ImaginPart = 0;
}

public Complex(int r, int i){
this.RealPart = r;
this.ImaginPart = i
}

public Complex complexAdd(Complex a){
this.RealPart += a.RealPart;
this.ImaginPart += a.ImaginPart;
//這里返回什麼?復數值是指哪個?還是復數對象?沒有說清楚。我這里返回復數對象。
return this;
}

public String ToString(){
return ""+this.RealPart + this.ImaginPart;//return "" + 1 + 2 = "12";不知道這里要求是不是這樣。有些話寫的我沒法理解。你的a + bi是個啥?如果是返回實數和虛數的和就給 1 + 2的部分加上括弧。
}
}

❸ java:創建復數類並實現復數的基本運算

package main;

public class Complex {
private int a;
private int b;

public Complex() {
this.a = 0;
this.b = 0;
}

public Complex(int a, int b) {
this.a = a;
this.b = b;
}

public int getA() {
return a;
}

public void setA(int a) {
this.a = a;
}

public int getB() {
return b;
}

public void setB(int b) {
this.b = b;
}

@Override
public String toString() {
return "(" + a + "+" + b + "i)";
}
}

package main;

public class ComplexTest {
public static Complex add(Complex c1, Complex c2) {
return new Complex(c1.getA() + c2.getA(), c1.getB() + c2.getB());
}

public static Complex subtract(Complex c1, Complex c2) {
return new Complex(c1.getA() - c2.getA(), c1.getB() - c2.getB());
}

public static Complex multiply(Complex c1, Complex c2) {
return new Complex(c1.getA() * c2.getA() - c1.getB() * c2.getB(),
c1.getB() * c1.getA() + c1.getA() * c2.getB());
}

public static Complex division(Complex c1, Complex c2) {
int a = c1.getA();
int b = c1.getB();
int c = c2.getA();
int d = c2.getB();
return new Complex((a *c + b * d) / (c * c + d * d), (b * c - a * d) / (c * c + d * d));
}

public static void main(String []args) {
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex();
System.out.println(c1);
System.out.println(c2);

System.out.println(add(c1, c2));
}
}

熱點內容
python處理excel文件 發布:2025-02-06 16:36:09 瀏覽:439
演算法相對定位 發布:2025-02-06 16:32:42 瀏覽:725
java程序的編譯和執行 發布:2025-02-06 16:21:45 瀏覽:416
什麼是淘寶帳號和密碼 發布:2025-02-06 16:21:36 瀏覽:495
解壓前面簽 發布:2025-02-06 16:02:00 瀏覽:324
華碩訪問點 發布:2025-02-06 15:56:57 瀏覽:331
excel拼接sql 發布:2025-02-06 15:50:10 瀏覽:501
加密手機直播 發布:2025-02-06 15:49:31 瀏覽:535
自帶ftp伺服器好用嗎 發布:2025-02-06 15:26:11 瀏覽:110
win7訪問xp區域網 發布:2025-02-06 15:17:07 瀏覽:525