2016-05-11 5 views
4

私は次のPascalコードをC++に翻訳しようとしていましたが、問題の "else else"構造を見つけました。私はこれまでにこれを見たことがないので、誰かが何をしているのか、C++(あるいはC言語)の同等物は何かを教えてくれますか?ダブル 'else'文のパスカル

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文だ、HTTP([こちら]最後の抜粋を参照してください。 org/Else) – emlai

+2

それぞれの 'else'を''もしあなたが魔法がないことがわかるならば。 –

+0

ありがとう、ありがとう。しかし、私はそれを見落としたとは信じられません。 –

答えて

3

私はちょうどエディタ(構文カラー強調のための言語としてパスカルを選択できるコモドなど)にコピーし、あなたが書いたテキストを自分で読むことができるように再フォーマットしました。

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; 

今はもっと理解できると思いませんか?

+0

これは正しいですが、もし 'if A then then B and C and D then'を 'if A and B and C and D then'( 'if if'で始まる2つのif)に変更しました。 –

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'にマッチします。私は 'begin'と' end'を使ってコードをもっと明確にするでしょう。 –

+1

あなたは恐ろしいくぼみをより恐ろしいくぼみに置き換えました! – sergiol

+2

@sergiol:それはあなたの選択ではないかもしれませんが、元の質問のインデントは最悪です。それはコードの構造を伝えません。この答えのインデントはかなり標準的ですが、C-ishである4つのスペース(または3)を除きます。 –

1

beginendの組み合わせは、コードを読みやすく分かりやすくするために、構文で必須ではない場合でも、しばしば便利です。 (begin{end}の等価であるのと等価であることを考える;あなたはfor(int i = 0; i < 10; i++) SomeCode();を書くことができますが、それはfor(int i = 0; i < 10; i++) { SomeCode(); }を使用するために通常より明らかだ

追加beginendペアとあなたが投稿したように、コード、。 。//wiki.freepascal:ノーオペレーションelseまたは削除2、適切な、そしてより適切なフォーマットが私にははるかに読みやすいようだ

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'ノイズが多すぎます。 –

+0

@RudyVelthuis:彼はアルゴリズムをC++に渡しているので、 'begin-end'ノイズが少なくなります! XD – sergiol

+0

@Rudy:しかし、非常に明確な実行フロー。 –