(四)栈—逆波兰计算器

一、基本介绍

二、应用实例

    (3+4)×5−6:对应的后缀表达式是 3 4 + 5 × 6 -。


    后缀表达式的计算机求值过程为从左至右扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,
    用运算符对它们做相应的计算(次顶元素和栈顶元素),并将结果入栈。重复上述过程直到表达式最右端,最后运算得出的值即为表达式的结果。

    针对后缀表达式求值步骤如下:
            从左至右扫描,将3和4压入堆栈。
            遇到+运算符,弹出4和3(4为栈顶元素,3为次顶元素),计算出3+4的值,得7,再将7入栈。
            将5入栈。
            接下来是×运算符,因此弹出5和7,计算出7×5=35,将35入栈。
            将6入栈。
            最后是-运算符,计算出35-6的值,即29,由此得出最终结果。

package stack;

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

public class SuffixExpression {
    public static void main(String[] args) {
        String suffixExpression = "3 4 + 5 × 6 -";
        List<String> list = getList(suffixExpression);
        int result = calculate(list);
        System.out.println("结果 = " + result);
    }

    private static List<String> getList(String suffixExpression) {
        String[] s = suffixExpression.split(" ");
        List<String> list = new ArrayList<>(s.length);
        for (String temp : s) {
            list.add(temp);
        }
        return list;
    }

    private static int calculate(List<String> list) {
        SuffixStack stack = new SuffixStack(16);
        int num1 = 0;
        int num2 = 0;
        int res = 0;
        for (String item : list) {
            if (item.matches("\\d+")) {     // 匹配的是多位数
                stack.push(Integer.parseInt(item));
            } else {
                num1 = stack.pop();
                num2 = stack.pop();
                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 {
                    System.out.println("无效运算符!");
                    return -1;
                }
                stack.push(res);
            }
        }
        //  最后运算得出的值即为表达式的结果
        return stack.pop();
    }
}

class SuffixStack{
    private int[] arr;
    private int maxSize;
    private int top;

    public SuffixStack(int maxSize) {
        this.maxSize = maxSize;
        top = -1;
        arr = new int[maxSize];
    }

    public void push(int val) {
        if (isFull()) {
            throw new RuntimeException("栈满,入栈失败!");
        }
        arr[++top] = val;
        System.out.println("入栈成功~");
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空,出栈失败!");
        }
        return arr[top--];
    }

    public void print() {
        if (isEmpty()) {
            throw new RuntimeException("栈空,出栈失败!");
        }
        System.out.print("栈元素:");
        for (int i = top; i >= 0 ; i--) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public boolean isEmpty() {
        if (top == -1) {
            return true;
        }
        return false;
    }

    public boolean isFull() {
        if (top == maxSize - 1) {
            return true;
        }
        return false;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来得晚一些也行

观众老爷,请赏~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值