waysun一路阳光

不轻易服输,不轻言放弃.--心是梦的舞台,心有多大,舞台有多大。踏踏实实做事,认认真真做人。

  BlogJava :: 首页 :: 新随笔 :: 联系 ::  :: 管理 ::
  167 随笔 :: 1 文章 :: 64 评论 :: 0 Trackbacks
转自:http://www.blogjava.net/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一米阳光 阅读(174) 评论(0)  编辑  收藏 所属分类: 总结备用

只有注册用户登录后才能发表评论。


网站导航: