#author("2026-07-11T23:54:03+09:00;2023-02-23T23:33:34+09:00","default:vip","vip")
#author("2026-07-11T23:57:53+09:00;2023-02-23T23:33:34+09:00","default:vip","vip")
[[練習問題(アルゴリズム編)]]

 '''
 python main.py 1 2 + 4 5 + *
 > 27.0
 python main.py 1 2 + 3 * 4 5 / -
 > 8.2
 python main.py 5 13 + 6 / 2 % 4 5 * -
 > -19.0
 '''
 import sys
 st = []
 for e in sys.argv[1:]:
   try:
     if e=='+':
       b, a = st.pop(), st.pop()
       st.append(a+b)
     elif e=='-':
       b, a = st.pop(), st.pop()
       st.append(a-b)
     elif e=='*':
       b, a = st.pop(), st.pop()
       st.append(a*b)
     elif e=='/':
       b, a = st.pop(), st.pop()
       st.append(a/b)
     elif e=='%':
       b, a = st.pop(), st.pop()
       st.append(a%b)
     else:
       st.append(float(e))
   except IndexError:
     print('IndexError: 演算には2個の数値が必要です。')
     print('IndexError: 演算には2個の数が必要です。')
     exit(1)
 print(st[-1] if st else 0.0)

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS