2015-04-05 25 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方法中的字符串。