Skip to content

Instantly share code, notes, and snippets.

@def-
Forked from Learath2/equ.nim
Last active August 29, 2015 14:23
Show Gist options
  • Save def-/70ef4afa22552b3ce20d to your computer and use it in GitHub Desktop.
Save def-/70ef4afa22552b3ce20d to your computer and use it in GitHub Desktop.
from strutils import replace, find, parseInt
echo "Input an operation:"
try:
let
line = stdin.readLine.replace(" ", "")
opindex = line.find({'+','-','*','/'})
if opindex == -1:
raise newException(ValueError, "No known operand found")
let
op = line[opindex]
arg1 = line[0..opindex-1].parseInt
arg2 = line[opindex+1..^1].parseInt
result = case op
of '+': arg1 + arg2
of '-': arg1 - arg2
of '*': arg1 * arg2
of '/': arg1 div arg2
else: 0
echo "Result = ", result
except:
echo "An error occured: ", getCurrentExceptionMsg()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment