Java表达式求值

题目描述

设计一个表达式求值的程序 ,支持+,-,*,/,%,(,)以及=运算符组成的表达式的运算。

假设数字只包含int和float,并且全部的表达式都是正确的。

注意表达式向上cast:

除法 1/3=0,1.0/2=0.5

int+float=float

表达式最后的=可以包含也可以不包含

样例输入输出

样例1

输入:
1+1.0
输出:
2.0

样例2

输入:
(55)%11(9+2)
输出:
33

样例3

输入:
19.2+8.67/3=
输出:
22.09

样例4

输入:
9.2*3
输出:
27.599998

题解

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        String inputString = myScanner.nextLine();

        // 建立输入数组

        ArrayList<Character> inputArrayList = new ArrayList<Character>();
        for (int i = 0; i < inputString.length(); i++) {
            inputArrayList.add(inputString.charAt(i));
        }

        // 建立存储数字数组
        ArrayList<Double> inputNum = new ArrayList<Double>();
        // 定义顶部元素位置,便于之后出栈
        int topNum = -1;

        // 建立存储符号数组
        ArrayList<Character> inputOperater = new ArrayList<Character>();
        // 定义顶部元素位置,便于之后出栈
        int topOperater = -1;

        // 建立临时数组
        ArrayList<Character> tempNum = new ArrayList<Character>();
        // 定义顶部元素位置,便于之后出栈
        int tempTopNum = -1;

        int bracketNum = 0;
        for (int i = 0; i < inputString.length(); i++) {
            // 进行数字的提取
            // System.out.println(bracketNum);
            // 数字部分结束,将数字输入到数字数组中
            if ((inputArrayList.get(i) >= '0' && inputArrayList.get(i) <= '9') || inputArrayList.get(i) == '.') {
                if (i + 1 == inputString.length() || inputArrayList.get(i + 1) == '+'
                        || inputArrayList.get(i + 1) == '-' || inputArrayList.get(i + 1) == '*'
                        || inputArrayList.get(i + 1) == '/' || inputArrayList.get(i + 1) == '(' ||
                        inputArrayList.get(i + 1) == ')' || inputArrayList.get(i + 1) == '='
                        || inputArrayList.get(i + 1) == '%' || inputArrayList.get(i + 1) == ' ') {

                    tempNum.add(inputString.charAt(i));
                    tempTopNum += 1;
                    if (tempNum.contains('.')) {
                        int position = -1;
                        for (int j = 0; j < tempTopNum; j++) {
                            if (tempNum.get(j) == '.') {
                                position = j;
                                break;
                            }
                        }
                        double tempSum = 0;
                        for (int j = 0; j < position; j++) {
                            tempSum += (tempNum.get(j) - 48) * Math.pow(10, position - j - 1);
                        }
                        for (int j = position + 1; j <= tempTopNum; j++) {
                            tempSum += (tempNum.get(j) - 48) * Math.pow(10, position - j);
                        }
                        inputNum.add(tempSum);

                        tempNum.clear();
                        tempTopNum = -1;

                        topNum += 1;
                    } else {
                        double tempSum = 0;
                        for (int j = 0; j <= tempTopNum; j++) {
                            tempSum += (tempNum.get(j) - 48) * Math.pow(10, tempTopNum - j);
                        }
                        inputNum.add(tempSum);

                        tempNum.clear();
                        tempTopNum = -1;

                        topNum += 1;
                    }
                }
                // 将字符串中的数字部分添加到临时数字数组中
                else {
                    tempNum.add(inputString.charAt(i));
                    tempTopNum += 1;
                }
                // 对空格以及等号进行处理
            } else if (inputArrayList.get(i) == ' ' || inputArrayList.get(i) == '=') {
                continue;
            } else {
                // 运算符处理部分
                Calculate calculate = new Calculate();

                if (topOperater == -1 || inputArrayList.get(i) == '(') {
                    inputOperater.add(inputArrayList.get(i));
                    topOperater += 1;
                    if (inputArrayList.get(i) == '(') {
                        bracketNum += 1;
                    }
                } else if (inputArrayList.get(i) != ')' && calculate.returnValue(inputArrayList.get(i)) > calculate
                        .returnValue(inputOperater.get(topOperater))) {
                    inputOperater.add(inputArrayList.get(i));
                    topOperater += 1;
                } else {
                    if (inputArrayList.get(i) == ')') {
                        while (topNum - 1 >= 0 && topOperater >= 0 && inputOperater.get(topOperater) != '(') {
                            double tempCal = calculate.returnCal(inputNum.get(topNum - 1), inputNum.get(topNum),
                                    inputOperater.get(topOperater));
                            if (inputNum.get(topNum - 1) % 1 == 0 && inputNum.get(topNum) % 1 == 0) {
                                tempCal = tempCal - tempCal % 1;
                            }
                            inputNum.remove(topNum);
                            inputNum.remove(topNum - 1);
                            topNum -= 2;

                            topNum += 1;
                            inputNum.add(tempCal);
                            inputOperater.remove(topOperater);
                            topOperater -= 1;

                        }
                        inputOperater.remove(topOperater);
                        topOperater -= 1;
                        bracketNum -= 1;
                    } else {
                        while (topNum - 1 >= 0 && topOperater >= 0 && inputOperater.get(topOperater) != '('
                                && calculate.returnValue(inputArrayList.get(i)) <= calculate
                                .returnValue(inputOperater.get(topOperater))) {
                            double tempCal = calculate.returnCal(inputNum.get(topNum - 1), inputNum.get(topNum),
                                    inputOperater.get(topOperater));
                            if (inputNum.get(topNum - 1) % 1 == 0 && inputNum.get(topNum) % 1 == 0) {
                                tempCal = tempCal - tempCal % 1;
                            }
                            inputNum.remove(topNum);
                            inputNum.remove(topNum - 1);
                            topNum -= 2;

                            topNum += 1;
                            inputNum.add(tempCal);
                            inputOperater.remove(topOperater);
                            topOperater -= 1;
                        }
                        inputOperater.add(inputArrayList.get(i));
                        topOperater += 1;
                    }
                }
            }
        }
        Calculate calculate = new Calculate();
        while (!inputOperater.isEmpty()) {
            double temp1 = inputNum.get(topNum);
            double temp2 = inputNum.get(topNum - 1);

            inputNum.remove(topNum);
            inputNum.remove(topNum - 1);
            topNum -= 2;

            inputNum.add(calculate.returnCal(temp2, temp1, inputOperater.get(topOperater)));
            topNum += 1;

            inputOperater.remove(topOperater);
            topOperater -= 1;
        }
        if (inputNum.get(0) % 1 == 0) {
            System.out.println(inputNum.get(0).intValue());
        } else {
            System.out.println(inputNum.get(0).floatValue());
        }
    }
}

// 用于判断每个运算符的优先级
class Calculate {
    public Calculate() {
    }

    public int returnValue(char inputChar) {
        if (inputChar == '+' || inputChar == '-') {
            return 1;
        } else if (inputChar == '(') {
            return 0;
        } else {
            return 2;
        }
    }

    public double returnCal(double inputDouble1, double inputDouble2, char inputChar) {
        if (inputChar == '+') {
            return inputDouble1 + inputDouble2;
        } else if (inputChar == '-') {
            return inputDouble1 - inputDouble2;
        } else if (inputChar == '*') {
            return inputDouble1 * inputDouble2;
        } else if (inputChar == '/') {
            return inputDouble1 / inputDouble2;
        } else if (inputChar == '%') {
            return inputDouble1 % inputDouble2;
        } else {
            return inputDouble1;
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值