帕斯卡语中的双重"else"语句

4
我试图将以下Pascal代码翻译成C ++,但我在问题中遇到了“else else”结构。 我以前从未见过这个,所以有人可以告诉我它的作用以及它的C ++(或者也许是C)等效物吗?
  Procedure Force(Q:Int64;V,K:Integer);
   Var i,j,t:Integer;
    begin
     if K<=0 then
      if (Q>=A)and(Q Mod KK =0)and(V>=S)and(V<=F)then Out:=Out+1 else else
       For i:=0 to 9 do
        if (Q+(i+1)*h[k-1]>=A)and(Q+i*h[k-1]<=B) then
         if (Q+(i+1)*h[K-1]<B)and(Q+i*h[k-1]>=A) then
          Begin
           M:=(Q+i*h[k-1]) Mod KK;
           For j:=0 to 9*(K-1) do
            For t:=0 to KK-1 do
             if D[K-1,j,t]>0 then
              if (V+i+j>=S)and(V+i+j<=F)and((t+M) Mod KK=0) then
                 Out:=Out+D[K-1,j,t];
           end else
            if Odd(N-K+1) then Force(Q+i*h[k-1],V+i,K-1) else
                               Force(Q+i*h[k-1],V+i,K-1);
    end;

4
这是一个空的else语句,请参见这里的最后一段代码。 - emlai
2
将每个 else 与一个 if 对齐,你会发现这并不神奇。 - Martin York
我明白了,非常感谢。不过我真没想到我会疏忽那样的事情。 - Дмитрий Румянцев
3个回答

3

我刚刚将你写的文本复制到编辑器中(例如Komodo,其中可以选择Pascal作为语法颜色突出显示的语言),并以我自己能够阅读的方式重新格式化了它。

procedure Force(Q:Int64;V,K:Integer);
var 
  i,j,t:Integer;
begin
  if K<=0 then
    if (Q>=A) and (Q Mod KK =0) and (V>=S) and (V<=F) then
      Out:=Out+1
    else
  else
    for i:=0 to 9 do begin
      if (Q+(i+1)*h[k-1]>=A) and (Q+i*h[k-1] <= B) then
        if (Q+(i+1)*h[K-1]<B) and (Q+i*h[k-1] >= A) then begin
          M := (Q+i*h[k-1]) Mod KK;
          for j:=0 to 9*(K-1) do begin
            for t:=0 to KK-1 do begin
              if D[K-1,j,t] > 0 then
                if (V+i+j >= S) and (V+i+j <= F) and ((t+M) mod KK = 0) then
                  Out:=Out+D[K-1,j,t];
            end; {for t}
          end; {for j}
        end else
          if Odd(N-K+1) then
            Force(Q+i*h[k-1],V+i,K-1)
          else
            Force(Q+i*h[k-1],V+i,K-1);
      end;
    end;
end;

你不觉得现在更易懂了吗?

这是正确的,但我会将 if A then if B and C and D then 改为 if A and B and C and D then。(指以 if D[ 开头的两个 if 语句) - Rudy Velthuis

1
通常情况下,即使语法不要求,使用beginend成对出现也是非常有用的,这样可以使代码更易读懂。(将begin视为{的等价物,将end视为}的等价物;虽然你可以编写for(int i = 0; i < 10; i++) SomeCode();,但通常更清晰的做法是使用for(int i = 0; i < 10; i++) { SomeCode(); }。)
因此,你发布的代码中,适当添加了beginend成对出现,删除了一个或两个无操作的else,并进行了更合适的格式化,这让我觉得代码更易读懂。
Procedure Force(Q: Int64; V, K: Integer);
Var
  i, j, t: Integer;
begin
  if K <= 0 then
  begin
    if (Q >= A) and (Q Mod KK = 0) and (V >= S) and (V <= F) then
      Out := Out + 1;
  end
  else
  begin
    For i := 0 to 9 do
    begin
      if (Q + (i + 1) * h[K - 1] >= A) and (Q + i * h[K - 1] <= B) then
      begin
        if (Q + (i + 1) * h[K - 1] < B) and (Q + i * h[K - 1] >= A) then
        begin
          M := (Q + i * h[K - 1]) Mod KK;
          For j := 0 to 9 * (K - 1) do
          begin
            For t := 0 to KK - 1 do
            begin
              if D[K - 1, j, t] > 0 then
              begin
                if (V + i + j >= S) and (V + i + j <= F) and
                   ((t + M) Mod KK = 0) then
                  Out := Out + D[K - 1, j, t];
              end;
            end;
          end;
        end
        else if Odd(N - K + 1) then
          Force(Q + i * h[K - 1], V + i, K - 1)
        else
          Force(Q + i * h[K - 1], V + i, K - 1);
      end;
    end;
  end;
end;

1
我个人认为这样写不够易读。begin-end 的噪音太多了。 - Rudy Velthuis
@RudyVelthuis: 他正在将算法转化为C++,这样他就会少些begin-end的噪音!XD - sergiol
我个人认为,如果没有太多的begin-end噪音(或者在其他一些语言中是{-}噪音),执行流程同样清晰。 - Rudy Velthuis

0

这是一些可怕的缩进。如果我们更好地缩进它,就可以看到发生了什么:

if K<=0 then
    if (Q>=A)and(Q Mod KK =0)and(V>=S)and(V<=F) then 
        Out:=Out+1 
    else 
else
   For i:=0 to 9 do

这里,第一个else适用于if (Q>=A),但它是空的。


1
每个 else 匹配最近的可能的 if。我会使用 beginend 来使代码更清晰。 - Keith Thompson
1
你把可怕的缩进替换成了更可怕的缩进!! - sergiol
2
@sergiol:也许这不是你的选择,但原问题中的缩进是最糟糕的。它无法传达代码的结构。这个答案中的缩进非常标准,除了4个空格(或3个),这有点像C语言。 - Rudy Velthuis
2
@sergiol:一个没有语句(或者说是空语句)的else并不是缩进问题。它被添加在这里是为了让第二个else与第一个if绑定,而不是与第二个if绑定。鉴于这个决定,缩进是正确的。(正如我所提到的,一个更好的解决方案可能是使用beginend。) - Keith Thompson
1
@sergiol:就像Keith Thompson所说的那样。这不是缩进问题。我甚至同意Keith的观点,在这种情况下,我会使用begin和end而不是双else,但后者也是有效的。对于这种情况,这个答案展示了正确的缩进方式。 - Rudy Velthuis
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接