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
650
kaibun.pdf
mizti
0
660
dfs1.pdf
mizti
0
610
2012/11/16 問題1
mizti
0
550
Featured
See All Featured
Darren the Foodie - Storyboard
khoart
PRO
4
4k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
270
The Pragmatic Product Professional
lauravandoore
37
7.5k
Believing is Seeing
oripsolob
1
240
Typedesign – Prime Four
hannesfritz
42
3.2k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
410
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
2
2.2k
Between Models and Reality
mayunak
4
500
Testing 201, or: Great Expectations
jmmastey
46
8.3k
Utilizing Notion as your number one productivity tool
mfonobong
4
620
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