Skip to content

Instantly share code, notes, and snippets.

@mindplace
Created March 29, 2016 21:24
Show Gist options
  • Save mindplace/62a7005f92be9ef28bec1d70df734d18 to your computer and use it in GitHub Desktop.
Save mindplace/62a7005f92be9ef28bec1d70df734d18 to your computer and use it in GitHub Desktop.
class RPNCalculator
attr_accessor :calculator
def initialize
@stack = []
@value = 0
end
def push(number)
@stack << number
end
def plus
raise "calculator is empty" if @stack[-2..-1] == nil
@value = @stack[-2..-1].inject(:+)
@stack[-2..-1] = @value
end
def minus
raise "calculator is empty" if @stack[-2..-1] == nil
@value = @stack[-2..-1].inject(:-)
@stack[-2..-1] = @value
end
def divide
raise "calculator is empty" if @stack[-2..-1] == nil
items = @stack[-2..-1]
@value = items[0].to_f / items[1].to_f
@stack[-2..-1] = @value
end
def times
raise "calculator is empty" if @stack[-2..-1] == nil
@value = @stack[-2..-1].inject(:*)
@stack[-2..-1] = @value
end
def value
@value
end
def tokens(string)
string_stack = string.split
string_stack.map{|x| x.match(/[*+-\/]/) ? x.to_sym : x.to_i}
end
def evaluate_stack(symbol)
operations = {"+"=>"plus", "-"=>"minus", "*"=>"times", "/"=>"divide"}
operations.each do |operand, method|
if operand.to_sym == symbol.to_sym
self.send(method)
end
end
end
def evaluate(string)
string_stack = tokens(string)
updating_stack = string_stack
while string_stack.length != 1
string_stack.each_index do |i|
symbol = string_stack[i]
first_num = string_stack[i - 2]
second_num = string_stack[i - 1]
next unless symbol.is_a?(Symbol)
@stack << first_num.to_i
@stack << second_num.to_i
evaluate_stack(symbol)
symbol_index = updating_stack.index(symbol)
updating_stack[symbol_index - 2..symbol_index] = @value
break
end
string_stack = updating_stack
end
string_stack[0]
end
def input
puts "Please enter input one operand or operator at a time."
puts "Press enter on an empty line and calculator will run."
puts "Please begin to enter items now."
string = ""
while true
item = gets.chomp
break if item.empty?
string << item + " "
end
puts "Result: #{evaluate(string)}"
end
end
if __FILE__ == $PROGRAM_NAME
calc = RPNCalculator.new
calc.input
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment