Java与数学表达式分裂

时间:2015-01-06 22:11:49

标签: java math

我正在尝试拆分数学表达式。

String number = "100+500";

String[] split = new String[3];

我想制作

  • split [0] =“100”
  • split [1] =“+”
  • split [2] =“500”

我尝试了这个,但我不知道该怎么写分裂。

split = number.split(????);

6 个答案:

答案 0 :(得分:6)

您希望在不消耗任何输入的情况下在数字和非数字之间进行分割......您需要环顾四周:

String[] split = number.split("(?<=\\d)(?=\\D)|(?<=\\D)(?=\\d)");

什么是火车破坏正则表达式?

它正在表达这个答案的最初句子:

  • (?<=\d)表示前一个字符是数字
  • (?=\D)表示下一个字符是非数字
  • (?<=\d)(?=\D)一起匹配数字与非数字之间
  • regexA|regexB表示regexA regexB匹配,用作上述点,但非数字然后数字用于反之亦然逻辑

重要的一点是环顾四周非消费,因此拆分时不会吞噬任何输入。


这是一些测试代码:

String number = "100+500-123/456*789";
String[] split = number.split("(?<=\\d)(?=\\D)|(?<=\\D)(?=\\d)");
System.out.println(Arrays.toString(split));

输出:

[100, +, 500, -, 123, /, 456, *, 789]

答案 1 :(得分:3)

关闭蝙蝠,我不知道拆分的任何库例程。自定义拆分例程可以是这样的:

/**
 * Splits the given {@link String} at the operators +, -, * and /
 * 
 * @param string
 *            the {@link String} to be split.
 * @throws NullPointerException
 *             when the given {@link String} is null.
 * @return a {@link List} containing the split string and the operators.
 */
public List<String> split(String string) throws NullPointerException {
    if (string == null)
        throw new NullPointerException("the given string is null!");
    List<String> result = new ArrayList<String>();

    // operators to split upon
    String[] operators = new String[] { "+", "-", "*", "/" };

    int index = 0;
    while (index < string.length()) {
        // find the index of the nearest operator
        int minimum = string.length();
        for (String operator : operators) {
            int i = string.indexOf(operator, index);
            if (i > -1)
                minimum = Math.min(minimum, i);
        }

        // if an operator is found, split the string
        if (minimum < string.length()) {
            result.add(string.substring(index, minimum));
            result.add("" + string.charAt(minimum));
            index = minimum + 1;
        } else {
            result.add(string.substring(index));
            break;
        }
    }

    return result;
}

一些测试代码:

System.out.println(split("100+10*6+3"));
System.out.println(split("100+"));

输出:

[100, +, 10, *, 6, +, 3]
[100, +]

答案 2 :(得分:2)

您还可以使用Java中的Pattern / Matcher类:

    String expression = "100+34";
    Pattern p = Pattern.compile("(\\d+)|(\\+)");
    Matcher m = p.matcher(expression);
    String[] elems = new String[m.groupCount() +1];
    int i=0;

    while(m.find())
    {
        elems[i++] = m.group();
    }

答案 3 :(得分:1)

你可以做一些简单而不是疯狂的正则表达式;只需用空格填充+

String number = "100+500";
number = number.replace("+", " + ");

现在你可以在白色空间拆分:

String[] split = number.split(" ");

现在你的指数将被设定:

split[0] = "100";
split[1] = "+";
split[2] = "500";

要检查所有算术符号,如果要避免使用正则表达式,可以使用以下方法:

public static String replacing(String s) {
   String[] chars = {"+", "-", "/", "="};

   for (String character : chars) {
      if (s.contains(character)) {
         s = s.replace(character, " " + character + " ");//not exactly elegant, but it works
      }
   }
   return s;
}

//in main method
number = replacing(number);
String[] split = number.split(" ");

答案 4 :(得分:0)

您可以拆分表达式字符串,然后在具有纯标记和分类标记的结果中拆分。 mXparser库支持此以及计算过程。请按照以下示例:

你非常简单的例子“100 + 500”:

import org.mariuszgromada.math.mxparser.*;
...
...
Expression e = new Expression("100+500");
mXparser.consolePrintTokens( e.getCopyOfInitialTokens() );

结果:

[mXparser-v.4.0.0]  --------------------
[mXparser-v.4.0.0] | Expression tokens: |
[mXparser-v.4.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.4.0.0] |    TokenIdx |       Token |        KeyW |     TokenId | TokenTypeId |  TokenLevel |  TokenValue |   LooksLike |
[mXparser-v.4.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.4.0.0] |           0 |         100 |       _num_ |           1 |           0 |           0 |       100.0 |             |
[mXparser-v.4.0.0] |           1 |           + |           + |           1 |           1 |           0 |         NaN |             |
[mXparser-v.4.0.0] |           2 |         500 |       _num_ |           1 |           0 |           0 |       500.0 |             |
[mXparser-v.4.0.0]  ---------------------------------------------------------------------------------------------------------------

更复杂的例子“2 * sin(x)+(3 / cos(y)-e ^(sin(x)+ y))+ 10”:

import org.mariuszgromada.math.mxparser.*;
...
...
Argument x = new Argument("x");
Argument y = new Argument("y");
Expression e = new Expression("2*sin(x)+(3/cos(y)-e^(sin(x)+y))+10", x, y);
mXparser.consolePrintTokens( e.getCopyOfInitialTokens() );

结果:

[mXparser-v.4.0.0]  --------------------
[mXparser-v.4.0.0] | Expression tokens: |
[mXparser-v.4.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.4.0.0] |    TokenIdx |       Token |        KeyW |     TokenId | TokenTypeId |  TokenLevel |  TokenValue |   LooksLike |
[mXparser-v.4.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.4.0.0] |           0 |           2 |       _num_ |           1 |           0 |           0 |         2.0 |             |
[mXparser-v.4.0.0] |           1 |           * |           * |           3 |           1 |           0 |         NaN |             |
[mXparser-v.4.0.0] |           2 |         sin |         sin |           1 |           4 |           1 |         NaN |             |
[mXparser-v.4.0.0] |           3 |           ( |           ( |           1 |          20 |           2 |         NaN |             |
[mXparser-v.4.0.0] |           4 |           x |           x |           0 |         101 |           2 |         NaN |             |
[mXparser-v.4.0.0] |           5 |           ) |           ) |           2 |          20 |           2 |         NaN |             |
[mXparser-v.4.0.0] |           6 |           + |           + |           1 |           1 |           0 |         NaN |             |
[mXparser-v.4.0.0] |           7 |           ( |           ( |           1 |          20 |           1 |         NaN |             |
[mXparser-v.4.0.0] |           8 |           3 |       _num_ |           1 |           0 |           1 |         3.0 |             |
[mXparser-v.4.0.0] |           9 |           / |           / |           4 |           1 |           1 |         NaN |             |
[mXparser-v.4.0.0] |          10 |         cos |         cos |           2 |           4 |           2 |         NaN |             |
[mXparser-v.4.0.0] |          11 |           ( |           ( |           1 |          20 |           3 |         NaN |             |
[mXparser-v.4.0.0] |          12 |           y |           y |           1 |         101 |           3 |         NaN |             |
[mXparser-v.4.0.0] |          13 |           ) |           ) |           2 |          20 |           3 |         NaN |             |
[mXparser-v.4.0.0] |          14 |           - |           - |           2 |           1 |           1 |         NaN |             |
[mXparser-v.4.0.0] |          15 |           e |           e |           2 |           9 |           1 |         NaN |             |
[mXparser-v.4.0.0] |          16 |           ^ |           ^ |           5 |           1 |           1 |         NaN |             |
[mXparser-v.4.0.0] |          17 |           ( |           ( |           1 |          20 |           2 |         NaN |             |
[mXparser-v.4.0.0] |          18 |         sin |         sin |           1 |           4 |           3 |         NaN |             |
[mXparser-v.4.0.0] |          19 |           ( |           ( |           1 |          20 |           4 |         NaN |             |
[mXparser-v.4.0.0] |          20 |           x |           x |           0 |         101 |           4 |         NaN |             |
[mXparser-v.4.0.0] |          21 |           ) |           ) |           2 |          20 |           4 |         NaN |             |
[mXparser-v.4.0.0] |          22 |           + |           + |           1 |           1 |           2 |         NaN |             |
[mXparser-v.4.0.0] |          23 |           y |           y |           1 |         101 |           2 |         NaN |             |
[mXparser-v.4.0.0] |          24 |           ) |           ) |           2 |          20 |           2 |         NaN |             |
[mXparser-v.4.0.0] |          25 |           ) |           ) |           2 |          20 |           1 |         NaN |             |
[mXparser-v.4.0.0] |          26 |           + |           + |           1 |           1 |           0 |         NaN |             |
[mXparser-v.4.0.0] |          27 |          10 |       _num_ |           1 |           0 |           0 |        10.0 |             |
[mXparser-v.4.0.0]  ---------------------------------------------------------------------------------------------------------------

要了解Token.tokenId和Token.tokenTypeId意味着您需要引用API documentation和parsertokens部分。例如,在Operator类中你有

  1. Operator.TYPE_ID - 如果Token被识别为Operator,则对应于Token.tokenTypeId
  2. Operator.OPERATOR_NAME_ID - 如果Token被识别为特定的OPERATOR_NAME,则对应于Token.tokenId。
  3. 请关注mXparser tutorial以便更好地理解。

    祝你好运

答案 5 :(得分:0)

由于+,-,*基本上所有数学符号都是特殊字符,因此您可以在拆分函数中将“ \\”放在此类之前

String number = "100+500";
String[] numbers = number.split("\\+");
for (String n:numbers) {
  System.out.println(n);
}