下面跟着尚硅谷韩老师敲的将平常的四则运算中缀表达式转换为后缀表达式写的功能简单逆波兰 计算器
package com.atguigu.stack;
import java.security.Key;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* @author shkstart
* @create 2021 -09-25 8:14
*/
public class PolandNotation {
public static void main(String[] args) {
//完成将中缀表达式转换出后缀表达式的代码
String exprission = "1+((2+3)*4)-5";
List<String> InfixExpressionList = toInfixExpressionList(exprission);
System.out.println(InfixExpressionList);
List<String> parseSuffixExpreesionList = parseSuffixExpreesionList(InfixExpressionList);
System.out.println("后缀表达式对应的list" + parseSuffixExpreesionList);
System.out.printf("expression=%d",calculat(parseSuffixExpreesionList));
/*
// String suffixExpression = "30 4 + 5 * 6 -";
String suffixExpression = "4 5 * 8 - 60 + 8 2 / +";
List<String> list = getListString(suffixExpression);
System.out.println("rpnList=" + list);
int res = calculat(list);
System.out.println(" 计算的结果是 =" + res);
*/
}
//核心一:将中缀表达式对应的list转换为后缀表达式对应的list
public static List<String> parseSuffixExpreesionList(List<String> ls){
//定义俩个栈
Stack<String> s1 = new Stack<>();//符号栈
//说明:因为s2这个栈在整个转换过程中没有pop操作,而且后面我们需要逆序操作
//因此比较麻烦,直接使用List<String>来代替
List<String> s2 = new ArrayList<>();//存放中间结果的List s2
for(String item : ls){
//如果是一个数,就加入s2
if(item.matches("\\d+")){
s2.add(item);
}else if(item.equals("(")){
s1.push(item);
}else if(item.equals(")")){
//如果是右括号,则一次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这对括号丢弃
while(!s1.peek().equals("(")){
s2.add(s1.pop());
}
s1.pop();//!!!将( 弹出栈。消除小括号
}else {
//当item的优先级 小于等于 s1栈顶的优先级时,将s1栈顶的运算符弹出并加入s2中,再猜将item与s1的新栈顶相比较
//
while(s1.size() != 0 && Operation.getValue(s1.peek()) >= Operation.getValue(item)){
s2.add(s1.pop());
}
//需要将item压入栈中
s1.push(item);
}
}
while (s1.size() != 0){
s2.add(s1.pop());
}
return s2;//因为是存放到list中,因此按顺序输出就是对应的逆波兰表达式
}
//核心二:将中缀表达式转换为对应的list
public static List<String> toInfixExpressionList(String s){
List<String> ls = new ArrayList<>();
int i = 0;
String str = "";
char c ;
do{
if((c = s.charAt(i)) < 48 || (c = s.charAt(i)) > 57){
ls.add(c + "");
i++;
}else{
while(i < s.length() && (c=s.charAt(i)) >= 48 && (c=s.charAt(i)) <= 57){
str += c;
i++;
}
ls.add(str);
str = "";
}
}while(i < s.length());
return ls;
}
public static List getListString(String str){
String[] str1 = str.split(" ");
List<String> list = new ArrayList<String>();
for(String stri : str1){
list.add(stri);
}
return list;
}
public static int calculat(List<String> is){
Stack<String> stack = new Stack<>();
for(String item : is){
if(item.matches("\\d+")){
stack.push(item);
}else{
int num2 = Integer.parseInt(stack.pop());
int num1 = Integer.parseInt(stack.pop());
int res = 0;
if(item.equals("+")){
res = num1 + num2;
}else if(item.equals("-")){
res = num1 - num2;
}else if(item.equals("*")){
res = num1 * num2;
}else if(item.equals("/")){
res = num1 / num2;
}else {
System.out.println("字符串输入错误");
}
stack.push(res + "");
}
}
return Integer.parseInt(stack.pop());
}
}
//编写一个类Operation,可以返回一个运算符 对应的优先级
class Operation{
private static int ADD = 1;
private static int SUB = 1;
private static int MUL = 2;
private static int DIV = 2;
//写一个方法,返回对应的优先级数字
public static int getValue(String operation){
int result = 0;
switch (operation){
case "+":
result = ADD;
break;
case "-":
result = SUB;
break;
case "*":
result = MUL;
break;
case "/":
result = DIV;
break;
default:
System.out.println("不存在该运算符");
break;
}
return result;
}
}
|