`
cary
  • 浏览: 84272 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

逆波兰式计算

    博客分类:
  • J2EE
阅读更多
下面的逆波兰式,不支持单目运算,以及{}、〔〕这两类的括号运算。对精度提供了设置,根据你输入的浮点数的小数位的位数来适应,返回和操作数相同位数的结果。由于写的匆忙,好存在很多缺陷。不足的地方希望能给出批评。
java 代码
 
  1. 1.stack.java  
  2.   
  3. import java.util.LinkedList;  
  4.   
  5. /** 
  6.  * @author 
  7.  * 
  8.  * @2007-2-8 
  9.  * @to-Email 
  10.  */  
  11. public class Stack {  
  12.   
  13.     private LinkedList stack = new LinkedList();  
  14.     private int top = -1;  
  15.      
  16.     public void push(Object obj)  
  17.     {  
  18.         top++;  
  19.         stack.addFirst(obj);  
  20.     }  
  21.      
  22.     public Object pop()  
  23.     {  
  24.         Object obj = stack.getFirst();  
  25.         top--;  
  26.         stack.removeFirst();  
  27.         return obj;  
  28.     }  
  29.      
  30.     public Object top()  
  31.     {  
  32.         return stack.getFirst();  
  33.     }  
  34.      
  35.     public boolean isEmpty()  
  36.     {  
  37.         if(top == -1 || stack.size() == 0)  
  38.             return true;  
  39.         else  
  40.             return false;  
  41.     }  
  42. }  


2.将输入的表达式转换成逆波兰式
java 代码
 
  1. package structure;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.StringTokenizer;  
  6.   
  7. /** 
  8.  * @author wei 
  9.  *  
  10.  * @2007-2-8 
  11.  * @to-Email 
  12.  */  
  13. public class Calculate {  
  14.   
  15.     private List expression = new ArrayList();  
  16.     private List operator = new ArrayList();  
  17.     private Stack stack;  
  18.       
  19.     public Calculate(String input)  
  20.     {  
  21.         stack = new Stack();  
  22.         StringTokenizer st = new StringTokenizer(input, "+-*/()"true);  
  23.         while (st.hasMoreElements()) {  
  24.             expression.add(st.nextToken());  
  25.         }  
  26.     }  
  27.       
  28.     public static boolean isOperator(String operator) {  
  29.         if ("+".equals(operator) || "-".equals(operator)  
  30.                 || "*".equals(operator) || "/".equals(operator)  
  31.                 || "(".equals(operator) || ")".equals(operator))  
  32.             return true;  
  33.         else  
  34.             return false;  
  35.     }  
  36.       
  37.     public List doTrans()  
  38.     {  
  39.         for(int i = 0;i
  40.         {  
  41.             String str = (String)expression.get(i);  
  42.               
  43.             if(str.equals("+") || str.equals("-"))  
  44.             {  
  45.                 gotOper(str,1);  
  46.             }else if(str.equals("*") || str.equals("/"))  
  47.             {  
  48.                 gotOper(str,2);  
  49.             }else if(str.equals("("))  
  50.             {  
  51.                 stack.push("(");  
  52.             }else if(str.equals(")"))  
  53.             {  
  54.                 gotParen(str);  
  55.             }else  
  56.             {  
  57.                 operator.add(str);  
  58.             }  
  59.         }  
  60.           
  61.         while(!stack.isEmpty())  
  62.         {  
  63.             operator.add(stack.pop());  
  64.         }  
  65.         return operator;  
  66.     }  
  67.       
  68.     public void gotOper(String opThis, int prec1) { // got operator from input  
  69.         while (!stack.isEmpty()) {  
  70.             String opTop = (String) stack.pop();  
  71.             if (opTop.equals("(")) // if it's a '('  
  72.             {  
  73.                 stack.push(opTop); // restore '('  
  74.                 break;  
  75.             } else // it's an operator  
  76.             {  
  77.                 int prec2; // precedence of new op  
  78.   
  79.                 if (opTop.equals("+")  || opTop.equals("-")) // find new op prec  
  80.                     prec2 = 1;  
  81.                 else  
  82.                     prec2 = 2;  
  83.                 if (prec2 < prec1) // if prec of new op less  
  84.                 { // than prec of old  
  85.                     stack.push(opTop); // save newly-popped op  
  86.                     break;  
  87.                 } else  
  88.                     // prec of new not less  
  89.                     //output = output + opTop;  
  90.                     operator.add(opTop);// than prec of old  
  91.             } // end else (it's an operator)  
  92.         } // end while  
  93.         stack.push(opThis); // push new operator  
  94.     }   
  95.   
  96.     public void gotParen(String ch) { // got right paren from input  
  97.         while (!stack.isEmpty()) {  
  98.             String chx = (String)stack.pop();  
  99.             if (chx.equals("(")) // if popped '('  
  100.                 break// we're done  
  101.             else  
  102. //              output = output + chx; // output it  
  103.             operator.add(chx);  
  104.         }   
  105.     }   
  106.       
  107.       
  108.     public static void main(String[] args)  
  109.     {  
  110.         Calculate calculate = new Calculate("2+(4-3)*2");  
  111.         List result = calculate.doTrans();  
  112.           
  113.         for(int i=0;i
  114.         {  
  115.             System.out.println(result.get(i));  
  116.         }  
  117.     }  
  118. }  

3.后序表达式计算
java 代码
 
  1. package structure;  
  2.   
  3. import java.math.BigDecimal;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. /** 
  8.  * @author  
  9.  * 
  10.  * @2007-2-8 
  11.  * @to-Email 
  12.  */  
  13. public class PostControl {  
  14.   
  15.     private List operator = new ArrayList();  
  16.     private Stack stack = new Stack();  
  17.       
  18.     public PostControl(List list)  
  19.     {  
  20.         operator = list;  
  21.     }  
  22.       
  23.     public String doParse()  
  24.     {  
  25.         BigDecimal z = new BigDecimal("0.00");  
  26.         for(int i= 0;i
  27.         {  
  28.             String str = (String)operator.get(i);  
  29.               
  30.             if(!isOperator(str))  
  31.             {   stack.push(str);}  
  32.             else  
  33.             {  
  34.                 BigDecimal y = new BigDecimal((String)stack.pop());  
  35.                 BigDecimal x =new BigDecimal((String)stack.pop());  
  36.                   
  37.                 if(str.equals("+"))  
  38.                 {  
  39.                     z = x.add(y);  
  40.                 }else if(str.equals("-"))  
  41.                 {  
  42.                     z= x.subtract(y);  
  43.                 }else if(str.equals("*"))  
  44.                 {  
  45.                     z = x.multiply(y);  
  46.                 }else if(str.equals("/"))  
  47.                 {  
  48.                     z = x.divide(y);  
  49.                 }else  
  50.                 {  
  51.                     z= new BigDecimal("0.00");  
  52.                 }  
  53.                   
  54.                 stack.push(String.valueOf(z));  
  55.             }  
  56.         }  
  57.         return (String)stack.pop();  
  58.     }  
  59.       
  60.       
  61.     public static boolean isOperator(String operator) {  
  62.         if ("+".equals(operator) || "-".equals(operator)  
  63.                 || "*".equals(operator) || "/".equals(operator)  
  64.                 || "(".equals(operator) || ")".equals(operator))  
  65.             return true;  
  66.         else  
  67.             return false;  
  68.     }  
  69.       
  70.       
  71.     public static void main(String[] args)  
  72.     {  
  73.         Calculate calculate = new Calculate("2+((4.5-3)*2+3)*2");  
  74.         List result = calculate.doTrans();  
  75.           
  76.         PostControl p = new PostControl(result);  
  77.         System.out.println(p.doParse());  
  78.     }  
  79. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics