On Nov 30, 3:28 pm, Ruby Quiz <ja... / grayproductions.net> wrote:
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
>
> There are many different ways to write mathematical equations.  Infix notation
> is probably the most popular and yields expressions like:
>
>         2 * (3 + 5)
>
> Some people like to work with a postfix notation (often called Reverse Polish
> Notation or just RPN) though, which doesn't require parentheses for the same
> equation:
>
>         2 3 5 + *

#!/usr/bin/ruby

$prec_tbl = {
  ['*', '+'] => true,
  ['*', '-'] => true,
  ['/', '+'] => true,
  ['/', '-'] => true,
  ['-', '-'] => true,
  ['/', '/'] => true
}

def precede?(top, op)
  $prec_tbl[[top, op]]
end

def infix(arr, top = nil)
  throw "invalid postfix expression" unless !arr.empty?
  op = arr.pop
  if op =~ /\+|\-|\*|\//
    right = infix(arr, op)
    left = infix(arr, op)
    par = precede?(top, op)
    (par ? "(" : "") + "#{left} #{op} #{right}" + (par ? ")" : "")
  else
    op
  end
end

STDIN.each do |line|
  arr = line.split(/\s+/)
  begin
    res = infix(arr)
    throw "invalid postfix expression" unless arr.empty?
    puts "#{res} => #{eval(res)}"
  rescue
    STDERR.puts $!
  end
end


--
Alex Shulgin