用逆波兰表达式(后缀表达式)实现简单计算器

最近在学习数据结构,用栈来实现一个逆波兰表达式求值

基本步骤

1. 将普通的数学表达式 ===> 逆波兰表达式(后缀表达式)

2. 根据逆波兰表达式求出结果

以下是代码

package calc;

import java.util.Stack;

/**
 * Auther:vincent-Dou
 * Date: 2019/4/21
 * Time: 23:06
 * Description:算术表达式求值(用后缀表达式来求值)
 */
public class NBL {

    public static void main(String[] agrs) {
        String a = "652";
        String s = "(3*2+5 -1) / 2";
        System.out.println(s);
        System.out.println(suffixEvaluation(infixToSuffix(s)));
    }

    //将中中缀表达式 ====》 后缀表达式
    //遇见加号要将栈元素全部弹出或者到 ( 停止在放加号
    //遇见 ) 要将栈弹出到 遇见(
    public static String[] infixToSuffix(String s) {
        Stack<Character> sta = new Stack<>();
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < s.length(); ) {
            //当为数字时
            if ((String.valueOf(s.charAt(i))).matches("[0-9]")) {
                while (i < s.length() && (String.valueOf(s.charAt(i))).matches("[0-9]")) {
                    str.append(s.charAt(i));
                    i++;
                }
                str.append(',');

                //遇见 + 号时;
            } else if (s.charAt(i) == '+') {
                while (!sta.empty()) {
                    if (sta.peek() == '(') {
                        break;
                    }
                    str.append(sta.pop().toString());
                    str.append(',');
                }
                sta.push(s.charAt(i));
                i++;
            } else if (s.charAt(i) == '-') {
                sta.push(s.charAt(i));
                i++;
            } else if (s.charAt(i) == '*') {
                sta.push(s.charAt(i));
                i++;
            } else if (s.charAt(i) == '/') {
                sta.push(s.charAt(i));
                i++;
            } else if (s.charAt(i) == '(') {
                sta.push(s.charAt(i));
                i++;
            } else if (s.charAt(i) == ')') {
                while (sta.peek() != '(') {
                    str.append(sta.pop());
                    str.append(',');
                }
                sta.pop();
                i++;
            } else if (s.charAt(i) == ' ') {
                i++;
            }
        }
        while (!sta.empty()) {
            str.append(sta.pop());
            str.append(',');
        }
        String str1 = str.toString().trim();
        String[] str2 = str1.split(",");
//        for(String i : str2){
//            System.out.print(i + " ");
//        }
//        System.out.println();
        return str2;
    }

    //根据后缀表达式求值
    public static int suffixEvaluation(String[] tokens) {
        Stack<Integer> s = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i].equals("+")) {
                s.push(s.pop() + s.pop());
            } else if (tokens[i].equals("-")) {
                int second = Integer.valueOf(s.pop());
                int first = Integer.valueOf(s.pop());
                s.push(first - second);
            } else if (tokens[i].equals("*")) {
                s.push(s.pop() * s.pop());
            } else if (tokens[i].equals("/")) {
                int second = Integer.valueOf(s.pop());
                int first = Integer.valueOf(s.pop());
                s.push(first / second);
            } else {
                s.push(Integer.valueOf(tokens[i]));
            }
        }
        return Integer.valueOf(s.pop());
    }
}

感觉无聊做了图形界面:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值