基于栈的后缀算术表达式求值(Java)

通过栈计算一个不带括号的后缀表达式。

输入样例:
1 2+8 2-7 4-/*=
1 2+=
1 2/=
=

输出样例:
6.00
3.00
0.50

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

public class Main {
    
    
    private static String postfixExpression = "";
    private static final LinkedList<String> postfixList = new LinkedList<>();
    private static final Stack<Double> stack = new Stack<>();

    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        while (true) {
    
    
            postfixExpression = scanner.nextLine();
            if (postfixExpression.equals("=")) break;

            split();
            System.out.printf("%.2f\n", calculate());
            postfixList.clear();
        }
    }

    /**
     * 从输入的字符串中提取操作数和运算符,并存入链表
     */
    private static void split() {
    
    
        int i = 0;
        while (i < postfixExpression.length()) {
    
    
            if (postfixExpression.charAt(i) != 32 && !isOperator(postfixExpression.charAt(i))) {
    
    //扫描到操作数
                int j = i + 1;
                while (postfixExpression.charAt(j) != 32 && !isOperator(postfixExpression.charAt(j)) && j < postfixExpression.length()) {
    
    
                    j++;
                }
                postfixList.add(postfixExpression.substring(i, j));
                i = j;
            } else {
    
    //扫描到运算符或空格
                if (isOperator(postfixExpression.charAt(i))) postfixList.add(postfixExpression.substring(i, i + 1));
                i++;
            }
        }
    }

    /**
     * 后缀计算主函数
     * @return
     */
    private static double calculate() {
    
    
        label:
        for (String s : postfixList) {
    
    
            switch (s) {
    
    
                case "+": {
    
    
                    /*右操作数先出栈*/
                    double rightOperand = stack.pop();
                    double leftOperand = stack.pop();
                    stack.push(leftOperand + rightOperand);
                    break;
                }
                case "-": {
    
    
                    double rightOperand = stack.pop();
                    double leftOperand = stack.pop();
                    stack.push(leftOperand - rightOperand);
                    break;
                }
                case "*": {
    
    
                    double rightOperand = stack.pop();
                    double leftOperand = stack.pop();
                    stack.push(leftOperand * rightOperand);
                    break;
                }
                case "/": {
    
    
                    double rightOperand = stack.pop();
                    double leftOperand = stack.pop();
                    stack.push(leftOperand / rightOperand);
                    break;
                }
                case "=":
                    break label;
                default:
                    stack.push(Double.valueOf(s));
                    break;
            }
        }
        return stack.pop();
    }
    
    /**
     * 判断当前扫描到的字符是否是运算符
     * @return
     */
    private static boolean isOperator(char c) {
    
    
        return (c == '+' || c == '-' || c == '*' || c == '/' || c == '=');
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43686863/article/details/123597357