解析字符串以获得公式 [英] Parsing a string to get a formula

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

问题描述

我正在尝试解析一个字符串,以获得类似公式的东西。我可以通过以下示例最好地解释我的问题:



示例1:(第0年12345年第1年/ 23451年)-1)* 100



解决方案

1.(

2. /

3.)-1)* 100



12345,1

23451,0



例2: [[1年1234] - (第2年4567)] /(第3年7890)* 100



解决方案

1 。(

2. -

3.)/(

4.)* 100


1234,1

4567,2

7890,3



这很容易当我们手动完成时,不幸的是我需要为数百个输入做这件事。这些例子随着任务提供给我。注意在示例2中如何忽略第三个括号,但是输入表达式仍然可以从解决方案中无错误地构建。



我只需要一些关于逻辑的帮助,如果我们可以从中得到一些东西,那么我可以把它写成一般的功能。

解决方案

那么,找到关于这个主题的足够材料的方法之一是搜索CodeProject: http://www.codeproject.com/search.aspx?q=Expression+parser&doctypeid=1 [ ^ ]。



但请不要告诉我这不是VBA。问题或多或少是严重的,能够解决严重问题的人很少同意在VBA上浪费时间。如果你想要一个VBA解决方案,请阅读一些相关的文章,了解代码的工作原理,并用你喜欢的语言和系统来实现它。



我认为这是另一种选择方法可能很有成效:在您的代码中托管一些可用的语言解释器。在实践中,最可能和最明显的候选者是Javascript:它已经具有函数 eval https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval [ ^ ])允许解释和从同一个脚本运行一些用字符串编写的脚本: http://www.codeproject.com/search.aspx?q=%28Javascript+interpreter%29+OR+%28%28host+OR+embed%29+Javascript%29& ; doctypeid = 1 [ ^ ]。



要熟悉托管/嵌入解释器的问题,您可以执行类似复杂查询的操作: http://www.codeproject.com/search.aspx?q=%28Javascript+interpreter%29+OR+% 28%28host + OR +嵌入%29 + Javascript%29& doctypeid = 1 [ ^ ]。



当然,您可以使用这些查询进行全球网络搜索,Google / Bing。这样,我发现一些文章甚至用于VB(但不是VBA):

http: //www.freevbcode.com/ShowCode.asp?ID=2090 [ ^ ],

http:// www。 freevbcode.com/ShowCode.asp?ID=1263 [ ^ ]。



主要障碍?当然是VBA。你只需要最简单的功能,它看起来像。因此,最简单的路线可能是在学习主要想法后,查看一些可用的表达式解释器并编写一些简约实现。



简而言之,这个想法是:单个字符串中的输入表达式被解析为表达式树。然后通过从根开始递归遍历树来执行计算: http://en.wikipedia.org/wiki/Expression_tree [ ^ ]。



所以,这是我个人更喜欢的另一种选择:按照上面引用的文章从头开始编写代码。



-SA

I am trying to parse a string to get something like a formula out of it. My question can be best explained by the following examples:

example 1: (12345 in year 1 / 23451 in year 0)-1)*100

solution
1. (
2. /
3. )-1)*100

12345, 1
23451, 0

example 2: [[1234 in year 1) - (4567 in year 2)] / (7890 in year 3) * 100

solution
1. (
2. -
3. )/(
4. )*100

1234, 1
4567, 2
7890, 3

It is quite easy when we do it manually but unfortunately I need to do it for hundreds of inputs. The examples were provided to me along with the task. Notice how the third brackets have been ignored in example 2 but the input expression can still be framed without error, from the solution.

I just need some help with the logic, if we can get something out of it, so it I can write it like a general fucntion.

解决方案

Well, one of the ways to find enough material on the topic is searching CodeProject: http://www.codeproject.com/search.aspx?q=Expression+parser&doctypeid=1[^].

But please, don't tell me "this is not VBA". The problem is more or less serious, and people capable of solving serious problems rarely agree to waste time on VBA. If you want a VBA solution, read some relevant article, understand how the code works and implement it in the language and the system you prefer.

I think that an alternative approach could be fruitful: hosting some available language interpreter in your code. In practice, the most likely and apparent candidate would be Javascript: it already has the function eval (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval[^]) which allows to interpret and run a script written in some string, from the same script: http://www.codeproject.com/search.aspx?q=%28Javascript+interpreter%29+OR+%28%28host+OR+embed%29+Javascript%29&doctypeid=1[^].

To get familiar with the problem of hosting/embedding of the interpreter, you can perform something like this "sophisticated" query: http://www.codeproject.com/search.aspx?q=%28Javascript+interpreter%29+OR+%28%28host+OR+embed%29+Javascript%29&doctypeid=1[^].

Of course, you can do a global Web search with these queries, Google/Bing it. This way, I found some articles even for VB (but not VBA):
http://www.freevbcode.com/ShowCode.asp?ID=2090[^],
http://www.freevbcode.com/ShowCode.asp?ID=1263[^].

The main obstacle? VBA, of course. You just need the simplest features, it looks like. So, probably, the shortest route will be to look at some available expression interpreter and write some minimalistic implementation after learning the main ideas.

In brief, the idea is: the input expression in a single string is parsed into an expression tree. Then the calculation is performed by recursive traversal of the tree starting from the root: http://en.wikipedia.org/wiki/Expression_tree[^].

So, this is yet another option I would personally prefer: write the code from scratch following the article referenced above.

—SA


这篇关于解析字符串以获得公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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