字尾表示式

2021-07-29 19:44:41 字數 697 閱讀 4278

完整的題目不能複述了,概括一下題目就是輸入乙個16進製制字尾表示式(string型),輸出其結果(int型)。

不需要考慮異常情況(即測試用例一定是正確的表示式),不需要考慮優先順序。

因為python用的比較順手,就拿python寫了:

def postfixcalc(str):

pstack=

nummap =

for i in range(0,len(str)):

if '+' == str[i]:

n1 = pstack.pop()

n2 = pstack.pop()

elif '-' == str[i]:

n1 = pstack.pop()

n2 = pstack.pop()

elif '*' == str[i]:

n1 = pstack.pop()

n2 = pstack.pop()

else:

if str[i]<='9' and str[i]>='0':

else:

return pstack[0]

def main():

str = raw_input()

print postfixcalc(str)

if __name__ == '__main__':

main()

字尾表示式 中綴到字尾表示式

輸入空格跳出迴圈 while k getchar n 字尾表示式 此 僅限於0 9內的加減乘除 include include include define long 10 using namespace std typedef struct stack qstack void init qstac...

字尾表示式

字尾表示式的計算和中綴表示式轉字尾表示式 此處的運算用的是鍊錶的表示方法 以下為三個會涉及到的標頭檔案 error.h 字尾表示式 created by kyle.yang on 14 12 2.ifndef error h define error h include using namespac...

字尾表示式

對於乙個算術表示式我們的一般寫法是這樣的 3 4 5 6 這中寫法是中序表示式 而後序表示式則是將運算子放在運算元的後面,如 3 4 5 6 可以看出後序表示式中沒有括號,只表達了計算的順序,而這個順序恰好就是計算器中的一般計算順序。建立乙個棧s 從左到右讀表示式,如果讀到運算元就將它壓入棧s中,如...