当前位置:网站首页>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
边栏推荐
- LeetCode 最大矩形,最大正方形系列 84. 85. 221. 1277. 1725. (单调栈,动态规划)
- Data governance: data governance management (Part V)
- Understand several related problems in JVM - JVM memory layout, class loading mechanism, garbage collection
- [medical segmentation] u2net
- Unity 使用Sqlite
- A little assistant for teenagers' physiological health knowledge based on wechat applet (free source code + project introduction + operation introduction + operation screenshot + Thesis)
- 移动端常用解决方案
- 2022第八届中国国际“互联网+”大学生创新创业大赛产业命题赛道开启报名!
- QT等待框制作
- 为什么用葫芦儿派盘取代U盘?
猜你喜欢

Detailed explanation of set

Crossing sect · paipan + Siyuan notes = private notebook

这才是大学生必备软件 | 知识管理

Chapitre d'apprentissage mongodb: Introduction à la première leçon après l'installation

MySQL converts milliseconds to time string

教务管理系统(免费源码获取)

穿越派 你的数据云行

葫芦儿 APP 使用帮助

Learn the customization and testing of fpga---ram IP from the bottom structure

穿越派·派盘 + Mountain Duck = 数据本地管理
随机推荐
How to create a progress bar that changes color according to progress
POL8901 LVDS转MIPI DSI 支持旋转图像处理芯片
喊我们大学生个人云服务特供商
Debug details under pycharm
数据库连接池的简单实现
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
SSM的教务管理系统(免费源码获取)
boot+jsp的高校社團管理系統(附源碼下載鏈接)
轩逸保养手册
基于TI DRV8424驱动步进电机实现调速和行程控制
TypeORM 框架
Detailed explanation of set
为了保护自己的数据,他奋斗了一天一夜
【笔记】电商订单数据分析实战
Know the future of "edge computing" from the Nobel prize!
2022.6.30-----leetcode. one thousand one hundred and seventy-five
【考研高数 自用】高数第一章基础阶段思维导图
【医学分割】u2net
Unity 使用Sqlite
C语言初阶——实现扫雷游戏