waysun一路陽光

          不輕易服輸,不輕言放棄.--心是夢的舞臺,心有多大,舞臺有多大。踏踏實實做事,認認真真做人。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
            167 隨筆 :: 1 文章 :: 64 評論 :: 0 Trackbacks
          轉自:http://www.aygfsteel.com/javacap/archive/2007/10/09/151566.html
          /**
           * 
           
          */
          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)循環,當棧非空,并且棧頂元素不是開括號,并且棧頂運算符優先級不低于輸入的運算符的優先級,反復將其輸出
               *     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 2009-04-15 22:05 weesun一米陽光 閱讀(195) 評論(0)  編輯  收藏 所屬分類: 總結備用
          主站蜘蛛池模板: 永春县| 彩票| 黄石市| 阿坝县| 酒泉市| 禹州市| 夹江县| 屯留县| 喀喇沁旗| 客服| 泽州县| 新源县| 广东省| 定襄县| 万源市| 磴口县| 乐至县| 开化县| 米脂县| 海宁市| 若尔盖县| 塔河县| 丰宁| 额尔古纳市| 乐亭县| 偃师市| 平武县| 库车县| 天津市| 阜新市| 大石桥市| 安丘市| 万安县| 凤凰县| 佛山市| 洛宁县| 化德县| 呼图壁县| 南华县| 胶州市| 大同市|