Java实现基本计算器

Java实现基本计算器,包括 加、减、乘、除、括号的运算。

import java.util.Arrays;
import java.util.List;
import java.util.Stack;

/**
 *
 * @author 55
 * @date 2022/4/12
 */
public class calc {
    public static void main(String[] args) {
        // 要计算的表达式
        String str = "12+2.3*35-45/(4+5)";

        // 转为逆波兰式
        String rpn = transformRPN(str);

        // 计算结果
        double result  = calc(rpn);

        System.out.println("表达式:"+ str +" = " + result);
    }

    // 转逆波兰式
    public static String transformRPN(String str){
        // 清掉表达式中的空格
        str = str.replace(" ", "");
        Stack<String> stack_value = new Stack<>();
        Stack<String> stack_symbol = new Stack<>();


        String num = "";
        for (int i = 0; i < str.length(); i++){
            String ch = str.substring(i,i+1);
            if (num.isEmpty() && isValue(ch)){
                num += ch;
            }else{
                if (isValue(ch)){
                    num += ch;
                }else {
                    if (num.length() > 0){
                        stack_value.push(num);
                        num = "";
                    }

                    if (ch.equals(")")){
                        results(stack_value, stack_symbol);
                        stack_symbol.pop();
                    } else if (ch.equals("(")){
                        stack_symbol.push(ch);
                    } else {
                        if (!stack_symbol.isEmpty()){
                            if (priority(ch) <= priority(stack_symbol.peek())) {
                                results(stack_value, stack_symbol);
                            }
                        }
                        stack_symbol.push(ch);
                    }
                }
            }
        }

        while (!stack_symbol.isEmpty()){
            results(stack_value, stack_symbol);
        }

        return stack_value.peek();
    }

    // 判断是否为数值,返回true为数值
    public static boolean isValue (String s) {
        return !s.equals("+") && !s.equals("-") && !s.equals("*") && !s.equals("/") && !s.equals("(") && !s.equals(")");
    }

    // 对中间结果进行拼接、入栈等操作
    public static void results(Stack<String> stack_value, Stack<String> stack_symbol) {
        String res = "";
        String s2 = stack_value.pop();
        String s1 = stack_value.pop();
        res += s1 + "," + s2;
        res += "," + stack_symbol.pop();
        stack_value.push(res);
    }

    // 计算各运算符的优先级
    public static int priority(String s) {
        int p = 0;
        switch (s) {
            case "+":
            case "-": {
                p = 1;
                break;
            }

            case "*":
            case "/": {
                p = 2;
                break;
            }
            case "(": {
                p = -1;
                break;
            }
        }
        return p;
    }

    // 转逆波兰式计算结果
    public static double calc(String rpn){
        List<String> rpnList = Arrays.asList(rpn.split(","));
        Stack<Double> stack = new Stack<>();

        for (String value : rpnList){
            if (isValue(value)){
                stack.push(Double.parseDouble(value));
            }else {
                Double v1 = stack.pop();
                Double v2 = stack.pop();
                Double result = calc(v2, v1, value);
                stack.push(result);
            }
        }

        return stack.peek();
    }

    // 计算二个值的结果
    public static double calc(double v1, double v2, String op){
        switch (op){
            case "+": return v1+v2;
            case "-": return v1-v2;
            case "*": return v1*v2;
            case "/": return v1/v2;
            default: break;
        }

        return 0;
    }
}
0%