package Stack;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* 将中缀表达式转后缀表达式,并计算其结果
* @Author: HML
* @Date: 2021/7/2 下午 3:37
* @Version 1.0
*/
public class PolandNotation2 {
public static void main(String[] args) {
String express = "1+((2+3)*4)*1-5";
List<String> strings = toList(express);
System.out.println(strings);
List<String> strings1 = parseSuffixExpression(strings);
System.out.println(strings1);
int calculate = calculate(strings1);
System.out.println(calculate);
}
/**
* 将中缀表达式转为后缀表达式
* @param express
* @return
*/
public static List<String> parseSuffixExpression(List<String> express){
//这里根据前面的理论分析得到有一个栈S2这个栈是中间栈,只是将元素压入栈,并没有将元素出栈,故这里我们可以使用ArrayList来替代栈
//这里我们仅需一个栈即可
Stack<String> S2 = new Stack<>();
//用来存储运算符
ArrayList<String> S1 = new ArrayList<String>();
//遍历传进来的express
for (String item :express){
//数字的处理方式
if (item.matches("\\d+")){
S1.add(item);
}else if (item.equals("(")){//如果是左括号的话,也是直接入栈
S2.push(item);
}else if(item.equals(")")){
//右括号时,这里的操作如下:
// 依次弹出S2中的运算符,并将其压入S1中,直至遇到(时,停止,同时将这一对括号去除
while(!S2.peek().equals("(")){
S1.add(S2.pop());
}
//退出while后,此时,我们将(也出栈,这里要值得注意的是while循环到(时他只是停止了,并没有将(出栈,故我们需要手动将其出栈
S2.pop();
}else{
//这里是运算符了
/**
* 1. 当item的优先级小于等于栈顶的运算符优先级时,将S2中的运算符弹出加入到S1中,
* 2. 再次转到上面的步骤去判断
*/
//这里我们怎么去比较优先级呢?我们在这里应该再写一个比较优先级的方法
while (S2.size() !=0 && GetValue(S2.peek()) >= GetValue(item)){
S1.add(S2.pop());
}
//将item压入栈
S2.push(item);
}
}
//最后将战中的运算符依次弹出并压入list
//遍历栈
while (S2.size() != 0){
S1.add(S2.pop());
}
return S1;
}
//将中缀表达式转换
public static List<String> toList(String express){
ArrayList<String> list = new ArrayList<>();
//遍历这个窜进来的值
int i = 0;//指针,用于遍历这个 中缀表到时
String str;//用于多位数拼接
char ch;//字符串的查找
do {
//若express是一个非数字,就是运算符,我们将其放入ch
if ((ch = express.charAt(i)) <48 || (ch = express.charAt(i))>57){
list.add(""+ch);
i++;
}else{
//这里就是一个数了,需考虑多位数的情况
str = "";
while (i < express.length() && (ch = express.charAt(i)) >= 48 && ((ch = express.charAt(i)) <= 57)){
str += ch;
i++;
}
list.add(str);
}
}while (i<express.length());
return list;
}
//完成对逆波兰表达式的计算
//从左至右扫描,将3,4 压栈
//遇到+,弹出4,3 ,计算4+3,并将结果入栈
//将5入栈
//遇到*,将5,7出栈进行计算,并将35压入栈
//遇到-号,计算35-6,并将29返回即可
//这里计算应该是拿到上面list的值去计算
public static int calculate(List<String> list){
//创建一个栈
Stack<String> stack = new Stack<String>();
//遍历list
for (String item:list){
//这里考虑如何判断是运算符还是数字,故这里使用正则表达式去求值
if (item.matches("\\d+")){//这里匹配的是多位数
//数字直接入栈
//System.out.println(item);
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("没有该运算符~~~");
throw new RuntimeException("没有该运算符~~~");
}
//将res入栈
stack.push(""+res);
}
}
//将最后的运算结果出栈
// System.out.println("最后的运算结果="+Integer.parseInt(stack.pop()));//这里要打印的话下一步返回会报错,因为这里已经执行了出栈操作,故这里我们不用打印它,
//把它留在主函数中进行打印即可
return Integer.parseInt(stack.pop());
}
public static int GetValue(String oper){
int ADD = 1;
int SUB = 1;
int MUL = 2;
int DIV = 2;
int result=0;
switch (oper){
case "+":
result = ADD;
break;
case "-":
result = SUB;
break;
case "*":
result = MUL;
break;
case "/":
result = DIV;
break;
default:
//System.out.println("不存在运算符");
break;
}
return result;
}
}
中缀表达式转后缀学习笔记
最新推荐文章于 2024-07-18 13:20:33 发布