用java实现一个带括号的计算器(无界面)

import java.io.*;
import java.util.Stack;

public class test {
    public static boolean NotOperator(char c) { //用于判断字符c是运算符还是操作数
        return switch (c) {
            case '#', '+', '-', '*', '/', '(', ')' -> false;
            default -> true;
        };
    }

    static char[][] OP = {
            {'>', '>', '<', '<', '<', '>', '>'},
            {'>', '>', '<', '<', '<', '>', '>'},
            {'>', '>', '>', '>', '<', '>', '>'},
            {'>', '>', '>', '>', '<', '>', '>'},
            {'<', '<', '<', '<', '<', '=', 'E'},
            {'>', '>', '>', '>', 'E', '>', '>'},
            {'<', '<', '<', '<', '<', 'E', '='},
    };

    public static int GetOperatorID(char Operator) {
        return switch (Operator) {
            case '+' -> 0;
            case '-' -> 1;
            case '*' -> 2;
            case '/' -> 3;
            case '(' -> 4;
            case ')' -> 5;
            case '#' -> 6;
            default -> -1;
        };
    }

    public static char Precede(char Operator1, char Operator2) {
        int OperatorID1, OperatorID2;
        OperatorID1 = GetOperatorID(Operator1);
        OperatorID2 = GetOperatorID(Operator2);
        return (OP[OperatorID1][OperatorID2]);
    }

    //Operator()将以op1、op2作为第一、二操作数
    //cal作为运算符进行计算,返回计算结果
    public static double Operator(double op1, char cal, double op2) {
        return switch (cal) {
            case '+' -> op1 + op2;
            case '-' -> op1 - op2;
            case '*' -> op1 * op2;
            case '/' -> op1 / op2;
            default -> 0;
        };
    }

    public static double ExpEvaluation(char[] ex) { //读入一个简单算术表达式并计算其值
        char[] exp = new char[ex.length + 1];
        System.arraycopy(ex, 0, exp, 0, ex.length);
        exp[ex.length] = '#';
        char thera;
        int i = 0;
        double b, a, val;
        Stack OPTR = new Stack<>(); //运算符栈
        Stack OPND = new Stack<>(); //操作数栈
        OPTR.push('#');
        while (exp[i] != '#' || OPTR.peek() != '#')
            if (NotOperator(exp[i])) { //不是运算符,入栈
                double temp; //存放数字的临时变量
                //将字符转换为10进制数
                temp = exp[i] - '0';
                i++;
                while (NotOperator(exp[i])) { //将逐个读入的操作数各位转换为十进制数
                    temp = temp * 10 + exp[i] - '0';
                    i++;
                }
                OPND.push(temp);
            } else
                switch (Precede(OPTR.peek(), exp[i])) { //Precede()用于判断栈顶元素和表达式当前元素的优先级
                    //返回'<''>'、'='表
                    case '<' -> { //栈顶元素优先权低,入栈
                        OPTR.push(exp[i]);
                        i++;
                    }
                    case '=' -> { //优先权相等,脱括号并接收下一字符
                        OPTR.pop();
                        i++;
                    }
                    case '>' -> { //栈顶元素优先权高,退栈并将运算结果入栈
                        thera = OPTR.pop();
                        b = OPND.pop();
                        a = OPND.pop();
                        OPND.push(Operator(a, thera, b));
                    }
                }
        val = OPND.peek();
        return val;
    }

    public static void main(String[] args) {
        BufferedReader br;
        String s;
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("输入一个允许加括号的四则运算表达式");
            s = br.readLine();
            char[] us = s.toCharArray();
            System.out.println("表达式计算值为:" + ExpEvaluation(us));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

你可能感兴趣的:(自我学习归纳,java)