Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 链接堆栈中中缀到后缀的括号检查_Java_Stack - Fatal编程技术网

Java 链接堆栈中中缀到后缀的括号检查

Java 链接堆栈中中缀到后缀的括号检查,java,stack,Java,Stack,我有四节课 其中一个包含我的linkedstack设置 一种是infixtopostfix,用于优先级排序和转换 用于匹配的括号 评估后缀 我已经在这里设置了几乎所有内容,但不管我怎么说,它仍然返回false。 另一方面,我在上的成绩相当!stackMatch.pop().equals(c)不工作,因为它是带有“!”的对象类型这是个问题 我的程序简单明了: LinkedStack.java public class LinkedStack implements StackInterface {

我有四节课

  • 其中一个包含我的linkedstack设置
  • 一种是infixtopostfix,用于优先级排序和转换
  • 用于匹配的括号
  • 评估后缀
  • 我已经在这里设置了几乎所有内容,但不管我怎么说,它仍然返回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
    
    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
                    }
                }
            }
        }
    
    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();
    }
    }
    
    InfixToPostfix.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
    
    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
                    }
                }
            }
        }
    
    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();
    }
    }
    
    import java.util.*;
    公共类InfixToPostfix{
    圆括号p=新圆括号();
    LinkedStack堆栈=新LinkedStack();
    String token=”“;//字符串的每个标记
    String output=“;//包含后缀表达式的字符串
    Character topOfStackObject=null;//堆栈的顶部对象,转换为Character对象
    char charValueOfTopOfStack='';//字符对象的基本值
    /**
    *将中缀表达式转换为后缀。如果表达式无效,则引发异常。
    *@param是中缀表达式
    *@以字符串形式返回后缀表达式
    *提示:StringTokenizer对于这种迭代方式非常有用
    */
    //公共字符串convertToPostfix(字符串s)引发异常{
    //}
    专用布尔等标量(字符c){
    
    返回((c>='0'&&c='a'&&c问题可能是,您正在尝试测试匹配的
    出现时
    当前位于堆栈顶部)
    ,但在
    中,c
    是默认字符,
    ,因此您测试
    是否位于堆栈顶部,而不是