<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    E81086713E446D36F62B2AA2A3502B5EB155

    Java雜家

    雜七雜八。。。一家之言

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      40 Posts :: 1 Stories :: 174 Comments :: 0 Trackbacks
    /**
     * 
     
    */
    package com.yovn.algo;

    import java.util.Stack;
    import java.util.Vector;

    /**
     * 
    @author yovn
     *
     
    */
    public class Caculator {

        
        
    class Item
        {
            
    boolean ops;
            
    int value;
            
            Character opVal;
            
    int opPriority;
        }
        
        Stack
    <Item> opStack=new Stack<Item>();
        Vector
    <Item> calcStack=new Vector<Item>();
        
    /**
         * 
         
    */
        
    public Caculator() {
            
    // TODO Auto-generated constructor stub
        }
        
        
        
        
        
    public int calc()
        {
            Stack
    <Item> tmp=new Stack<Item>();
            
    while(!calcStack.isEmpty())
            {
                Item it
    =calcStack.remove(0);
                
                
    if(!it.ops)
                {
                    tmp.push(it);
                }
                
    else
                {
                    
    int op2=tmp.pop().value;
                    
    int op1=tmp.pop().value;
                    Item newItem
    =new Item();
                    newItem.ops
    =true;
                    
    switch(it.opVal)
                    {
                    
    case '+':
                        newItem.value
    =op1+op2;
                        
                        
    break;
                    
    case '-':
                        newItem.value
    =op1-op2;
                        
    break;
                    
    case '*':
                        
                        newItem.value
    =op1*op2;
                        
    break;
                    
    case '/':
                        newItem.value
    =op1/op2;
                        
    break;
                    }
                    tmp.push(newItem);
                }
            }
            
    return tmp.pop().value;
        }
        
    /**
         * 1)數字直接輸出
         * 2)開括號則壓棧
         * 3)閉括號把棧中元素依次輸出直到遇到開括號
         * 4)運算符時
         *     a)循環(huán),當棧非空,并且棧頂元素不是開括號,并且棧頂運算符優(yōu)先級不低于輸入的運算符的優(yōu)先級,反復將其輸出
         *     b)把輸入運算符壓棧
         * 5)輸出棧內剩余元素
         * 
    @param in
         * 
    @return
         
    */
        
    public String transInfixToPosfix(String in)
        {
            
    char[] cin=in.toCharArray();
            StringBuffer buffer
    =new StringBuffer();
           
            
    for(int i=0;i<cin.length;i++)
            {
                Item newItem
    =new Item();
                newItem.opPriority
    =1;
                newItem.ops
    =false;
                
                
    switch(cin[i])
                {
                
                
    case '+':
                    newItem.opPriority
    =1;
                    newItem.ops
    =true;
                    newItem.opVal
    ='+';
                    doOps(buffer, newItem);
                    
                    
    break;
                
    case '-':
                    newItem.opPriority
    =1;
                    newItem.ops
    =true;
                    newItem.opVal
    ='-';
                    doOps(buffer, newItem);
                    
    break;
                
    case '*':
                    newItem.opPriority
    =2;
                    newItem.ops
    =true;
                    newItem.opVal
    ='*';
                    doOps(buffer, newItem);
                    
    break;
                
    case '/':
                    newItem.opPriority
    =2;
                    newItem.ops
    =true;
                    newItem.opVal
    ='/';
                    doOps(buffer, newItem);
                    
    break;
                    
                
    case '(':
                    newItem.ops
    =true;
                    newItem.opVal
    ='(';
                    opStack.push(newItem);
                    
                    
    break;
                
    case ')':
                    
    boolean meetClose=false;
                    
    while(!opStack.isEmpty())
                    {
                        Item item
    =opStack.peek();
                        
    if(item.ops&&item.opVal!='(')
                        {
                            calcStack.add(item);
                            opStack.pop();
                            buffer.append(item.opVal);
                        }
                        
    else if(item.ops)
                        {
                            opStack.pop();
                            meetClose
    =true;
                            
    break;
                        }
                    }
                    
    if(!meetClose)
                    {
                        
    throw new RuntimeException();
                    }
                    
    break;
                    
                
    default:
                    
    int j=i;
                    
    for(;j<cin.length&&cin[j]>='0'&&cin[j]<='9';j++);
                    
    if(j==i)
                    {
                        
    throw new RuntimeException("wrong input.");
                    }
                    newItem.ops
    =false;
                    newItem.value
    =Integer.parseInt(new String(cin,i,j-i));
                    buffer.append(newItem.value);
                    calcStack.add(newItem);
                    i
    =j-1;
                    
    break;
                    
                    
                }
            }
            
    while(!opStack.isEmpty())
            {
                Item item
    =opStack.pop();
                calcStack.add(item);
                buffer.append(item.opVal);
                
            }
            
    return buffer.toString();
            
        }



        
    private void doOps(StringBuffer buffer, Item newItem) {
            
    while(!opStack.isEmpty())
            {
                Item item
    =opStack.peek();
                
    if(item.opVal!='('&&item.opPriority>=newItem.opPriority)
                {
                    calcStack.add(item);
                    opStack.pop();
                    buffer.append(item.opVal);
                }
                
    else
                {
                    
    break;
                }
            }
            opStack.push(newItem);
        }
        

        
    /**
         * 
    @param args
         
    */
        
    public static void main(String[] args) {
            Caculator calc
    =new Caculator();
            
            System.out.println(
    "1+2*3+7-(4/2+8)/5="+calc.transInfixToPosfix("1+2*3+7-(4/2+8)/5"));
            System.out.println(
    "value is:"+calc.calc());

        }

    }
    posted on 2007-10-09 22:48 DoubleH 閱讀(997) 評論(1)  編輯  收藏 所屬分類: Memorandum

    Feedback

    # re: 表達式求值Java粗糙版 2007-12-20 21:45 sitinspring
    以后要多來看看.  回復  更多評論
      

    主站蜘蛛池模板: 校园亚洲春色另类小说合集 | 一个人看的免费观看日本视频www| 最刺激黄a大片免费网站| 亚洲精品国产精品乱码不卡√ | 亚洲天堂免费在线| 亚洲日产2021三区在线| aⅴ在线免费观看| 亚洲噜噜噜噜噜影院在线播放| 亚洲美女视频免费| 亚洲 欧洲 日韩 综合在线| 午夜一区二区免费视频| 色五月五月丁香亚洲综合网| 四虎在线播放免费永久视频 | 亚洲国产成人无码AV在线| 国产精品国产午夜免费福利看| 美女尿口扒开图片免费| 2022中文字字幕久亚洲| 日本免费久久久久久久网站| 亚洲国产精品成人精品软件| 香蕉视频在线观看免费国产婷婷| 污污视频免费观看网站| 亚洲精品无码不卡在线播HE| 最近中文字幕电影大全免费版| 亚洲精品国产精品国自产网站| 免费一级做a爰片久久毛片潮喷| fc2免费人成在线| 中文字幕在线观看亚洲| 性xxxx视频播放免费| selaoban在线视频免费精品| 亚洲人成影院在线| 青青草国产免费久久久下载| jizz免费在线影视观看网站| 久久亚洲AV成人无码| 国产国产人免费人成免费视频 | 亚洲AV永久无码精品网站在线观看 | 久久久久亚洲av无码专区导航| 成人黄动漫画免费网站视频| xxxxxx日本处大片免费看| 亚洲精品视频免费在线观看| 爱情岛论坛网亚洲品质自拍| 久久免费看黄a级毛片|