zoukankan      html  css  js  c++  java
  • 前缀表达式求值 (支持多位数和负数)

    1.运行过程

      1. 输入

        输入一个前缀表达式字符串,每个元素用一个空格隔开

      2.前序遍历方式构造表达式二叉树

      3.后序遍历得到后缀后求值。

    2.代码如下

    import java.util.*;
    public class MakeBinaryTree {
    
        public static int i=0;
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner is=new Scanner(System.in);
            while(is.hasNext()) {
                i=0;
                String target=is.nextLine();
                TreeNode<String> exp=null;
                String[] c=target.split(" ");
                exp=preMake(exp,c);
                StringBuilder sb=new StringBuilder();
                toPostExp(exp,sb);
                System.out.println(calcuPost(sb.toString()));
            }
            is.close();
        }
        
        
        
        private static void toInExp(TreeNode<String> root,StringBuilder sb) {
            if(root!=null) {
                toInExp(root.left,sb);
                sb.append(root);
                toInExp(root.right,sb);
            }
        }
        
        private static void toPostExp(TreeNode<String> root,StringBuilder sb) {
            if(root!=null) {
                toPostExp(root.left,sb);
                toPostExp(root.right,sb);
                sb.append(root+" ");
            }
        }
        
        private static int calcuPost(String post) {
            Stack<Integer> stk=new Stack<>();
            String[] ss=post.split(" ");
            for(String s:ss) {
                if(s.matches("-?\d+")) {
                    int a=Integer.parseInt(s);
                    stk.push(a);
                }else {
                    char c=s.charAt(0);
                    int b=stk.pop();
                    int a=stk.pop();
                    int t;
                    if(c=='+')
                        t=a+b;
                    else if(c=='-')
                        t=a-b;
                    else if(c=='*')
                        t=a*b;
                    else
                        t=a/b;
                    stk.push(t);
                }
            }
            return stk.pop();
        }
        private static TreeNode<String> preMake(TreeNode<String> root,String[] c) {
            if(i==c.length)
                return null;
            if(c[i].matches("-?\d+"))
                return new TreeNode<String>(c[i++]);
            else {
                root=new TreeNode<String>(c[i++]);
                root.left=preMake(root.left,c);
                root.right=preMake(root.right,c);
                return root;
            }
        }
        
    }
    class TreeNode<E>{
        E element;
        TreeNode<E> left;
        TreeNode<E> right;
        public TreeNode(E e) {
            this.element=e;
        }
        public String toString() {
            return element.toString();
        }
    }
  • 相关阅读:
    MySQL监控、性能分析——工具篇
    [转载]Error -27796: Failed to connect to server
    Tomcat最大连接数问题
    Jconsole的使用
    通过jconsole监控tomcat JVM 内存、线程、CPU
    Tomcat部署web项目
    tomcat部署web项目的3中方法
    在linux下修改oracle的sys和system的密码和用户解锁
    静默安装oracle11G
    Linux 卸载Oracle 11G
  • 原文地址:https://www.cnblogs.com/lshao/p/8612979.html
Copyright © 2011-2022 走看看