为什么我得到一个空堆栈异常?

时间:2014-03-29 20:52:34

标签: java stack

请在这方面提供帮助,错误显示为空堆栈异常。

代码:

import java.util.Stack;

public class Stacks {


    public static void main(String[] arg)
    {

        String s[] = {"5 + ) * ( 2",
//                              " 2 + ( - 3 * 5 ) ",
                      "(( 2 + 3 ) * 5 ) * 8 ",
                      "5 * 10 + ( 15 - 20 ) )  - 25",
                      " 5 + ( 5 *  10 + ( 15 - 20 )  - 25 ) * 9"
                     };

        for (int i = 0; i < s.length; i++)
        {

            Arithmetic a = new Arithmetic(s[i]);

            if (a.isBalance())
            {
                System.out.println("Expression " + s[i] + " is balanced\n");
                a.postfixExpression();
                System.out.println("The post fixed expression is " + a.getPostfix());
                a.evaluateRPN();

            }
            else
                System.out.println("Expression is not balanced\n");
        }
    }

    private static class Arithmetic {
        String str = "";

        Stack<Character> stack = new Stack<>(); 
        String postFix = "";
        public Arithmetic(String str) 
        {
           this.str = str;
           this.postFix = postFix;
        }   
    private boolean isBalance() 
    {
         Stack<Character> stack = new Stack<>();
         for(int i = 0; i < str.length(); i++)
         {

         if(str.charAt(i) == '(' )
             stack.push(str.charAt(i));
         else if(str.charAt(i) == ')')
         {
             if(stack.isEmpty() || stack.pop() != '(')
                 return false;       
         }  

         }
         return stack.isEmpty();


    }

        private void evaluateRPN() {

        }

        private String getPostfix() {
        return postFix;
        }

        @SuppressWarnings("empty-statement")
        private void postfixExpression() {
         Stack<Character> stack = new Stack<>();
         for(int i = 0; i < str.length(); i++)
        {
           if(Character.isDigit(str.charAt(i)))
               postFix += " " + str.charAt(i);
           else if(str.charAt(i) == '+' || str.charAt(i) == '-' ||
                   str.charAt(i) == '*' || str.charAt(i) == '/' ||
                   str.charAt(i) == '%' || str.charAt(i) == '(' ||
                   str.charAt(i) == ')' )
           {
           do{
                   stack.push(str.charAt(i));


           }

             while(stack.isEmpty());
           } 



           if(str.charAt(i) == '(' || str.charAt(i) == ')')
           {
               if(str.charAt(i) == '(')
                   stack.push(str.charAt(i));
               else if(str.charAt(i) == ')')
               {
                   do
                   {
                       do{
                            postFix += stack.pop();
                       }while(stack.pop() != ')');
                   }while(!stack.empty());

               }
           }

            if(str.charAt(i) == '+' || str.charAt(i) == '-' ||
                   str.charAt(i) == '*' || str.charAt(i) == '/' )
           {
               if(str.charAt(i) == '+' || str.charAt(i) == '-')
               {
                do{
                   postFix += stack.pop();
                   }while ((stack.pop() != '(') || !stack.empty());
                   postFix += str.charAt(i);
               }
               if(str.charAt(i) == '*' || str.charAt(i) == '/')                  
               {
                   if(stack.pop() == '+' || stack.pop() == '-')
                   {
                       stack.push(str.charAt(i));
                   }
               }
            }
        }
         do{
             postFix += stack.pop();
            }while(!stack.empty());

    }

    }

}

1 个答案:

答案 0 :(得分:1)

当您测试使用peek功能时,否则您将删除该项目。当你这样做时:

if(stack.pop() == '+' || stack.pop() == '-')

并且您的堆栈包含[*]

当您致电stack.pop()时,您会删除*,之后您的筹码将为空,您将在第二次测试(stack.pop() == '-')中获得异常。

您需要验证代码并更改逻辑。