用Java以OO方式表示逻辑运算

发布于 2024-11-17 12:57:11 字数 692 浏览 7 评论 0原文

我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如痴如狂 2024-11-24 12:57:12

遵循您的接口约定:

interface Conditional {
    public Condition Condition();
    public int TrueValue();
    public int FalseValue();
}

我将使用类,而不是引用整数,我也会引用 Expression 类。

Following your interface convention:

interface Conditional {
    public Condition Condition();
    public int TrueValue();
    public int FalseValue();
}

I would use classes and instead of referring to ints, I would refer to an Expression class too.

Smile简单爱 2024-11-24 12:57:12

与其采用这种方法,不如使用 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 in right 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 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文