简体   繁体   English

如何拆分 java 中组件的数学表达式?

[英]How to split mathematical expression on components in java?

I have some issue about split mathematical expression.我有一些关于拆分数学表达式的问题。

I want to split every number, operator and parentheses, and send them to an array, this is my code for different case:我想拆分每个数字、运算符和括号,并将它们发送到一个数组,这是我针对不同情况的代码:

String infix = "-(1/10)^(-2)-(-2)^(5)-2-3+1^(-2)";

//List<String>    tokenList = Arrays.asList(infix.split("(?<=[+*/()!^-])|(?=[+*/()!^-])"));
//List<String>    tokenList = Arrays.asList(infix.split("(?=[-+*/()])|(?<=[^-+*/][-+*/])|(?<=[()])"));
//List<String>     tokenList = Arrays.asList(infix.split("((?<=\\^)|(?=\\^))"));
List<String>     tokenList = Arrays.asList(infix.split("((?<=[+*/()!])|(?=[+*/()!]))|((?<=\\^)|(?=\\^))|([0-9]+(?<=[-])|(?=[-]))"));

i can't separate it.我不能把它分开。 I often got wrong expression我经常表达错误

[1, 10, /, -2, ^, -2, 5, -2, -3, ^, 1, -2, ^, +, -, -] [1、10、/、-2、^、-2、5、-2、-3、^、1、-2、^、+、-、-]

My program can't divide minus sign.我的程序不能除减号。 if have -1-1 my program divide it like [-1,-1].如果有-1-1 ,我的程序将其划分为 [-1,-1]。 Please, help me solve this issue.请帮我解决这个问题。

Agree with Aaron.同意亚伦。 The solution could be based on FSM (finite state machine).该解决方案可以基于 FSM(有限 state 机器)。 Essentially, you have a string expression, and you are starting parsing.本质上,您有一个字符串表达式,并且您正在开始解析。 Also, consider there's a temporary string variable that is empty for now.此外,请考虑暂时为空的临时字符串变量。 When you read the first char, you go and write it to the variable and decide, what it is: in this example, it could be a number, an operator, or a space.当您读取第一个字符时,您将 go 写入变量并决定它是什么:在此示例中,它可以是数字、运算符或空格。 You jump to a special state (which should also be stored somewhere).你跳到一个特殊的 state (它也应该存储在某个地方)。 Then you read the next char in the expression, and now you have the history (the current state and the previously read chars).然后您读取表达式中的下一个字符,现在您有了历史记录(当前的 state 和先前读取的字符)。 Here you can decide if the token is ended or not, and depending on that you can jump to another state.在这里,您可以决定令牌是否结束,并根据此情况跳转到另一个 state。

For example, you read "1" .例如,您阅读"1" You know, that, for instance, it could be a number of any length.你知道,例如,它可以是任意长度的数字。 You persist "1" in a variable and read the next char.您将"1"保留在变量中并读取下一个字符。 If there's another digit, you could be sure that there's still the same number.如果有另一个数字,您可以确定仍然存在相同的数字。 If there's a space, or another operator, you push the "1" from the first step to a list of tokens or wherever, clear the string, and persist the next character in it.如果有空格或其他运算符,则将第一步中的"1"推送到标记列表或任何位置,清除字符串,并将下一个字符保留在其中。

Quite often it could be implemented using a switch-case operator.很多时候,它可以使用switch-case运算符来实现。

I found answers on my issue ( code below).我找到了关于我的问题的答案(下面的代码)。 It's very easier way to solve this problem.这是解决这个问题的非常简单的方法。

List<String>     tokenList = Arrays.asList(infix.split(
        "((?<=\\^)|(?=\\^))|((?<=\\s[-+]\\s)" +
        "|(?=\\s[-+]\\s))|((?<=[()])|(?=[()]))" +
        "|((?<=[*/])|(?=[*/]))" +
        "|((?<=\\s[*/]\\s))"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM