如何评价在C#中的自定义括号体现在哪里? [英] How to evaluate custom parenthesis expression in C#?

查看:193
本文介绍了如何评价在C#中的自定义括号体现在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的高级搜索功能,在表达我需要评估会是这个样子,在地方括号:

I am working on an Advanced Search feature where the expression I need to evaluate will look something like this with the parenthesis in place:

((Loan number is 1000 And
Lock Date is less than 12/03/2015) Or
Borrower SSN contains 12345) And
((Buy date is between 12/01/2015 and 23/02/2016 And
APR is less than 20000) Or
Loan amount is greater than 60000)

或者用简单的话

((condition1 And condition2) Or condition 3) And ((condition4 And condition5) Or condition6).

如果我们看一下括号中,条件1和条件2已经先进行评估,然后输出这是与条件3等等执行...

If we look at the parenthesis, the condition1 and condition2 has to be evaluated first and then the output of this is executed with condition 3 and so on....

我们有API在一个时间来评估两个条件。然而,在这种情况下所面临的挑战是

We have API to evaluate two conditions at a time. However the challenge in this context is

1)如何确定相应的括号和第一对其进行评估。然后用这个中间结果进行进一步的评估?

1) How to identify the corresponding parenthesis and evaluate them first. And then use this intermediate result for further evaluation?.

2)如何找到未使用括号?例如(((条件1和条件2))),在这种情况下,尽管不需要它有3个起始和3闭括号这是一个有效的表达

2)How to find unused parenthesis? For example (((condition1 And condition2))), in this case the though it is not required there are 3 starting and 3 closing parenthesis which is a valid expression.

我试图找到一些算法的这里这里

I tried finding some algorithm here and here

不过这需要基于令牌操纵这一次读取一个字符,这是一个算术表达式评估该计算机理解。在我的情况下,这些东西都是定制的,我们应该找到一个算法来做到这一点。任何人都可以建议我的方案更好的方法?

However this takes token based manipulation which reads one character at a time and it is an arithmetic expression evaluation that computer understands. In my case these things are custom and we should find a algorithm to do this. Can anyone suggest a better approach for my scenario?

推荐答案

如果我理解正确的话,你已经得到了表达式求值。你需要的是根据本括号分裂评估。我会使用一个循环,我在其中会发现内部括号组,使用此正则表达式:

If I understand you correctly, you already got the expression evaluator. What you need is to split the evaluations according to the parenthesis. I'd use a loop, in which I'd find inner parenthesis groups, using this regex:

\(([^()]*)\)

然后,如果发现,随着结果替换它们的。计算功能,并重复,直到最终的字符串仍然存在,没有括号

Then, if found, replace them with the result of your evaluation routine, and repeat until a final string remains, without parenthesis.

伪代码:

Find a string enclosed by (), not containing any ()
If found
    Replace it with the evaluated value of the string (including parenthesis)
    Go again
Return result

关于的括号,让他们来处理的相同。他们会在你的计算程序,作为一个单一的价值而告终。

About the unused parenthesis, let them be treated the same. They'll end up in your evaluation routine as a single value.

勾选此小提琴。而不是评估它返回一个随机数, 0 1 ,但它展示了逻辑。

Check this fiddle. Instead of evaluating it returns a random number, 0 or 1, but it demonstrates the logic.

希望这有助于。

问候。

这篇关于如何评价在C#中的自定义括号体现在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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