while 1:
try:
#先把運算式每個字元,存成陣列
mid_oper = input()
mid_oper_list = mid_oper.split() #存所有符號
mid_oper_list_len = len(mid_oper_list) #求有多少元素
oper_behide = [] #後序式陣列
oper_stack = [] #存入運算子
for i in range(mid_oper_list_len): #分析,並將值存入陣列
#判斷是否為運算子
if mid_oper_list[i] == "*" or mid_oper_list[i] == "/" or mid_oper_list[i] == "+" or mid_oper_list[i] == "-" :
if len(oper_stack) == 0 or oper_stack[-1] == "(": #如果裡面沒有運算子,則不用比較,直接存入,而如果跟括號比較就代表接下來的都在括內部,存入括號當作分界線
oper_stack.append(mid_oper_list[i])
elif oper_stack[-1] == "*" or oper_stack[-1] == "/": #進行比較,如果堆疊的是 * / ,可以直接存入
oper_behide.append(oper_stack[-1])
del oper_stack[-1] #和上面那一步進行PUSH動作
oper_stack.append(mid_oper_list[i])
elif oper_stack[-1] == "+" or oper_stack[-1] == "-": #加減優先度最低,有兩種情況要判斷
if mid_oper_list[i] == "*" or mid_oper_list[i] == "/":
oper_stack.append(mid_oper_list[i])
elif oper_stack[-1] == "+" or oper_stack[-1] == "-":
oper_behide.append(oper_stack[-1])
del oper_stack[-1] #和上面那一步進行PUSH動作
oper_stack.append(mid_oper_list[i])
elif mid_oper_list[i] == "(":
oper_stack.append(mid_oper_list[i])
elif mid_oper_list[i] == ")": #拉出右括號特別辨別,因為右括號代表結束,以左括號所代表的分界線也該消失,要先純入左括號後的運算子歐
while(oper_stack[-1] != "(" ):
oper_behide.append(oper_stack[-1])
del oper_stack[-1] #順便刪去
del oper_stack[-1] #脫離迴圈代表最後面已經是"(",刪除分界線
else:
oper_behide.append(mid_oper_list[i])
Num_stack = len(oper_stack) #把剩餘的運算子存入
for i in range(Num_stack):
oper_behide.append(oper_stack[-1])
del oper_stack[-1]
Num_oper = len(oper_behide) #輸出結果
for i in range(Num_oper):
if i == Num_oper - 1:
print(oper_behide[i])
else:
print(oper_behide[i] + " ",end = "")
except EOFError:
pass
最近剛學python,還沒到優化自己程式的地步,有需要的人可以看看,我測試幾筆資料都可以,程式上有問題也麻煩跟我說,謝謝。
while 1:
try:
#先把運算式每個字元,存成陣列
mid_oper = input()
mid_oper_list = mid_oper.split() #存所有符號
mid_oper_list_len = len(mid_oper_list) #求有多少元素oper_behide = [] #後序式陣列
oper_stack = [] #存入運算子for i in range(mid_oper_list_len): #分析,並將值存入陣列
#判斷是否為運算子
if mid_oper_list[i] == "*" or mid_oper_list[i] == "/" or mid_oper_list[i] == "+" or mid_oper_list[i] == "-" :
if len(oper_stack) == 0 or oper_stack[-1] == "(": #如果裡面沒有運算子,則不用比較,直接存入,而如果跟括號比較就代表接下來的都在括內部,存入括號當作分界線
oper_stack.append(mid_oper_list[i])
elif oper_stack[-1] == "*" or oper_stack[-1] == "/": #進行比較,如果堆疊的是 * / ,可以直接存入
oper_behide.append(oper_stack[-1])
del oper_stack[-1] #和上面那一步進行PUSH動作
oper_stack.append(mid_oper_list[i])
elif oper_stack[-1] == "+" or oper_stack[-1] == "-": #加減優先度最低,有兩種情況要判斷
if mid_oper_list[i] == "*" or mid_oper_list[i] == "/":
oper_stack.append(mid_oper_list[i])
elif oper_stack[-1] == "+" or oper_stack[-1] == "-":
oper_behide.append(oper_stack[-1])
del oper_stack[-1] #和上面那一步進行PUSH動作
oper_stack.append(mid_oper_list[i])elif mid_oper_list[i] == "(":
oper_stack.append(mid_oper_list[i])elif mid_oper_list[i] == ")": #拉出右括號特別辨別,因為右括號代表結束,以左括號所代表的分界線也該消失,要先純入左括號後的運算子歐
while(oper_stack[-1] != "(" ):
oper_behide.append(oper_stack[-1])
del oper_stack[-1] #順便刪去
del oper_stack[-1] #脫離迴圈代表最後面已經是"(",刪除分界線
else:
oper_behide.append(mid_oper_list[i])Num_stack = len(oper_stack) #把剩餘的運算子存入
for i in range(Num_stack):
oper_behide.append(oper_stack[-1])
del oper_stack[-1]Num_oper = len(oper_behide) #輸出結果
for i in range(Num_oper):
if i == Num_oper - 1:
print(oper_behide[i])
else:
print(oper_behide[i] + " ",end = "")
except EOFError:
pass
最近剛學python,還沒到優化自己程式的地步,有需要的人可以看看,我測試幾筆資料都可以,程式上有問題也麻煩跟我說,謝謝。
#先把運算式每個字元,存成陣列mid_oper_list = input().split() #存所有符號mid_oper_list_len = len(mid_oper_list) #求有多少元素
oper_behide = [] #後序式陣列oper_stack = [] #存入運算子
for i in mid_oper_list: #讀值if (i in "+-*/"): #判斷是否為運算子if len(oper_stack) == 0 or oper_stack[-1] == "(": #如果裡面沒有運算子,則不用比較,直接存入,而如果跟括號比較就代表接下來的都在括內部,存入括號當作分界線oper_stack.append(i)elif (oper_stack[-1] in "*/"):#進行比較,如果堆疊最高層的是 * / ,可以直接存入oper_behide.append(oper_stack.pop(-1)) #進行進行POP動作oper_stack.append(i)elif (oper_stack[-1] in "+-"): #如果堆疊最高層的是 + -,因為加減優先度最低,有兩種情況要判斷if (i in "*/"): #比較*/的情況oper_stack.append(i) #比較沒成功,記得不用輸出*或/,因為比較的意義在於現在這個情況,是否要進行運算#比如a+b*c,當讀取到*時是在考慮要不要進行a+b,如果輸出*,就會變成a*belif (i in "+-"):oper_behide.append(oper_stack.pop(-1))oper_stack.append(i)elif (i in "("): #拿左括號當作分界線oper_stack.append(i)elif (i in ")"): #遇到右括號代表結束while(oper_stack[-1] != "(" ): #取值到左括號為止oper_behide.append(oper_stack.pop(-1))del oper_stack[-1] #脫離迴圈代表最後面已經是"(",刪除分界線else:oper_behide.append(i)
Num_stack = len(oper_stack) #把剩餘的運算子存入for i in range(Num_stack):oper_behide.append(oper_stack.pop(-1))
Num_oper = len(oper_behide) #輸出結果for i in range(Num_oper):if i == Num_oper - 1:print(oper_behide[i])else:print(oper_behide[i] + " ",end = "")這是今天剛優化的程式碼,不過我就懶得再加while