Advertisement
James1337

Unit Calculator

Jul 28th, 2014
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 1.76 KB | None | 0 0
  1. #NoTrayIcon
  2.  
  3. ; Unit Calculator
  4. ; http://redd.it/2bxntq
  5.  
  6. MsgBox(64, "3 metres to inches", UnitCalculator("3 metres to inches"))
  7.  
  8. Func UnitCalculator($s)
  9.     Local $a, $i, $x
  10.     $a = StringRegExp($s, "(?i)^\h*(\d+(?:\.\d+)?)\h+([a-z ]+)\h+to\h+([a-z ]+)\h*$", 3)
  11.     If @error Then Return SetError(1, 0, "invalid input -- check redd.it/2bxntq")
  12.     $x = _UnitCalculator($a[0], $a[1], $a[2])
  13.     If @error Then Return SetError(1, 0, StringFormat("%.2f %s can't be converted to %s", $a[0], $a[1], $a[2]))
  14.     Return StringFormat("%.2f %s is %.2f %s", $a[0], $a[1], $x, $a[2])
  15. EndFunc
  16. Func _UnitCalculator($N, $oldUnits, $newUnits)
  17.     Local Static $bData = "0x6D65747265732D3E6D65747265733A310D0A696E636865732D3E6D65747265733A302E30323534" _
  18.     & "0D0A6D696C65732D3E6D65747265733A313630392E33340D0A6174746F706172736563732D3E6D65747265733A302E303330" _
  19.     & "383536373735380D0A6B696C6F6772616D732D3E6B696C6F6772616D733A310D0A706F756E64732D3E6B696C6F6772616D73" _
  20.     & "3A302E3435333539320D0A6F756E6365732D3E6B696C6F6772616D733A302E303238333439350D0A686F6773686561647320" _
  21.     & "6F6620426572796C6C69756D2D3E6B696C6F6772616D733A3434302E37", $sData = BinaryToString($bData)
  22.     Local $aSRE, $b = False
  23.     $aSRE = __UnitCalculator($sData, $oldUnits, "metres")
  24.     If @error Then
  25.         $aSRE = __UnitCalculator($sData, $oldUnits, "kilograms")
  26.         If @error Then Return SetError(1, 0, $N)
  27.         $N *= $aSRE[0] ; N is now in kilograms
  28.         $b = True
  29.     Else
  30.         $N *= $aSRE[0] ; N is now in metres
  31.     EndIf
  32.     $aSRE = __UnitCalculator($sData, $newUnits, ($b ? "kilograms" : "metres"))
  33.     If @error Then Return SetError(2, 0, $N)
  34.     Return $N / $aSRE[0]
  35. EndFunc
  36. Func __UnitCalculator(ByRef $1, $2, $3)
  37.     Local $4 = StringRegExp($1, StringFormat("\Q%s->%s:\E(\d+(?:\.\d+)?)", $2, $3), 3)
  38.     SetError(@error, @extended)
  39.     Return $4
  40. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement