Skip to content

Instantly share code, notes, and snippets.

@Learath2
Created June 28, 2015 17:40
Show Gist options
  • Save Learath2/f1cc87187de85273205f to your computer and use it in GitHub Desktop.
Save Learath2/f1cc87187de85273205f to your computer and use it in GitHub Desktop.
Calculator
from strutils import replace,find,parseInt
echo("Input an operation:")
var line = readLine(stdin)
line = replace(line, " ", "");
let
opindex = find(line, {'+','-','*','/'})
op = line[opindex]
arg1 = parseInt(line[0..opindex-1])
arg2 = parseInt(line[opindex+1..high(line)])
var result: int = 0
case op
of '+':
result = arg1 + arg2
of '-':
result = arg1 - arg2
of '*':
result = arg1 * arg2
of '/':
result = (arg1 / arg2).int
else:
discard
echo("Result = ", result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment