【发布时间】:2014-08-17 22:04:16
【问题描述】:
我正在制作一个反向波兰符号计算器。我写了一个游戏计划并试图填写它。我正处于我想要将数组中的整数移动到我的新数组中的部分(使用 shift 将其从数组中永久删除)。
这应该很容易,但我就是想不通。我试着用像
这样的东西来识别整数Integer.match(x)
x.class == Integer
但这不起作用。我也尝试过构建一些东西来拉取所有不在操作数组中的东西,比如这个失败的东西:
x.include?(operations)
我是新手,不擅长编码。如果你想责备我的代码是多么草率或者我发布的错误,那么已经有人这样做了。我真的很努力。谢谢,
class RPNCalculator
def initialize(problem)
@problem = problem
end
def resolve
array = @problem.split(" ")
@array = array
operations = ["+", "-", "*"]
@operations = operations
queue = []
@queue = queue
array.map! { |x| /[0-9]/.match(x) ? x.to_i : x}
array.each do |x|
#THIS IS WHERE I'M BUILDING & FAILING
array.shift(x)
queue << x
end
end
# iterate through the array
# if array elements don't match a operations element, push them into the queue
# once you have a match on the function, launch a method
# make 3 new RPN methods, one each for + - *
# method launched should affect last 2 items in the queue.
# result of that method gets pushed to the end of the queue.
# return the remaining value in the queue as the answer
# placeholder for method +
# placeholder for method -
# placeholder for method *
end
test = "1 3 +"
mytest = RPNCalculator.new(test)
mytest.resolve
【问题讨论】: