Java实现的复数类

               

class Complex{

// 定义属性

double RealPart;

double ImagePart;

// 定义构造函数

public Complex(){

}

public Complex(double R,double I){

this.RealPart=R;

this.ImagePart=I;

}

// 定义公有方法

void setReal(double r){

this.RealPart=r;

}

void setImage(double i){

this.ImagePart=i;

}

double getReal(){

return this.RealPart;

}

double getImage(){

return this.ImagePart;

}

Complex ComplexAdd(Complex a){ // 加

return new Complex

(this.RealPart+a.RealPart,this.ImagePart+a.ImagePart);

}

Complex ComplexSub(Complex a){ // 减

return new Complex(this.RealPart-a.RealPart,this.ImagePart-

a.ImagePart);

}

Complex ComplexMulti(Complex a){ // 乘

double r=this.RealPart*a.RealPart-this.ImagePart*a.ImagePart; double i=this.RealPart*a.ImagePart+this.ImagePart*a.RealPart; return new Complex(r,i);

}

Complex ComplexDiv(Complex a){ // 除

if(this.RealPart==0&&a.ImagePart==0){ // 根据后面得到的内容 System.out.println("输入的被除数不能都为0!\n"); return null;

}


// 化简得到公式为:(a+bi)/(c+di)

// 实体部分:(a*c+b*d)/(a*a+d*d)

// 虚体部分:(b*c-a*d)/(a*a+d*d)

double xx=this.RealPart*this.RealPart+a.ImagePart*a.ImagePart; double r=(this.RealPart*a.RealPart+this.ImagePart*a.ImagePart)/xx; double i=(this.ImagePart*a.RealPart-this.RealPart*a.ImagePart)/xx; return new Complex(r,i);

}

boolean isEqual(Complex a){

return (this.RealPart==a.RealPart&&this.ImagePart==a.ImagePart); }

String ToString(){

return this.RealPart+"+"+this.ImagePart+"i";

}

}

public class one{

public static void main(String[] args){

Complex a=new Complex();

Complex b=new Complex(1,2);

Complex c=new Complex(3,4);

System.out.println("a的值为:"+a.ToString());

System.out.println("b的值为:"+b.ToString());

System.out.println("c的值为:"+c.ToString());

System.out.println();

System.out.println("a.RealPart:"+a.getReal());

System.out.println("b.RealPart:"+b.getReal());

System.out.println("c.RealPart:"+c.getReal());

System.out.println();

Complex d=b.ComplexAdd(c);

System.out.println("d的值:"+d.ToString());

System.out.println("d.ImagePart:"+d.getImage());

Complex e=b.ComplexMulti(c);

System.out.println("e的值:"+e.ToString());

System.out.println("e.ImagePart:"+e.getImage());

System.out.println("d和e相等吗:"+d.isEqual(e));

// 设置值后

e.setReal(4.0);


} } e.setImage(6.0); System.out.println("设置了值后d和e相等吗:"+d.isEqual(e));

对于学习有困难不知道如何提升自己可以加扣:578024144  进行交流得到帮助,获取学习资料


           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用复数类(Complex)来实现复数运算。下面是一个简单的例子: ```java public class Complex { private double real; // 实部 private double imaginary; // 虚部 public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } // 加法运算 public Complex add(Complex other) { double realPart = this.real + other.real; double imaginaryPart = this.imaginary + other.imaginary; return new Complex(realPart, imaginaryPart); } // 减法运算 public Complex subtract(Complex other) { double realPart = this.real - other.real; double imaginaryPart = this.imaginary - other.imaginary; return new Complex(realPart, imaginaryPart); } // 乘法运算 public Complex multiply(Complex other) { double realPart = this.real * other.real - this.imaginary * other.imaginary; double imaginaryPart = this.real * other.imaginary + this.imaginary * other.real; return new Complex(realPart, imaginaryPart); } // 除法运算 public Complex divide(Complex other) { double realPart = (this.real * other.real + this.imaginary * other.imaginary) / (other.real * other.real + other.imaginary * other.imaginary); double imaginaryPart = (this.imaginary * other.real - this.real * other.imaginary) / (other.real * other.real + other.imaginary * other.imaginary); return new Complex(realPart, imaginaryPart); } @Override public String toString() { return real + " + " + imaginary + "i"; } } ``` 使用示例: ```java Complex a = new Complex(1, 2); Complex b = new Complex(3, 4); // 加法 Complex c = a.add(b); System.out.println(c); // 输出 4.0 + 6.0i // 减法 Complex d = a.subtract(b); System.out.println(d); // 输出 -2.0 - 2.0i // 乘法 Complex e = a.multiply(b); System.out.println(e); // 输出 -5.0 + 10.0i // 除法 Complex f = a.divide(b); System.out.println(f); // 输出 0.44 + 0.08i ``` 以上代码实现了复数的加、减、乘、除运算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值