Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
問題1. 解答例
Search
mizti
November 18, 2012
510
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
問題1. 解答例
mizti
November 18, 2012
More Decks by mizti
See All by mizti
CutSticks.pdf
mizti
0
660
Maze Maker
mizti
0
630
「銀将ご乱心」
mizti
0
990
paint.pdf
mizti
1
640
kaibun.pdf
mizti
0
660
dfs1.pdf
mizti
0
610
2012/11/16 問題1
mizti
0
550
Featured
See All Featured
Become a Pro
speakerdeck
PRO
31
6.2k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
730
Facilitating Awesome Meetings
lara
57
7.1k
Being A Developer After 40
akosma
91
590k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
230
Fireside Chat
paigeccino
42
4k
Done Done
chrislema
186
16k
Visualization
eitanlees
152
17k
SEO for Brand Visibility & Recognition
aleyda
0
4.7k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Music & Morning Musume
bryan
47
7.4k
Transcript
あくまで書き方の一例
ソースコードとか class PolishProcessor attr_accessor :arr def initialize(s) #与えられた文字列を解析して配列に入れる @arr =
arraynize(s) end def evaluate @stack = [] # arrが空になるまで、末尾から一つづつ数字を取り出す while((popped = @arr.pop) != nil) do if Integer === popped then # 取り出されたのが数字だったら、スタックに積む @stack.push popped end if popped == "+" || popped == "*" || popped == "-" then # 取り出されたのが演算子だったら、スタックにつまれた数字2つを取り出し て計算してまたスタックに積む num1 = @stack.pop num2 = @stack.pop @stack.push calc(popped, num1, num2) end end @stack[0] end # 与えられた演算子と数字2つから計算を行う def calc(operator, num1, num2) if operator == "+" then return num1 + num2 elsif operator == "*" then return num1 * num2 elsif operator == "-" then return num1 - num2 else return "Error" end end # 与えられた文字列を分解して配列に入れる def arraynize(s) ret = s.gsub("("," ").gsub(")", " ").split(/¥s* ¥s*/) ret = ret.select{|item| item!=""} ret = ret.collect{|i| if i =~ /¥d+/ then i.to_i else i end} end
見えません
というわけで 絵にしました
答えの一例: 問題1.ポーの惑星 (* (- 9 20) (+ 7 (- 999
108)) ) 1. (したごしらえ)読み込んだものから( )を半角スペースで置き換え 2. (したごしらえ) スペースを区切り文字にして配列に入れ、更に空文字””を除去 ["*", "-", 9, 20, "+", 7, "-", 999, 108] * - 9 20 + 7 - 999 108 * - 9 20 + 7 - 999 108 3. 後方から順番に1要素ずつ取り出してスタックに積んで行く ["*", "-", 9, 20, "+", 7, "-"] 108 999 108 999 ウワー ウワー ウワー ウワー
答えの一例: 問題1.ポーの惑星(2) ["*", "-", 9, 20, "+", 7] 108 999
4. 演算子が積まれたら、下にある2文字とあわせて計算、結果をスタックに入れる "-" “-” 891 5. 配列が空になるまで繰り返して、スタックに最後に残った数字が答えとなる 891 7 + 898 898 20 9 “-” 898 -11 898 -11 “*” -9878