endlesslove_1998

Reducing fractions

Dec 9th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 0.87 KB | None | 0 0
  1. Local $n = InputBox("Endless Love", "Enter the numerator of the fraction:")
  2. Local $d = InputBox("Endless Love", "Enter the denominator of the fraction:")
  3. If $d = 0 Then Exit MsgBox(0x10, "Error", "Denominator can't be zero!")
  4.  
  5. _ReduceFraction($n, $d)
  6.  
  7. MsgBox(0, "Endless Love", "Reduced fraction: " & ($n==0 ? $n : $n & "/" & $d))
  8.  
  9. ; Reduce a fraction
  10. Func _ReduceFraction(ByRef $iNumerator, ByRef $iDenominator)
  11.     Local $iGCD = _GCD($iNumerator, $iDenominator)
  12.     $iNumerator /= $iGCD
  13.     $iDenominator /= $iGCD
  14.     If $iDenominator < 0 Then
  15.         $iNumerator = -$iNumerator
  16.         $iDenominator = -$iDenominator
  17.     EndIf
  18. EndFunc
  19.  
  20.  
  21. ; Calculate the Greatest Common Divisor of $a and $b
  22. Func _GCD($a, $b)
  23.     $a = Abs($a)
  24.     $b = Abs($b)
  25.  
  26.     If ($a = 0 Or $b = 0) Then Return $a + $b
  27.  
  28.     While ($a <> $b)
  29.         If $a > $b Then
  30.             $a -= $b
  31.         Else
  32.             $b -= $a
  33.         EndIf
  34.     WEnd
  35.     Return $a
  36. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment