2017-04-25 6 views
0

私はいくつかの問題を抱えています。誰でも私の問題を解決することができます。式とルーピングのプロセス

  1. 最初の質問;数学

    set a 3 
    puts [format "%.3f" [expr ($a+2)/2]] 
    

    なぜ出力はまだ2.000ですか?

  2. 2番目の質問; 「最初のループ処理」プロセス/ INCRのブレーク「第三ルーピング」のプロセスを停止して継続する方法

    for {set numA 0} {$numA < 5} {incr numA} {  ;#First Looping process 
        for {set numB 0} {$numB < $oneNum} {incr numB} { ;#Second Looping process 
        for {set numC 0} {$numC < $twoNum} {incr numC} { ;#Third Looping process 
         if {$dataA == $dataB} { 
         #How to stop incr "Third Looping" process and continue "First Looping" incr process 
         } 
        } 
        } 
    } 
    
    最初の質問については

答えて

1

は、exprそれが生成する結果がコマンドに指定されていることを知りません。浮動小数点数が必要なので、両方のオペランドが整数なので、整数除算を使用しています。

我々は何も変更せずに、いくつかの段階で全体のことを行うことができます

set a 3 
set b [expr ($a+2)/2] 
puts "b=$b" 
puts [format "%.3f" $b] 

あなたは($a+2)/22であることを見ることができるように、私は、同様の中間値をプリントアウトしてきました。 Tclの整数部は切り捨てられます。それが真実であるとすれば、format2.000という値をレンダリングすることは不思議ではありません。


2番目の質問では、Tclはマルチレベルbreakをサポートしていません。しかし、それは問題として非常に頻繁に起きることはなく、ヘルパ・プロシージャの戦略的使用によって処理できるケースの大半は、

詳細には、breakを使用して、現在の最も内側のループを停止することができますが、最も内側のループのみを停止することができます。その間のSecond Looping processは、最も内側の入れ子レベルのbreakによって停止されません。直接これに対処するための最も簡単なメカニズムは、カスタム例外コードですが、それはほとんど簡単です:

set MyException 123; # Small integer greater than 5 
for {set numA 0} {$numA < 5} {incr numA} {     #First Looping process 
    for {set numB 0} {$numB < $oneNum} {incr numB} {   #Second Looping process 
     try { 
      for {set numC 0} {$numC < $twoNum} {incr numC} { #Third Looping process 
       if {$dataA == $dataB} { 
        return -level 0 -code $MyException 
       } 
      } 
     } on $MyException {} { 
      break 
     } 
    } 
} 

あなたが代わりにフラグ変数を使用することにより深くトリッキーなカスタム例外コードを避けることができます:

for {set numA 0} {$numA < 5} {incr numA} {    #First Looping process 
    for {set numB 0} {$numB < $oneNum} {incr numB} {  #Second Looping process 
     set foundIt false 
     for {set numC 0} {$numC < $twoNum} {incr numC} { #Third Looping process 
      if {$dataA == $dataB} { 
       # Set the flag to indicate we've found our matching case 
       set foundIt true 
       # Terminate the inner loop 
       break 
      } 
     } 
     if {$foundIt} { 
      break 
     } 
    } 
} 

このかなり醜いです(特にロジックが複雑になった場合)が、動作します。幸いにも、それは非常に頻繁に上がらない。より頻繁に、人はこのようなコードを分割:

proc innerSearch {numA} { 
    global oneNum twoNum 
    for {set numB 0} {$numB < $oneNum} {incr numB} {  #Second Looping process 
     for {set numC 0} {$numC < $twoNum} {incr numC} { #Third Looping process 
      if {$dataA == $dataB} { 
       # Stop the Second Looping by returning from the procedure 
       return 
      } 
     } 
    } 
} 

for {set numA 0} {$numA < 5} {incr numA} {    #First Looping process 
    innerSearch $numA 
} 

これは、より多くのニーモニック名が使用される場合には、実際には多くの意味をなす傾向があります。

+0

おかげに行く続けるので、私はまだ理解していけないのです。私に例を挙げてみたいですか? –

+0

ありがとうございます! –

1

算術演算子 "/"の出力は、適用されているデータの種類によって異なります。あなたは整数値を持っているので、出力は整数です。あなたは2番目の質問については

puts [format "%.3f" [expr ($a+2)/double(2)]] 
2.500 

次のように最初に浮かぶように値のいずれかをひそかでき、コマンドは、第3のループを停止し、最初の1つの

for {set numA 0} {$numA < 5} {incr numA} {  ;#First Looping process 
    for {set numB 0} {$numB < $oneNum} {incr numB} { ;#Second Looping process 
    for {set numC 0} {$numC < $twoNum} {incr numC} { ;#Third Looping process 
     if {$dataA == $dataB} { 
     continue 
     } 
    } 
    } 
}