将简单表达式传递给方法

将简单表达式传递给方法

本文介绍了高效编码-将简单表达式传递给方法(并对其进行求值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在构建一种方法,我想为其传递(以及其他方式)一个简单的比较表达式:< left_value> < operator> < right_value> ;.
然后,该方法将执行比较并返回结果.

表达式两侧的数据结构可以是double或datetime类型的普通变量或一维数组.如果数据是数组,则将对数组的每个元素求表达式.

我正在考虑将表达式作为文本字符串发送,然后进行解析,或者将表达式作为三个参数(left,op和right)传递.

现在,我的问题是如何在我的方法中组织代码.目前,我仅用一段代码即可对输入的每种可能的变体进行评估.但是,这给了我大量的代码.
2种数据类型* 2种数据结构类型* 2个运算符的侧面*最少6个运算符给出48种可能的组合.这是很多switch/select case语句来处理应该相当简单的事情.

由于我将创建许多采用类似参数的方法,因此,如果我可以更高效地编写代码,将会有很大帮助.我没有将信息传递给方法的任何特定方式,因此所有选项都是打开的.

与往常一样,所有建议都将受到欢迎.

Hello

I''m constructing a method for which I would like to pass (amongst other things) a simple comparison expression: <left_value> <operator> <right_value>.
The method would then perform the comparison and return the result.

The data structure on either side of the expression can be a plain variable or a one-dimensional array, of type double or datetime. If the data is an array, the expression will be evaluated for each element of the array.

I was thinking of sending the expression it as a text string and then parse it, or pass the expression as three arguments (left, op and right).

Now, my problem is how to organize the code in my method. Currently I simply have one piece of code to perform the evaluation for each possible variant of the input. However, this gives me a tremendous amount of code.
2 data types * 2 types of data structures * 2 sides of the operator * minimum 6 operators gives 48 possible combinations. That''s a lot of switch/select case statements to handle something which should be fairly simple.

As I will create many methods taking similar arguments, it would help a lot if I could write the code more efficiently. I am not locked to any particular way of passing the information to the method, so all options are open.

As always, all suggestions are most welcome.

推荐答案




这篇关于高效编码-将简单表达式传递给方法(并对其进行求值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:57