- 60 名前:名前は開発中のものです。 mailto:sage [02/06/29 23:23 ID:???]
- とりあえずここまで… 残りは後で考えます
#! /usr/bin/env ruby def infix_to_postfix(expr) stack = ['('] result = [] operator_priority = { '*' => 50, '/' => 50, '+' => 20, '-' => 20, '(' => 10, ')' => 10, } expr.each{ |x| if '+ - * /'.split.include?(x) # operator while operator_priority[stack.last] >= operator_priority[x] result.push(stack.pop) end stack.push(x) else result.push(x) # operand はそのまま出力 end } while not stack.empty? and operator_priority[stack.last] >= operator_priority[')'] result.push(stack.pop) end result.pop # 最後は'('が入ってるので捨てる result end puts infix_to_postfix(ARGV[0].split).join
|

|