“评估后缀表达式” Ruby 程序
我尝试制作一个小脚本来计算 Ruby 中的后修复表达式。
def evaluate_post(expression)
my_stack = Stack.new
expression.each_char do |ch|
begin
# Get individual characters and try to convert it to integer
y = Integer(ch)
# If its an integer push it to the stack
my_stack.push(ch)
rescue
# If its not a number then it must be an operation
# Pop the last two numbers
num2 = my_stack.pop.to_i
num1 = my_stack.pop.to_i
case ch
when "+"
answer = num1 + num2
when "*"
answer = num1* num2
when "-"
answer = num1- num2
when "/"
answer = num1/ num2
end
# If the operation was other than + - * / then answer is nil
if answer== nil
my_stack.push(num2)
my_stack.push(num1)
else
my_stack.push(answer)
answer = nil
end
end
end
return my_stack.pop
end
- 我不知道在不使用这种粗略方法或正则表达式的情况下检查表达式中的字符是否为整数的更好方法。 你们有什么建议吗?
- 有没有办法抽象案例。 Ruby 中有 eval("num1 ch num2") 函数吗?
I tried to make a small script to evaluate post-fix expressions in Ruby.
def evaluate_post(expression)
my_stack = Stack.new
expression.each_char do |ch|
begin
# Get individual characters and try to convert it to integer
y = Integer(ch)
# If its an integer push it to the stack
my_stack.push(ch)
rescue
# If its not a number then it must be an operation
# Pop the last two numbers
num2 = my_stack.pop.to_i
num1 = my_stack.pop.to_i
case ch
when "+"
answer = num1 + num2
when "*"
answer = num1* num2
when "-"
answer = num1- num2
when "/"
answer = num1/ num2
end
# If the operation was other than + - * / then answer is nil
if answer== nil
my_stack.push(num2)
my_stack.push(num1)
else
my_stack.push(answer)
answer = nil
end
end
end
return my_stack.pop
end
- I dont know a better way to check if the character in the expression is an Integer without using this crude method or Regular Expressions. Do you guys have any suggestions?
- Is there a way to abstract the cases. Is there an eval("num1 ch num2") function in Ruby?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想检查一个字符串是否是一个整数,Integer() 是一种优雅的方法,因为它可以确保你对整数的定义与 ruby 的相匹配。 如果您不想使用它,因为它会引发异常,那么正则表达式可以很好地工作 - 为什么要避免它们? 另请注意,在整数情况下,您可以简单地将 y 推入堆栈,而不是 ch,并且在弹出时不需要 to_i 调用。 至于另一个问题,ruby确实有一个eval。
if you want to check if a string is an integer, Integer() is an elegant way to do it, because it makes sure your definition of an integer matches ruby's. if you would rather not use that because it throws an exception, regular expressions work nicely - why avoid them? also, note that in the integer case, you can simply push y onto your stack, not ch, and not need the to_i calls when popping. as for the other question, ruby does indeed have an eval.
我不懂ruby所以我不回答你的问题。 不过,这里存在一个算法问题。 对于加法、乘法操作数的顺序并不重要,但对于减法和除法,您应该将第一个操作数减去并除以第二个操作数。 第一个是堆栈中较深的一个。 因此,您应该交换这两行:
I don't know ruby so I don't answer your questions. There is an algorithmic problem there, though. For add, multiply the order of operands doesn't matter but for subtraction and division, you should subtract and divide the first operand by the second. The first is the one deeper in stack. As a result, you should swap these two lines: