后缀问题的缀(我的问题在哪里?)-紧急情况

时间:2019-12-20 04:06:06

标签: java infix-operator

我在代码中遇到问题,以将中缀转换为后缀 这是我的代码:代码给了我额外的“”(空格)。 仅显示在后缀语句的输出行中。每个值都是一个单独的空格。

问题仅在最后一次操作中

它只有操作数,而我可以得到类似的功能

  

20

     

((10 /(2-6 + 9))*(5-14)* 3

第一个字母是数字,第二个字母是条目

class Hot {    
    public static String infixToPostfix(String str) {
        LinkedListStack<String> stack = new LinkedListStack<>();
        String[] st = str.split("");
        String result = "";
        for (String s : st) {
            if (operator(s)) {
                if (")".equals(s)) {
                    while (!stack.isEmpty() && !"(".equals(stack.getTop())) {
                        result += stack.pop();
                    }
                    if (!stack.isEmpty()) {
                        stack.pop();
                    }
                } else {
                    if (!stack.isEmpty() && !isLowerPrecedence(s, stack.getTop())) {
                        stack.push(s);
                    } else {
                        while (!stack.isEmpty() && isLowerPrecedence(s, stack.getTop())) {
                            String top = stack.pop();
                            if (!"(".equals(top)) {
                                result += top;
                            }
                        }
                        stack.push(s);
                    }
                }
            } else {
                result += s;
            }
        }
        while (!stack.isEmpty()) {
            result += stack.pop();
        }

        return result;
    }

    private static boolean isLowerPrecedence(String s, String s1) {
        switch (s) {
        case "+":
            return !("+".equals(s1) || "(".equals(s1));
        case "-":
            return !("-".equals(s1) || "(".equals(s1));

        case "*":
            return "/".equals(s1) || "^".equals(s1) || "(".equals(s1);
        case "/":
            return "*".equals(s1) || "^".equals(s1) || "(".equals(s1);

        case "^":
            return "(".equals(s1);

        case "(":
            return false;

        default:
            return false;
        }

    }

    private static boolean operator(String s) {
        return "+".equals(s) || "-".equals(s) || "*".equals(s) || "/".equals(s) || "^".equals(s) || "(".equals(s) ||
                ")".equals(s);
    }

    public static void main(String[] args) {
       // InfixToPostfix itp = new InfixToPostfix();
       // System.out.println("The Postfix expression for A*B-(C+D)+E is: " +infixToPostfix("A*B-(C+D)+E"));
        System.out.println("The Postfix expression for 1+2*4/5-7+3/6 is: " + infixToPostfix("( ( 10 / ( 2 - 6 + 9 ) ) * ( 5 - 14 ) * 3"));
      //  System.out.println("The Postfix expression for a+(b*c)/d is: " + infixToPostfix("a+(b*c)/d"));
    }


    }
    class LinkedListStack<E> {

    private Node<E> head;

    private static class Node<E> {
        E item;
        Node<E> next;

        public Node(E item, Node<E> next) {
            this.item = item;
            this.next = next;
        }
    }

    public void push(E item) {
        System.out.println("push: " + item);
        Node<E> newNode = new Node<>(item, null);
        newNode.next = head;
        head = newNode;
    }

    public E pop() {
        if (isEmpty()) {
            System.out.println("stack is Empty -> empty stack exception");
            return null;
        }
        System.out.println("pop: " + head.item);
        E data = head.item;
        head = head.next;
        return data;
    }

    public boolean isEmpty() {
        return head == null;
    }

    public E getTop() {
        return head.item;
    }
}

结果应为:

  

10 2 6-9 + / 5 14-* 3 *

但是我明白了:

  

10 2 6-9 + / 5 14-3 **

0 个答案:

没有答案