Java复数运算

class test {
    public static void main(String[] args) {

        String s = "(2,3i)+(4,5i)";
        int p1 = s.indexOf("+");
        String one = s.substring(0, p1);
        String two = s.substring(p1 + 1);

        Complex c1 =  convert(one);
        Complex c2 =  convert(two);
        Complex c = Complex.add(c1, c2);
        System.out.println(c);
        //把一个字符串类似(2,3i)转化为Complex对象


    }

    private static Complex convert(String input) {
        int p2 = input.indexOf(",");
        double x, y;
        x = Double.parseDouble(input.substring(1, p2));
        y = Double.parseDouble(input.substring(p2 + 1, input.length() - 2));
        return new Complex(x, y);
    }
}

    class Complex {
        double real;
        double image;

        public Complex(double real, double image) {
            this.real = real;
            this.image = image;
        }

        @Override
        public String toString() {
            return "Complex{" +
                    "real=" + real +
                    ", image=" + image +
                    '}';
        }

        public  static Complex add(Complex c1, Complex c2) {
            return new Complex(c1.real + c2.real, c1.image + c2.image);
        }

    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值