使用java中的堆栈在中缀表达式中创建括号

我需要编写一个 Java 程序,从标准输入中获取有效的右括号中缀表达式 (RPIE) 并输出等效的全括号中缀表达式 (FPIE)。例如,如果输入是:a+20)/bc) 53.4-d))),则输出应该是 ((a+20)/((bc) (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());

    }


}


万千封印
浏览 180回答 2
2回答

慕桂英546537

public final class ParenthesisCreator implements Function<String, String> {&nbsp; &nbsp; private final IntPredicate isOperator;&nbsp; &nbsp; public ParenthesisCreator() {&nbsp; &nbsp; &nbsp; &nbsp; this(ch -> ch == '/' || ch == '*' || ch == '+' || ch == '-');&nbsp; &nbsp; }&nbsp; &nbsp; public ParenthesisCreator(IntPredicate isOperator) {&nbsp; &nbsp; &nbsp; &nbsp; this.isOperator = isOperator;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String apply(String expr) {&nbsp; &nbsp; &nbsp; &nbsp; Deque<String> stack = new LinkedList<>();&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder buf = null;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < expr.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char ch = expr.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ch == ')') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (buf != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stack.push(buf.insert(0, '(').append(')').toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (stack.size() >= 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String two = stack.pop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String one = stack.pop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stack.push('(' + one + two + ')');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (isOperator.test(ch) && buf == null && !stack.isEmpty())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stack.push(stack.pop() + ch);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (buf = buf == null ? new StringBuilder() : buf).append(ch);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return String.join("", stack);&nbsp; &nbsp; }}System.out.println(new ParenthesisCreator().apply("a+20)/b-c)53.4-d)))"));&nbsp; &nbsp; // ((a+20)/((b-c)(53.4-d)))

撒科打诨

public class FixExpressionParentheses {&nbsp; &nbsp; public String fixExpression(String expression) {&nbsp; &nbsp; &nbsp; &nbsp; String[] tokenArray = expression.split(" ");&nbsp; &nbsp; &nbsp; &nbsp; Stack<String> operators = new Stack<>();&nbsp; &nbsp; &nbsp; &nbsp; Stack<String> operands = new Stack<>();&nbsp; &nbsp; &nbsp; &nbsp; for (String token: tokenArray) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (token) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "+", "-", "*", "/", "sqrt" -> operators.push(token);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ")" -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String operator = operators.pop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String operandTwo = operands.pop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String operandOne = operands.pop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String newToken = "( " + operandOne + " " + operator + " "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ operandTwo + " )";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operands.push(newToken);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default -> operands.push(token);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return operands.pop();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java