Java expression calculator using inverse Polish expression algorithm

From , 3 Years ago, written in Java, viewed 215 times.
URL https://pastebin.vip/view/42edd1ec
  1. package com.infogrid.g2b;  
  2.  
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.  
  6.  
  7. public class Op {  
  8.        
  9.     private static final Map<String,Integer> ops = new HashMap<String, Integer>();  
  10.     static{  
  11.         ops.put("+",10);  
  12.         ops.put("-",10);  
  13.         ops.put("*",20);  
  14.         ops.put("/",20);  
  15.         ops.put("%",20);  
  16.         ops.put("(",100);  
  17.         ops.put(")",100);  
  18.     }  
  19.        
  20.     public static boolean isSign(String sign1){  
  21.         Integer s = ops.get(sign1);  
  22.         if(s==null)  
  23.             return false;  
  24.         else  
  25.             return true;  
  26.     }  
  27.        
  28.     public static int compare(String sign1,String sign2){  
  29.         Integer p1 = ops.get(sign1);  
  30.         Integer p2 = ops.get(sign2);  
  31.         if(p1==null)  
  32.             throw new IllegalArgumentException("符号:"+sign1+"不存在!");  
  33.         if(p2==null)  
  34.             throw new IllegalArgumentException("符号:"+sign2+"不存在!");  
  35.         return p1-p2;  
  36.     }  
  37.        
  38.     public static Object cal(Object x,Object y,String sign){  
  39.         Double a=0.0,b=0.0;  
  40.         a = Double.valueOf(x+"");  
  41.         b = Double.valueOf(y+"");  
  42.         if(sign.equals("+"))  
  43.             return a+b;  
  44.         if(sign.equals("-"))  
  45.             return a-b;  
  46.         if(sign.equals("*"))  
  47.             return a*b;  
  48.         if(sign.equals("/"))  
  49.             return a/b;  
  50.         if(sign.equals("%"))  
  51.             return a%b;  
  52.         throw new IllegalArgumentException("操作符不合法!");  
  53.     }  
  54. }  
  55. package com.infogrid.g2b;  
  56.  
  57. import java.util.ArrayList;  
  58. import java.util.List;  
  59. import java.util.Stack;  
  60.  
  61. public class Calculator {  
  62.        
  63.     private List<String> list = new ArrayList<String>();  
  64.     private Stack<String> stack = new Stack<String>();  
  65.        
  66.        
  67.     private List<String> resolveExpr(String exp){      
  68.         String opert=exp.replaceAll("\\d*\\.\\d+?", "");  
  69.         List<String> list=new ArrayList<String>();      
  70.         int pidx=-1;      
  71.         for(int i=0;i<opert.length();i++){      
  72.             String p=opert.substring(i, i+1);      
  73.             pidx=exp.indexOf(p);      
  74.             if(exp.substring(0,pidx).trim().length()!=0){      
  75.                 list.add(exp.substring(0, pidx));      
  76.             }      
  77.             list.add(exp.substring(pidx, pidx+1));      
  78.             exp=exp.substring(pidx+1);      
  79.         }      
  80.         if(exp.length()>0){      
  81.             list.add(exp);      
  82.         }      
  83.         return list;      
  84.     }      
  85.        
  86.     private void dealSign(String s){  
  87.         if(stack.size()==0){  
  88.             stack.push(s);  
  89.             return;  
  90.         }  
  91.         String ps = stack.pop();  
  92.         if(Op.compare(s, ps)>0||ps.equals("(")){  
  93.             if(s.equals(")")){  
  94.                 list.add(ps);  
  95.                 while(stack.size()>0){  
  96.                     ps = stack.pop();  
  97.                     if(ps.equals("("))  
  98.                         break;  
  99.                     list.add(ps);  
  100.                 }  
  101.             }else{  
  102.                 stack.push(ps);  
  103.                 stack.push(s);  
  104.             }  
  105.         }else{  
  106.             list.add(ps);  
  107.             dealSign(s);  
  108.         }  
  109.     }  
  110.        
  111.     private void dealVar(String s){  
  112.         list.add(s);  
  113.     }  
  114.        
  115.     private Double getResult(){  
  116.         for(String s:list){  
  117.             if(!Op.isSign(s)){  
  118.                 stack.push(s);  
  119.                 continue;  
  120.             }  
  121.             Object a = 0,b = 0;  
  122.             if(stack.size()>0)  
  123.                 b = stack.pop();  
  124.             if(stack.size()>0)  
  125.                 a = stack.pop();  
  126.             stack.push(Op.cal(a, b, s)+"");  
  127.         }  
  128.         return Double.valueOf(stack.pop());  
  129.     }  
  130.        
  131.     public Double calculate(String expression){  
  132.         List<String> ss = resolveExpr(expression);  
  133.         for(String s:ss){  
  134.             if(Op.isSign(s)){  
  135.                 dealSign(s);  
  136.             }else{  
  137.                 dealVar(s);  
  138.             }  
  139.         }  
  140.         while(stack.size()>0){  
  141.             list.add(stack.pop());  
  142.         }  
  143.         System.out.println(list);  
  144.            
  145.         return getResult();  
  146.     }  
  147.        
  148.        
  149.  
  150.        
  151.     public static void main(String[] args) {  
  152.         System.out.println(new Calculator().calculate("1.5+2.1+((4/2)-6/((2+1)*2))+6%4"));  
  153.     }  
  154. }  
  155. //java/7794

Reply to "Java expression calculator using inverse Polish expression algorithm"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website