C/C++の宿題を片付けます 100 at TECH
[2ch|▼Menu]
4:デフォルトの名無しさん
07/11/22 05:21:43
>>902

#!/usr/bin/env ruby

def to_rpol(s)
a = s.reverse.split
a.each_with_index do |e, i|
if e == "("
a[i + 0] = sprintf("%s %s %s", a[i - 3], a[i - 1], a[i - 2])
a[i - 1] = nil; a[i - 2] = nil; a[i - 3] = nil; a[i - 4] = nil
end
end
a.compact!
a.each_with_index do |e, i|
if e == "*" || e == "/"
a[i + 1] = sprintf("%s %s %s", a[i - 1], a[i + 1], e)
a[i + 0] = nil; a[i - 1] = nil
end
end
a.compact!
a.each_with_index do |e, i|
if e == "+" || e == "-"
a[i + 1] = sprintf("%s %s %s", a[i - 1], a[i + 1], e)
a[i + 0] = nil; a[i - 1] = nil
end
end
a.compact!
return a.to_s
end

puts to_rpol("( 5 - 4 ) / 3 + 2 * 1")


次ページ
続きを表示
1を表示
最新レス表示
スレッドの検索
類似スレ一覧
話題のニュース
おまかせリスト
▼オプションを表示
暇つぶし2ch

5315日前に更新/123 KB
担当:undef