服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - java实现字符串四则运算公式解析工具类的方法

java实现字符串四则运算公式解析工具类的方法

2021-05-17 15:22零度anngle Java教程

今天小编就为大家分享一篇java实现字符串四则运算公式解析工具类的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

项目中用到用户定义运算公式进行就算的需求,这样需要进行字符串四则运算解析,下面提供字符串公式四则运算解析与计算工具类,需要的同学可参考。

工具类如下:formulacalculator.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package org.nercita.bcp.record.util;
 
import java.util.arraylist;
import java.util.linkedlist;
 
/**
 * @author zhangwenchao
 * @since 2016-08-26
 * 公式计算的工具类
 */
public class formulacalculator {
    
    private static boolean isrightformat = true;
 
    public static double getresult(string formula){
        double returnvalue = 0;
        try{
            returnvalue = doanalysis(formula);
        }catch(numberformatexception nfe){
            system.out.println("公式格式有误,请检查:" + formula);
        }catch(exception e){
            e.printstacktrace();
        }
        if(!isrightformat){
            system.out.println("公式格式有误,请检查:" + formula);
        }
        return returnvalue;
    }
 
    private static double doanalysis(string formula){
        double returnvalue = 0;
        linkedlist<integer> stack = new linkedlist<integer>();
        int curpos = 0;
        string beforepart = "";
        string afterpart = "";
        string calculator = "";
        isrightformat = true;
        while(isrightformat&&(formula.indexof('(') >= 0||formula.indexof(')') >= 0)){        
            curpos = 0;
            for(char s : formula.tochararray()){
                if(s == '('){ 
                    stack.add(curpos);
                }else if(s == ')'){ 
                    if(stack.size() > 0){ 
                        beforepart = formula.substring(0, stack.getlast()); 
                        afterpart = formula.substring(curpos + 1); 
                        calculator = formula.substring(stack.getlast() + 1, curpos); 
                        formula = beforepart + docalculation(calculator) + afterpart; 
                        stack.clear(); 
                        break
                    }else
                        system.out.println("有未关闭的右括号!"); 
                        isrightformat = false
                    }
                }
                curpos++;
            }
            if(stack.size() > 0){
                system.out.println("有未关闭的左括号!");
                break;
            }
        }
        if(isrightformat){
            returnvalue = docalculation(formula);
        }
        return returnvalue;
    }
    
    private static double docalculation(string formula) {
        arraylist<double> values = new arraylist<double>();
        arraylist<string> operators = new arraylist<string>();
        int curpos = 0;
        int prepos = 0;
        int minus = 0;     
        for (char s : formula.tochararray()) {
             if ((s == '+' || s == '-' || s == '*' || s == '/') && minus !=0 && minus !=2) {                                            
                 values.add(double.parsedouble(formula.substring(prepos, curpos).trim()));             
                 operators.add("" + s);                
                 prepos = curpos + 1;               
                 minus = minus +1;
             }else{             
                 minus =1;              
             }
             curpos++;     
        }
        values.add(double.parsedouble(formula.substring(prepos).trim()));
        char op;
        for (curpos = 0; curpos <= operators.size() - 1; curpos++) {                        
            op = operators.get(curpos).charat(0);
            switch (op) {
            case '*':
                values.add(curpos, values.get(curpos) * values.get(curpos + 1));
                values.remove(curpos + 1);
                values.remove(curpos + 1);
                operators.remove(curpos);
                curpos = -1;
                break;
            case '/':
                values.add(curpos, values.get(curpos) / values.get(curpos + 1));
                values.remove(curpos + 1);
                values.remove(curpos + 1);
                operators.remove(curpos);
                curpos = -1;
                break;
            }
        }
        for (curpos = 0; curpos <= operators.size() - 1; curpos++) {
            op = operators.get(curpos).charat(0);
            switch (op) {
            case '+':
                values.add(curpos, values.get(curpos) + values.get(curpos + 1));
                values.remove(curpos + 1);
                values.remove(curpos + 1);
                operators.remove(curpos);
                curpos = -1;
                break;
            case '-':
                values.add(curpos, values.get(curpos) - values.get(curpos + 1));
                values.remove(curpos + 1);
                values.remove(curpos + 1);
                operators.remove(curpos);
                curpos = -1;
                break;
            }
        }
        return values.get(0).doublevalue();
    }
 
    public static void main(string[] args) {    
        system.out.println(formulacalculator.getresult("3-(4*5)+5"));  
        system.out.println(formulacalculator.getresult("7/2-(-4)"));           
        system.out.println(formulacalculator.getresult("1287763200000-1276272000000")/(3600*24*1000));
    }
    
}

支持四则运算,同时支持负数解析。

另附,小数数据保留位数工具类,setnumberprecision.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package org.nercita.bcp.record.util;
 
import java.text.decimalformat;
 
/**
 
 * @author zhangwenchao
 * 小数点 精度的工具类
 */
public class setnumberprecision {
        
    public static string setnumberprecision(double x,int number){       
        string p="#########0";      
        if(number==0){          
            p="#########0";     
        }else{       
            p="#########0.";         
            for(int i=0;i<number;i++){//for的巧妙运用       
                p=p+"0";         
            }       
        }        
        decimalformat f = new decimalformat(p);          
        string s = f.format(x).tostring();       
        return s;       
    }
    
}

以上这篇java实现字符串四则运算公式解析工具类的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/zmx729618/article/details/52328377

延伸 · 阅读

精彩推荐