java 实现 eval功能


上一文中实现eval方法如下
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine se = manager.getEngineByName("js");

本文实现该方法,将字符串转成运算符(菜鸟一个,大佬们多多指点)
 1 public static String calcul(String arg){
 2         /**
 3          * 将String 转成运算式
 4          */
 5         //list 为出现符号的坐标
 6         List list = new ArrayList();
 7         //list1 乘除优先级为2,加减优先级为1
 8         List list1 = new ArrayList();
 9         //list2 取数
10         List list2 = new ArrayList();
11 
12         for(int i=0;i<arg.length();i++){
13             if(arg.substring(i,i+1).equals("+") || arg.substring(i,i+1).equals("-") ){
14                 list.add(i);
15                 list1.add(1);
16             }else if(arg.substring(i,i+1).equals("*") || arg.substring(i,i+1).equals("/")){
17                 list.add(i);
18                 list1.add(2);
19             }
20         }
21 
22         // list2取数转成double
23         for(int i=0;i<=list.size();i++){
24             if(i==0){
25                 list2.add(Double.valueOf((arg.substring(0,(int)list.get(i)))));
26             }else if(i==list.size()) {
27                 list2.add((Double.valueOf(arg.substring((int)list.get(i-1)+1))));
28             }else {
29                 list2.add((Double.valueOf(arg.substring((int)list.get(i-1)+1,(int)list.get(i)))));
30             }
31 
32         }
33         //计算乘除
34         for(int i=0;i<list1.size();i++){
35             if((int)list1.get(i)==2){
36                 double b=0;
37                 String s = arg.substring((int)list.get(i),(int)list.get(i)+1);
38                 if(s.equals("*")){
39                     b = (double)list2.get(i) * (double)list2.get(i+1);
40                 }else if(s.equals("/")){
41                     b = (double)list2.get(i) / (double)list2.get(i+1);
42                 }
43                 list1.set(i,-1);
44                 list2.set(i,b);
45                 list2.set(i+1,b);
46 
47             }
48         }
49 
50 
51 
52         //list3 取剩余的数,list4取加减后(未避免多个加减号周围不是想要的值)
53         List list3 = new ArrayList();
54         List list4 = new ArrayList();
55         for(int i=0;i<list1.size();i++){
56             if((int)list1.get(i)==1){
57                 double b=0;
58                 String s = arg.substring((int)list.get(i),(int)list.get(i)+1);
59                 if(s.equals("+")){
60                     list4.add("+");
61                 }else if(s.equals("-")){
62                     list4.add("-");
63                 }
64                 if(i==0 ){
65                     list3.add((double)list2.get(i));
66                 }else  if(i==list1.size() -1){
67                     list3.add((double)list2.get(i+1));
68                 }else{
69                     list3.add((double)list2.get(i));
70                     list3.add((double)list2.get(i+1));
71                 }
72             }
73         }
74         //计算加减
75         double c = 0;
76         for(int i=0;i<list4.size();i++){
77             String s1 = list4.get(i).toString();
78             if(s1.equals("+")){
79                 c = (double)list3.get(i) + (double)list3.get(i+1);
80 
81             }else if(s1.equals("-")){
82                 c = (double)list3.get(i) - (double)list3.get(i+1);
83             }
84             list3.set(i,c);
85             list3.set(i+1,c);
86         }
87 //        System.out.println(list2);
88 //        System.out.println(list3);
89 //        System.out.println(list4);
90 
91         return String.format("%.2f", list3.get(list3.size()-1));
92     }
View Code

 

 

完整的:

  1 import javax.script.ScriptEngine;
  2 import javax.script.ScriptEngineManager;
  3 import javax.script.ScriptException;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 
  7 /**
  8         *  有1个字符串(包含若干个加减乘除运算列),格式如下:2*3/7-1*6+4=;1.5+1/4*3.243-5*6=;2.32+30/4-64*1=;......" * ,请编写代码计算结果(保留2位小数)
  9         *  预期输出:
 10         *  2*3/7-1*6+4=?
 11         *  1.5+1/4*3.243-5*6==?
 12         *  2.32+30/4-64*1=-?
 13         */
 14 class test {
 15     public String calculate(String arg) throws ScriptException {
 16         String result = "";
 17         //TODO
 18         //按照分号分割
 19         String  temp[] = arg.split(";");
 20         //以下方法实现 eval功能
 21         ScriptEngineManager manager = new ScriptEngineManager();
 22         ScriptEngine se = manager.getEngineByName("js");
 23         test c =new test();
 24         for(String s:temp){
 25             //去掉等于号
 26             String sNew = s.substring(0,s.length()-1);
 27             //保留二位小数
 28             String sNewTwo = c.calcul(sNew);
 29 //            String sNewTwo =   String.format("%.2f", (Double) se.eval(sNew)).toString();
 30             result = result + s+ sNewTwo + "\n";
 31         }
 32         result = result.substring(0,result.length()-2);
 33         return result;
 34     }
 35 
 36     public static String calcul(String arg){
 37         /**
 38          * 将String 转成运算式
 39          */
 40         //list 为出现符号的坐标
 41         List list = new ArrayList();
 42         //list1 乘除优先级为2,加减优先级为1
 43         List list1 = new ArrayList();
 44         //list2 取数
 45         List list2 = new ArrayList();
 46 
 47         for(int i=0;i<arg.length();i++){
 48             if(arg.substring(i,i+1).equals("+") || arg.substring(i,i+1).equals("-") ){
 49                 list.add(i);
 50                 list1.add(1);
 51             }else if(arg.substring(i,i+1).equals("*") || arg.substring(i,i+1).equals("/")){
 52                 list.add(i);
 53                 list1.add(2);
 54             }
 55         }
 56 
 57         // list2取数转成double
 58         for(int i=0;i<=list.size();i++){
 59             if(i==0){
 60                 list2.add(Double.valueOf((arg.substring(0,(int)list.get(i)))));
 61             }else if(i==list.size()) {
 62                 list2.add((Double.valueOf(arg.substring((int)list.get(i-1)+1))));
 63             }else {
 64                 list2.add((Double.valueOf(arg.substring((int)list.get(i-1)+1,(int)list.get(i)))));
 65             }
 66 
 67         }
 68         //计算乘除
 69         for(int i=0;i<list1.size();i++){
 70             if((int)list1.get(i)==2){
 71                 double b=0;
 72                 String s = arg.substring((int)list.get(i),(int)list.get(i)+1);
 73                 if(s.equals("*")){
 74                     b = (double)list2.get(i) * (double)list2.get(i+1);
 75                 }else if(s.equals("/")){
 76                     b = (double)list2.get(i) / (double)list2.get(i+1);
 77                 }
 78                 list1.set(i,-1);
 79                 list2.set(i,b);
 80                 list2.set(i+1,b);
 81 
 82             }
 83         }
 84 
 85 
 86 
 87         //list3 取剩余的数,list4取加减后(未避免多个加减号周围不是想要的值)
 88         List list3 = new ArrayList();
 89         List list4 = new ArrayList();
 90         for(int i=0;i<list1.size();i++){
 91             if((int)list1.get(i)==1){
 92                 double b=0;
 93                 String s = arg.substring((int)list.get(i),(int)list.get(i)+1);
 94                 if(s.equals("+")){
 95                     list4.add("+");
 96                 }else if(s.equals("-")){
 97                     list4.add("-");
 98                 }
 99                 if(i==0 ){
100                     list3.add((double)list2.get(i));
101                 }else  if(i==list1.size() -1){
102                     list3.add((double)list2.get(i+1));
103                 }else{
104                     list3.add((double)list2.get(i));
105                     list3.add((double)list2.get(i+1));
106                 }
107             }
108         }
109         //计算加减
110         double c = 0;
111         for(int i=0;i<list4.size();i++){
112             String s1 = list4.get(i).toString();
113             if(s1.equals("+")){
114                 c = (double)list3.get(i) + (double)list3.get(i+1);
115 
116             }else if(s1.equals("-")){
117                 c = (double)list3.get(i) - (double)list3.get(i+1);
118             }
119             list3.set(i,c);
120             list3.set(i+1,c);
121         }
122 //        System.out.println(list2);
123 //        System.out.println(list3);
124 //        System.out.println(list4);
125 
126         return String.format("%.2f", list3.get(list3.size()-1));
127     }
128 
129     public static void main(String [] args) throws ScriptException{
130         test testNew = new test();
131         String s = "2*3/7-1*6+4=;1.5+1/4*3.243-5*6=;2.32+30/4-64*1=;1+2*3-3/2=";
132         String sNew = testNew.calculate(s);
133         System.out.println(sNew);
134 
135     }
136 }
View Code

 

posted @ 2021-04-10 23:43  菜小鱼~  阅读(1732)  评论(0编辑  收藏  举报