E81086713E446D36F62B2AA2A3502B5EB155

          Java雜家

          雜七雜八。。。一家之言

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            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)數(shù)字直接輸出
               * 2)開括號則壓棧
               * 3)閉括號把棧中元素依次輸出直到遇到開括號
               * 4)運算符時
               *     a)循環(huán),當(dāng)棧非空,并且棧頂元素不是開括號,并且棧頂運算符優(yōu)先級不低于輸入的運算符的優(yōu)先級,反復(fù)將其輸出
               *     b)把輸入運算符壓棧
               * 5)輸出棧內(nèi)剩余元素
               * 
          @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 閱讀(1007) 評論(1)  編輯  收藏 所屬分類: Memorandum

          Feedback

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

          主站蜘蛛池模板: 金阳县| 镇坪县| 庄浪县| 涿鹿县| 安国市| 保山市| 民县| 中阳县| 增城市| 马关县| 鸡泽县| 额济纳旗| 双桥区| 五家渠市| 武穴市| 鲁甸县| 孟津县| 延吉市| 新乡县| 新邵县| 庆安县| 阳曲县| 白河县| 天门市| 卢湾区| 曲靖市| 长岛县| 顺昌县| 秀山| 新郑市| 柏乡县| 武鸣县| 文水县| 沁阳市| 江达县| 平湖市| 梓潼县| 平顺县| 东宁县| 三门峡市| 壶关县|