1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Joke] 計算を間違える逆ポーランド記法カルキュレータ

Last updated at Posted at 2024-11-07

計算を間違える逆ポーランド記法の計算機を作りました。
chmod +x r.py./r.py 10 20 \*としてください。

*/?にはシェルエスケープ\が必要です。

Usage: ./r.py <スペースで区切った逆ポーランド記法の式>

エラーチェックはしてません。

r.py
#!/usr/bin/python3
import sys
import random
stack=[]
s=sys.argv[1:]

while(s):
  e=s[0]
  if (e=='+'):
    stack.append(stack.pop()+stack.pop())
  elif (e=='-'):
    a=stack.pop()
    stack.append(stack.pop()-a)
  elif (e=='*'):
    stack.append(stack.pop()*stack.pop())
  elif (e=='/'):
    a=stack.pop()
    stack.append(stack.pop()/a)
  elif (e=='?'):
    v=stack.pop()
    stack.append(v)
    print(v+random.randint(1,7))

  else:
    stack.append(float(e))
  s=s[1:]

print(stack.pop()+random.randint(1,7))
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?