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

復數java

發布時間: 2022-08-08 02:52:49

『壹』 java創建一個復數類

packagetable;

publicclassComplex
{
doublereal;
doubleimaginary;
publicstaticfinalComplexZERO=newComplex(0,0);
publicstaticfinalComplexONE=newComplex(1,0);
publicstaticfinalComplexI=newComplex(0,1);

publicComplex(doublereal,doubleimaginary)
{
this.real=real;
this.imaginary=imaginary;
}

publicdoublemagnitude()
{
returnMath.sqrt(this.real*this.real+this.imaginary*this.imaginary);
}

publicComplexnegative()
{
returnnewComplex(-real,-imaginary);
}

publicdoublevalueOf()
{
returnthis.real;
}

publicComplexadd(Complexa,Complexb)
{
returnnewComplex(a.real+b.real,a.imaginary+b.imaginary);
}

publicComplexsubtract(Complexa,Complexb)
{
returnnewComplex(a.real-b.real,a.imaginary-b.imaginary);
}

publicComplexmultiply(Complexa,Complexb)
{
returnnewComplex(a.real*b.real-a.imaginary*b.imaginary,a.real*b.imaginary+a.imaginary*b.real);
}

@Override
publicStringtoString()
{
StringBuilderbuilder=newStringBuilder();
builder.append("Complex[real=").append(real).append(",imaginary=").append(imaginary).append("]");
returnbuilder.toString();
}
}

『貳』 java里怎樣表示復數啊

ic class Complex
{
private double real,im; //實部,虛部

public Complex(double real, double im) //構造方法
{
this.real = real;
this.im = im;
}

public Complex(double real) //構造方法重載
{
this(real,0);
}

public Complex()
{
this(0,0);
}

public Complex(Complex c) //拷貝構造方法
{
this(c.real,c.im);
}

public boolean equals(Complex c) //比較兩個對象是否相等
{
return this.real==c.real && this.im==c.im;
}

public String toString()
{
return "("+this.real+"+"+this.im+"i)";
}

public void add(Complex c) //兩個對象相加
{ //改變當前對象,沒有返回新對象
this.real += c.real;
this.im += c.im;
}

public Complex plus(Complex c) //兩個對象相加,與add()方法參數一樣不能重載
{ //返回新創建對象,沒有改變當前對象
return new Complex(this.real+c.real, this.im+c.im);
}

public void subtract(Complex c) //兩個對象相減
{ //改變當前對象,沒有返回新對象
this.real -= c.real;
this.im -= c.im;
}

public Complex minus(Complex c) //兩個對象相減,與subtract()方法參數一樣不能重載
{ //返回新創建的對象,沒有改變當前對象
return new Complex(this.real-c.real, this.im-c.im);
}
}

class Complex__ex
{
public static void main(String args[])
{
Complex a = new Complex(1,2);
Complex b = new Complex(3,5);
Complex c = a.plus(b); //返回新創建對象
System.out.println(a+" + "+b+" = "+c);
}
}

/*
程序運行結果如下:
(1.0+2.0i) + (3.0+5.0i) = (40.0+7.0i)

*/

『叄』 用java 編寫一個復數類

public class Complex {

protected int a;

protected int b;

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

public String toString() {
return this.a + " + " + this.b + "i";
}

public static Complex addition(Complex complex1, Complex complex2) {
int a = complex1.a + complex2.a;
int b = complex1.b + complex2.b;
return new Complex(a, b);
}

public static Complex subtract(Complex complex1, Complex complex2) {
int a = complex1.a - complex2.a;
int b = complex1.b - complex2.b;
return new Complex(a, b);
}

public static Complex multiplication(Complex complex1, Complex complex2) {
int a = complex1.a * complex2.a - complex1.b * complex2.b;
int b = complex1.b * complex2.a + complex1.a * complex2.b;
return new Complex(a, b);
}

public static Complex division(Complex complex1, Complex complex2) throws Exception {
if (complex2.a == 0) {
throw new Exception("complex2.a is 0");
}
if (complex2.b == 0) {
throw new Exception("complex2.b is 0");
}
int a = (complex1.a * complex2.a + complex1.b * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
int b = (complex1.b * complex2.a - complex1.a * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
return new Complex(a, b);
}
}

//測試用的類
public class ComplexTest {

public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Complex complex1 = new Complex(1, 2);
Complex complex2 = new Complex(3, 4);
System.out.println(complex1 + " + " + complex2 + " = " + Complex.addition(complex1, complex2));
System.out.println(complex1 + " - " + complex2 + " = " + Complex.subtract(complex1, complex2));
System.out.println(complex1 + " x " + complex2 + " = " + Complex.multiplication(complex1, complex2));
System.out.println(complex1 + " / " + complex2 + " = " + Complex.division(complex1, complex2));
}
}

『肆』 在java中復數的結構包括實部和虛部什麼是實部什麼是虛部

復數是形如
a+bi
的數,
其中
a,
b
是實數,a稱為實部,b稱為虛部,i是虛數單位,
i^2
=
-1.
實數可以用一條直線(數軸)上的點表示,
類似地,
復數可以用一個平面(稱為復平面)上的點表示.
在復平面上,a+bi
用點Z(a,b)
表示。Z與原點的距離r稱為Z的模|Z|=√(a^2+b^2)
復數a+bi,b=0為實數,b不等於0為虛數,
a=0且b不等於0時為純虛數。
復數的三角形式是
Z=r(cosx+isinx)
中x,r是實數,rcosx稱為實部,rsinx稱為虛部,i是虛數單位。Z與原點的距離r稱為Z的模,x稱為輻角。

『伍』 java 定義一個復數類

//餘下的自己完成

import java.util.Scanner;

public class ComplexOperation {

static Scanner s = new Scanner(System.in);

public Complex option(Complex c1, Complex c2, String opch) {
Complex r = new Complex();
if("+".equals(opch)) {
r.setReaPart(c1.getReaPart() + c2.getReaPart());
r.setVirPart(c1.getVirPart() + c2.getVirPart());

} else if("-".equals(opch)) {
r.setReaPart(c1.getReaPart() - c2.getReaPart());
r.setVirPart(c1.getVirPart() - c2.getVirPart());
}
return r;
}

public Complex read(String info) {
System.out.println(info);
Complex c = new Complex();
System.out.print("實部: ");
c.setReaPart(s.nextInt());
System.out.print("虛部: ");
c.setVirPart(s.nextInt());
return c;
}

public static void main(String[] args) {

// ComplexOperation co = new ComplexOperation();
// Complex c1 = co.read("輸入復數一");
// Complex c2 = co.read("輸入復數二");
// System.out.print("輸入運算符: ");
// String opch = s.next();
// System.out.print("結果是: " + co.option(c1, c2, opch));
// double d = 2.36;
// int len = 1;
// String format = "%" + len + ".2f";
// System.out.printf(format, d);

}

}
class Complex{
private int reaPart;
private int virPart;

public Complex() {
}
public Complex(int r, int v) {
this.reaPart = r;
this.virPart = v;

}

public String toString() {
int tag = this.getVirPart();
if(tag == 0) {
return getReaPart() + "";
} else if(tag > 0) {
return getReaPart() + "+" + getVirPart() + "i";
} else {
return getReaPart() + "-" + -getVirPart() + "i";
}

}
public int getReaPart() {
return reaPart;
}
public void setReaPart(int reaPart) {
this.reaPart = reaPart;
}
public int getVirPart() {
return virPart;
}
public void setVirPart(int virPart) {
this.virPart = virPart;
}

}

『陸』 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中怎麼表示復數

public class Complex { /**成員屬性定義實部和虛部 *@param real_part 實部 *@param imagin_part 虛部 */ private double real_part; private double imagin_part; //構造方法,初始化 public Complex() { this(0.0,0.0); } //構造方法的重載

『捌』 java編寫復數

public class Complex {

/**成員屬性定義實部和虛部
*@param real_part 實部
*@param imagin_part 虛部
*/
private double real_part;
private double imagin_part;

//構造方法,初始化
public Complex() {
this(0.0,0.0);
}

//構造方法的重載
public Complex(double real_part, double imagin_part) {
this.real_part = real_part;
this.imagin_part = imagin_part;
}

//取得實部
public void getReal_part(double real_part) {
this.real_part = real_part;
}

//取得虛部
public void getImagin_part() {
this.imagin_part = imagin_part;
}

//復數自加
public Complex addComplex(Complex c) {
this.real_part += c.real_part;
this.imagin_part += c.imagin_part;
return this;
}

//復數自減
public Complex decComplex(Complex c) {
this.real_part -= c.real_part;
this.imagin_part -= c.imagin_part;
return this;
}

//定義輸出格式
public String toString() {

//按照虛部正負,不同顯示
if(this.imagin_part >=0)
return this.real_part + "+" + this.imagin_part + "i";
return this.real_part + "-" + this.imagin_part + "i";
}
}

//測試類
class TestComplex {
public static void main(String[] args) {

//一個c1復數對象
Complex c1 = new Complex(1.0,1.0);

//輸出自加結果
c1.addComplex(c1);
System.out.println(c1.toString());

//輸出自減結果
c1.decComplex(c1);
System.out.println(c1.toString());
}
}

『玖』 用JAVA定義個復數類

public class Complex {

private double x;//實部
private double y;//虛部

public Complex(){}

/**構造函數
* @param x 實數部分
* @param y 虛數部分
*/
public Complex(double x,double y){
super();
this.x = x;
this.y = y;
}

/**求模
* @return 該復數的模
*/
public double mod(){
return x * x + y * y;
}

/**復數間加法
* @param complex 加數
* @return 計算結果
*/
public Complex add(Complex complex){
double x = this.x + complex.x;
double y = this.y + complex.y;
return new Complex(x,y);
}

/**復數與實數的加法
* @param a 加數
* @return 計算結果
*/
public Complex add(double a){
return this.add(new Complex(a,0));
}

/**復數間減法
* @param complex 減數
* @return 計算結果
*/
public Complex subtract(Complex complex){
double x = this.x - complex.x;
double y = this.y - complex.y;
return new Complex(x,y);
}

/**復數與實數的減法
* @param a 減數
* @return 計算結果
*/
public Complex subtract(double a){
return subtract(new Complex(a,0));
}

/**復數間乘法
* @param complex 乘數
* @return 計算結果
*/
public Complex multiply(Complex complex){
double x = this.x * complex.x - this.y * complex.y;
double y = this.y * complex.x + this.x * complex.y;
return new Complex(x,y);
}

/**復數間除法
* @param complex 除數
* @return 計算結果
*/
public Complex divide(Complex complex){
double x = (this.x * complex.x + this.y * complex.y) / (complex.mod());
double y = (this.y * complex.x - this.x * complex.y) / (complex.mod());
return new Complex(x,y);
}

public String toString(){
StringBuffer sb = new StringBuffer();
if(x != 0){
sb.append(x);
if(y > 0){
sb.append("+" + y + "i");
}else if(y < 0){
sb.append(y + "i");
}
}else{
if(y != 0){
sb.append(y + "i");
}
}
if(x == 0 && y == 0){
return "0";
}
return sb.toString();
}

public double getX() {
return x;
}

public void setX(double x) {
this.x = x;
}

public double getY() {
return y;
}

public void setY(double y) {
this.y = y;
}

public static void main(String[] args) {
Complex a = new Complex(2,0.5);
Complex b = new Complex(0.5,2);
System.out.println("(" + a + ")+(" + b + ")=" + a.add(b));
System.out.println("(" + a + ")+" + 2 + "=" + a.add(2));
System.out.println("(" + a + ")-(" + b + ")=" + a.subtract(b));
System.out.println("(" + a + ")-" + 2 + "=" + a.subtract(2));
System.out.println("(" + a + ")*(" + b + ")=" + a.multiply(b));
System.out.println("(" + a + ")/(" + b + ")=" + a.divide(b));
}

}

熱點內容
豬豬俠腳本沒反應 發布:2025-01-22 08:08:37 瀏覽:811
賽博朋克跟永劫無間哪個配置高 發布:2025-01-22 08:07:07 瀏覽:534
請盡快上傳 發布:2025-01-22 08:06:22 瀏覽:188
河北編程培訓 發布:2025-01-22 08:01:42 瀏覽:591
a星演算法視頻 發布:2025-01-22 07:55:01 瀏覽:878
快手安卓怎麼直播 發布:2025-01-22 07:54:58 瀏覽:937
買伺服器搭建vpn 發布:2025-01-22 07:53:21 瀏覽:808
路由器忘記密碼如何解 發布:2025-01-22 07:38:47 瀏覽:154
5分鍾視頻編譯 發布:2025-01-22 07:36:33 瀏覽:772
asp執行存儲過程 發布:2025-01-22 07:35:55 瀏覽:127