一、前缀表达式(波兰表达式)
- 前缀表达式又称波兰式,前缀表达式的运算符位于操作数之前
- 举例说明: (3+4)×5-6 对应的前缀表达式就是 - × + 3 4 5 6
1. 前缀表达式的计算机求值
-
从右至左扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,用运算符对它们做相应的计算(栈顶元素 和 次顶元素),并将结果入栈;重复上述过程直到表达式最左端,最后运算得出的值即为表达式的结果
-
例如: (3+4)×5-6 对应的前缀表达式就是 - × + 3 4 5 6 , 针对前缀表达式求值步骤如下:
- 从右至左扫描,将6、5、4、3压入堆栈
- 遇到+运算符,因此弹出3和4(3为栈顶元素,4为次顶元素),计算出3+4的值,得7,再将7入栈
- 接下来是×运算符,因此弹出7和5,计算出7×5=35,将35入栈
- 最后是-运算符,计算出35-6的值,即29,由此得出最终结果
二、中缀表达式
- 中缀表达式就是常见的运算表达式,如(3+4)×5-6
- 中缀表达式的求值是我们人最熟悉的,但是对计算机来说却不好操作(前面我们讲的案例就能看的这个问题),因此,在计算结果时,往往会将中缀表达式转成其它表达式来操作(一般转成后缀表达式.)
- 下面是用中缀表达式通过栈模拟实现计算器
- 链接: https://blog.csdn.net/cheng87995211/article/details/105799695
居中的图片:
三、后缀表达式
- 后缀表达式又称逆波兰表达式,与前缀表达式相似,只是运算符位于操作数之后
- 举例说明: (3+4)×5-6 对应的后缀表达式就是 3 4 + 5 × 6 –
1. 后缀表达式的计算机求值
- 从左至右扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,用运算符对它们做相应的计算(次顶元素 和 栈顶元素),并将结果入栈;重复上述过程直到表达式最右端,最后运算得出的值即为表达式的结果
- 例如: (3+4)×5-6 对应的后缀表达式就是 3 4 + 5 × 6 -
- 从左至右扫描,将3和4压入堆栈;
- 遇到+运算符,因此弹出4和3(4为栈顶元素,3为次顶元素),计算出3+4的值,得7,再将7入栈;
- 将5入栈;
- 接下来是×运算符,因此弹出5和7,计算出7×5=35,将35入栈;
- 将6入栈;
- 最后是-运算符,计算出35-6的值,即29,由此得出最终结果
四、中缀转后缀表达式
- 初始化两个栈:运算符栈s1和储存中间结果的栈s2;
- 从左至右扫描中缀表达式;
- 遇到操作数时,将其压s2;
- 遇到运算符时,比较其与s1栈顶运算符的优先级:
- 如果s1为空,或栈顶运算符为左括号“(”,则直接将此运算符入栈;
- 否则,若优先级比栈顶运算符的高,也将运算符压入s1;
- 否则,将s1栈顶的运算符弹出并压入到s2中,再次转到(4-1)与s1中新的栈顶运算符相比较;
- 代码实现
public class InfixToSuffix {
//测试
public static void main(String[] args) {
String expression = "6 * ( ( 3 + 6 ) * ( ( 1 + 2 ) + ( 3 + 2 ) ) )";
// 6 3 6 + 1 2 + *+ 6 + ( 3 + 6 ) * ( 1 + 2 )
List<String> list = getList(expression);
String calculate = calculate(list);
System.out.println(calculate);
}
//把所有元素放到一个集合
public static List<String> getList(String expression){
String[] split = expression.split(" ");
ArrayList<String> list = new ArrayList<>();
for (String ele:split){
list.add(ele);
}
return list;
}
//转换方法
public static String calculate(List<String> list) {
//存放符号
Stack s1 = new Stack<String>();
//储存中间结果
Stack s2 = new Stack<String>();
for (String ele : list) {//遍历数组
//如果是数字
if (ele.matches("\\d+")) {
s2.push(ele);
} else {
if (s1.empty()){
//如果为空直接入栈
s1.push(ele);
}else if(ele.equals("(")){
//如果为"("直接入栈
s1.push(ele);
}else if(s1.peek().equals("(")){
//如果栈顶为"("直接入栈
s1.push(ele);
} else if(priority(ele)>priority((String) s1.peek())){
//如果优先级大于栈顶优先级
s1.push(ele);
}else if(ele.equals(")")){
//循环将符号在s1取出压入s2
while (true){
String pop = (String) s1.pop();
if (pop.equals("(")){
break;
}else {
s2.push(pop);
}
}
}else{
//如果都不满足就取出s1符号push到s2,直到满足条件再将符号push到s1
while(true) {
if (s1.empty()||s1.peek().equals("(")||priority(ele)>priority((String) s1.peek())){
s1.push(ele);
break;
}else{
String pop = (String) s1.pop();
s2.push(pop);
}
}
}
}
}
if (!s1.empty()){
while (true){
String pop = (String) s1.pop();
s2.push(pop);
if (s1.empty()){
break;
}
}
}
if (!s2.empty()){
while (true){
String pop = (String) s2.pop();
s1.push(pop);
if (s2.empty()){
break;
}
}
}
String value = "";
//取出s1元拼接,就是后缀表达式
while(true){
value += s1.pop()+" ";
if (s1.empty()){
break;
}
}
return value;
}
//判断优先级
public static int priority(String str) {
if (str.equals("*") || str.equals("/")) {
return 1;
} else if (str.equals("+") || str.equals("-")) {
return 0;
} else {
return -1;
}
}
}
五、逆波兰计算器
- 输入一个逆波兰表达式(后缀表达式),使用栈(Stack), 计算其结果
- 支持小括号和多位数整数,因为这里我们主要讲的是数据结构,因此计算器进行简化,只支持对整数的计算。
- 代码实现
public class PolanNotation {
public static void main(String[] args) {
String expression = "6 3 6 + 1 2 + 3 2 + + * *";
List<String> list = getList(expression);
int calculate = calculate(list);
System.out.println(calculate);
}
//获取每一个元放到集合
public static List<String> getList(String expression){
String[] split = expression.split(" ");
ArrayList<String> list = new ArrayList<>();
for (String ele:split){
list.add(ele);
}
return list;
}
public static int calculate(List<String> list){
Stack<String> stack = new Stack<String>();
int res = 0;
for (String ele:list){
if (ele.matches("\\d+")){
//如果是数字直接push
stack.push(ele);
}else{
//反之进行计算
int num2 = Integer.parseInt(stack.pop());
int num1 = Integer.parseInt(stack.pop());
if (ele.equals("-")){
res = num1-num2;
}else if (ele.equals("+")){
res = num1+num2;
}else if (ele.equals("*")){
res = num1*num2;
}else if (ele.equals("/")){
res = num1/num2;
}
stack.push(String.valueOf(res));
}
}
return Integer.parseInt(stack.pop());
}
}