使用Java堆栈括号创建在缀表达式

问题描述 投票:0回答:1

我需要写一个Java程序,从标准输入一个有效的右括号的中缀表达式(RPIE)采用并输出等效满括号中缀表达式(FPIE)。例如,如果输入是:A + 20)/ B-C)53.4-d))),输出应该是((A + 20)/((B-C)(53.4-d)))。

我试图实现如下但未达到解决方案。谁能帮我?

import java.util.Scanner;
import java.util.Stack;

public class ParenthesisCreator {

    static private String expression;
    private Stack<Character> stack = new Stack<Character>();

    public ParenthesisCreator(String input) {
        expression = input;
    }

    public String rtParenthesisInfixToFullParenthesis() {
        String postfixString = "";

        for (int index = 0; index < expression.length(); ++index) {
            char value = expression.charAt(index);
            if (value == ')') {
                stack.push(')'); 
                stack.push('(');
                Character oper = stack.peek();
                while (!stack.isEmpty()) {
                    stack.pop();
                    postfixString += oper.charValue();
                    if (!stack.isEmpty())                 
                        oper = stack.peek(); 
                }
            } else {
                postfixString += value;
            }
        }

        return postfixString;
    }


    public static void main(String[] args) {
        System.out.println("Type an expression written in right parenthesized infix: ");
        Scanner input = new Scanner(System.in);
        String expression = input.next();
        // Input: a+20)/b-c)*53.4-d)))
        // Desired output is: ((a+20)/((b-c)*(53.4-d)))
        ParenthesisCreator convert = new ParenthesisCreator(expression);
        System.out.println("This expression writtien in full parenthesized is: \n" + convert.rtParenthesisInfixToFullParenthesis());
    }

}
java
1个回答
0
投票
public final class ParenthesisCreator implements Function<String, String> {
    private final IntPredicate isOperator;

    public ParenthesisCreator() {
        this(ch -> ch == '/' || ch == '*' || ch == '+' || ch == '-');
    }

    public ParenthesisCreator(IntPredicate isOperator) {
        this.isOperator = isOperator;
    }

    @Override
    public String apply(String expr) {
        Deque<String> stack = new LinkedList<>();
        StringBuilder buf = null;

        for (int i = 0; i < expr.length(); i++) {
            char ch = expr.charAt(i);

            if (ch == ')') {
                if (buf != null) {
                    stack.push(buf.insert(0, '(').append(')').toString());
                    buf = null;
                } else if (stack.size() >= 2) {
                    String two = stack.pop();
                    String one = stack.pop();
                    stack.push('(' + one + two + ')');
                } else
                    throw new IllegalArgumentException();
            } else if (isOperator.test(ch) && buf == null && !stack.isEmpty())
                stack.push(stack.pop() + ch);
            else
                (buf = buf == null ? new StringBuilder() : buf).append(ch);
        }

        return String.join("", stack);
    }
}

演示

System.out.println(new ParenthesisCreator().apply("a+20)/b-c)53.4-d)))"));    // ((a+20)/((b-c)(53.4-d)))
© www.soinside.com 2019 - 2024. All rights reserved.