計算機概論(最少需要多大容量的推疊)

2010-10-17 1:05 am
(A*B)+C/(D-E)的後序式(Postfix),最少需要多大容量的推疊(Stack)才能執行無誤?
(A)2
(B)3
(C)4
(D)5
答案:(C)4

這是為什麼?
怎麼計算出來的?
更新1:

【考試名稱:】92年特種考試地方政府公務人員考試(四等) 【類科名稱:】 資訊處理 題目:(第25題) http://wwwc.moex.gov.tw/examnew1/92/20/000c40.pdf 答案要到這裡面找,(內頁的連結我不會貼) http://wwwc.moex.gov.tw/qanda/qanda_3.htm

回答 (1)

2010-10-19 4:00 pm
✔ 最佳答案
Can you clarify the question a bit more? Are you asking about the stack used for the evaluation of the postfix string or the conversion from infix to postfix?

2010-10-19 08:00:37 補充:
I am assuming this is for evaluation of the postfix notation.
From the given infix notation, the postfix notation is: A B * C D E - / +
character: A
action: push A
stack: A

character: B
action: push B
stack: B

character: *
action: pop 2 values, compute and push the result back (R1= A*B)
stack: R1

character: C
action: push C
stack: R1 C

character: D
action: push D
stack: R1 C D

character: E
action: push E
stack: R1 C D E

character: -
action: pop 2 values, compute and push the result back (R2=D-E)
stack: R1 C R2

character: /
action: pop 2 values, compute and push the result back (R3=C/R2)
stack: R1 R3

character: +
action: pop 2 values, compute and push the result back (R4=R1+R2)
stack: R4

From above, your stack only needs a size of 4. (to store R1, C, D, E)
The procedure is in wiki: http://en.wikipedia.org/wiki/Reverse_Polish_notation


收錄日期: 2021-05-02 10:12:06
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20101016000015KK05551

檢視 Wayback Machine 備份