• 后缀表达式(逆波兰表达式)的计算---栈实现


    后缀表达式的计算,代码如下:

     1 package com.pangzi.stucture;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 import java.util.Stack;
     6 
     7 //逆波兰表达式
     8 public class PolandNotation {
     9 
    10     public static void main(String[] args) {
    11         //定义一个逆波兰表达式
    12         //(3+4)*5-6 => 34+5*6-     中缀转后缀
    13         String suffixExpression = "3 4 + 5 * 6 -";
    14         //先将表达式放入arrayList里面
    15         //将arraylist传递给一个方法,然后遍历list配合栈完成计算
    16         List<String> rpnList = getListString(suffixExpression);
    17         
    18         int res = calculate(rpnList);
    19         
    20         System.out.println(res);//打印结果
    21         
    22     }

    首先定义一个main方法,然后在其中定义需要计算的表达式,并传入后面需要实现的方法里。方法实现如下:

     1 //将逆波兰表达式放入arraylist中
     2     public static List<String> getListString(String suffixExpression){
     3         //将suffixExpression分割
     4         String[] split = suffixExpression.split(" ");
     5         List<String> list = new ArrayList<String>();
     6         //遍历数组,放入list
     7         for(String ele:split){
     8             list.add(ele);
     9         }
    10         return list;
    11     }

    将表达式放入list

     1     //完成对逆波兰表达式的运算
     2     //从左至右扫描,遇到数字就直接压栈,遇到运算符就弹出栈中存在的数字(栈顶元素和次顶元素)并进行计算,
     3     //次顶在前,栈顶在后这样进行计算,然后将结果入栈,不断重复得出最终结果。
     4     public static int calculate(List<String> ls){
     5         //创建一个栈
     6         Stack<String> stack = new Stack<String>();
     7         //遍历ls
     8         for(String item:ls){
     9             //使用正则表达式取出数
    10             if(item.matches("\d+")){
    11                 stack.push(item);//如果是数就把它放进去
    12             }else{//如果不是数字就pop出两个数并运算,然后再入栈
    13                 int num2 = Integer.parseInt(stack.pop());//先弹栈的顶元素
    14                 int num1 = Integer.parseInt(stack.pop());//后弹栈的次顶元素
    15                 int res = 0;//定义运算结果
    16                 if(item.equals("+")){
    17                     res = num1 + num2;
    18                 }else if(item.equals("-")){
    19                     res = num1 - num2;
    20                 }else if(item.equals("*")){
    21                     res = num1 * num2;
    22                 }else if(item.equals("/")){
    23                     res = num1 / num2;
    24                 }else{
    25                     throw new RuntimeException("运算符有误,请检查");
    26                 }
    27                 //把res入栈
    28                 stack.push(""+res);
    29             }
    30         }
    31         //最后留在栈中的就是这个结果,将字符串转换为int并返回。
    32         return Integer.parseInt(stack.pop());
    33     }
    34 }

    然后对表达式进行计算,并返回结果。

  • 相关阅读:
    【Git&GitHub idea中使用Git 03】
    【Git&GitHub 本地库和远程库交互 02】
    【Git&GitHub 本地库操作 01】
    C Primer Plus学习笔记【11章节】
    Python包,模块理解,以及通过元类自动化注册属性。
    pandas.DataFrame.from_dict的使用介绍
    C Primer Plus学习笔记 第10章 编程练习
    CMOS与BIOS的区别(转帖)
    Python 元类详解 __new__、__init__、__call__[补充说明]
    Python 元类详解 __new__、__init__、__call__[收官之作]
  • 原文地址:https://www.cnblogs.com/xiaoboxueit/p/13098536.html
Copyright © 2020-2023  润新知