2010-11-18 32 views
0

我正在做一個家庭作業任務,要求我創建一個計算器,將給定的表達式從中綴改爲postfix,然後進行評估。我必須使用堆棧,但可以選擇任何我希望的堆棧實現,只要我不使用JCF中的java.util.Stack即可。我選擇了一個基於引用的堆棧。Stack計算器:由於鑄造問題而無法評估後綴表達式

我遇到的問題是在我的evaluatePostfix方法。爲了評估表達式,我必須將我的操作數變量作爲整數來投射,但eclipse似乎並不那麼喜歡。我不斷收到「java.lang.Character不能轉換爲java.lang.Integer」錯誤。我不知道如何解決這個問題。有沒有人有任何見解?

這裏是我的代碼:

public class InfixToPostfixAndEvaluateCalculator { 

    private String infix; 
    private String postfix; 
    private int result; 

    public InfixToPostfixAndEvaluateCalculator() { 
    infix=null; 
    postfix=null; 
    result=0; 
    } 

    public InfixToPostfixAndEvaluateCalculator(String infix) { 
    this.infix=infix; 
    postfix=null; 
    result=0; 
    } 

    public String getInfix() { 
    return infix; 
    } 
    public String getPostfix() { 
    return postfix; 
    } 
    public int getresult() { 
    return result; 
    } 
    public void setInfix(String infix) { 
    this.infix=infix; 
    } 
    public void setPostfix(String postfix) { 
    this.postfix=postfix; 
    } 

    public String toString() { 
    return " Infix: "+infix+"\n Postfix: "+postfix+"\n Result: "+result+"\n"; 
    } 


    public String infixToPostfix() { //Carrano 2nd ed. p.354 
    //opStack is a stack of Character objects, such as '+','-','*','/', and ')' 
    StackInterface opStack=new StackReferenceBased(); 
    String postfixExp=""; //the expression to be built in this method 

    //for each character ch in the string infix 
    for (int i=0; i<infix.length(); i++) { 
     char ch=infix.charAt(i); 
     switch (ch) { 
     //if ch is an operator 
     case '+': case '-': case '*': case '/': 
      while ((!opStack.isEmpty()) 
      && (!opStack.peek().equals('(')) 
      && (precedence(ch) <= precedence((Character)opStack.peek()))){ 
      postfixExp = postfixExp + opStack.pop(); 
      } 
      opStack.push(ch); 
      break; 
     case '(': //add to stack 
      opStack.push(ch); 
      break; 
     case ')': //start popping things off the stack until you find opening parenthesis, use peak 
     while (!((Character)opStack.peek()).equals('(')){ 
      postfixExp = postfixExp + opStack.pop(); 

      }//end while 
      opStack.pop(); 
      break; 
     default: //ch is an operand 
      postfixExp = postfixExp + ch; 
      break; 
     }//end of switch 
    }//end of for 
    System.out.println("End of for loop."); 
    //append to postfixExp the operators remaining in the stack 
    while (! opStack.isEmpty()) { 
     postfixExp=postfixExp+((Character) opStack.pop()).charValue(); 
    }//end of while 

    postfix=postfixExp; //update the instance variable 
    return postfixExp; 
    }//end of infixToPostfix() 

    //little helper function to determine precedence value of an operator 
    // *,/ have value of, say 20 
    // +,- have value of, say 10 
    private int precedence(char ch) { 
    int prec = 20; 
    int prec2 = 10; 
    if (ch == '*' || ch == '/'){ 
    return prec; 
    } 
    if (ch == '+' || ch == '-'){ 
    return prec2; 
    } 
    return -1; 
    } 


    public int evaluatePostfix() { //Carrano 2nd ed. pp.350-351 
    //valueStack is a stack of Integer objects: 
    StackInterface valueStack=new StackReferenceBased(); 
    //variables for the operands: 
    int operand1, operand2; 
    //for each character ch in the string postfix 
    for (int i=0; i<postfix.length(); i++) { 
     char ch=postfix.charAt(i); 
     switch (ch) { 
     //if ch is an operator 
     case '+': 
      operand2 = (Integer)valueStack.pop(); 
      operand1 = (Integer)valueStack.pop(); 
      result = operand1 + operand2; 
      valueStack.push(result); 
      break; 
     case '-': 
      operand2 = (Integer)valueStack.pop(); 
      operand1 = (Integer)valueStack.pop(); 
      result = operand1 - operand2; 
      valueStack.push(result); 
      break; 
     case '*': 
      operand2 = (Integer)valueStack.pop(); 
      operand1 = (Integer)valueStack.pop(); 
      result = operand1 * operand2; 
      valueStack.push(result); 
      break; 
     case '/': 
      operand2 = (Integer)valueStack.pop(); 
      operand1 = (Integer)valueStack.pop(); 
      result = operand1/operand2; 
      valueStack.push(result); 
      break; 
     default: //ch is an operand 
      valueStack.push(ch); 
      break; 
     }//end of switch 
    }//end of for 
    //at the end, the value of the expression will be on the top of the stack 
    result=((Integer) valueStack.pop()).intValue(); 
    return result; 
    }//end of evaluatePostfix() 

} // end StackTest 
+1

哪一行是錯誤? – irrelephant 2010-11-18 00:43:56

+0

發佈異常跟蹤,這樣我們就不用猜測是什麼方法拋出它了。 – 2010-11-18 00:45:58

+0

哦,對不起!它在evaluatePostfix()正確的第一種情況下:operand2 =(Integer)valueStack.pop(); – Bell 2010-11-18 17:35:26

回答

1

是的,你可以不投人品整數。

要做到這一點,您可以使用,

Integer.parseInt(String.valueOf(valueStack.pop())); 

parseInt函數不走字作爲參數,因此,你必須先轉換成String,然後轉換爲整數。

+0

這工作!謝謝。我將來也會使用它。 =) – Bell 2010-11-18 17:33:35