1 / 15

運算式求值與轉換 -stack 之應用

運算式求值與轉換 -stack 之應用. Lai Ah Fur. 前序式 (prefix) 求值. 處理過程 : 由右而左掃描運算式 : 二種處理方式 ( 1) 由右而左讀取運算元 / 子 (2) 將運算元 / 子由左而右 push 到 stack, 再由 stack 中 pop ( 順序即為由右而左 ) 掃描到運算元 (0..9),push 到 stack 掃描到運算子 (+-*/), 由 stack 中 pop 出二元素 . 並進行計算 , 最後將計算結果 push 到 stack

Download Presentation

運算式求值與轉換 -stack 之應用

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 運算式求值與轉換-stack之應用 Lai Ah Fur

  2. 前序式(prefix)求值 • 處理過程: • 由右而左掃描運算式:二種處理方式 (1)由右而左讀取運算元/子 (2)將運算元/子由左而右push到stack,再由stack中pop (順序即為由右而左) • 掃描到運算元(0..9),push到stack • 掃描到運算子(+-*/),由stack中pop出二元素. 並進行計算,最後將計算結果push到stack • 當掃描運算式結束,將由stack中pop出最後運算式結果.若stack中仍有元素表示運算式寫法有誤

  3. 前序式求值example • *+47-82 • 處理過程: • Scan 2, push into stack • Scan 8 , push into stack • Scan -, pop 2 operands from stack, then evaluate, finally push the result into stack • Scan 7, push into stack • Scan 4, push into stack • Scan +, pop 2 operands from stack, then evaluate, finally push the result into stack • Scan *, pop 2 operands from stack, then evaluate, finally push the result into stack • Finally, pop the result from the stack The first operand: Subtrahend The second :Minuend(被)

  4. 後序式(postfix)求值 • 處理過程: • 由左而右掃描運算式 • 掃描到運算元(0..9),push到stack • 掃描到運算子(+-*/),由stack中pop出二元素. 並進行計算,最後將計算結果push到stack • 當掃描運算式結束,將由stack中pop出最後運算式結果.若stack中仍有元素表示運算式寫法有誤

  5. 後序式求值example • 47+82-* • 處理過程: • Scan 4, push into stack • Scan 7, push into stack • Scan +, pop 2 operands from stack, then evaluate, finally push the result into stack • Scan 8, push into stack • Scan 2, push into stack • Scan -, pop 2 operands from stack, then evaluate, finally push the result into stack • Scan *, pop 2 operands from stack, then evaluate, finally push the result into stack • Finally, pop the result from the stack The first operand: :Minuend(被) The second:Subtrahend 2 8 11 8-2

  6. 8 7 4 * + 中序式求值example • 4+7*8-2 • 處理過程: • Scan 4, push into operand stack • Scan +, push into operator stack • Scan 7, push into operand stack • Scan *,priority:*(目前)>+(stack top), push into operator stack • Scan 8, push into stack • Scan -, priority:-(目前)<=*(stack top), pop 2 operands from stack and pop * from operator stack, then evaluate, push the result into operand stack; priority:-(目前)<=+(stack top) ,pop 2 operands from stack and pop + from operator stack, then evaluate, push the result into operand stack; finally push – into operator stack. • Scan 2, push into stack • Scan over, pop 2 operands from stack and pop - from operator stack • 自行練習: 3+4*5*6-7-8處理過程 • Problem: 未處理“()”問題 4+7*8-2

  7. 中序式(infix)求值(1) • 處理過程: • 由左而右掃描運算式,且須二個stack • 掃描到運算元(0..9),push到operand stack • 掃描到運算子(+-*/), push到operator Stack先檢查 : (1)if operator stack is empty,將目前之運算子push到operator stack;(2)比較目前之運算子優先權是否大於operator stack top之運算子,若是則直接將目前之運算子push到operator;否則(<=),由operand stack中pop出二運算元, operator stack中pop出運算子,並進行計算, 計算結果push到operand stack.最後再將目前之運算子push到operator stack • 當掃描運算式結束,將operand stack中pop出二元素, operator stack中pop出運算子,並進行計算, 計算結果push到stack.

  8. 中序式求值(2) • (4+7)*(8-2) • 處理過程: • 掃描到運算子“(“, 無條件push到operator stack. • 掃描到運算子“)“, pop出operator stack之 operator,進行計算,直到遇到”(” • 其他,同前

  9. 中序轉後序 A Example: A+B*C/D-E 轉換程序:由左而右掃描運算式,且須一個operator stack • Scan A:輸出至後序運算式之後端 • Scan +:push至operator stack (目前堆疊is empty) • Scan B:輸出至後序運算式之後端 • Scan *:push至operator stack (目前堆疊top之operator’s priority <“*” priority) • Scan C:輸出至後序運算式之後端 • Scan /:pop堆疊top之operator且輸出至後序運算式之後端, 最後再將“/”push至operator stack (目前堆疊top之operator’s priority =“/” priority) • Scan D:輸出至後序運算式之後端 • Scan -:pop堆疊top之operator ”/”及“+”且輸出至後序運算式之後端, 最後再將“-”push至operator stack (目前堆疊top之operator and next top operators’ priority >=“/” priority) • Scan E:輸出至後序運算式之後端 • pop堆疊top之operator ”-”,輸出至後序運算式之後端 AB ABC ABC*D ABC*D/+E-

  10. 中序轉後序 Example: A+(B*C/D-E-K)*F-G*H SOLUTION: ABC*D/E-K-F*+GH*-

  11. 中序轉前序 E Example: A+B*C/D-E 轉換程序:由右而左掃描運算式,且須一個operator stack • Scan E:輸出至後序運算式之前端 • Scan -:push至operator stack (目前堆疊is empty) • Scan D:輸出至後序運算式之前端 • Scan /:push至operator stack (目前堆疊top之operator’s priority <=“/” priority) • Scan C:輸出至後序運算式之前端 • Scan *:將“*”push至operator stack (目前堆疊top之operator’s priority =“*” priority) • Scan B:輸出至後序運算式之前端 • Scan +:pop堆疊top之operator ”*”及“/”且輸出至後序運算式之前端, 最後再將“+”push至operator stack (目前堆疊top之operator and next top operators’ priority >“+” priority) • Scan A:輸出至後序運算式之前端 • pop堆疊top之operator ”+”及”-” 且輸出至後序運算式之前端 DE CDE /*BCDE A/*BCDE -+A/*BCDE

  12. 中序轉前序 Example: A+(B*C/D-E-K)*F-G*H SOLUTION: -+A*--/*BCDEKF*GH

  13. 作業四 • 功能 • 中序轉前序(數字/變數) • 中序轉後序(數字/變數) • 前序求值 • 後序求值 • 中序求值 • 介面: applet (java) / GUI (C++) • 須顯示轉序/求值詳細過程中:顯示stack及輸出字串之內容

  14. Homework 5:simple BASIC simulator (interpreter) Syntax: • 接受之variable:A~Z共52個(分大小寫) • Basic Command:print,input(如input a), 指定敘述(含++,--) • 接受Math expression,如a+b*3, 56-4*9/2 • Operator:+,-,*,/,^ • Statement前加行號,可不按順序 Interpreter 操作環境 • Load ‘…bas’命令:載入.bas程式檔,顯示於input box • Save命令:將 input box存檔 • run命令:依行號順序,解譯程式,顯示於output .遇syntax error ,顯示訊息於output • cls命令:清除memory程式,及所有variable內容 • quit:離開interpreter

  15. Testing example for homework • 10 a=10*5-(10-4) • 20 c=30 • 31 d=(a*c-10)/2-7*c • 25 print a,c,a*c,45/(5+9-2)*2 • 30 input c • 40 print d,a,c • 45 print “a=“,a,”c=“,c

More Related