中缀表达式转后缀表达式并计算

中缀表达式转后缀表达式:

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

计算后缀表达式:

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

public class SuffixTest {
    public static void main(String[] args) {
        String exp = "10+(2+3)*4-15";
        List list = toList(exp);
        System.out.println(list);
        String suffix = toSuffix(list);
        System.out.println(suffix);
        int cal = cal(suffix);
        System.out.println(cal);
    }

    //把中缀表达式放入List
    public static List<String> toList(String str){
        List<String> list = new ArrayList<>();

        for (int i = 0; i < str.length();i++){
            StringBuffer temp = new StringBuffer();
            temp.append(str.charAt(i));
            if(str.charAt(i) >= 48 && str.charAt(i) <= 57){
                while (i < (str.length() - 1) && str.charAt(i + 1) >= 48 && str.charAt(i + 1) <= 57){
                    temp.append(str.charAt(++i));
                }
            }
            list.add(temp.toString());
        }

        return list;
    }
    
    //得到后缀表达式
    public static String toSuffix(List<String> list){
        Stack<String> res = new Stack();
        Stack<String> oper = new Stack();

        for (String str : list){
              if (str.matches("\\d+")){
                  res.push(str);
                  continue;
              }

              if (str.equals("(")){
                  oper.push(str);
                  continue;
              }

              if (str.equals(")")){
                  while (!oper.peek().equals("(")){
                      String temp = oper.pop();
                      res.push(temp);
                  }
                  oper.pop();
                  continue;
              }

              if (oper.size() == 0 || oper.peek() == "(" || getPrioty(str) > getPrioty(oper.peek())){
                  oper.push(str);
              }else{
                  while (oper.size() != 0 && getPrioty(str) <= getPrioty(oper.peek())){
                      String temp = oper.pop();
                      res.push(temp);
                  }
                  oper.push(str);
              }
        }

        while (oper.size() != 0){
            String temp = oper.pop();
            res.push(temp);
        }

        String str = "";
        while(res.size() != 0){
            str = res.pop() + " " + str;
        }

        return str;
    }

    //得到运算符的优先级
    public static int getPrioty(String str){
        if (str.equals("+") || str.equals("-")){
            return 1;
        }

        if (str.equals("*") || str.equals("/")){
            return 2;
        }
        return -1;
    }


    //计算后缀表达式
    public static int cal(String s){
        List<Integer> nums = new ArrayList<>();
        Stack<Integer> stack = new Stack<>();
        String[] sp = s.split(" ");

        for (String str : sp){
            if (str.matches("\\d+")){
                stack.push(Integer.parseInt(str));
            }else{
                int num1 = stack.pop();
                int num2 = stack.pop();
                int res = 0;
                if (str.equals("+")){
                    res = num1 + num2;
                }else if (str.equals("-")){
                    res = num2 - num1;
                }else if (str.equals("*")){
                    res = num1 * num2;
                }else if (str.equals("/")){
                    res = num2 / num1;
                }
                stack.push(res);
            }
        }
        return stack.pop();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值