圆括号在链接堆栈中检查中缀到后缀

时间:2014-06-04 19:26:45

标签: java stack

我有四节课。

  1. 一个包含我的linkedstack设置
  2. 一个是用于优先级排序和转换的infixtopostfix
  3. 匹配的括号
  4. 评估后缀
  5. 我已经在这里设置了几乎所有东西,但无论如何我仍然会返回false。 另一方面,!stackMatch.pop().equals(c)上的我的等号不起作用,因为它是带有'!'的对象类型是一个问题。

    我的程序简单明了:

    LinkedStack.java

    public class LinkedStack implements StackInterface {
        private Node top;
    
        public LinkedStack() {
    
            top = null;
    
        }  // end default constructor
    
        public boolean isEmpty() {
    
            return top ==  null;
    
        }  // end isEmpty
    
    
        public void push(Object newItem) {
    
            Node n = new Node();
            n.setData(newItem);
            n.setNext(top);
            top = n;
    
        }  // end push
    
        public Object pop() throws Exception {
    
            if (!isEmpty()) {
                Node temp = top;
                top = top.getNext();
                return temp.getData();
            } else {
                throw new Exception("StackException on pop: stack empty");
            }  // end if
    
        }  // end pop
    
        public Object peek() throws Exception {
    
            if (!isEmpty()) {
                return top.getData();
            } else {
                throw new Exception("StackException on peek: stack empty");
            }  // end if
    
        }  // end peek
    
    }  // end LinkedStack
    

    InfixToPostfix.java

    import java.util.*;
    
    public class InfixToPostfix {
    
        Parenthesis p = new Parenthesis();
        LinkedStack stack = new LinkedStack();
        String token = "";  // each token of the string
        String output = ""; // the string holding the postfix expression
        Character topOfStackObject = null;  // the top object of the stack, converted to a Character Object
        char charValueOfTopOfStack = ' '; // the primitive value of the Character object
    
        /**
         * Convert an infix expression to postfix. If the expression is invalid, throws an exception.
         * @param s the infix expression
         * @return the postfix expression as a string
         * hint:  StringTokenizer is very useful to this iteratively
         */
        //public String convertToPostfix(String s) throws Exception {
    
    
         //}
    
    
    
        private boolean isOperand (char c){
            return ((c>= '0' && c <= '9') || (c >= 'a' && c<= 'z'));
        }
        public void precedence(char curOp, int val) throws Exception {
            while (!stack.isEmpty()) {
    
                char topOp = (Character) stack.pop();
                //     charValueOfTopOfStack = topOfStackObject.charValue();
    
    
                if (topOp == '(') {
                    stack.push(topOp);
                    break;
                }// it's an operator
                else {// precedence of new op
                    int prec2;
    
                    if (topOp == '+' || topOp == '-') {
                        prec2 = 1;
                    } else {
                        prec2 = 2;
                    }
    
                    if (prec2 < val) // if prec of new op less
                    { //    than prec of old
                        stack.push(topOp); // save newly-popped op
                        break;
                    } else // prec of new not less
                    {
                        output = output + topOp; // than prec of old
                    }
                }
            }
        }
    

    Parenthesis.java

    import java.util.*;
    public class Parenthesis{
    
        private LinkedStack stack = new LinkedStack();
        private Object openBrace;
        private String outputString;
    
    
        /**
         * Determine if the expression has matching parenthesis using a stack
         *
         * @param expr the expression to be evaluated
         * @return returns true if the expression has matching parenthesis
         */
        public boolean match(String expr) {
    
        LinkedStack stackMatch = new LinkedStack();
    
    
        for(int i=0; i < expr.length(); i++) { 
    
           char c = expr.charAt(i);
            if(c == '(')
                stackMatch.push(c); 
    
    
            else if(c == ')'){
                 if (stackMatch.isEmpty() || !stackMatch.pop().equals(c))
                 return false;
            }
        }
        return stackMatch.isEmpty();
    }
    }
    

    只是想给你所有这些,所以你可以帮助我。我已经编写了一些测试,我们只是在将它推到堆栈上的括号问题上苦苦挣扎,但是无法将它与右括号进行比较,因此它可以检查是否有足够的内容在检查时确保它不是空的。

1 个答案:

答案 0 :(得分:0)

问题可能是,当(到来时,您正在尝试测试匹配的)当前是否在堆栈顶部,但c是实际角色,{{1因此,您测试)是否位于堆栈顶部,而不是)