当前位置:首页 » 编程语言 » 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));
}
}

热点内容
安卓如何设置桌面返回键 发布:2025-02-06 13:58:15 浏览:48
bi可视化php 发布:2025-02-06 13:50:15 浏览:931
shell写脚本文件 发布:2025-02-06 13:47:32 浏览:231
健身器材脚本 发布:2025-02-06 13:46:36 浏览:856
怎么从手机里卸载存储卡 发布:2025-02-06 13:35:04 浏览:644
诛仙青云志2ftp 发布:2025-02-06 13:34:48 浏览:34
mill91编程 发布:2025-02-06 13:10:27 浏览:294
华为平板怎么储存服务器文件 发布:2025-02-06 12:49:21 浏览:482
php查询结果数组 发布:2025-02-06 12:31:05 浏览:717
怎样把照片压缩打包 发布:2025-02-06 12:15:19 浏览:498