欢迎来到天天文库
浏览记录
ID:8970070
大小:65.66 KB
页数:3页
时间:2018-04-13
《java复数加减乘除程序》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、//将以下程序保存为ComplexNumber.java,调试后即可//classComplexNumber{privatedoublerealPart;privatedoubleiPart;publicComplexNumber()//实部、虚部置为0{this.realPart=0;this.iPart=0;}publicComplexNumber(doubler,doublei)//r为实部初值i为虚部初值{this.realPart=r;this.iPart=i;}publicdoublegetRealPart()//获复数对象的实部{returnrealPart
2、;}publicdoublegetIPart()//获复数对象的虚部{returniPart;}publicvoidsetRealPart(doubled)//将实部设置为给定数字{this.realPart=d;}publicvoidsetIPart(doubled)//将虚部设置为给定数字{this.iPart=d;}publicComplexNumbercomplexAdd(ComplexNumberc)//当前复数对象与形参复数对象相加{ComplexNumberresult=newComplexNumber();result.realPart=this.real
3、Part+c.realPart;result.iPart=this.iPart+c.iPart;returnresult;}publicComplexNumbercomplexAdd(doublec)//当前复数对象与形参实数对象相加{ComplexNumberresult=newComplexNumber();result.realPart=this.realPart+c;returnresult;}publicComplexNumbercomplexMinus(ComplexNumberc)//当前复数对象与形参复数对象相减{ComplexNumberresult=n
4、ewComplexNumber();result.realPart=this.realPart-c.realPart;result.iPart=this.iPart-c.iPart;returnresult;}publicComplexNumbercomplexMinus(doublec)//当前复数对象与形参实数对象相减{ComplexNumberresult=newComplexNumber();result.realPart=this.realPart-c;returnresult;}publicComplexNumbercomplexMulti(ComplexNu
5、mberc)//当前复数对象与形参复数对象相乘{ComplexNumberresult=newComplexNumber();result.realPart=this.realPart*c.realPart-this.iPart*c.iPart;result.iPart=this.iPart*c.realPart+this.realPart*c.iPart;returnresult;}publicComplexNumbercomplexMulti(doublec)//当前复数对象与形参实数对象相乘{ComplexNumberresult=newComplexNumber(
6、);result.realPart=this.realPart*c;result.iPart=this.iPart*c;returnresult;}publicStringtoString()//把当前复数对象的实部、虚部组合成a+bi的字符串形式输出{if(realPart!=0)//实部不为0的情况if(iPart>0)returnrealPart+"+"+iPart+"i";//实部不为0且虚部大于0的情况elseif(iPart==0)returnString.valueOf(realPart);//实部不为0且虚部等于0的情况elsereturnString.v
7、alueOf(realPart)+String.valueOf(iPart)+"i";//实部不为0且虚部小于0的情况elsereturnString.valueOf(iPart)+"i";//实部为0的情况}publicstaticvoidmain(Stringargs[]){ComplexNumbercomplex1=newComplexNumber(4,5);ComplexNumbercomplex2=newComplexNumber(3,-4);System.out.println("("+complex1+")*("
此文档下载收益归作者所有