練習問題(アルゴリズム編)

'''
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個の数が必要です。')
    exit(1)
print(st[-1] if st else 0.0)

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2023-02-23 (木) 23:33:34