双倍';其他';pascal语句 我试图把下面的Pascal代码翻译成C++,当我偶然发现“其他的”结构时。我以前从来没有见过这个,所以有人能告诉我它是做什么的吗?它是什么C++(或者C)等价物?< /P> 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; 过程力(Q:Int64;V,K:Integer); 变量i,j,t:整数; 开始 如果K=A)和(Q Mod KK=0)和(V>=S)和(V=A)和(Q+i*h[K-1]0,那么 如果(V+i+j>=S)和(V+i+j

双倍';其他';pascal语句 我试图把下面的Pascal代码翻译成C++,当我偶然发现“其他的”结构时。我以前从来没有见过这个,所以有人能告诉我它是做什么的吗?它是什么C++(或者C)等价物?< /P> 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; 过程力(Q:Int64;V,K:Integer); 变量i,j,t:整数; 开始 如果K=A)和(Q Mod KK=0)和(V>=S)和(V=A)和(Q+i*h[K-1]0,那么 如果(V+i+j>=S)和(V+i+j,c++,pascal,C++,Pascal,这是一个可怕的缩进。如果我们缩进得更好,我们可以看到发生了什么: 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 如果K=A)和(Q Mod KK=0)和(V>=S)和(V=A),但它是空的。我刚刚复制到一个编辑器(例如Komodo,在那里你可以选择Pascal作为语法颜色高亮

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

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

如果K=A)和(Q Mod KK=0)和(V>=S)和(V=A)
,但它是空的。

我刚刚复制到一个编辑器(例如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;
过程力(Q:Int64;V,K:Integer);
变量
i、 j,t:整数;
开始
如果K=A)和(Q Mod KK=0)和(V>=S)和(V=A)和(Q+i*h[K-1]0,那么

如果(V+i+j>=S)和(V+i+j使用
begin
end
对非常有用,即使它们不是语法所要求的,只是为了使代码更可读和更容易理解。(想想
begin
{
的等价物,
end
}
;虽然您可以为(inti=0;i<10;i++)SomeCode();
编写
,但通常更清楚地使用
(inti=0;i<10;i++){SomeCode();}

因此,您发布的代码,在适当的地方添加了
begin
end
对,删除了一两个no-op
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;
过程力(Q:Int64;V,K:Integer);
变量
i、 j,t:整数;
开始
如果K=A)和(Q Mod KK=0)和(V>=S)和(V=A)和(Q+i*h[K-1]=A),那么
开始
M:=(Q+i*h[K-1])模KK;
对于j:=0到9*(K-1)do
开始
对于t:=0到KK-1 do
开始
如果D[K-1,j,t]>0,那么
开始

如果(V+i+j>=S)和(V+i+j这是一个空的else语句,请参见最后一段。将每个
else
if
对齐,您将看到没有魔力。我明白了,非常感谢。但我不敢相信我忽略了类似的内容。每个
else
都与最接近的
if
匹配。我会使用
begin
>结束
让代码更清晰。你用更可怕的缩进替换了可怕的缩进!!@sergiol:这可能不是你的选择,但原始问题中的缩进是最糟糕的。它没有传达代码的结构。这个答案中的缩进很标准,除了4个空格(或3个空格)这相当于C-ish。@sergiol:a
else
没有语句(或者更确切地说是空语句)不是缩进问题。它添加在这里,以便第二个
else
绑定到第一个
if
而不是第二个。根据该决定,缩进是正确的。(正如我提到的,一个可以说更好的解决方案是使用
begin
end
)@塞吉奥:基思·汤普森说的。这不是缩进问题。我甚至同意基思的观点,在这里,我会使用开始和结束,而不是双精度的else,但后者也是有效的。在这种情况下,这个答案显示了适当的缩进。这是正确的,但我会将
如果a,那么如果B和C,那么D
改为
如果a和BC和D,然后< /COD>(两个IFS,从Debug开始),如果D[[/COD> ]),我个人发现不太可读。太多的代码>开始结束< /代码>噪声。@ RouyVeluui:他正在把算法传递给C++,所以他将有更少的<代码>开始结束< /代码>噪音!XD@I如果没有太多的开始-结束噪音,我个人会发现执行流程同样清晰(或者,在其他一些语言中,{-}噪音)。