/* 簡単なプログラムをウプしよう */at GAMEDEV /* 簡単なプログラムをウプしよう */ - 暇つぶし2ch■コピペモード□スレを通常表示□オプションモード□このスレッドのURL■項目テキスト59:名前は開発中のものです。 02/06/29 20:37 逆ポーランドに直すのはかなり簡単だろ。 構文解析でも基礎中の基礎じゃないか。 優先順位と括弧にだけ気をつければ良いんだから。 60:名前は開発中のものです。 02/06/29 23:23 とりあえずここまで… 残りは後で考えます #! /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 次ページ最新レス表示レスジャンプ類似スレ一覧スレッドの検索話題のニュースおまかせリストオプションしおりを挟むスレッドに書込スレッドの一覧暇つぶし2ch