Java使用栈实现综合计算器(中缀表达式转后缀表达式)

最近在深入学习栈数据结构,顺便实现了一下用栈实现一个整数计算器的功能。
对于一般用来表达算式的字符串,形如"10+((2+3)*4)-5",我们可不可以自己手写一个计算器用来计算其结果呢?答案显而易见,下面给出详解代码。
(本文旨在讨论数据结构与算法的应用,并不考虑计算器的功能全面性)

1.首先我们定义一个用来将字符串转换成list集合的方法。

public static ArrayList<String> getList(String expression){
        ArrayList<String> list = new ArrayList<>();//定义集合,用来存储字符串中的元素
        String temp = "";//该字符串用来拼接多位数
        
        //遍历字符串
        for(int i = 0;i < expression.length();i++){
            char item = expression.charAt(i);
            if(item >= 48 && item <= 57){//判断取出值的ASCII码,如果在48至57范围内,则认为是数字
                temp += item;
            }else {
                //不是数字则为运算符
                //在将运算符存入集合前,先判断是否有数字未存入集合
                if(!temp.equals("")){
                    list.add(temp);
                    temp = "";
                }
                list.add("" + item);
            }
        }
        if(!temp.equals("")){
            list.add(temp);
            temp = "";
        }
        return list;
    }

2.然后我们定义一个用来比较运算符优先级的方法

public static int priority(String oper){
        if(oper.equals("*") || oper.equals("/")){
            return 1;
        }else if(oper.equals("+") || oper.equals("-")){
            return 0;
        }else {
            throw new RuntimeException();
        }
    }

3.本文目的是实现后缀表达式的计算,所以在调用getList()方法拿到中缀表达式集合后,我们还需要将中缀表达式转后缀表达式,如:10+((2+3)*4)-5的后缀表达式为:10 2 3 + 4 * + 5 - 。这也是代码的核心部分。

public static List<String> getSuffix(List<String> list){
        List<String> suffix = new ArrayList<>();
        Stack<String> stack = new Stack<>();

        //遍历传过来的中缀表达式
        for(String item : list){
            //先处理遍历到的值为数字,小括号的情况
            if(item.matches("\\d+")){//正则表达式判断是否是一个数字,如果是则直接放入集合
                suffix.add(item);
            }else if(item.equals("(")){//遇到左括号则直接入栈
                stack.push(item);
            }else if(item.equals(")")){//遇到右括号,依次将栈顶元素弹栈并放入集合,直到栈顶为左括号为止
                while (!stack.peek().equals("(")){
                    suffix.add(stack.pop());
                }
                stack.pop();//将栈顶的左括号丢弃
            }else {
                //剩下的情况则为遍历到运算符(+,-,*,/)
                if(stack.isEmpty()){//如果当前栈为空,直接将运算符入栈
                    stack.push(item);
                }else {
                    /*
                      剩下都是当前栈不为空的情况,这里的步骤为:
                      1.如果栈顶为左括号,直接入栈
                      2.如果当前运算符的优先级小于等于栈顶运算符的优先级,则弹出栈顶运算符存入集合,并重新判断是否满足这4条规则
                      3.如果当前运算符优先级大于栈顶运算符,直接入栈
                      4.如果栈为空,直接入栈(在执行第二条时,也有可能将栈的大小变为空)
                     */
                    while (true){
                        if(stack.size() == 0){
                            stack.push(item);
                            break;
                        }
                        if(stack.peek().equals("(")){
                            stack.push(item);
                            break;
                        }
                        if(priority(stack.peek()) >= priority(item)){
                            suffix.add(stack.pop());
                        }else {
                            stack.push(item);
                            break;
                        }
                    }
                }
            }
        }
        //最后将栈中剩余元素依次弹栈并存入集合中
        while (stack.size() != 0){
            suffix.add(stack.pop());
        }
        return suffix;
    }

4.编写一个计算后缀表达式的方法,计算后缀表达式的思路为:遍历后缀表达式,遇到数字则直接压栈;遇到运算符则弹出栈中两个元素进行该运算符对应的运算,并将运算结果压栈。待遍历完成后,最后留在栈中的元素则为最终计算结果。

public static int calculation(List<String> expression){
        Stack<String> stack = new Stack<>();
        for(String item: expression){//遍历后缀表达式
            if(item.matches("\\d+")){//如果是数字,则直接压栈
                stack.push(item);
            }else {
                //是运算符的情况,弹出栈中两个数字,并进行与之对应的运算
                int num1 = Integer.parseInt(stack.pop());
                int num2 = Integer.parseInt(stack.pop());
                int res = 0;
                if(item.equals("+")){
                    res = num1 + num2;
                } else if(item.equals("-")){
                    res = num2 - num1;
                }else if(item.equals("*")){
                    res = num1 * num2;
                }else if(item.equals("/")){
                    res = num2 / num1;
                }else {
                    throw new RuntimeException("运算符有误!");
                }
                //将运算结果压栈
                stack.push(String.valueOf(res));
            }
        }
        return Integer.parseInt(stack.pop());
    }

5.我们最后编写一个方法将上述所有方法统一起来

public static int calculator(String expression){
        return calculation(getSuffix(getList(expression)));
    }

测试一下:

public static void main(String[] args) {
        String expression = "10+((2+3)*4)-5";
        System.out.println(calculator(expression));
    }

运行结果:
在这里插入图片描述

完整代码:

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Suffix {
    public static void main(String[] args) {
        String expression = "10+((2+3)*4)-5";
        System.out.println(calculator(expression));
    }

    public static int calculator(String expression){
        return calculation(getSuffix(getList(expression)));
    }

    public static int priority(String oper){
        if(oper.equals("*") || oper.equals("/")){
            return 1;
        }else if(oper.equals("+") || oper.equals("-")){
            return 0;
        }else {
            throw new RuntimeException();
        }
    }

    public static int calculation(List<String> expression){
        Stack<String> stack = new Stack<>();
        for(String item: expression){//遍历后缀表达式
            if(item.matches("\\d+")){//如果是数字,则直接压栈
                stack.push(item);
            }else {
                //是运算符的情况,弹出栈中两个数字,并进行与之对应的运算
                int num1 = Integer.parseInt(stack.pop());
                int num2 = Integer.parseInt(stack.pop());
                int res = 0;
                if(item.equals("+")){
                    res = num1 + num2;
                } else if(item.equals("-")){
                    res = num2 - num1;
                }else if(item.equals("*")){
                    res = num1 * num2;
                }else if(item.equals("/")){
                    res = num2 / num1;
                }else {
                    throw new RuntimeException("运算符有误!");
                }
                //将运算结果压栈
                stack.push(String.valueOf(res));
            }
        }
        return Integer.parseInt(stack.pop());
    }

    public static ArrayList<String> getList(String expression){
        ArrayList<String> list = new ArrayList<>();//定义集合,用来存储字符串中的元素
        String temp = "";//该字符串用来拼接多位数

        //遍历字符串
        for(int i = 0;i < expression.length();i++){
            char item = expression.charAt(i);
            if(item >= 48 && item <= 57){//判断取出值的ASCII码,如果在48至57范围内,则认为是数字
                temp += item;
            }else {
                //不是数字则为运算符
                //在将运算符存入集合前,先判断是否有数字未存入集合
                if(!temp.equals("")){
                    list.add(temp);
                    temp = "";
                }
                list.add("" + item);
            }
        }
        if(!temp.equals("")){
            list.add(temp);
            temp = "";
        }
        return list;
    }

    public static List<String> getSuffix(List<String> list){
        List<String> suffix = new ArrayList<>();
        Stack<String> stack = new Stack<>();

        //遍历传过来的中缀表达式
        for(String item : list){
            //先处理遍历到的值为数字,小括号的情况
            if(item.matches("\\d+")){//正则表达式判断是否是一个数字,如果是则直接放入集合
                suffix.add(item);
            }else if(item.equals("(")){//遇到左括号则直接入栈
                stack.push(item);
            }else if(item.equals(")")){//遇到右括号,依次将栈顶元素弹栈并放入集合,直到栈顶为左括号为止
                while (!stack.peek().equals("(")){
                    suffix.add(stack.pop());
                }
                stack.pop();//将栈顶的左括号丢弃
            }else {
                //剩下的情况则为遍历到运算符(+,-,*,/)
                if(stack.isEmpty()){//如果当前栈为空,直接将运算符入栈
                    stack.push(item);
                }else {
                    /*
                      剩下都是当前栈不为空的情况,这里的步骤为:
                      1.如果栈顶为左括号,直接入栈
                      2.如果当前运算符的优先级小于等于栈顶运算符的优先级,则弹出栈顶运算符存入集合,并重新判断是否满足这4条规则
                      3.如果当前运算符优先级大于栈顶运算符,直接入栈
                      4.如果栈为空,直接入栈(在执行第二条时,也有可能将栈的大小变为空)
                     */
                    while (true){
                        if(stack.size() == 0){
                            stack.push(item);
                            break;
                        }
                        if(stack.peek().equals("(")){
                            stack.push(item);
                            break;
                        }
                        if(priority(stack.peek()) >= priority(item)){
                            suffix.add(stack.pop());
                        }else {
                            stack.push(item);
                            break;
                        }
                    }
                }
            }
        }
        //最后将栈中剩余元素依次弹栈并存入集合中
        while (stack.size() != 0){
            suffix.add(stack.pop());
        }

        return suffix;
    }

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值