Loading

前缀、中缀、后缀表达式

前缀表达式

  1. 前缀表达式又称波兰式,前缀表达式的运算符位于操作数之前。
  2. 举例:(3+4)*5-6的前缀表达式为 - * + 3 4 5 6

前缀表达式计算机求值

 从右至左扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,用运算符对他们做相应的计算(栈顶元素和次顶元素),并将结果入栈;重复上述过程直到表达式最左端,最后运算得出的值即为表达式的结果。
举例:

求前缀表达式:- * + 3 4 5 6的值:
  1. 从右至左扫描,将6、5、4、3压入堆栈;
  2. 遇到 + 运算符,因此弹出3和4(3为栈顶元素,4为次顶元素),计算 3+4 的值,得7,再将7入栈;
  3. 接下来是 * 运算符,因此弹出7和5,计算 7*5=35 ,将35入栈。
  4. 最后是 - 运算符,计算 35-6 的值,即29,得最终结果29.

中缀表达式

  1. 中缀表达式就是常见得运算表达式,如(3+4)*5-6
  2. 中缀表达式求值是我们人熟悉的,但是对于计算机来说却不好操作,因此,在计算结果时,往往会将中缀表达式转成其他表达式来操作。

后缀表达式

  1. 后缀表达式又称逆波兰表达式,与前缀表达式相似,只是运算符位于操作数之后。
  2. 举例: (3+4)*5-6的后缀表达式为3 4 + 5 * 6 -

后缀表达式计算机求值

从左至右扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,用运算符对它们做相应的计算。并将结果入栈;重复上述过程直到表达式最右端,最后运算出的值即为表达式的结果。

举例: 求 (3+4)*5-6 的后缀表达式 (3 4 + 5 * 6 -)
    1. 从左至右扫描,将3和4入栈;
    2. 遇到运算符 + ,因此弹出4和3,计算 3+4 的值,再将7入栈;
    3. 将5入栈;
    4. 接下来是 * 运算符,因此弹出5和7,计算出 7*5=35,将35入栈;
    5. 将6入栈;
    6. 最后是 - 运算符,计算 35-6 的值,即29,由此得出最终结果。

中缀表达式转换为后缀表达式

  1. 初始化两个栈:运算符栈s1和储存中间结果栈s2;
  2. 从左至右扫描中缀表达式;
  3. 遇到操作数时,将其压入s2;
  4. 遇到运算符时,比较其与s1栈顶运算符的优先级:
    (1)如果s1为空,或栈顶运算符为左括号“(” ,则直接将此运算符入栈;
    (2)否则,若优先级比栈顶运算符高,也将运算符压入是s1;
    (3)否则,将s1栈顶的运算符弹出并压入到s2中,再次转到4.1步骤与s1中新的栈顶运算符相比较;
  5. 遇到括号时:
    (1)如果时左括号“(” ,则直接压入s1;
    (2)如果是右括号“)” ,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这一对括号丢弃;
  6. 重复步骤2至5,直到表达式的最右边;
  7. 将s1中剩余元素依次弹出并压入s2;
  8. 依次弹出s2中的元素并输出,结果的逆序即为中缀表达式对应的后缀表达式。

逆波兰计算器:

目标:可以完成整数加减乘除(包含括号)的计算器。

package com.stack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class SimpleCalculator {
    public static void main(String[] args) {
        String s = "1+(2*3)" +
                "";
        List<String> infix = toInfixExpression(s);
        List<String> postfix = toPostfixExpression(infix);
        System.out.println(calculate(postfix));
    }
    //1. 将字符串转化为中缀表达式,并存入到List中
    public static List<String> toInfixExpression(String str){
        ArrayList<String> infix = new ArrayList<>();
        int i = 0;
        char c;
        String s;
        do{
            if((c = str.charAt(i)) < 48 || (c = str.charAt(i)) >57){
                infix.add(""+c);
                i++;
            }else{
                s = "";
                while(i < str.length() && (c = str.charAt(i)) >= 48 && (c = str.charAt(i)) <= 57){
                    s += c;
                    i++;
                }
                infix.add(s);
            }
        }while(i < str.length());
        return infix;
    }
    //2. 设置运算符优先级
    public static int getPriority(String s){
        final int ADDITION=1;
        final int SUBTRACTION=1;
        final int MULTIPLICATION=2;
        final int DIVISION=2;
        int result;
        switch (s){
            case "+":
                result = ADDITION;
                break;
            case "-":
                result = SUBTRACTION;
                break;
            case "*":
                result = MULTIPLICATION;
                break;
            case "/":
                result = DIVISION;
                break;
            default:
                System.out.println("不存在该运算符!");
                result = -1;
        }
        return result;
    }
    //3. 将中缀表达式转换为后缀表达式,并存入到List中
    public static List<String> toPostfixExpression(List<String> infix){
        Stack<String> s1 = new Stack<>();
        ArrayList<String> postfix = new ArrayList<>();
        for (String s : infix) {
            if(s.matches("\\d+")){
                postfix.add(s);
            }else if(s.equals("(")){
                s1.push(s);
            }else if(s.equals(")")){
                while(!s1.peek().equals("(")){
                    postfix.add(s1.pop());
                }
                s1.pop();
            }else{
                while(s1.size() != 0 && getPriority(s1.peek()) >= getPriority(s)){
                    postfix.add(s1.pop());
                }
                s1.push(s);
            }
        }
        while(s1.size() != 0){
            postfix.add(s1.pop());
        }
        return postfix;
    }
    //4. 计算后缀表达式
    public static int calculate(List<String> postfix){
        Stack<String> stack = new Stack<>();
        for (String s : postfix) {
            if(s.matches("\\d+")){
                stack.push(s);
            }else{
                int a = Integer.parseInt(stack.pop());
                int b = Integer.parseInt(stack.pop());
                int result = 0;
                if(s.equals("+")){
                    result = a + b;
                }else if(s.equals("-")){
                    result = b - a;
                }else if(s.equals("*")){
                    result = a * b;
                }else if(s.equals("/")){
                    result = b / a;
                }else{
                    throw new RuntimeException("运算符异常!");
                }
                stack.push(""+result);
            }
        }
        return Integer.parseInt(stack.peek());

    }
}

posted @ 2021-11-10 17:35  IamHzc  阅读(3967)  评论(0编辑  收藏  举报