Skip to content

Instantly share code, notes, and snippets.

@ready4god2513
Created October 13, 2014 19:15
Show Gist options
  • Save ready4god2513/ac2ac52cc82b59dfe97b to your computer and use it in GitHub Desktop.
Save ready4god2513/ac2ac52cc82b59dfe97b to your computer and use it in GitHub Desktop.
class Stack
class UnderFlowError < StandardError;end
def initialize
@stack = []
end
def empty?
@stack.empty?
end
def push(val)
@stack.push(val)
end
def pop
raise UnderFlowError, "Stack is empty" if empty?
@stack.pop
end
def peek
@stack.last
end
end
class RPNExpression
def initialize(expr)
@expr = expr
end
def evaluate
stack = Stack.new
tokens.each do |token|
case
when numeric?(token)
stack.push(token.to_f) # Casting to a float instead of integer
when token.match(/^[\+\-\*]$/) # Just check for an operator
rhs = stack.pop
lhs = stack.pop
stack.push(lhs.send(token, rhs))
else
raise "whaaaat I don't know this token? #{token}"
end
end
stack.pop
end
private
def tokens
@expr.split(" ")
end
def numeric?(token)
Float(token) rescue false
end
end
puts RPNExpression.new("4 7 3 + 8 * 16.2 + *").evaluate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment