我正在尝试基于 Shutting-yard 算法构建自己的表达式评估器。我很难将String
对象添加到我拥有的堆栈中。即使满足条件,我的代码每次都会跳过这些行。有什么建议么?我operatorStack
声明为:
Stack operatorStack = new Stack();
我认为这与我的 if 语句有关。我已经使用 Eclipse 调试器对其进行了测试,当currentChar
变量显示为"("
它仍然跳过推送到堆栈时。
这是有问题的摘录:
int count = 0;
String currentChar=new String();
//While loop to test for the conditions stated in the Shunting-yard algorithm.
while (count<=temp.length()) {
currentChar = temp.substring(count, count+1);
if(currentChar == "(")
operatorStack.push(currentChar);
if(expressionEval(currentChar) instanceof Integer)
outputQueue.offer((Integer)expressionEval(currentChar));
if(currentChar == "+" ||
currentChar == "-" ||
currentChar == "<" ||
currentChar == "?") {
while(operatorStack.peek() == "+" ||
operatorStack.peek() == "-" ||
operatorStack.peek() == "<" ||
operatorStack.peek() == "?") {
outputQueue.offer((Integer)operatorStack.peek());
operatorStack.pop();
}
operatorStack.push(currentChar);
}