当前位置:网站首页>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
边栏推荐
- excel高级绘图技巧100讲(一)-用甘特图来展示项目进度情况
- 输入一个表达式(用字符串表示),求这个表达式的值。
- POL8901 LVDS转MIPI DSI 支持旋转图像处理芯片
- Data governance: metadata management implementation (Part IV)
- HDU - 1024 Max Sum Plus Plus(DP)
- Don't put your notes and videos everywhere!
- vsCode函数注解/文件头部注解快捷键
- Codeforces Round #803 (Div. 2)vp
- Mongodb學習篇:安裝後的入門第一課
- 从底层结构开始学习FPGA----RAM IP的定制与测试
猜你喜欢
![[excel] column operation, which performs specific column for data in a cell, such as text division by comma, colon, space, etc](/img/c8/e3e31ad9ef214d97228cb501dd752f.jpg)
[excel] column operation, which performs specific column for data in a cell, such as text division by comma, colon, space, etc
![Fiber Bragg grating (FBG) notes [1]: waveguide structure and Bragg wavelength derivation](/img/83/97c0c6e23cbb7b4c2b630c217c5206.jpg)
Fiber Bragg grating (FBG) notes [1]: waveguide structure and Bragg wavelength derivation

Boot + jsp University Community Management System (with source Download Link)

千万不要把笔记视频乱放!

穿越派·派盘 + 思源笔记 = 私人笔记本

How to transmit and share 4GB large files remotely in real time?

JDBC common interview questions

【考研高数 武忠祥+880版 自用】高数第二章基础阶段思维导图

Crossing pie · pie pan + Mountain duck = local data management

HCM 初学 ( 一 ) - 简介
随机推荐
Memtable for leveldb source code analysis
Fiber Bragg grating (FBG) notes [1]: waveguide structure and Bragg wavelength derivation
OneFlow源码解析:算子签名的自动推断
Boot + jsp University Community Management System (with source Download Link)
HCM 初学 ( 三 ) - 快速输入PA70、PA71 浏览员工信息PA10
论文学习记录随笔 多标签之GLOCAL
Continue to learn MySQL
【问题思考总结】为什么寄存器清零是在用户态进行的?
Floweable source code annotation (40) class delegation
[QT] QT after addition, subtraction, multiplication and division, two decimal places are reserved
Multi table operation - foreign key cascade operation
Trust guessing numbers game
Ssm+mysql second-hand trading website (thesis + source code access link)
教务管理系统(免费源码获取)
我从技术到产品经理的几点体会
从诺奖知“边缘计算”的未来!
On the first day of the new year, 3000 Apache servers went down
Deeply understand the underlying implementation principle of countdownlatch in concurrent programming
Detailed explanation of set
Send you through the data cloud