在java中将中缀字符串拆分为字符串数组
我正在开发一个小型科学计算器,它可以使用中缀到后缀算法。我的输入是中缀字符串。。而我的在java中将中缀字符串拆分为字符串数组,java,string,postfix-operator,infix-operator,Java,String,Postfix Operator,Infix Operator,我正在开发一个小型科学计算器,它可以使用中缀到后缀算法。我的输入是中缀字符串。。而我的中缀到后缀转换逻辑需要数组的字符串。那么如何拆分中缀字符串,如下所示: 到字符串数组,其中每个操作数和运算符都是数组元素。这样地 等等 请注意,字符串中没有空格,因此无法根据regex进行拆分。“请查看的答案 这应该可以做到: Pattern p = Pattern.compile("(?:(\\d+)|([+-*/\\(\\)]))"); Matcher m = p.matcher("100+(0.03*
中缀
到后缀
转换逻辑需要数组
的字符串
。那么如何拆分中缀字符串,如下所示:
到字符串数组,其中每个操作数和运算符都是数组元素。这样地
等等
请注意,字符串中没有空格,因此无法根据regex进行拆分。“请查看的答案 这应该可以做到:
Pattern p = Pattern.compile("(?:(\\d+)|([+-*/\\(\\)]))");
Matcher m = p.matcher("100+(0.03*55)/45-(25+55)");
List<String> tokens = new LinkedList<String>();
while(m.find())
{
String token = m.group( 0 ); //group 0 is always the entire match
tokens.add(token);
}
Pattern p=Pattern.compile((?:(\\d+))|([+-*/\\(\\)]);
匹配器m=p.Matcher(“100+(0.03*55)/45-(25+55)”;
列表标记=新的LinkedList();
while(m.find())
{
String token=m.group(0);//组0始终是整个匹配项
令牌。添加(令牌);
}
您可以使用正则表达式解析存储在字符串中的数学表达式。
请检查我的实验@显然每个字符都是分开的标记,除了可能带有点的连续数字。因此,一个简单的解决方案是迭代字符串,然后当您看到一个数字前面有另一个数字(或十进制分隔符,一个点)时,将该字符添加到前一个标记中,否则将其添加到新标记中 代码如下:
public static List<String> getTokens(String inputString) {
List<String> tokens = new ArrayList<String>();
// Add the first character to a new token. We make the assumption
// that the string is not empty.
tokens.add(Character.toString(inputString.charAt(0)));
// Now iterate over the rest of the characters from the input string.
for (int i = 1; i < inputString.length(); i++) {
char ch = inputString.charAt(i); // Store the current character.
char lch = inputString.charAt(i - 1); // Store the last character.
// We're checking if the last character is either a digit or the
// dot, AND if the current character is either a digit or a dot.
if ((Character.isDigit(ch) || ch == '.') && (Character.isDigit(lch) || lch == '.')) {
// If so, add the current character to the last token.
int lastIndex = (tokens.size() - 1);
tokens.set(lastIndex, tokens.get(lastIndex) + ch);
}
else {
// Otherwise, add the current character to a new token.
tokens.add(Character.toString(ch));
}
}
return tokens;
}
公共静态列表getTokens(String inputString){
List tokens=new ArrayList();
//将第一个字符添加到新标记中。我们假设
//字符串不是空的。
add(Character.toString(inputString.charAt(0));
//现在迭代输入字符串中的其余字符。
对于(int i=1;i
请注意,此方法比大多数正则表达式方法快以下是我将使用的算法: 从一个空字符串数组和一个空字符串缓冲区开始
- 从字符0移动到字符n
- 对于当前字符确定类型(数字/句点、打开参数、关闭参数、数学运算符)
- 如果当前字符类型与最后一个字符类型相同
- 将当前字符添加到缓冲区
- 若不相同,则将缓冲区放入字符串数组中,并启动一个新的缓冲区
public static void main(String[] args) {
String input = "100+(0.03*55)/45-(25+55)";
String test[] = input.split("((?<=[\\+\\-\\*\\/\\(\\)\\{\\}\\[\\]])|(?=[\\+\\-\\*\\/\\(\\)\\{\\}\\[\\]]))");
System.out.println(Arrays.toString(test));
}
publicstaticvoidmain(字符串[]args){
字符串输入=“100+(0.03*55)/45-(25+55)”;
字符串测试[]=input.split((?)和regex(?:(\\d+))([+-*/\\(\\])
最外层的组未被捕获,因此它从组0
开始。可能会提高性能。输出不正确,[100,+,(0.03,*,55),/,45,-,(25,+,55)]你能解释一下它是如何工作的吗?或者你能给我一个链接,让我研究这个分割函数是如何工作的吗?我用过简单正则表达式的分割函数,但不是那样,@AfzalAshraf更新了
expString.split("(?<=[-+*/\\(\\)])|(?=[-+*/\\(\\)])");
String str = "100+(0.03*55)/45-(25+55)";
String[] outs = str.split("(?<=[-+*/\\(\\)])|(?=[-+*/\\(\\)])");
for (String element : outs)
{
System.out.println(element);
}
100
+
(
0.03
*
55
)
/
45
-
(
25
+
55
)
public static List<String> getTokens(String inputString) {
List<String> tokens = new ArrayList<String>();
// Add the first character to a new token. We make the assumption
// that the string is not empty.
tokens.add(Character.toString(inputString.charAt(0)));
// Now iterate over the rest of the characters from the input string.
for (int i = 1; i < inputString.length(); i++) {
char ch = inputString.charAt(i); // Store the current character.
char lch = inputString.charAt(i - 1); // Store the last character.
// We're checking if the last character is either a digit or the
// dot, AND if the current character is either a digit or a dot.
if ((Character.isDigit(ch) || ch == '.') && (Character.isDigit(lch) || lch == '.')) {
// If so, add the current character to the last token.
int lastIndex = (tokens.size() - 1);
tokens.set(lastIndex, tokens.get(lastIndex) + ch);
}
else {
// Otherwise, add the current character to a new token.
tokens.add(Character.toString(ch));
}
}
return tokens;
}
public static void main(String[] args) {
String input = "100+(0.03*55)/45-(25+55)";
String test[] = input.split("((?<=[\\+\\-\\*\\/\\(\\)\\{\\}\\[\\]])|(?=[\\+\\-\\*\\/\\(\\)\\{\\}\\[\\]]))");
System.out.println(Arrays.toString(test));
}