AmMrWu

a fish.

导航

计算器算法

     oneday做了计算器的界面,网上搜了下算法,但是搜不到具体的java代码,闲来无事,写了个,欢迎大家来喷! 
* @author superGenius吴大仙^^!蛋疼仔 
* 计算器算法的实现 
   *定义两个堆栈,一个放置操作数,一个放置操作符 
   *1.首先把得到的数学表达式转化成为逆波兰式 Reverse Polish Notation 
   *  对于一个表达式,遇到数字便+入到新的逆波兰式,假如遇到的是操作符,首先比较其和操作符堆栈里面 
   *  操作符的优先级,假如优先级较高,便加入到操作符堆栈中,or+入到逆波兰式中 
   * 2.计算逆波兰式 
   * 遍历波兰式,遇到数字便放入堆栈,遇到操作符,操作数堆栈弹出,执行相应的操作符操作,util...then ok; 

Java代码  收藏代码
  1. import java.util.HashMap;  
  2. import java.util.Map;  
  3.   
  4. import java.util.Stack;  
  5.   
  6.   
  7.   
  8. public class CalculatorIn {  
  9.     private Stack<Double> operandStack=new Stack<Double>();//操作数堆栈  
  10.     private Stack<String> operatorStack=new Stack<String>();//操作符堆栈  
  11.     private String expression;//算数表达式  
  12.     private double result=0.0;//计算结果  
  13.     private Map<String,Integer> priorityMap=new HashMap<String,Integer>();//用于存储操作符优先级的Map  
  14.     //初始化优先级约定(可根据计算的复杂程度扩展)  
  15.     public CalculatorIn()  
  16.     {  
  17.         priorityMap.put("+",0);  
  18.         priorityMap.put("-",0);  
  19.         priorityMap.put("*"1);  
  20.         priorityMap.put("/"1);  
  21.          
  22.     }  
  23.      
  24.     public int getPriority(String op)//得到一个操作符的优先级  
  25.     {  
  26.         return priorityMap.get(op);  
  27.     }  
  28.      
  29.     public boolean highPriority(String op)//判断操作符的优先级在堆栈里面是否最为高  
  30.     {  
  31.         int opPri=getPriority(op);//当前操作符的优先级  
  32.         if(!operatorStack.empty())  
  33.         {  
  34.         for(String s:operatorStack)  
  35.         {  
  36.             int priority=getPriority(s);  
  37.             if(opPri<priority)  
  38.                 return false;  
  39.              
  40.         }  
  41.         }  
  42.         return true;  
  43.     }  
  44.     //把表达式转化成逆波兰式  
  45.     public String expToIpn()  
  46.     {  
  47.         int index=0;  
  48.         int end=0;  
  49.         String Ipn="";  
  50.         for(int i=0;i<expression.length();i++)  
  51.         {  
  52.             String temps=String.valueOf(expression.charAt(i));  
  53.             if(temps.matches("[0-9.]"))//检查是否是数字  
  54.             {  
  55.                 end++;  
  56.             }  
  57.             else  
  58.             {  
  59.                 String tempOperand=expression.substring(index,end);//得到操作数  
  60.                 Ipn+=tempOperand+",";  
  61.                 String tempOperator=expression.substring(end,++end);//得到操作符  
  62.                 if(tempOperator.equals("!"))//假如到表达式的最后将操作符 全部弹出  
  63.                     {  
  64.                     while(!operatorStack.empty())  
  65.                     {  
  66.                          Ipn+=operatorStack.pop()+",";  
  67.                      }  
  68.                     }  
  69.                  else  
  70.                  {  
  71.                 if(highPriority(tempOperator))//优先级高的压入操作符堆栈  
  72.                     {  
  73.                     operatorStack.push(tempOperator);  
  74.                     }  
  75.                  else  
  76.                     {  
  77.                      while(!operatorStack.empty())//  
  78.                     {  
  79.                          Ipn+=operatorStack.pop()+",";  
  80.                      }  
  81.                       
  82.                      operatorStack.push(tempOperator);  
  83.                     }  
  84.                 //System.out.println(tempOperand+","+tempOperator);  
  85.                 index=end;  
  86.             }  
  87.             }  
  88.              
  89.         }  
  90.         return Ipn;  
  91.      
  92.     }  
  93.     public double calculateIpn(String[] Ipn)//计算逆波兰式  
  94.     {  
  95.            
  96.         for(int i=0;i<Ipn.length;i++)  
  97.         {  
  98.         //    System.out.println(Ipn[i]);  
  99.             if(Ipn[i].matches("^[0-9]+.?[0-9]*$"))//正则表达式判断是数字  
  100.             {  
  101.                 operandStack.push(Double.parseDouble(Ipn[i]));  
  102.             }  
  103.                 else  
  104.                 {  
  105.                     popOperand(Ipn[i]);  
  106.                 }  
  107.         }  
  108.         return result;  
  109.          
  110.     }  
  111.     //遇到操作符时,弹出操作数,进行相应操作,并保村result  
  112.     public void popOperand(String operator)  
  113.     {  
  114.         double d1=operandStack.pop();  
  115.         double d2=operandStack.pop();  
  116.         System.out.println(d1+operator+d2);  
  117.         if(operator.equals("+"))  
  118.             result=d2+d1;  
  119.         if(operator.equals("-"))  
  120.             result=d2-d1;  
  121.         if(operator.equals("*"))  
  122.             result=d2*d1;  
  123.         if(operator.equals("/"))  
  124.             result=d2/d1;  
  125. //System.out.println(result);  
  126.             operandStack.push(result);  
  127.          
  128.     }  
  129.   
  130.     public Stack getOperandStack() {  
  131.         return operandStack;  
  132.     }  
  133.     public void setOperandStack(Stack operandStack) {  
  134.         this.operandStack = operandStack;  
  135.     }  
  136.     public Stack getOperatorStack() {  
  137.         return operatorStack;  
  138.     }  
  139.     public void setOperatorStack(Stack operatorStack) {  
  140.         this.operatorStack = operatorStack;  
  141.     }  
  142.     public String getexpression_r() {  
  143.         return expression;  
  144.     }  
  145.     public void setexpression_r(String expression) {  
  146.         this.expression = expression;  
  147.     }  
  148.     public double getResult() {  
  149.         return result;  
  150.     }  
  151.     public void setResult(double result) {  
  152.         this.result = result;  
  153.     }  
  154.   
  155.     public static void main(String[] args)  
  156.     {  
  157.         CalculatorIn cal=new CalculatorIn();  
  158.         String exp="32+1*2*2/2*2-2/2!";  
  159.         cal.setexpression_r(exp);  
  160.         String[] Ipn=cal.expToIpn().split(",");  
  161.          for(int i=0;i<Ipn.length;i++)  
  162.         System.out.println(Ipn[i]);  
  163.         System.out.println(cal.calculateIpn(Ipn));  
  164.     }  

posted on 2011-09-07 21:39  RorySmart  阅读(7008)  评论(2编辑  收藏  举报