C/C++の宿題を片付けます 100at TECH
C/C++の宿題を片付けます 100 - 暇つぶし2ch4:デフォルトの名無しさん
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