Java中的数学表达式(字符串)转数字

发布于 2024-10-02 11:01:15 字数 473 浏览 8 评论 0原文

我试图找到类似 Java Embedding Plugin (JEP) 的东西,它可以评估数学公式(字符串)并返回答案。

但它也应该计算一个变量,例如: (25+36+x)*2 = 25 应该给出: x = -11

有点像 http://www.wolframalpha.com/,但它不应该那么通用,它应该可以离线工作。

开源是首选。

我的小计算器项目 http://sourceforge.net/projects/calex/ 需要它。

I'm trying to find something like Java Embedding Plugin (JEP) that can evaluate a mathematical formula (string) and give back the answer.

But it should also calculate a variable, for example: (25+36+x)*2 = 25 should give: x = -11

A little like http://www.wolframalpha.com/, but it shouldn't be that versatile, and it should work off-line.

Open source is preferred.

I need it for my little calculator project, http://sourceforge.net/projects/calex/.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘年祭陌 2024-10-09 11:01:15

这称为算术评估。实现此目的的最简单方法之一是使用 Edsger Dijkstra Shunting-yard_algorithm

调车场算法是
数学解析方法
以中缀表示法指定的方程。
它可用于产生输出
逆波兰表示法 (RPN) 或作为
抽象语法树(AST)。这
算法是由 Edsger 发明的
Dijkstra 并命名为“调车场”
算法因为它的操作
类似于铁路调车
院子。与 RPN 的评估一样,
调车场算法为
基于堆栈的。中缀表达式是
最数学符号形式
人们习惯了,比如3+4
或 3+4*(2−1)。对于那里的转换
是两个文本变量(字符串),
输入和输出。还有一个
尚未保存运算符的堆栈
添加到输出队列。要转换,
程序按顺序读取每个符号
并以此为基础做一些事情
符号。

但我在一些 stackoverflow 用户博客上看到了您正在寻找的确切解决方案,但我不记得地址了(就像“代码猴子主义”)。它是轻量级类,您可以在小程序中使用它(您还可以定义常量和重置值)。

编辑:找到它:http://tech.dolhub.com/Code/数学评估

线性递归数学计算器

这个数学表达式求值器的诞生是因为需要一个占用空间小且高效的解决方案,该解决方案可以合理有效地评估任意表达式,而无需预编译。我需要一些可以对变量、表达式进行基本数学运算的东西,例如:“Top+2”、“Bottom-2”和“(Right+1-Left)/2”。

互联网上的研究发现了许多相当好的解决方案,所有这些都围绕着创建解析树(这是有道理的)。问题是 - 它们都相当庞大,我无法仅仅为了数学而将 100K 添加到我的小程序大小。所以我开始想知道这个问题的线性递归解决方案。最终结果是一个性能可接受的单一类,没有外部依赖项,重量不到 10 KiB。

This is called Arithmetic evaluation. One of the easiest way to implement this is using Edsger Dijkstra Shunting-yard_algorithm.

The shunting-yard algorithm is a
method for parsing mathematical
equations specified in infix notation.
It can be used to produce output in
Reverse Polish notation (RPN) or as an
abstract syntax tree (AST). The
algorithm was invented by Edsger
Dijkstra and named the "shunting yard"
algorithm because its operation
resembles that of a railroad shunting
yard. Like the evaluation of RPN, the
shunting yard algorithm is
stack-based. Infix expressions are the
form of mathematical notation most
people are used to, for instance 3+4
or 3+4*(2−1). For the conversion there
are two text variables (strings), the
input and the output. There is also a
stack that holds operators not yet
added to the output queue. To convert,
the program reads each symbol in order
and does something based on that
symbol.

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).

Edit: Found it: http://tech.dolhub.com/Code/MathEval

A Linear-Recursive Math Evaluator

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".

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.

最初的梦 2024-10-09 11:01:15

我根据 Dijkstra 的 Shunting Yard 算法发布了一个表达式求值器,遵循 Apache 许可证 2.0

http://projects.congrace.de/exp4j/index.html

I released an expression evaluator based on Dijkstra's Shunting Yard algorithm, under the terms of the Apache License 2.0:

http://projects.congrace.de/exp4j/index.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文