Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement expression calculation Source Code in JAVA

Shulou Source: shulou.com Published: 2022-06-02 12:42:07 09月20日 Update

This article mainly shows you "how to achieve the source code of expression calculation in JAVA", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to achieve expression calculation source code in JAVA" this article.

Operator supported: +-* /% > 0)

Return 1

Else

Return 0

}

Else if (opt.equals ("&")) {

If (dbValue1 > 0 & & dbValue2 > 0)

Return 1

Else

Return 0

}

Else if (opt.equals ("=")) {

If (dbValue1 = = dbValue2)

Return 1

Else

Return 0

}

} catch (Exception e)

{

Throw new ExpressionException ("value" + value1 + "or" + value2 + "is illegal when performing the" + opt + "operation!)

}

Throw new ExpressionException ("operator" + opt + "illegal!")

}

Protected String getValue (String oldValue) throws ExpressionException {

String reg = "^ ([a-zA-Z0-9] +) (([a-zA-Z0-9). ()] +) $"

If (this.isFunctionCal (oldValue)) {

Pattern p = Pattern.compile (reg)

Matcher m = p.matcher (oldValue)

M.find ()

Return calFunction (m.group (1), m.group (2))

}

Return oldValue

}

Protected boolean isFunctionCal (String value) {

String reg = "^ ([a-zA-Z0-9] +) (([a-zA-Z0-9). ()] +) $"

Return value.matches (reg)

}

Protected String calFunction (String function, String value) throws ExpressionException {

String lowerFun = function.toLowerCase ()

Double db = 0

Try

{

Db = Double.valueOf (this.getValue (value)) .doubleValue ()

If (lowerFun.equals ("log")) {

Return String.valueOf (Math.log (db))

}

Else if (lowerFun.equals ("square")) {

Return String.valueOf (Math.pow (db, 2))

}

Else if (lowerFun.equals ("sqrt")) {

Return String.valueOf (Math.sqrt (db))

}

Else if (lowerFun.equals ("sin")) {

Return String.valueOf (Math.sin (db))

}

Else if (lowerFun.equals ("asin")) {

Return String.valueOf (Math.asin (db))

}

Else if (lowerFun.equals ("cos")) {

Return String.valueOf (Math.cos (db))

}

Else if (lowerFun.equals ("tan")) {

Return String.valueOf (Math.tan (db))

}

Else if (lowerFun.equals ("atan")) {

Return String.valueOf (Math.atan (db))

}

Else if (lowerFun.equals ("ceil")) {

Return String.valueOf (Math.ceil (db))

}

Else if (lowerFun.equals ("exp")) {

Return String.valueOf (Math.exp (db))

}

} catch (Exception e)

{

Throw new ExpressionException ("function" + function + "value" + value + "illegal!")

}

Throw new ExpressionException ("function" + function + "not supported!")

}

Public static void main (String [] args)

{

BaseExpression be = new Base_Expression ()

String exp = "sin (ceil (sqrt (100) * 29" 20 "30" 3 "0 | 0 | 1" 1 "5" 2 "

Try

{

System.out.println (be.calculate (exp))

}

Catch (ExpressionException eE)

{

System.out.println (eE.getMessage ())

}

}

}

Expression exception class code:

Public class ExpressionException extends Exception {

Public ExpressionException (String msg)

{

Super (msg)

}

}

The above is all the content of this article "how to implement expression evaluation source code in JAVA". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Expressions operations source code operators content functions articles support code learning help meaning easy to understand more organization knowledge components ribbon industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info Huawei OPPO Reno Redmi