表达式求解——Java

package stact;

import java.util.Scanner;

public class EvaluateException {

    /*
    * 运算符
    * 比较两个运算符的优先级
    * */
    private static String compare(String first, String second) {
        if (first.equals("+") || first.equals("-")) {
            if (second.equals("=") || second.equals("+") || second.equals("-") || second.equals(")")) {
                return ">";
            } else if (second.equals("*") || second.equals("/") || second.equals("(")) {
                return "<";
            }
        } else if (first.equals("*") || first.equals("/")) {
            if (second.equals("=") || second.equals("+") || second.equals("-") || second.equals("*") || second.equals("/") || second.equals(")")) {
                return ">";
            } else if (second.equals("(")) {
                return "<";
            }
        } else if (first.equals("(")) {
            if (second.equals("+") || second.equals("-") || second.equals("*") || second.equals("/") || second.equals("(")) {
                return "<";
            } else if (second.equals(")")) {
                return ">";
            }
        }
        return null;
    }
    /*
    * 计算表达式的值
    * */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String exp = sc.nextLine();
        Stact<Double> num = new Stact<>();   //数据栈
        Stact<String> ch = new Stact<>();     //运算符栈  符号只包括“+、-、*、/、(、)、=”英文符号
        //暂时假设所有各项之间用空格分开已经,例如:1 + 2 + ( 4 - 2 ) =
        String []items = exp.split(" ");
        for ( int i = 0; i < items.length; i ++) {
            String item = items[i];
            try {
                double n = Double.parseDouble(item);
                //是数字
                num.push(n);
            } catch (NumberFormatException e) {
                //不是数字
                if (ch.base == ch.top) {
                    //栈为空
                    ch.push(item);
                } else {
                    switch (compare(ch.top(),item)) {
                        case ">": {
                            //如果栈顶的元素优先级高
                            switch (ch.pop()) {
                                case "+": num.push(num.pop() + num.pop());i --;
                                break;
                                case "-": {
                                    //栈顶元素做减数
                                    double a = num.pop();
                                    double b = num.pop();
                                    num.push(b - a);
                                    i --;
                                }
                                break;
                                case "*": num.push(num.pop() * num.pop());i --;
                                break;
                                case "/":{
                                    //栈顶元素做除数
                                    double a = num.pop();
                                    double b = num.pop();
                                    num.push(b / a);
                                    i --;
                                }
                                break;
                                case "(": break;
                            }
                            break;
                        }
                        case "<": {
                            ch.push(item);
                        }
                    }
                }
            }
        }
        System.out.println(exp + " " +num.top());
    }
    static class Stact<E> {
        int base;
        int top;
        Object []notes = new Object[10000];

        public Stact() {
            this.top = 0;
            this.base = 0;
        }

        /*
         * 删除栈顶元素并返回
         * */
        public E pop() {
            return (E)notes[-- top];
        }

        /*
         * top获取栈顶元素
         * */
        public E top() {
            return (E)notes[top - 1];
        }

        /*
         * 向栈中添加一个元素
         * */
        public void push(E e) {
            notes[top ++] = e;
        }
    }
}

发布了57 篇原创文章 · 获赞 55 · 访问量 1950

猜你喜欢

转载自blog.csdn.net/qq_40561126/article/details/104271878
今日推荐