2017-04-25 6 views
0

나는 몇 가지 문제가 있으며 누구든지 다음과 같이 내 문제를 해결할 수 있기를 바랍니다.표현 및 루핑 과정

  1. 첫 번째 질문; 수학

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

    출력이 여전히 2.000 인 이유는 무엇입니까?

  2. 두 번째 질문; "첫 루핑"프로세스/증분을위한 휴식 "셋째 루핑"프로세스를 중지하고 계속하는 방법

    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가 생산하는 결과가 명령에 제공되고 있음을 알 수 없습니다 부동 소수점 수를 원하기 때문에 두 피연산자 모두 정수이므로 정수 나누기를 사용합니다. 당신이 ($a+2)/22 것을 볼 수 있도록 나뿐만 아니라 중간 값을 인쇄 한

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

;

우리는 아무 것도 변경하지 않고 여러 단계에서 모든 일을 할 수 Tcl의 정수부가 내림합니다. 그게 사실이라면, format이 값을 2.000으로 렌더링한다는 것이 놀랄 일이 아닙니다.


두 번째 질문의 경우 Tcl은 다중 레벨 break을 지원하지 않습니다. 그것은 매우 자주 문제가되지 않으며, 헬퍼 절차의 전략적 사용에 의해 처리 될 수있는 대부분의 경우입니다.

보다 자세히는 break을 사용하여 현재 가장 안쪽의 루프는 중지하지만 가장 안쪽 루프는 중지 할 수 있습니다. 사이의 Second Looping process은 가장 안쪽의 중첩 수준 인 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 
     } 
    } 
} 

이 :

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 
     } 
    } 
} 

대신 플래그 변수를 사용하여 깊이 까다로운 사용자 정의 예외 코드를 피할 수 꽤 추악합니다 (특히 로직이 복잡해지면).하지만 작동합니다. 다행히도, 그것은 자주 나오지 않습니다.

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

arithmatic operator "/"의 출력은 적용되는 데이터 유형에 따라 다릅니다. 정수 값을 가지므로 출력은 정수입니다. 당신은 두 번째 질문에 대한

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

다음과 같이 먼저 떠 값 중 하나를 은밀한 할 수 명령은 세 번째 루프를 중지하고 첫 번째

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 
     } 
    } 
    } 
}