当前位置:网站首页>Enter an expression (expressed as a string) and find the value of this expression.
Enter an expression (expressed as a string) and find the value of this expression.
2022-07-01 05:44:00 【Ape Xiaocai cool】
When brushing algorithm problems , Come across a problem , Make a note of , The title is like this :
Enter an expression ( Represented by string ), Find the value of this expression .
Ensure that the valid characters in the string include [‘0’-‘9’],‘+’,‘-’, ‘*’,‘/’ ,‘(’, ‘)’,‘[’, ‘]’,‘{’ ,‘}’. And the expression must be legal .
Input description :
Enter an arithmetic expression
Output description :
Get the calculation results
Input :
3+2*{1+2*[-4/(8-6)+7]}
Output :
25
Look at the solution , as follows :
Scanner scan = new Scanner(System.in);
String input = s;
input = input.replace("[","(");
input = input.replace("{","(");
input = input.replace("}",")");
input = input.replace("]",")");
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("nashorn");
try {
System.out.println(scriptEngine.eval(input));
} catch (ScriptException e) {
throw new RuntimeException(e);
}
forehead , Beyond my imagination , This ScriptEngine It's amazing . Got it .
The general purpose is mainly to be able to parse general expressions , such as X >= 1(X Pass in as a parameter ) Such an expression , Also can use js Function syntax for , Create a java The same function exists in memory and can be called at any time , It is also possible to js Objects in are directly converted to java object .
Reference link :Java use ScriptEngine Parsing script
边栏推荐
- 码蹄集 - MT3114 · 有趣的平衡 - 用样例通俗地讲解
- He struggled day and night to protect his data
- 2/15 (awk, awk conditions, awk processing design can perform additional tasks, and use awk array +for loop to realize advanced search)
- Advanced cross platform application development (III): online resource upgrade / hot update with uni app
- 云盘里资料被和谐了,怎么办?
- Data governance: metadata management implementation (Part IV)
- CentOS 7 installed php7.0 using Yum or up2date
- Unity uses SQLite
- Continuous breakthrough and steady progress -- Review and Prospect of cross platform development technology of mobile terminal
- JDBC常见面试题
猜你喜欢
随机推荐
ssm+mysql二手交易网站(论文+源码获取链接)
Crossing pie · pie pan + Mountain duck = local data management
Printk debugging summary
Web Security (IX) what is JWT?
JDBC常见面试题
Leetcode top 100 questions 1 Sum of two numbers
Vscode function annotation / file header annotation shortcut
输入一个表达式(用字符串表示),求这个表达式的值。
Mongodb學習篇:安裝後的入門第一課
MySQL converts milliseconds to time string
[medical segmentation] u2net
穿越派 你的数据云行
Continue to learn MySQL
我从技术到产品经理的几点体会
SSGSSRCSR区别
mysql 将毫秒数转为时间字符串
Set set detailed explanation
加密狗资料搜集
Beauty of Mathematics - Application of Mathematics
bat操作ftp上传下载命令








