2010-11-18 25 views
0

Je travaille sur un devoir qui me demande de créer une calculatrice qui change l'expression qui lui est donnée de l'infixe au suffixe pour ensuite évaluer. Je dois le faire en utilisant des piles mais je peux choisir n'importe quelle implémentation de pile tant que je n'utilise pas java.util.Stack du JCF. J'ai choisi une pile référencée.Calculatrice de pile: Problème d'évaluation de l'expression du suffixe en raison d'un problème d'édition

Le problème que j'ai est dans ma méthode evaluatePostfix. Afin d'évaluer l'expression, j'ai dû convertir mes variables opérandes en nombres entiers, mais éclipse ne semble pas aimer ça. Je reçois toujours une erreur "java.lang.Character ne peut pas être convertie en java.lang.Integer". Je ne suis pas sûr de savoir comment résoudre ce problème. Quelqu'un at-il une idée?

Voici mon code:

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

Sur quelle ligne est l'erreur? – irrelephant

+0

Post trace exception, de cette façon, nous n'avons pas à deviner quelle méthode le lance. –

+0

Oh, je suis désolé! C'est dans evaluatePostfix() juste dans le premier cas: operand2 = (Integer) valueStack.pop(); – Bell

Répondre

1

Oui, vous ne pouvez pas jeter à caractère entier.

Pour ce faire, vous pouvez utiliser,

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

parseInt ne prend pas de caractère comme argument, vous devez convertir d'abord en chaîne puis à Integer.

+0

Cela a fonctionné! Je vous remercie. Je vais utiliser cela dans le futur aussi. =) – Bell