java表达式计算

来源:互联网 发布:产品演示动画制作软件 编辑:程序博客网 时间:2024/05/21 01:43

这两天看数据结构,看到处理表达式结果这部分,书上用的办法是把表达式转化为后缀的,然后用栈计算。算法想明白了,但是一直没有时间实现,先把别人的代码贴上来。

 

class Node{    //o:object    public E o;    //p:priority    public int p;    //l:left,r:right    public Node l,r;        public Node(E o){        this.o = o;    }}class Stack{    private Node cur;    private int size = 0;        public void push(E o){        Node n = new Node(o);        if(cur!=null){            cur.r = n;            n.l = cur;        }        cur = n;        size++;    }        public E pop(){        if(size==0)            return null;        try{            size--;            return (E)cur.o;        }finally{            cur = cur.l;            if(cur!=null)                cur.r = null;        }    }        public int size(){        return size;    }}class Tree{    private Node cur,root,start;    private Stack nodes;    private String s[],result;        public void insert(String s[]){            if(s.length<=2)            return;        int x = Parser.X_INIT;        nodes = new Stack();                //create a root in order to get a start        //and solve the condition of starting with "("        cur = new Node(Parser.operators[0]);        cur.l = new Node("0");        cur.p = Parser.p(Parser.operators[0])+x;        root = cur;        start = root;                for(int i=0;i n = new Node(s[i]);                        //while s is () , increase or decrease its priority            x += s[i].equals("(")?Parser.X_STEP:s[i].equals(")")?-Parser.X_STEP:0;                        if(Parser.isOperator(s[i])){                            n.p = Parser.p(s[i])+x;                                //while this node's priority is less than the previous'                 //then roll back                while(cur.p>n.p)                    rollBack();                                //while this node's priority is bigger than the previous'                 //then connect this to the right child                //and move the origin right child to its left child                if(cur.p0)            rollBack();                //remove the temp start:        //find the node which left child is the temp start        //then connect its left child to the temp start's right child        if(start==root){            root = start.r;        }else{            cur = root;            while(cur.l.l.l!=null)                cur = cur.l;            cur.l = cur.l.r;        }                }        //roll back to the parent tree    private void rollBack(){        Node temp = nodes.pop();        temp.r = cur;        cur = temp;        if(nodes.size()==0)            root = temp;    }        //iterate the tree prefixally    public String prefix(){        result = "";        prefixIterate(root);        return result;    }    private void prefixIterate(Node n){        if(n==null)            return;        result += n.o+" ";        prefixIterate(n.l);        prefixIterate(n.r);    }    //iterate the tree postfixally    public String postfix(){        result = "";        postfixIterate(root);        return result;    }    private void postfixIterate(Node n){        if(n==null)            return;        postfixIterate(n.l);        postfixIterate(n.r);        result += n.o+" ";    }}class Calculator{        //get the result from postfix expression    public static double calculateInPostfix(String s[]){        Stack num = new Stack();        double result = 0;        for(int i=0;i
原创粉丝点击