Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #NoTrayIcon
- ; Unit Calculator
- ; http://redd.it/2bxntq
- MsgBox(64, "3 metres to inches", UnitCalculator("3 metres to inches"))
- Func UnitCalculator($s)
- Local $a, $i, $x
- $a = StringRegExp($s, "(?i)^\h*(\d+(?:\.\d+)?)\h+([a-z ]+)\h+to\h+([a-z ]+)\h*$", 3)
- If @error Then Return SetError(1, 0, "invalid input -- check redd.it/2bxntq")
- $x = _UnitCalculator($a[0], $a[1], $a[2])
- If @error Then Return SetError(1, 0, StringFormat("%.2f %s can't be converted to %s", $a[0], $a[1], $a[2]))
- Return StringFormat("%.2f %s is %.2f %s", $a[0], $a[1], $x, $a[2])
- EndFunc
- Func _UnitCalculator($N, $oldUnits, $newUnits)
- Local Static $bData = "0x6D65747265732D3E6D65747265733A310D0A696E636865732D3E6D65747265733A302E30323534" _
- & "0D0A6D696C65732D3E6D65747265733A313630392E33340D0A6174746F706172736563732D3E6D65747265733A302E303330" _
- & "383536373735380D0A6B696C6F6772616D732D3E6B696C6F6772616D733A310D0A706F756E64732D3E6B696C6F6772616D73" _
- & "3A302E3435333539320D0A6F756E6365732D3E6B696C6F6772616D733A302E303238333439350D0A686F6773686561647320" _
- & "6F6620426572796C6C69756D2D3E6B696C6F6772616D733A3434302E37", $sData = BinaryToString($bData)
- Local $aSRE, $b = False
- $aSRE = __UnitCalculator($sData, $oldUnits, "metres")
- If @error Then
- $aSRE = __UnitCalculator($sData, $oldUnits, "kilograms")
- If @error Then Return SetError(1, 0, $N)
- $N *= $aSRE[0] ; N is now in kilograms
- $b = True
- Else
- $N *= $aSRE[0] ; N is now in metres
- EndIf
- $aSRE = __UnitCalculator($sData, $newUnits, ($b ? "kilograms" : "metres"))
- If @error Then Return SetError(2, 0, $N)
- Return $N / $aSRE[0]
- EndFunc
- Func __UnitCalculator(ByRef $1, $2, $3)
- Local $4 = StringRegExp($1, StringFormat("\Q%s->%s:\E(\d+(?:\.\d+)?)", $2, $3), 3)
- SetError(@error, @extended)
- Return $4
- EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement