亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用java中的堆棧在中綴表達式中創建括號

使用java中的堆棧在中綴表達式中創建括號

萬千封印 2022-06-04 16:55:56
我需要編寫一個 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());    }}
查看完整描述

2 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

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)))



查看完整回答
反對 回復 2022-06-04
?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

public class FixExpressionParentheses {


    public String fixExpression(String expression) {

        String[] tokenArray = expression.split(" ");


        Stack<String> operators = new Stack<>();

        Stack<String> operands = new Stack<>();


        for (String token: tokenArray) {

            switch (token) {

                case "+", "-", "*", "/", "sqrt" -> operators.push(token);

                case ")" -> {

                    String operator = operators.pop();

                    String operandTwo = operands.pop();

                    String operandOne = operands.pop();

                    String newToken = "( " + operandOne + " " + operator + " "

                                           + operandTwo + " )";

                    operands.push(newToken);

                }

                default -> operands.push(token);

            }

        }


        return operands.pop();

    }

}


查看完整回答
反對 回復 2022-06-04
  • 2 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號