2001/02/27
3 + 4.1 * 5 / 6 - 7.0 * 8 + 9 =+ - * / = 各個運算子 (operator) 的優先順序如下表所示:
上例中運算順序如下圖所示:
運算結果為 -40.583
current input is 3 push 3 to operand stack current input is + precedence of + is higher than NULL (top of operator stack) push + to operator stack current input is 4.1 push 4.1 to operand stack current input is * precedene of * is higher than + (top of operator stack) push * to operator stack current input is 5 push 5 to operand stack current input is / precedence of / is the same as * (top of operator stack) pop operator stack as opr (*) pop operand stack as opnd2 (5) pop operand stack as opnd1 (4.1) evaluate opnd1 opr opnd2 ie. 4.1 * 5 = 20.5 push 20.5 to operand stack current intput is still / precedence of / is higher than + (top of operator stack) push / to operator stack current input is 6 push 6 to operand stack current input is - precedence of - is lower than / (top of operator stack) pop operator stack as opr (/) pop operand stack as opnd2 (6) pop operand stack as opnd1 (20.5) evaluate 20.5 / 6 = 3.417 push 3.417 to operand stack current input is still - precedence of - is the same as + (top of operator stack) pop operator stack as opr (+) pop operand stack as opnd2 (3.417) pop operand stack as opnd1 (3) evaluate 3 + 3.417 = 6.417 push 6.417 to operand stack current input is still - precedence of - is higher than NULL (top of operator stack) push - to operator stack current input is 7.0 push 7.0 to operand stack current input is * precedence of * is higher than - (top of operator stack) push * to operator stack current input is 8 push 8 to operand stack current input is + precedence of + is lower than * (top of operator stack) pop operator stack to opr (*) pop operand stack to opnd2 (8) pop operand stack to opnd1 (7.0) evaluate 7.0 * 8 = 56 push 56 to operand stack current input is still + precedence of + is the same as - (top of operator stack) pop operator stack to opr (-) pop operand stack to opnd2 (56) pop operand stack to opnd1 (6.417) evaluate 6.417 - 56 = -49.583 push -49.583 to operand stack current input is still + precedence of + is higher than NULL (top of operator stack) push + to operator stack current input is 9 push 9 to operand stack current input is = precedence of = is lower than + (top of operator stack) pop operator stack to opr (+) pop operand stack to opnd2 (9) pop operand stack to opnd1 (-49.583) evaluate -49.583 + 9 = -40.583 push -40.583 to operand stack current input is still = precedence of = is higher than NULL (top of operator stack) push = to operator stack end of input pop operator to opr (=) pop operand stack to opnd1 (-40.583) evaluate -40.583 = Are operand stack and operator stack empty? return -40.583
遇見這樣子的運算式時, 程式無法算出正確結果而需要發出錯誤訊息, 要處理這種錯誤情況的話, 一種方法是等到堆疊上出現無法處理的狀況時程式才發出錯誤訊息, 所以上式 3 + * 5 會把 3 和 5, + 和 * 都分別放到堆疊上, 然後先做 3 * 5 = 15, 把 15 再放到堆疊上, 然後由堆疊上得到 '+' 運算子, 可是此時運算元堆疊上只剩下一個運算元, 所以無法處理 '+' 號這個二元 (binary) 運算子, 程式於此時會發出錯誤訊息。
在我們的作業中, 為了簡化你的程式, 你可以完全不處理等號以外的一元 (unary) 運算子, 如此可以歸納出一個簡單的法則, 就是運算元和運算子會交錯地出現, 如此程式可以很簡單地偵測出錯誤狀況, 不過如果你要處理一元運算子的話, 法則就會比較複雜了。
將來我們會提到如何製作線上的小算盤程式, 在處理視窗界面時, 所有的錯誤訊息必須要及時的回饋給使用者, 不能等到堆疊出錯了才告訴使用者, 此時必須使用更嚴謹的狀態圖來處理, 等到我們談到物件的狀態圖以後會再以這個範例讓大家來練習, 所以請大家一步一步地製作這個程式。
回
C++ 程式設計課程
首頁
製作日期: 02/25/2001 by 丁培毅 (Pei-yih Ting) E-mail: pyting@cs.ntou.edu.tw TEL: 02 24622192x6615 海洋大學 理工學院 資訊科學系 Lagoon |