如何在Java中解析复杂数字

时间:2016-03-28 18:00:03

标签: java

我正在尝试接受包含以下任何变体的字符串到我的方法中:

" 11 + 6 1" " 11-6i" " -11-6i"

使用下面的代码,前两个代码正常工作但由于某种原因,当字符串包含两个" - "字符我似乎无法正确解析它。

无论如何都要修改我的代码,以便能够解析" -11-6i"还有吗?

到目前为止,这是我的代码:

if (passedString.contains("+")) {

        int i = passedString.indexOf("+");
        int endIndex = passedString.indexOf("i");

        String real = passedString.substring(0, i);

        String imag = passedString.substring(i, endIndex);

        MyDouble realNum = new MyDouble(Double.parseDouble(real));
        MyDouble imagNum = new MyDouble(Double.parseDouble(imag));

        cn = new ComplexNumber(realNum, imagNum);

    } else {

        int i = passedString.indexOf("-");

        int endIndex = passedString.indexOf("i");

        String real = passedString.substring(0, i);

        String imag = passedString.substring(i, endIndex);

        MyDouble realNum = new MyDouble(Double.parseDouble(real));
        MyDouble imagNum = new MyDouble(Double.parseDouble(imag));

        cn = new ComplexNumber(realNum, imagNum);

    }

4 个答案:

答案 0 :(得分:1)

最好的方法是重用其他代码。看看Apache Commons Complex Numbers

以下是他们页面中的一个示例:

ComplexFormat cf = new ComplexFormat();
Complex c = cf.parse("1.110 + 2.222i");

答案 1 :(得分:0)

在else语句中,如果您搜索'的最后一个索引 - '它会找到第二个减号,而不是第一个减号,这就是indexOf的作用。

int i = passedString.lasIndexOf("-");

或者,您可以使用正则表达式来解析数字:

Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher(passedString);
String real = "0";
String imag = "0";
if(m.find()){
  real = m.group(0);
  if(m.find()){
     imag = m.group(0);
  }
 }

MyDouble realNum = new MyDouble(Double.parseDouble(real));
MyDouble imagNum = new MyDouble(Double.parseDouble(imag));

cn = new ComplexNumber(realNum, imagNum);

" - &#34?;可选择搜索一个主要的负号," \\ d"会寻找一个数字," +"将寻找该数字一次或多次

答案 2 :(得分:0)

我在scala中的解决方案仅适用于整数复数:

case class Complex(rel: Int, img: Int)
case class Triplet(sign: String, weight: Int, coord: Char) // -10i is ("-", -10, i)
val pattern = "([+-])?(\\d*)i?".r
//let say the following string is complex number
val cplxStr = "1 + j"
def triplet(part: String) = {
    def intOf(l: String): Int =  if(l == "") 1 else l.toInt

    val pattern(sn, w, c) = part
    if(part isEmpty) Triplet("", 0, "")
    else if(sn == "-") Triplet(sn,-1*intOf(w), c)
    else Triplet(sn,intOf(w),c)
}
val parts = s.replace("-", "+-").split("\\+").map(triplet)

var coeffs:Array[Int] = new Array(2)
for(Triplet(s,w,c) <- parts){
   if(c == "i") coeffs(1) = w
   else  coeffs(0) = w
}
Complex(coeffs(0), coeffs(1))

可以通过更改解析器来适应双倍。祝你好运

答案 3 :(得分:0)

public static Complex parse(String s) {
    if (s == "i") return new Complex(1);
    String[] temp = {};
    double res_real, res_imag;
    boolean real_nega = false, imag_nega = false; 
    String realString = null;
    String imagString = null;
    if (s.charAt(0) == '-') {
        s = s.substring(1);
        real_nega = true;
    }
    if (Global.isDigit(s.charAt(0))) {
        if (s.contains("i")) {
            temp = new String[2];
            temp[0] = "0";
            temp[1] = s.split("i")[0];
            imag_nega = real_nega;
            real_nega = false;
        }
        else {
            temp = new String[2];
            temp[0] = s;
            temp[1] = "0";
        }
    }
    if (s.contains("+")) {
        temp = s.split("\\+");
    }
    else if (s.contains("-")) {
        temp = s.split("-");
        imag_nega = true;
    }
    if (temp[0].contains("i")) {
        realString = temp[1];
        imagString = temp[0].trim().split("i")[0];
    }
    else {
        realString = temp[0];
        imagString = temp[1].trim().split("i")[0];
    }
    res_real = Double.parseDouble(realString);
    res_imag = Double.parseDouble(imagString);
    if (real_nega) res_real = -res_real;
    if (imag_nega) res_imag = -res_imag;
    return new Complex(res_real, res_imag);
}