2015-04-05 58 views
0

我一直在试图找出我的代码的错误无济于事。除了评估后缀表达式之外,我应该为中缀翻译器编写一个中缀。我的代码运行,但不幸的是它不会返回正确的值。Postfix评估器不返回正确的结果

我有一个计算器GUI,只要等号被按下,就会调用下面显示的代码。计算器将一个由空格分隔的字符串作为参数传递。然后我在这个空格分隔的字符串上使用String Tokenizer并从那里开始工作。如果有帮助,我可以提供计算器GUI的代码。

我的问题在于计算器提供的答案。例如,如果我输入(5 + 2),计算器将返回2作为答案,如果再次按等号,则返回5作为答案。如果输入(5 * 2)+(3 * 9),则返回9作为答案,如果再次按等号,则返回5作为一个答案。我尝试了多次通过我的代码,但不幸的是我一直无法找到我的错误。任何帮助将不胜感激!

免责声明:我知道关于使用String Tokenizer的附注。我会用别的东西,但那是需求之一。我还没有执行任何错误检查或检查优先级,因为我想确保它能正常工作,假设输入正确并且先不过分复杂。另外,我知道我的代码不会正确地处理像(5 + 2)-1这样的东西,因为在1周围缺少括号。但是再次,它不能用于比这更简单的事情。 。一旦我能用简单的输入工作,我会担心这一点。最后,这确实是一项家庭作业,但请不要以为我完全是为了完成这件事。只是几个指针将不胜感激。

这里是我的代码:

public class ExpressionEvaluator { 

Stack<String> myStack = new Stack<>(); 
Queue<String> myQueue = new Queue<>(); 

String curToken; //Current token of my tokenized string. 
double temp1; //Place holder for first value of the calc section. 
double temp2; //Place holder for second value of the calc section. 

public String processInput(String s) { 

    StringTokenizer st = new StringTokenizer(s); 

    while (st.hasMoreTokens()) { 

     curToken = st.nextToken(); 

     if (openParenthesis(curToken)) { 
      myStack.push(curToken); 
     } 

     if (closeParenthesis(curToken)) { 
      do { 
       myQueue.enqueue(myStack.pop()); 
      } while (!openParenthesis(myStack.peek())); 
     } 

     if (isOperator(curToken)) { 
      while (!myStack.isEmpty() && !openParenthesis(myStack.peek())) { 
       myQueue.enqueue(myStack.pop()); 
      } 
      myStack.push(curToken); 
     } 
     if (isDouble(curToken)) { 
      myQueue.enqueue(curToken); 
     } 
    } 

    while (!myStack.isEmpty()) { 
     myQueue.enqueue(myStack.pop()); 
    } 

    while (!myQueue.isEmpty()) { 
     if (isDouble(myQueue.peek())) { 
      myStack.push(myQueue.dequeue()); 
     } 

     else if (isOperator(myQueue.peek())) { 
      temp1 = Double.parseDouble(myStack.pop()); 
      temp2 = Double.parseDouble(myStack.pop()); 
      myStack.push(Double.toString(calc(temp1, temp2))); 
     } 

     else { 
      myQueue.dequeue(); 
     } 
    } 
    return myStack.pop(); 
} 

//Private methods used to simplify/clarify some things. 


//Checks if input is an operator, returns true if it is 
private boolean isOperator(String str) { 
    if (str == "+") {return true;} 
    else if (str == "-") {return true;} 
    else if (str == "*") {return true;} 
    else if (str == "/") {return true;} 
    else if (str == "^") {return true;} 
    else {return false;} 
} 

//Checks if input is an open parenthesis "(", returns true if it is 

private boolean openParenthesis(String str) { 
    if (str == "(") {return true;} 
    else {return false;} 
} 

//Checks if input is a close parenthesis ")", returns true if it is 

private boolean closeParenthesis(String str) { 
    if (str == ")") {return true;} 
    else {return false;} 
} 

//Checks if input is a double, returns true if it is 
//I actually got this method from Stack Overflow, so thanks! 

private boolean isDouble(String str) { 
    try { 
     Double.parseDouble(str); 
     return true; 
    } catch (NumberFormatException e) { 
     return false; 
    } 
} 

//Method used to actually do the calculations. I have 
//a feeling this is where my problem is, but I can't 
//think of a way to fix it. 

private double calc(double a, double b) { 
    String op = myQueue.dequeue(); 

    if (op == "+") {return a+b;} 
    else if (op == "-") {return a-b;} 
    else if (op == "*") {return a*b;} 
    else if (op == "/") {return a/b;} 
    else if (op == "^") {return Math.pow(a, b);} 
    else {throw new UnknownElementException(null, "ERROR");} 
} 
} 

对不起,我奇怪的压痕。任何帮助将不胜感激!

回答

1

有一个名为的调度算法,该算法指定如何将中缀表示法转换为后缀表示法(也称为“反转波形表示法”)。那么你将不必担心运营商的优先权。

它使用队列和堆栈。基本上,当你遇到一个数字时,将它添加到队列中。当你遇到操作员时,你可以将它们推入堆栈。一旦Shunting-Yard Algorithm

逆波兰表达式,你可以很容易地评估它像这里描述:

你可以在这里找到详细的算法 Postfix evaluation algorithm

+0

谢谢,我会看看这个! – kevinivan05 2015-04-05 01:17:17

0

我终于想通了!我使用==而不是.equals()来比较我的isOperator,closeParenthesis和openParenthesis方法中的字符串。