java表达式计算

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

 

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值