当前位置:网站首页>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
边栏推荐
- vsCode函数注解/文件头部注解快捷键
- Wild melon or split melon?
- Qt编译时,出现 first defined here,原因及解决方法
- Floweable source code annotation (40) class delegation
- How to transmit and share 4GB large files remotely in real time?
- json数据比较器
- HDU - 1069 Monkey and Banana(DP+LIS)
- Set set detailed explanation
- Actual combat: basic use of Redux
- Typeorm framework
猜你喜欢
随机推荐
【问题思考总结】为什么寄存器清零是在用户态进行的?
Xuanyi maintenance manual
Web Security (x) what is OAuth 2.0?
Data governance: data governance framework (Part I)
Know the future of "edge computing" from the Nobel prize!
Actual combat: basic use of Redux
【考研高数 自用】高数第一章基础阶段思维导图
POL8901 LVDS转MIPI DSI 支持旋转图像处理芯片
CentOS 7使用yum安装PHP7.0
boot+jsp的高校社团管理系统(附源码下载链接)
为什么用葫芦儿派盘取代U盘?
Brief description of activation function
【考研高数 武忠祥+880版 自用】高数第二章基础阶段思维导图
OneFlow源码解析:算子签名的自动推断
Continue to learn MySQL
rust猜数字游戏
Mongodb学习篇:安装后的入门第一课
输入一个表达式(用字符串表示),求这个表达式的值。
从底层结构开始学习FPGA----RAM IP的定制与测试
json数据比较器









