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
4959c2c5
Commit
4959c2c5
authored
Feb 28, 2021
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
公式处理
parent
7e8d62a0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
23 deletions
+45
-23
CalcHelper.java
...src/main/java/com/yanzuoguang/util/helper/CalcHelper.java
+29
-23
TestCalcHelper.java
yzg-util-base/src/test/java/helper/TestCalcHelper.java
+16
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/CalcHelper.java
View file @
4959c2c5
...
@@ -106,34 +106,24 @@ public class CalcHelper {
...
@@ -106,34 +106,24 @@ public class CalcHelper {
double
ret
=
0
;
double
ret
=
0
;
if
(
formula
.
matches
(
REGEX_QUOT
))
{
if
(
formula
.
matches
(
REGEX_QUOT
))
{
// 获取第一个括号和最后一个括号
// 获取第一个括号和最后一个括号
Pattern
p
=
Pattern
.
compile
(
REGEX_QUOT
);
Matcher
matcher
=
getMatcher
(
REGEX_QUOT
,
formula
);
Matcher
matcher
=
p
.
matcher
(
formula
);
if
(!
matcher
.
find
()){
throw
new
RuntimeException
(
"正则表达式错误"
);
}
double
quotResult
=
calcProc
(
matcher
.
group
(
2
),
varValues
);
double
quotResult
=
calcProc
(
matcher
.
group
(
2
),
varValues
);
String
formulaTo
=
matcher
.
group
(
1
)
+
quotResult
+
matcher
.
group
(
3
);
String
mutualKey
=
"@temp"
+
varValues
.
size
();
ret
=
calcProc
(
formulaTo
,
varValues
);
varValues
.
put
(
mutualKey
,
quotResult
);
String
formulaTo
=
matcher
.
group
(
1
)
+
mutualKey
+
matcher
.
group
(
3
);
ret
=
calcProc
(
formulaTo
,
varValues
);
}
else
if
(
formula
.
matches
(
REGEX_CALC_ADD_PLUS
))
{
}
else
if
(
formula
.
matches
(
REGEX_CALC_ADD_PLUS
))
{
// 判断是否包含+-运算符号
// 判断是否包含+-运算符号
Pattern
p
=
Pattern
.
compile
(
REGEX_CALC_ADD_PLUS
);
Matcher
matcher
=
getMatcher
(
REGEX_CALC_ADD_PLUS
,
formula
);
Matcher
matcher
=
p
.
matcher
(
formula
);
double
leftResult
=
calcProc
(
matcher
.
group
(
1
),
varValues
);
if
(!
matcher
.
find
()){
double
rightResult
=
calcProc
(
matcher
.
group
(
3
),
varValues
);
throw
new
RuntimeException
(
"正则表达式错误"
);
ret
=
calcItem
(
matcher
.
group
(
2
),
leftResult
,
rightResult
);
}
double
leftResult
=
calcProc
(
matcher
.
group
(
1
),
varValues
);
double
rightResult
=
calcProc
(
matcher
.
group
(
3
),
varValues
);
ret
=
calcItem
(
matcher
.
group
(
2
),
leftResult
,
rightResult
);
}
else
if
(
formula
.
matches
(
REGEX_CALC_MULTIPLY_MOD
))
{
}
else
if
(
formula
.
matches
(
REGEX_CALC_MULTIPLY_MOD
))
{
// 判断是否包含*/运算符号
// 判断是否包含*/运算符号
Pattern
p
=
Pattern
.
compile
(
REGEX_CALC_MULTIPLY_MOD
);
Matcher
matcher
=
getMatcher
(
REGEX_CALC_MULTIPLY_MOD
,
formula
);
Matcher
matcher
=
p
.
matcher
(
formula
);
double
leftResult
=
calcProc
(
matcher
.
group
(
1
),
varValues
);
if
(!
matcher
.
find
()){
double
rightResult
=
calcProc
(
matcher
.
group
(
3
),
varValues
);
throw
new
RuntimeException
(
"正则表达式错误"
);
ret
=
calcItem
(
matcher
.
group
(
2
),
leftResult
,
rightResult
);
}
double
leftResult
=
calcProc
(
matcher
.
group
(
1
),
varValues
);
double
rightResult
=
calcProc
(
matcher
.
group
(
3
),
varValues
);
ret
=
calcItem
(
matcher
.
group
(
2
),
leftResult
,
rightResult
);
}
else
if
(
formula
.
matches
(
REGEX_DOUBLE
))
{
}
else
if
(
formula
.
matches
(
REGEX_DOUBLE
))
{
ret
=
StringHelper
.
toDouble
(
formula
);
ret
=
StringHelper
.
toDouble
(
formula
);
}
else
{
}
else
{
...
@@ -143,6 +133,22 @@ public class CalcHelper {
...
@@ -143,6 +133,22 @@ public class CalcHelper {
return
ret
;
return
ret
;
}
}
/**
* 匹配正则表达式和公式
*
* @param regex 正则表达式
* @param formula 公式
* @return 匹配后的结果
*/
private
static
Matcher
getMatcher
(
String
regex
,
String
formula
)
{
Pattern
p
=
Pattern
.
compile
(
regex
);
Matcher
matcher
=
p
.
matcher
(
formula
);
if
(!
matcher
.
find
())
{
throw
new
RuntimeException
(
"正则表达式错误"
);
}
return
matcher
;
}
private
static
double
calcItem
(
String
flag
,
double
a
,
double
b
)
{
private
static
double
calcItem
(
String
flag
,
double
a
,
double
b
)
{
switch
(
flag
)
{
switch
(
flag
)
{
case
"+"
:
case
"+"
:
...
...
yzg-util-base/src/test/java/helper/TestCalcHelper.java
View file @
4959c2c5
...
@@ -22,5 +22,21 @@ public class TestCalcHelper {
...
@@ -22,5 +22,21 @@ public class TestCalcHelper {
}
}
}
}
}));
}));
System
.
out
.
println
(
CalcHelper
.
calc
(
" a * 120 + 2 * ( a + b * d )"
,
new
CalcHelper
.
CalcParameter
()
{
@Override
public
double
getValue
(
String
parameterName
)
{
switch
(
parameterName
){
case
"a"
:
return
5
;
case
"b"
:
return
3
;
case
"d"
:
return
11
;
default
:
return
0
;
}
}
}));
}
}
}
}
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