逆波兰表达式

原文:https://blog.csdn.net/linraise/article/details/20459751

算法:中缀表达式转换成后缀表达式
输入:中缀表达式串
输出:后缀表达式串

PROCESS BEGIN:
1.从左往右扫描中缀表达式串s,对于每一个操作数或操作符,执行以下操作;
2.IF (扫描到的s[i]是操作数DATA)
      将s[i]添加到输出串中;
3.IF (扫描到的s[i]是开括号’(’)
将s[i]压栈;
4.WHILE (扫描到的s[i]是操作符OP)
IF (栈为空 或 栈顶为’(’ 或 扫描到的操作符优先级比栈顶操作符高)
将s[i]压栈;
BREAK;
ELSE
出栈至输出串中
5.IF (扫描到的s[i]是闭括号’)’)
栈中运算符逐个出栈并输出,直到遇到开括号’(’;
开括号’('出栈并丢弃;
6.返回第1.步
   7.WHILE (扫描结束而栈中还有操作符)
操作符出栈并加到输出串中
PROCESS END

以下为自己的实现

package cn.hozi.arithmetic.Stack;

import java.util.PriorityQueue;
import java.util.Stack;

/**
 * 逆波兰表达式
 *
 */
public class Reverse_Polish_notation {
    public static void main(String[] args) {
        Reverse_Polish_notation r = new Reverse_Polish_notation();
        String s = r.reverse_Polish_notation("1-3+4");
        System.out.println("逆波兰表达式:"+s);
        System.out.println("计算结果:"+r.calculation_Reverse_Polish_notation(s));

    }

    /**
     * 中缀转换到逆波兰表达式
     * @param infix 中缀表达式
     * @return 后缀表达式
     */
    public String reverse_Polish_notation(String infix){
        StringBuilder sb = new StringBuilder();
        Stack<Character> stack = new Stack<>();
        for(int i =0;i<infix.length();i++){
            char c = infix.charAt(i);
            if(c-'0'<=9 && c-'0'>=0){
                sb.append(c);
            }else if(c == '('){
                stack.add(c);
            }else if(c==')'){
                //栈中运算符逐个出栈并输出,直到遇到开括号'(';
                //开括号'('出栈并丢弃;
                while(true){
                    char c1 = stack.pop();
                    if(c1 == '('){
                        break;
                    }
                    sb.append(c1);
                }
            }else {
                //碰到操作符
                while (true) {
                    if(stack.size()==0){
                        stack.add(c);
                        break;
                    }
                    char c1 = stack.peek();
                    if ( c1 == '(' || judgeOperator(c, c1)) {
                        stack.add(c);
                        break;
                    }else {
                        sb.append(stack.pop());
                    }
                }
            }
        }
        while (true){
            if (stack.size()<=0){
                break;
            }else {
                sb.append(stack.pop());
            }
        }
        return sb.toString();
    }

    /**
     * 判断操作符的优先级
     * @param c 扫描的操作符
     * @param c1 栈中操作符
     * @return
     */
    private boolean judgeOperator(char c, char c1) {
        if(c =='*'|c=='/'){
            return true;
        }else{
            return false;
        }
    }

    public int calculation_Reverse_Polish_notation(String reverse_Polish_notation){
        Stack<Integer> stack = new Stack<>();
        for(int i =0;i<reverse_Polish_notation.length();i++){
            char c = reverse_Polish_notation.charAt(i);
            String string = String.valueOf(c);
            if(c-'0'<=9 && c-'0'>=0){
                //操作数
                stack.add(Integer.parseInt(string));
            }else{
                //操作符
                //弹两个并计算
                int num1 = stack.pop();
                int num2 = stack.pop();
                if(c=='+'){
                    int total = num1+num2;
                    stack.add(total);
                }else if(c=='-'){
                    int total = num2-num1;
                    stack.add(total);
                }else if(c=='*'){
                    int total = num2*num1;
                    stack.add(total);
                }else if(c=='/'){
                    int total = num2/num1;
                    stack.add(total);
                }
            }
        }
        return stack.pop();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值