用Java以OO方式表示逻辑运算
我的java程序将允许根据用户定义的逻辑操作过滤csv文件的内容。数据将从两列 csv 文件的内容中读取
left right
5 10
2 6
,用户将在命令行上引用左右提供逻辑条件操作。程序应根据逻辑运算的结果过滤 csv 文件的内容。
IF ((left < right) AND (left !=10)) THEN 5 ELSE right
我计划将操作类型表示为
enum Operation
>,
<,
==,
!=
public boolean evaluate(int left,int right);
支持评估方法的枚举。操作的特定逻辑将在特定枚举类型内实现。
Condition 接口将支持访问左操作数、操作和右操作数的方法。
interface Condition
{
public int getLeft();
public Operation();
public int getRight();
}
对于 AND 情况,我只需维护一个条件列表并迭代该列表以确保所有条件均为 true。但是如何显示 i 代表 IF THEN ELSE 关键字呢?
My java program will allow the content of a csv file to be filtered based on a user defined logical operation. Data will be read from the content of two column csv file
left right
5 10
2 6
and user will provide a logical condition operation on the command line referencing left and right. The program should filter to content of the csv files based on the outcome of the logical operation.
IF ((left < right) AND (left !=10)) THEN 5 ELSE right
I plan to represent the operation types as an enum
enum Operation
>,
<,
==,
!=
public boolean evaluate(int left,int right);
which would support an evaluate method. The specific logic for the operation would be implemented within the specific enum types.
A Condition interface would support methods to access the left operand, operation and right operand
interface Condition
{
public int getLeft();
public Operation();
public int getRight();
}
For a AND case I would just maintain a list of Conditions and iterate over the list to ensure that all conditions are true. But how show i represent the IF THEN ELSE key words?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
遵循您的接口约定:
我将使用类,而不是引用整数,我也会引用
Expression
类。Following your interface convention:
I would use classes and instead of referring to ints, I would refer to an
Expression
class too.与其采用这种方法,不如使用 Velocity 之类的方法怎么样?基本上,您读取数组中
left
中的所有数字以及不同数组中right
中的所有数字,并将这些数组传递到 Velocity 上下文,然后让用户输入 Velocity如果有条件,则风格并让 Velocity 处理其余的事情?How about rather than taking that approach you use something like Velocity? You basically read all the numbers in
left
in an array and all the numbers inright
in a different array and pass these arrays to the Velocity context then let the user type a Velocity style if conditional and let Velocity take care of the rest ?