数学表达式(字符串)到Java中的数字 [英] Mathematical expression (string) to number in Java

查看:242
本文介绍了数学表达式(字符串)到Java中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找可以评估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 = -11

But 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_algorithm

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


分流码算法是一种
方法,用于解析以中缀表示法指定的数学
方程。
它可用于以
反向波兰表示法(RPN)或
抽象语法树(AST)生成输出。
算法是由Edsger
Dijkstra发明的,并命名为分流码
算法,因为它的操作
类似于铁路分流
码的算法。与RPN的评估一样,
调车码算法是基于堆栈的
。中缀表达式是数学符号的
形式,大多数是b $ b人习惯,例如3 + 4
或3 + 4 *(2-1)。对于转换,
是两个文本变量(字符串),
输入和输出。还有一个
堆栈,它将运算符还没有
添加到输出队列中。要转换,
程序按
顺序读取每个符号,并根据
符号执行某些操作。

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.

但是我已经在一些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).

编辑:找到它:< a href =http://tech.dolhub.com/Code/MathEval =nofollow> http://tech.dolhub.com/Code/MathEval


线性递归数学求值器

A Linear-Recursive Math Evaluator

这个数学表达式求值器的诞生是因为需要占用很小的空间高效的解决方案,可以合理有效地评估任意表达,无需预编译。我需要一些可以用变量做基本数学的东西,例如: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中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆