E81086713E446D36F62B2AA2A3502B5EB155

Java杂家

杂七杂八。。。一家之言

BlogJava 首页 新随笔 联系 聚合 管理
  141 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)循环,当栈非空,并且栈顶元素不是开括号,并且栈顶运算符优先级不低于输入的运算符的优先级,反复将其输出
     *     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 阅读(977) 评论(1)  编辑  收藏 所属分类: Memorandum

Feedback

# re: 表达式求值Java粗糙版 2007-12-20 21:45 sitinspring
以后要多来看看.  回复  更多评论
  


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


网站导航: