本文介绍了数学表达式(字符串)到Java中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试查找可以评估a的 Java嵌入插件(JEP)数学公式(字符串)并给出答案。I'm trying to find something like Java Embedding Plugin (JEP) that can evaluate a mathematical formula (string) and give back the answer.但它也应该计算一个变量,例如:(25 + 36 + x)* 2 = 25应该给出:x = -11But it should also calculate a variable, for example: (25+36+x)*2 = 25 should give: x = -11有点像 http:// www .wolframalpha.com / ,但它不应该是多功能的,它应该脱机工作。A little like http://www.wolframalpha.com/, but it shouldn't be that versatile, and it should work off-line.开源是首选。我的小计算器项目需要它, http: //sourceforge.net/projects/calex/ 。I need it for my little calculator project, http://sourceforge.net/projects/calex/.推荐答案这叫做算术评价。一个实现此最简单的方法是使用Edsger Dijkstra算法分流-yard_algorithmThis is called Arithmetic evaluation. One of the easiest way to implement this is using Edsger Dijkstra Shunting-yard_algorithm.但是我已经在一些stackoverflow用户博客上看到了你正在寻找的确切解决方案,但我记不起地址(它就像'代码猴子')。它是轻量级的,你可以在applet中使用(你也可以定义常量和重置值)。But I have seen exact solution what you are looking for on some stackoverflow user blog, but I can't remember the address (it was like 'code monkeyism'). It was lightweight class, that you could use in applets (you could also define constants and reset values). 编辑:找到它: http://tech.dolhub.com/Code/MathEval这个数学表达式求值器的诞生是因为需要占用很小的空间高效的解决方案,可以合理有效地评估任意表达,无需预编译。我需要一些可以用变量做基本数学的东西,例如:Top + 2,Bottom-2和(Right + 1-Left)/ 2。This math expression evaluator was born out of a need to have a small-footprint and efficient solution which could evaluate arbitrary expressions reasonably efficiently without requiring pre-compilation. I needed something which would do basic math with variables, expressions like: "Top+2", "Bottom-2" and "(Right+1-Left)/2".互联网上的研究提出了许多相当不错的解决方案,所有解决方案都围绕创建解析树(这是有意义的)。问题是 - 它们都相当笨重,我不能为我的applet大小添加100K只是为了数学。所以我开始想知道这个问题的线性递归解决方案。最终结果是一个可接受的单一类,没有外部依赖,权重低于10 KiB。Research on the Internet turned up a number of fairly good solutions, all of which revolved around creating parse trees (which makes sense). The problem was - they were all rather bulky, and I couldn't afford to add 100K to my applet size just for math. So I started wondering about a linear recursive solution to the problem. The end result is an acceptably performing single class with no external dependencies, weighing in under 10 KiB. 这篇关于数学表达式(字符串)到Java中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 11:08
查看更多