Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
d4e8ae9f
Commit
d4e8ae9f
authored
Mar 01, 2021
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改实体位置
parent
f596f934
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
16 deletions
+48
-16
CalcHelper.java
...src/main/java/com/yanzuoguang/util/helper/CalcHelper.java
+43
-15
TestCalcHelper.java
yzg-util-base/src/test/java/helper/TestCalcHelper.java
+5
-1
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/CalcHelper.java
View file @
d4e8ae9f
...
...
@@ -20,6 +20,7 @@ public class CalcHelper {
private
static
final
String
REGEX_CALC_MULTIPLY_MOD
=
"(^.*?)([*/])(.*?$)"
;
private
static
final
String
REGEX_CALC_TAG
=
"[+\\-*/()]+"
;
private
static
final
String
EMPTY_CHAR
=
" "
;
public
static
String
TEMP_VAR_NAME
=
"@temp"
;
private
static
CalcHelper
calcInstance
=
new
CalcHelper
();
...
...
@@ -36,6 +37,18 @@ public class CalcHelper {
double
getValue
(
String
parameterName
);
}
/**
* 计算公式
*
* @param formula 公式内容,支持括号、空格、数字、+、-、*、/、变量名,如: A * ( B + C )
* @return 运算后的结果
*/
public
static
double
calc
(
String
formula
)
{
return
calcInstance
.
calRun
(
formula
,
StringHelper
.
EMPTY
,
null
);
}
/**
* 计算公式
*
...
...
@@ -44,7 +57,7 @@ public class CalcHelper {
* @return 运算后的结果
*/
public
static
double
calc
(
String
formula
,
CalcParameter
calcParameter
)
{
return
calcInstance
.
calRun
(
formula
,
calcParameter
);
return
calcInstance
.
calRun
(
formula
,
StringHelper
.
EMPTY
,
calcParameter
);
}
...
...
@@ -52,13 +65,27 @@ public class CalcHelper {
* 计算公式
*
* @param formula 公式内容,支持括号、空格、数字、+、-、*、/、变量名,如: A * ( B + C )
* @param tempName 临时变量名称
* @param calcParameter 获取变量值
* @return 运算后的结果
*/
public
static
double
calc
(
String
formula
,
String
tempName
,
CalcParameter
calcParameter
)
{
return
calcInstance
.
calRun
(
formula
,
tempName
,
calcParameter
);
}
/**
* 计算公式
*
* @param formula 公式内容,支持括号、空格、数字、+、-、*、/、变量名,如: A * ( B + C )
* @param tempName 临时变量名称
* @param calcParameter 获取变量值
* @return 运算后的结果
*/
private
double
calRun
(
String
formula
,
CalcParameter
calcParameter
)
{
private
double
calRun
(
String
formula
,
String
tempName
,
CalcParameter
calcParameter
)
{
if
(
StringHelper
.
isEmpty
(
formula
))
{
return
0
;
}
tempName
=
StringHelper
.
getFirst
(
tempName
,
TEMP_VAR_NAME
);
// 去掉公式空格
formula
=
formula
.
replaceAll
(
EMPTY_CHAR
,
""
);
// 获取所有的变量名称
...
...
@@ -66,7 +93,7 @@ public class CalcHelper {
// 获取所有变量值
Map
<
String
,
Double
>
varValues
=
getVarValues
(
varNames
,
calcParameter
);
// 返回计算结果
return
calcProc
(
formula
,
varValues
);
return
calcProc
(
formula
,
tempName
,
varValues
);
}
/**
...
...
@@ -98,9 +125,9 @@ public class CalcHelper {
*/
private
Map
<
String
,
Double
>
getVarValues
(
List
<
String
>
varNames
,
CalcParameter
calcParameter
)
{
// 获取所有的变量值
Map
<
String
,
Double
>
varValues
=
new
HashMap
<>();
Map
<
String
,
Double
>
varValues
=
new
HashMap
<>(
varNames
.
size
()
);
for
(
String
name
:
varNames
)
{
if
(
varValues
.
containsKey
(
name
))
{
if
(
varValues
.
containsKey
(
name
)
||
calcParameter
==
null
)
{
continue
;
}
double
value
=
calcParameter
.
getValue
(
name
);
...
...
@@ -113,37 +140,38 @@ public class CalcHelper {
* 最终计算结果
*
* @param formula 公式
* @param tempName 临时变量名称
* @param varValues 变量值
* @return
*/
private
double
calcProc
(
String
formula
,
Map
<
String
,
Double
>
varValues
)
{
private
double
calcProc
(
String
formula
,
String
tempName
,
Map
<
String
,
Double
>
varValues
)
{
double
ret
=
0
;
if
(
formula
.
matches
(
REGEX_QUOT
))
{
// 获取第一个括号和最后一个括号
Matcher
matcher
=
getMatcher
(
REGEX_QUOT
,
formula
);
double
quotResult
=
calcProc
(
matcher
.
group
(
2
),
varValues
);
String
mutualKey
=
"@temp"
+
varValues
.
size
();
double
quotResult
=
calcProc
(
matcher
.
group
(
2
),
tempName
,
varValues
);
String
mutualKey
=
TEMP_VAR_NAME
+
varValues
.
size
();
varValues
.
put
(
mutualKey
,
quotResult
);
String
formulaTo
=
matcher
.
group
(
1
)
+
mutualKey
+
matcher
.
group
(
3
);
ret
=
calcProc
(
formulaTo
,
varValues
);
ret
=
calcProc
(
formulaTo
,
tempName
,
varValues
);
}
else
if
(
formula
.
matches
(
REGEX_CALC_ADD_PLUS
))
{
// 判断是否包含+-运算符号
Matcher
matcher
=
getMatcher
(
REGEX_CALC_ADD_PLUS
,
formula
);
double
leftResult
=
calcProc
(
matcher
.
group
(
1
),
varValues
);
double
rightResult
=
calcProc
(
matcher
.
group
(
3
),
varValues
);
double
leftResult
=
calcProc
(
matcher
.
group
(
1
),
tempName
,
varValues
);
double
rightResult
=
calcProc
(
matcher
.
group
(
3
),
tempName
,
varValues
);
ret
=
calcItem
(
matcher
.
group
(
2
),
leftResult
,
rightResult
);
}
else
if
(
formula
.
matches
(
REGEX_CALC_MULTIPLY_MOD
))
{
// 判断是否包含*/运算符号
Matcher
matcher
=
getMatcher
(
REGEX_CALC_MULTIPLY_MOD
,
formula
);
double
leftResult
=
calcProc
(
matcher
.
group
(
1
),
varValues
);
double
rightResult
=
calcProc
(
matcher
.
group
(
3
),
varValues
);
double
leftResult
=
calcProc
(
matcher
.
group
(
1
),
tempName
,
varValues
);
double
rightResult
=
calcProc
(
matcher
.
group
(
3
),
tempName
,
varValues
);
ret
=
calcItem
(
matcher
.
group
(
2
),
leftResult
,
rightResult
);
}
else
if
(
formula
.
matches
(
REGEX_DOUBLE
))
{
ret
=
StringHelper
.
toDouble
(
formula
);
}
else
{
ret
=
varValues
.
get
(
formula
);
ret
=
StringHelper
.
toDouble
(
varValues
.
get
(
formula
)
);
}
//
System.out.println("公式: " + formula + " 值" + ret);
System
.
out
.
println
(
"公式: "
+
formula
+
" 值"
+
ret
);
return
ret
;
}
...
...
yzg-util-base/src/test/java/helper/TestCalcHelper.java
View file @
d4e8ae9f
...
...
@@ -22,8 +22,12 @@ public class TestCalcHelper {
}
}
}));
}
@Test
public
void
test1
()
{
System
.
out
.
println
(
CalcHelper
.
calc
(
" a * 120 + 2 * ( a + b * d )"
,
new
CalcHelper
.
CalcParameter
()
{
System
.
out
.
println
(
CalcHelper
.
calc
(
"
=
a * 120 + 2 * ( a + b * d )"
,
new
CalcHelper
.
CalcParameter
()
{
@Override
public
double
getValue
(
String
parameterName
)
{
switch
(
parameterName
){
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment