java 编写的复数加减乘除.docx
《java 编写的复数加减乘除.docx》由会员分享,可在线阅读,更多相关《java 编写的复数加减乘除.docx(19页珍藏版)》请在冰豆网上搜索。
java编写的复数加减乘除
secondComplex.java
packagesecondComplex;
publicclasssecondComplex{
privatedoublereal;
privatedoublevir;
publicsecondComplex(){
this(0.0,0.0);
}
publicsecondComplex(doublereal,doublevir){
this.real=real;
this.vir=vir;
}
publicsecondComplex(secondComplexc){
this.real=c.real;
this.vir=c.vir;
}
//求共轭复数
publicstaticsecondComplexConjugate(secondComplexa){
returnnewsecondComplex(a.real,-a.vir);
}
//求复数的相反数
publicsecondComplexoppositeComplex(secondComplexa){
returnnewsecondComplex(-a.real,-a.vir);
}
//求复数的倒数
publicstaticsecondComplexReciprocal(secondComplexa){
secondComplexb=secondComplex.Conjugate(a);
b.Multiplication(a);
b.real=b.real+b.vir;
a=secondComplex.Conjugate(a);
if(Math.abs(b.real)<1e-6){
b.real=0;
returnnull;
}
returnnewsecondComplex(a.real/b.real,a.vir/b.real);
}
/**
*this+=a
*@return
*
*/
publicvoidAdd(secondComplexa){
this.real+=a.real;
this.vir+=a.vir;
}
/**
*this-=a;
*/
publicvoidSub(secondComplexa){
a=a.oppositeComplex(a);
this.Add(a);
}
/**
*this*=a;
*/
publicvoidMultiplication(secondComplexa){
doubletmp;
tmp=this.real;
this.real=this.real*a.real-this.vir*a.vir;
this.vir=tmp*a.vir+this.vir*a.real;
}
/**
*this/=b
*@return
*/
publicvoidDivision(secondComplexa){
secondComplexb=Multiplication(this,secondComplex.Reciprocal(a));
if(b==null){
return;
}
this.real=b.real;
this.vir=b.vir;
}
/**
*
*c=this+a
*@return
*/
publicsecondComplexAdd(secondComplexa,inttype){
returnAdd(this,a);
}
/**
*c=this-a
*/
publicsecondComplexSub(secondComplexa,inttype){
a=a.oppositeComplex(a);
returnAdd(this,a);
}
/**
*c=this*a
*/
publicsecondComplexMultiplication(secondComplexa,inttype){
returnMultiplication(this,a);
}
/**
*c=this/b
*@return
*/
publicsecondComplexDivision(secondComplexa,inttype){
returnDivision(this,a);
}
/**
*c=a+b
*@parama
*@paramb
*@return
*/
publicstaticsecondComplexAdd(secondComplexa,secondComplexb){
returnnewsecondComplex(a.real+b.real,a.vir+b.vir);
}
/**
*c=a-b
*@return
*/
publicsecondComplexSub(secondComplexa,secondComplexb){
b=b.oppositeComplex(b);
returnAdd(a,b);
}
/**
*c=a*b
*@return
*/
publicstaticsecondComplexMultiplication(secondComplexa,secondComplexb){
if(b==null){
returnnull;
}
returnnewsecondComplex(a.real*b.real-a.vir*b.vir
a.real*b.vir+a.vir*b.real);
}
/**
*c=a/b
*@return
*/
publicsecondComplexDivision(secondComplexa,secondComplexb){
if(secondComplex.Reciprocal(b)==null){
returnnull;
}
returnMultiplication(a,secondComplex.Reciprocal(b));
}
publicdoublegetReal(){
if(Math.abs(real)<1e-6)
real=0;
returnreal;
}
publicvoidsetReal(doublereal){
this.real=real;
}
publicdoublegetVir(){
if(Math.abs(vir)<1e-6)
vir=0;
returnvir;
}
publicvoidsetVir(doublevir){
this.vir=vir;
}
publicStringtoString(){
if(Math.abs(this.getReal())<1e-6&&Math.abs(this.getVir())<1e-6){
System.out.println(0);
}
elseif(Math.abs(this.getReal())<1e-6){
if(Math.abs(this.getVir())<1e-6){
System.out.println(this.getReal());
}
elseif(Math.abs(this.getVir())==1)
{
if(this.getVir()>0){
System.out.println("i");
}else{
System.out.println("-i");
}
}else{
System.out.println(this.getVir()+"i");
}
}
elseif(Math.abs(this.getVir())<1e-6){
System.out.println(this.getReal());
if(Math.abs(this.getVir())==1)
{
System.out.print("");
if(this.getVir()>0){
System.out.println("+i");
}else{
System.out.println("-i");
}
}
elseif(Math.abs(this.getVir())>1e-6){
System.out.println(""+this.getVir()+"i");
}
}else{
if(this.getVir()>0){
System.out.println(this.getReal()+"+"+this.getVir()+"i");
}else{
System.out.println(this.getReal()+"-"+Math.abs(this.getVir())+"i");
}
}
return"";
}
}
Complex.java
packagesecondComplex;
publicclassComplexextendssecondComplex{
privatedoublemo;
privatedoublejiao;
publicComplex(){
this(1.0,Math.PI/4);
}
publicComplex(doublemo,doublejiao){
super(Complex.returnX(mo,jiao),Complex.returnY(mo,jiao));
}
publicstaticdoublereturnX(doublemo,doublejiao){
returnmo*Math.cos(jiao);
}
publicstaticdoublereturnY(doublemo,doublejiao){
returnmo*Math.sin(jiao);
}
publicstaticdoublereturnMo(doublex,doubley){
x=Math.abs(x);
y=Math.abs(y);
if(x<1e-6)
x=0;
if(y<1e-6)
y=0;
returnMath.sqrt(x*x+y*y);
}
publicstaticdoublereturnJiao(doublex,doubley){
doubletmp1;
intst1;
intst2;
doubletmp2;
//横轴正向:
1;反向:
-1;原点:
0
//纵轴正向:
1;反向:
-1;原点:
0
if(Math.abs(x)<1e-6){
tmp1=0;
st1=0;
}elseif(x>0){
tmp1=x;
st1=1;
}else{
tmp1=x;
st1=-1;
}
if(Math.abs(y)<1e-6){
tmp2=0;
st2=0;
}elseif(y>0){
tmp2=y;
st2=1;
}else{
tmp2=y;
st2=-1;
}
if(0==st1&&0==st2){
System.out.println("复数无辐角");
return-1000;
}elseif(0==st1&&0!
=st2){
if(st2>0){
returnMath.PI/2;
}else{
return-Math.PI/2;
}
}elseif(st1<0&&0==st2){
returnMath.PI;
}
elseif(st1>0&&tmp2!
=0.0){
returnMath.atan(tmp2/tmp1);
}elseif(st1<0&&tmp2!
=0.0){
returnMath.atan(tmp2/tmp1)-Math.PI;
}
return-1000;
}
publicStringtoString(){
if(Complex.returnJiao(this.getReal(),this.getVir())<=-900)
return"";
return"计算结果模值为:
"+((double)((int)(Complex.returnMo(this.getReal(),
this.getVir())*1000)))/1000+"角度为:
"+
((double)((int)(Complex.returnJiao(this.getReal(),this.getVir())*1000)))/1000;
}
publicdoublegetMo(){
returnmo;
}
publicvoidsetMo(doublemo){
this.mo=mo;
}
publicdoublegetJiao(){
returnjiao;
}
publicvoidsetJiao(doublejiao){
this.jiao=jiao;
}
/**
*this+=a
*/
publicvoidAdd(Complexa){
this.setReal(this.getReal()+a.getReal());
this.setVir(this.getVir()+a.getVir());
}
publicvoidSub(Complexa){
secondComplextmp=a.oppositeComplex(newsecondComplex(a.getReal(),a.getVir()));
Complexd=newComplex();
d.setReal(tmp.getReal());
d.setVir(tmp.getVir());
this.Add(d);
}
publicvoidMultiplication(Complexa){
this.setReal(this.getReal()*a.getReal()-this.getVir()*a.getVir());
this.setVir(this.getReal()*a.getVir()+this.getVir()*a.getReal());
}
publicvoidDivision(Complexa){
secondComplextmp=newsecondComplex(a.getReal(),a.getVir());
//Complexb=Multiplication(this,secondComplex.Reciprocal(tmp));
secondComplexb=secondComplex.Reciprocal(tmp);
if(b==null){
return;
}
Complexc=newComplex(Complex.returnMo(b.getReal(),b.getVir()),
Complex.returnJiao(b.getReal(),b.getVir()));
Complexd=Multiplication(this,c);
this.setReal(d.getReal());
this.setVir(d.getVir());
}
/**
*c=this+a
*@return
*/
publicComplexAdd(Complexa,inttype){
returnAdd(this,a);
}
publicComplexSub(Complexc,inttype){
secondComplexa=newsecondComplex(c.getReal(),c.getVir());
a=a.oppositeComplex(a);
Complexd=newComplex(Complex.returnMo(a.getReal(),a.getVir()),
Complex.returnJiao(a.getReal(),a.getVir()));
returnAdd(this,d);
}
publicComplexMultiplication(Complexa,inttype){
returnMultiplication(this,a);
}
publicComplexDivision(Complexa,inttype){
returnDivision(this,a);
}
/**
*c=a+b
*@return
*/
publicstaticComplexAdd(Complexa,Complexb){
secondComplexc=newsecondComplex(a.getReal()+b.getReal(),a.getVir()+b.getVir());
Complexd=newComplex(Complex.returnMo(c.getReal(),c.getVir()),
Complex.returnJiao(c.getReal(),c.getVir()));
returnd;
}
publicComplexSub(Complexa,Complexd){
secondComplexb=newsecondComplex(d.getReal(),d.getVir());
b=b.oppositeComplex(b);
Complexc=newComplex(Complex.returnMo(b.getReal(),b.getVir()),
Complex.returnJiao(b.getReal(),b.getVir()));
returnAdd(a,c);
}
publicstaticComplexMultiplication(Complexa,Complexb){
if(b==null){
returnnull;
}
secondComplexbb=newsecondComplex(a.getReal()*b.getReal()-a.getVir()*b.getVir()
a.getReal()*b.getVir()+a.getVir()*b.getReal());
Complexc=newComplex(Complex.returnMo(bb.getReal(),bb.getVir()),
Complex.returnJiao(bb.getReal(),bb.getVir()));
returnc;
}
publicComplexDivision(Complexa,Complexc){
secondComplexb=newsecondComplex(c.getReal(),c.getVir());
secondComplexf=secondComplex.Reciprocal(b);
if(f==null){
returnnull;
}
Complexd=newComplex(Complex.returnMo(f.getReal(),f.getVir()),
Complex.returnJiao(f.getReal(),f.getVir()));
returnMultiplication(a,d);
}
}
demoSecondComplex.java
packagesecondComplex;
publicclassdemoSecondComplex{
publicstaticvoidmain(String[]args){
System.out.println("--------------------mojiao-----------------");
Complexa=newComplex(1.0,0.5*Math.PI);
Complexb=newComplex(2.0,0.5*Math.PI);
a.Add(b);
System.out.println(a);
a=b.Add(a,0);
System.out.println(a);
Complexd=a.Add(b,a);
System.out.println(d);
a.Sub(b);
System.out.print