Quantcast
Channel: AutoIt v3 - General Help and Support
Viewing all articles
Browse latest Browse all 12506

Power

$
0
0
This is the code which I wrote with For loop to caculate Exponent..
It can be listed pow 1, pow2...,etc
Noraml Forloop ver.
[ autoit ]         
$x = InputBox("Power", "Base:") $y = InputBox("Power", "Exponent:") MsgBox(0, "", powFor($x, $y)) Func powFor($x, $y)     Dim $n, $t = $x, $c = 1, $p = 0     If $y == 0 Then         Return 1     ElseIf $y < 0 Then         For $i = StringRegExpReplace($y, '-(\d+)', '$1') To 1 Step - 1             If $c = 1 Then                 $x *= 1                 $c += 1                 $p += 1                 $n &= "p" & $p & " : " & (1 / $x) & @CRLF             Else                 $x *= $t                 $p += 1                 $n &= "p" & $p & " : " & (1 / $x) & @CRLF             EndIf         Next     Else         For $i = $y To 1 Step - 1             If $c == 1 Then                 $x *= 1                 $c += 1                 $p += 1                 $n &= "p" & $p & " : " & $x & @CRLF             Else                 $x *= $t                 $p += 1                 $n &= "p" & $p & " : " & $x & @CRLF             EndIf         Next     EndIf     Return $n EndFunc


And this is the Recursion for caculating Exponent..
But how can I list each pow in Recursion ver. as For loop ver.
Recursion ver.
[ autoit ]      
MsgBox(0, "", powRec($x, $y)) Func powRec($x, $y)     If $y == 0 Then         Return 1     ElseIf $y == 1 Then         Return $x     Else         Return $x * powRec($x, $y - 1)     EndIf EndFunc

Viewing all articles
Browse latest Browse all 12506

Trending Articles