我很困惑为什么它没有拆分字符串?我的数组字符串exp不包含任何东西,当我调试它是拆分错误?我想要做的是分割一个非常简单的表达式,如1 + 2 + 3,然后解析值,做一个计算器。
修改 嗨,为什么我分裂每个字符是因为我正在做一个计算器,并已阅读有关将中缀转换为后缀的内容,所以我需要拆分字符串然后循环遍历每个字符串并执行检查,如下所示,但是当我调试它时显示exp []为空
For each token in turn in the input infix expression:
* If the token is an operand, append it to the postfix output.
* If the token is an operator A then:
o While there is an operator B of higher or equal precidence than A at the top of the stack, pop B off the stack and append it to the output.
o Push A onto the stack.
* If the token is an opening bracket, then push it onto the stack.
* If the token is a closing bracket:
o Pop operators off the stack and append them to the output, until the operator at the top of the stack is a opening bracket.
o Pop the opening bracket off the stack.
When all the tokens have been read:
* While there are still operator tokens in the stack:
o Pop the operator on the top of the stack, and append it to the output.
// the main class
public class Main {
public static void main(String[] args) {
calcExpChecker calc = new calcExpChecker("1+2+3+4");
calc.legitExp();
calc.displayPostfix();
}
}
//the class
package javaapplication4;
import java.util.*;
public class calcExpChecker {
private String originalExp; // the orginal display passed
private boolean isItLegitExp; // the whole expression is it legit
private boolean isItBlank; // is the display blank?
private StringBuilder expression = new StringBuilder(50);
private Stack stack = new Stack();//stack for making a postfix string
calcExpChecker(String original)
{
originalExp = original;
}
//check for blank expression
public void isitBlank()
{
if(originalExp.equals(""))
{
isItBlank = true;
}
else
{
isItBlank = false;
}
}
//check for extra operators
public void legitExp()
{
String[] exp = originalExp.split(".");
for(int i = 0 ; i < exp.length ; i++)
{
if(exp[i].matches("[0-9]"))
{
expression.append(exp[i]);
}
else if(exp[i].matches("[+]"))
{
if(stack.empty())
{
stack.push(exp[i]);
}
else
{
while(stack.peek().equals("+"))
{
expression.append(stack.pop());
}
stack.push(exp[i]);
}
}
if (!stack.empty())
{
expression.append(stack.pop());
}
}
}
public void displayPostfix()
{
System.out.print(expression.toString());
}
}
答案 0 :(得分:3)
如果你让每个角色都成为分隔符,它们之间有什么关系?没有
如, 1 + 2 + 3 + 4 是一个分隔符?是的,好的,捕获它与下一个分隔符之间的所有内容。下一个分隔符? +。没有捕获。下一个分隔符? 2.等等
答案 1 :(得分:2)
您希望拆分每个角色,而是使用string.split("")
。
for (String part : string.split("")) {
// ...
}
或者更好,只需迭代string.toCharArray()
返回的每个字符。
for (char c : string.toCharArray()) {
// ...
}
使用字符,您可以使用优于大if/else
块的switch
statement。
答案 2 :(得分:0)
为什么你需要拆分每个角色&amp;而不是在String中使用 foreach 字符。这样你就不必像exp [i]那样引用。
无论如何,您可以使用“”而不是“。”进行拆分。
答案 3 :(得分:-1)
自白:
好吧我猜我的答案很糟糕,因为Java和C#之间存在细微差别。不过,也许它会帮助有相同问题的人,但在C#中!
顺便说一句,在C#中,如果你传入一个RegEx“。”你没有得到一个空数组,而是得到一个空格数组(“”),一个字符串中的每个字符边界。
修改强>
您可以将正则表达式传递给split()
函数:
string expressions = "10+20*4/2";
/* this will separate the string into an array of numbers and the operators;
the numbers will be together rather than split into individual characters
as "" or "." would do;
this should make processing the expression easier
gives you: {"10", "+", "20", "*", "4", "/", "2"} */
foreach (string exp in expressions.split(@"(\u002A)|(\u002B)|(\u002D)|(\u002F)"))
{
//process each number or operator from the array in this loop
}
<强>原始强>
String[] exp = originalExp.split(".");
您应该从split()
的返回值(原始的非拆分字符串)中获取至少一个字符串。如果字符串数组为空,则原始字符串可能为空。