后缀表达式

public class ReversePolishNotation {
public static void main(String[] args) {
//中缀表达式3*(17-15)+18/6的逆波兰表达式如下
String[] notation = {"3", "17", "15", "-", "*", "18", "6", "/", "+"};
int result = caculate(notation);
System.out.println("逆波兰表达式的结果为:" + result);
}
/**
* @param notaion 逆波兰表达式的数组表示方式
* @return 逆波兰表达式的计算结果
*/
public static int caculate(String[] notaion) {
//1.创建一个栈对象oprands存储操作数
Stack<Integer> oprands = new Stack<>();
//2.从左往右遍历逆波兰表达式,得到每一个字符串
for (int i = 0; i < notaion.length; i++) {
String curr = notaion[i];
//3.判断该字符串是不是运算符,如果不是,把该该操作数压入oprands栈中
Integer o1;
Integer o2;
Integer result;
switch (curr) {
case "+":
//4.如果是运算符,则从oprands栈中弹出两个操作数o1,o2
o1 = oprands.pop();
o2 = oprands.pop();
//5.使用该运算符计算o1和o2,得到结果result
result = o2 + o1;
//6.把该结果压入oprands栈中
oprands.push(result);
break;
case "-":
//4.如果是运算符,则从oprands栈中弹出两个操作数o1,o2
o1 = oprands.pop();
o2 = oprands.pop();
//5.使用该运算符计算o1和o2,得到结果result
result = o2 - o1;
//6.把该结果压入oprands栈中
oprands.push(result);
break;
case "*":
//4.如果是运算符,则从oprands栈中弹出两个操作数o1,o2
o1 = oprands.pop();
o2 = oprands.pop();
//5.使用该运算符计算o1和o2,得到结果result
result = o2 * o1;
//6.把该结果压入oprands栈中
oprands.push(result);
break;
case "/":
//4.如果是运算符,则从oprands栈中弹出两个操作数o1,o2
o1 = oprands.pop();
o2 = oprands.pop();
//5.使用该运算符计算o1和o2,得到结果result
result = o2 / o1;
//6.把该结果压入oprands栈中
oprands.push(result);
break;
default:
oprands.push(Integer.parseInt(curr));
break;
}
}
//7.遍历结束后,拿出栈中最终的结果返回
Integer result = oprands.pop();
return result;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值