Java 1.5:数学公式解析器 [英] Java 1.5: mathematical formula parser

查看:989
本文介绍了Java 1.5:数学公式解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我经常开发JTableModels,其中一些单元格必须包含一个简单的数学公式的结果。这个公式可以有:

Hello i often develop JTableModels in which some cells must contain the result of apliying a certain simple mathematical formula. This formulas can have:


  • 运算符(+, - ,*,/)

  • 数字常量

  • 其他单元格引用(包含数字)

  • 参数(引用名称为INTEREST_RATE的数字)

  • Operators (+,-,*,/)
  • Number constants
  • Other cell references (which contains numbers)
  • Parameters (numbers with a reference name like "INTEREST_RATE")

我经常解决它制作一个小的计算器类来解析公式,我定义了哪种语法。计算器类使用堆栈进行计算,语法总是使用波兰表示法。

I often resolve it making a little calculator class which parses the formula, which syntax i define. The calculator class uses a stack for the calcs, and the syntax uses allways a Polish notation.

但波兰表示法对我和我的用户来说都不自然。所以我的问题是......

But the Polish notation is unnatural for me and for my users. So my question is...

是否有一个运行在1.5 jvm的lib并且可以处理我的requeriments并使用正常的表示法(带括号,我不喜欢'知道公式的这种符号样式的名称吗?

PD它假设公式总是语法正确,我可以预处理不是常量提供它们的值

P.D it's supposed that the formulas are allways syntax correct and i can preprocess the numbers that are not constants to provide their values

推荐答案

你有没有想过 JSR-223 ?简而言之,这个规范允许Java开发人员与伟大的集成轻松动态语言及其解析器。使用这样的解析器,您需要定义解析器转换为需要定义内部DSL ,它解决了创建一个优秀的API,并让您的用户选择他们喜欢他们喜欢的Javascript / Groovy / Scala / WTF语法。

Have you thought about the benefits of JSR-223 ? in a few words, this spec allows Java developers to integrate with great ease dynamic languages and their parsers. Using such parser, your need for defining a parser transforms into the need for defining an internal DSL, which resolves into creating simply a good API, and letting your user choose wether they prefer Javascript/Groovy/Scala/WTF syntax they happen to prefer.

这篇关于Java 1.5:数学公式解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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