Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement expression calculation Source Code in JAVA

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report