2011-09-16 12 views
1

私はうんざりしています。私はこのコードを私が欲しいやり方で動作させるのにはとても近く、私はそれを理解できません。私はexの方程式を解くことを試みています。 3 2 +、これは5です。私が例えば "3 2 +"をmainmethodに置くとうまくいきますが、 "3 2 + 2 *"(10に等しい)のような3桁目を入力するとすぐに、配列outofboundserrorをnumber2 = s.pop()に戻します。以下のコードで説明します。どんな助けでも大歓迎です。スタックを使用したPostfixの評価。

HERESに接尾meethod:

public int PostfixEvaluate(String e){ 
     int number1; 
     int number2; 
     int result=0; 

     String[] tokens = e.split(" "); 

      for(int j = 0; j < tokens.length; j++){ 
       String token = tokens[j]; 
      if (!"+".equals(token) && !"*".equals(token) && !"-".equals(token) && !"/".equals(token)) { 
       s.push(Integer.parseInt(token)); 

     } else { 
       String Operator = tokens[j]; 
       number1 = s.pop(); 
       number2 = s.pop(); 
       if (Operator.equals("/")){ 
        result = number1/number2;} 
       else if(Operator.equals("*")){ 
        result = number1 * number2;} 
       else if(Operator.equals("+")){ 
        result = number1 + number2;} 
       else if(Operator.equals("-")){ 
        result = number1 - number2;} 
       else System.out.println("Illeagal symbol"); 
      } 
       s.push(result); 

        s.pop(); 
       } 


     //s.pop(); 
     System.out.println("Postfix Evauation = " + result); 

      return result; 
} 

public static void main(String[] args) { 
    Stacked st = new Stacked(100); 
    //String y = new String("((z * j)/(b * 8) ^2"); 
    String x = new String("2 2 2 * +"); 
    TestingClass clas = new TestingClass(st); 

    //clas.test(y); 
    clas.PostfixEvaluate(x); 

    } 

}

+0

「3 2 + 2 *」 - これは10 –

答えて

1

あなたは押した直後にポップされていますか?

s.push(result); 
s.pop(); 
+0

となりました。私はfor(ループ)の外側のelse {}とpop()の中で(結果)をプッシュしなければなりませんでした。 – TMan

+1

ええ、すぐにどこかに置いたデータを削除することはほとんどありません;) –

+0

これはどのように - "3 2 + 2 *" - 10に等しいです。 –

1

この解決策には別の論理エラーがあります。あなたは何をする必要があります:あなたは2/3にそれを評価しますので、あなたが32/を持っていた場合

number2 = s.pop(); 
number1 = s.pop(); 

あなたのソリューションが動作しません。

3
/** 
* Evaluate postfix arithmetic expression 
* 
* @example "1 12 23 + * 4 5/-" => 34.2 
* @author Yong Su 
*/ 
import java.util.Stack; 

class PostfixEvaluation { 

    public static void main(String[] args) { 
     String postfix = "1 12 23 + * 4 5/-"; 
     Double value = evaluate(postfix); 
     System.out.println(value); 
    } 

    /** 
    * Evaluate postfix expression 
    * 
    * @param postfix The postfix expression 
    */ 
    public static Double evaluate(String postfix) { 
     // Use a stack to track all the numbers and temporary results 
     Stack<Double> s = new Stack<Double>(); 

     // Convert expression to char array 
     char[] chars = postfix.toCharArray(); 

     // Cache the length of expression 
     int N = chars.length; 

     for (int i = 0; i < N; i++) { 
      char ch = chars[i]; 

      if (isOperator(ch)) { 
       // Operator, simply pop out two numbers from stack and perfom operation 
       // Notice the order of operands 
       switch (ch) { 
        case '+': s.push(s.pop() + s.pop());  break; 
        case '*': s.push(s.pop() * s.pop());  break; 
        case '-': s.push(-s.pop() + s.pop()); break; 
        case '/': s.push(1/s.pop() * s.pop()); break; 
       } 
      } else if(Character.isDigit(ch)) { 
       // Number, push to the stack 
       s.push(0.0); 
       while (Character.isDigit(chars[i])) 
        s.push(10.0 * s.pop() + (chars[i++] - '0')); 
      } 
     } 

     // The final result should be located in the bottom of stack 
     // Otherwise return 0.0 
     if (!s.isEmpty()) 
      return s.pop(); 
     else 
      return 0.0; 
    } 

    /** 
    * Check if the character is an operator 
    */ 
    private static boolean isOperator(char ch) { 
     return ch == '*' || ch == '/' || ch == '+' || ch == '-'; 
    } 
} 
3

number1の割り当てはnumber2の割り当ての後にする必要があります。 s.pop()は、上にある番号を削除して返します。

number2 = s.pop(); 
number1 = s.pop(); 
関連する問題