java实现中缀表达式转后缀表达式并输出运算结果

原则:数字输出,运算符进栈,括号匹配出栈,栈顶优先级低不出栈

public class postfix {

    public static String[] outPostfix(String[] infix){
        String[] postfix = new String[100];
        int index = 0;
        int bracket = 0; //标记括号的对数
        Map operatorMap = new HashMap<>(); // 用hashmap存储每个运算符的优先度
        operatorMap.put("+", 1);
        operatorMap.put("-", 1);
        operatorMap.put("*", 2);
        operatorMap.put("/", 2);
        Stack<String> postfixStack = new Stack<>(); //用于存储后缀算法的运算符
        for (int i = 0; i < infix.length && infix[i] != null; i++) {
            String current = infix[i];
            if (current.charAt(0) >= '0' && current.charAt(0) <= '9'){ //遇到数字直接放入后缀数组
                postfix[index++] = current;
            }else {
                if (postfixStack.isEmpty()){ //保证后缀运算符栈不为空
                    postfixStack.push(current);
                }
                /* *
                *如果括号已经全部处理,而且当前字符不是括号,
                *后缀运算符栈顶不是括号,并且栈顶运算符优先度小于当前运算符优先度,
                *将当前遍历运算符后边的数字放入后缀数组中,再将当前运算符放入后缀数组中。
                */
                else if (bracket == 0 && !isBracket(current) &&
                        !isBracket(postfixStack.peek()) &&
                        Integer.parseInt(String.valueOf(operatorMap.get(postfixStack.peek()))) < Integer.parseInt(String.valueOf(operatorMap.get(current)))){
                    postfix[index++] = infix[i + 1];
                    postfix[index++] = infix[i];
                    i++;
                }
                /**
                 * 同理 栈顶运算符优先度大于等于当前运算符优先度,
                 *将栈顶运算符放入后缀数组中,再将当前遍历的运算符放入栈中
                 */
                else if (bracket == 0 && !isBracket(current) &&
                        !isBracket(postfixStack.peek()) &&
                        Integer.parseInt(String.valueOf(operatorMap.get(postfixStack.peek()))) >= Integer.parseInt(String.valueOf(operatorMap.get(current)))){
                    postfix[index++] = postfixStack.pop();
                    postfixStack.push(current);
                }
                else {
                    if (current.equals("(")) bracket++; // 若遇到左括号,bracket加1
                    postfixStack.push(current);
                    if (postfixStack.peek().equals(")")){ //遇到右括号,bracket减1,同时将栈中括号间的运算符出栈
                        bracket--;
                        postfixStack.pop();
                        while (!postfixStack.peek().equals("(")){
                            postfix[index++] = postfixStack.pop();
                        }
                        postfixStack.pop();
                    }
                }
            }
        }
        while (!postfixStack.isEmpty()) postfix[index++] = postfixStack.pop();
        return postfix;
    }
    public static double result(String[] postfix){
        double result = 0;
        Stack<Double> computeStack = new Stack();
        for (int i = 0; i < postfix.length && postfix[i] != null; i++) {
            String current = postfix[i];
            //当前字符为数字,则入栈
            if (current.charAt(0) >= '0' && current.charAt(0) <= '9'){
                computeStack.push(todouble(current));
            }
            //当前字符为运算符,从栈顶取出两个数字运算出结果,并放入栈中
            else {
                switch (current){
                    case "+": result = computeStack.pop() + computeStack.pop();
                    computeStack.push(result);break;
                    case "-": result = -(computeStack.pop() - computeStack.pop());
                    computeStack.push(result);break;
                    case "*": result = computeStack.pop() * computeStack.pop();
                    computeStack.push(result);break;
                    case "/": result = 1 / (computeStack.pop() / computeStack.pop());
                    computeStack.push(result);break;
                    default: break;
                }
            }
        }
        return result;
    }
    public static double todouble(String str){
        double num = 0;
        num = Double.valueOf(Integer.parseInt(str));
        return num;
    }
    public static boolean isBracket(String str){
        if (str.equals("(") || str.equals(")")) return true;
        else return false;
    }
    public static void main(String[] args){
        String[] infix = new String[]{"9", "+", "(", "3",  "-", "1", ")", "*", "3", "+", "10", "/", "2"};
        String[] infix1 = new String[100];
        int i = 0;
        Scanner in=new Scanner(System.in);
        while (in.hasNext()){
            infix1[i++] = in.nextLine();
        }
        in.close();
        infix = outPostfix(infix1);
        for (i = 0; i < infix.length && infix[i] != null; i++) {
            System.out.print(infix[i] + " ");
        }
        System.out.print("\n");
        System.out.println(result(infix));
    }
}

输入:
2
*
(
3
+
3
*
(
5
+
4
)

)

后缀表达式:

2 3 3 5 4 + * + * 

结果:60.0
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值