【问题标题】:Logic to Divide IF-ELSE-ENDIF loop划分 IF-ELSE-ENDIF 循环的逻辑
【发布时间】:2013-02-14 13:27:08
【问题描述】:

这是我的第一个问题,如果有任何问题,请纠正我。 我在一个文档系统中有一些旧规则,我试图将它们转换为新的文档系统。 我有很多 IF-ENDIFIF-ELSE-ENDIF 嵌套在彼此内部,如下所示。需要一些逻辑来将以下输入转换为相应的输出。 需要算法帮助。谢谢

INPUT:  
IF (Cond 1)  
    IF(Cond 2)  
    ENDIF  
    IF(Cond3)  
    ELSE  
    ENDIF  
ELSE  
    IF(Cond4)  
    ELSE  
        IF(Cond5)  
        ELSE  
        ENDIF  
    ENDIF  
    IF(Cond6)  
    ENDIF  
ENDIF  

所需的输出:

    IF(Cond1) AND (Cond2)  
    IF(Cond1) AND (Cond3)  
    IF(Cond1) AND !(Cond3)  
    IF!(Cond1) AND (Cond4)  
    IF!(Cond1) AND !(Cond4)  AND (Cond5)  
    IF!(Cond1) AND !(Cond4) AND !(Cond5)  
    IF!(Cond1) AND (Cond6)  

【问题讨论】:

  • 你是怎么得出输出的?在代码中应用相同的逻辑...
  • 我手动输入了那个输出
  • 那么,您需要解析输入并从解析的内容生成输出?
  • 是的,我最初尝试使用 if 条件的 not(!) 删除 else,然后结合 if 条件,但不适用于所有情况。

标签: java algorithm logic


【解决方案1】:

我将假设您首先具有可以解析文件的逻辑。如果是这样,那么您应该最终得到一个抽象语法树,其中每个节点看起来像这样:

If
  |
  +--- Condition
  |
  +--- Positive statement
  |
  +--- Negative statement

Sequence
  |
  +--- Statement 1
  |
  +--- Statement 2
  |
  ...
  |
  +--- Statement n

Terminal

其中 Terminal 代表一个具体的语句。它们隐含在您的原始输入文件中。例如,“IF(COND2) ENDIF”将表示如下:

If
  |
  +--- Cond2
  |
  +--- Terminal
  |
  +--- (null)

在你的情况下,你的实际树看起来像这样:

If
  |
  +--- Cond1
  |
  +--- Sequence
  |      |
  |      +--- If
  |      |      |
  |      |      +--- Cond2
  |      |      |
  |      |      +--- Terminal
  |      |      |
  |      |      +--- (null)
  |      |
  |      +--- If
  |             |
  |             +--- Cond3
  |             |
  |             +--- Terminal
  |             |
  |             +--- Terminal
  |
  +--- If
       ...

要生成输出,您只需递归地沿着树向下走,沿途构建一堆条件,然后当您到达一条语句时,输出整个条件堆栈,并在它们之间使用 AND。这是一些伪代码:

void treeWalk(root):
    treeWalk(root, []);

void treeWalk(root, conditions):
    case root of:
        If(cond, positive, negative):
            if (positive is not null):
                treeWalk(positive, conditions + cond)
            if (negative is not null):
                treeWalk(negative, conditions + !cond)
        Sequence(statements):
            for each statement in statements:
                treeWalk(statements, conditions)
        Terminal:
            print "IF "
            for each condition in conditions:
                if (condition is not the last condition):
                    print " AND "
                print condition

这里我使用 + 表示将项目附加到列表中。假设 !cond 会导致打印出一个“!”的条件。在前面。

希望对你有帮助!

【讨论】:

    【解决方案2】:

    假设您将输入读取并解析为树状数据结构,其中每个节点代表一个“if-else”语句,子节点是嵌套的 if-else 语句,这里有一些粗略的伪代码,应该可以为您提供一般信息想法:

    process(tree,output)
      if tree == null
        write output to file
        return
      for each child in body of if
         process(child,output + "AND <condition of root node in tree>")
      for each child in body of else
         process(child,output + "AND !<condition of root node in tree>")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多