Java实现复数运算

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
else
System.out.println(this.realPart + "+" + this.imagePart + "i");
}
// add two complexes
public void add(Complex x, Complex y) {
this.realPart = x.realPart + y.realPart;
定义一个复数类complex,它的内部具有两个实例变量:realPart和imagPart,分别代表复数的实部和虚部,
编程实现要求的数学运算。
(1)实现两个复数相加。复数加运算的原则是:复数的实部和虚部分别相加。
(2)实现两个复数相减。复数减运算的原则是:复数的实部和虚部分别相减。
(3)输出运算结果,判断是否正确。
********************************************************************************************/
//程序清单
/*
* Complex.java
*/
public class Complex {
// real part
System.out.println("0");
else if (this.realPart == 0.0)
System.out.println(this.imagePart + "i");
else if (this.imagePart == 0.0)
System.out.println(this.realPart);
private double realPart;
// imag part
private double imagePart;
// default init
public Complex() {
this.realPwk.baidu.comrt = 0.0;
this.imagePart = 0.0;
}
// init with arguments
this.imagePart = x.imagePart + y.imagePart;
}
//sub two complexes
public void sub(Complex x, Complex y) {
this.realPart = x.realPart - y.realPart;
this.imagePart = x.imagePart - y.imagePart;
}
public static void main(String arg[]) {
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(2, 2);
Complex c3 = new Complex();
c1.show();
c2.show();
c3.add(c1, c2);
System.out.print("add : ");
c3.show();
c3.sub(c1, c2);
System.out.print("sub : ");
c3.show();
}
}
public Complex(double real, double image) {
this.realPart = real;
this.imagePart = image;
}
// show the complex
public void show() {
if ((this.realPart == 0.0) && (this.imagePart == 0.0))
相关文档
最新文档