当前位置:网站首页>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
边栏推荐
- Memtable for leveldb source code analysis
- MySQL converts milliseconds to time string
- What things you didn't understand when you were a child and didn't understand until you grew up?
- Daily code 300 lines learning notes day 11
- Mongodb學習篇:安裝後的入門第一課
- CentOS 7 installed php7.0 using Yum or up2date
- Unity project experience summary
- How to create a progress bar that changes color according to progress
- OneFlow源码解析:算子签名的自动推断
- Codeforces Round #803 (Div. 2)vp
猜你喜欢

MySQL数据迁移遇到的一些错误

College community management system based on boot+jsp (with source code download link)

Memtable for leveldb source code analysis

Ssm+mysql second-hand trading website (thesis + source code access link)

Actual combat: basic use of Redux

Build 2022 上开发者最应关注的七大方向主要技术更新

了解 JVM 中几个相关问题 — JVM 内存布局、类加载机制、垃圾回收

喊我们大学生个人云服务特供商

Advanced cross platform application development (III): online resource upgrade / hot update with uni app

论文学习记录随笔 多标签之GLOCAL
随机推荐
HDU - 1069 Monkey and Banana(DP+LIS)
HCM 初学 ( 一 ) - 简介
新手在挖财开通证券账户安全吗?
C语言初阶——实现扫雷游戏
RecycleView的一些使用
OneFlow源码解析:算子签名的自动推断
激活函数简述
基于微信小程序的青少年生理健康知识小助手(免费获取源码+项目介绍+运行介绍+运行截图+论文)
HDU - 1024 Max Sum Plus Plus(DP)
[medical segmentation] u2net
不是你脑子不好用,而是因为你没有找到对的工具
分片上传与断点续传
从诺奖知“边缘计算”的未来!
芯片,建立在沙粒上的帝国!
Unity项目心得总结
POL8901 LVDS转MIPI DSI 支持旋转图像处理芯片
vsCode函数注解/文件头部注解快捷键
On the first day of the new year, 3000 Apache servers went down
LeetCode 最大矩形,最大正方形系列 84. 85. 221. 1277. 1725. (单调栈,动态规划)
这才是大学生必备软件 | 知识管理