/
reverzni_kalkulator.py
56 lines (48 loc) · 1.55 KB
/
reverzni_kalkulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python
# -*- coding: utf8 -*-
# Soubor: reverzni_kalkulator.py
# Datum: 22.01.2015 10:29
# Autor: Marek Nožka, nozka <@t> spseol <d.t> cz
# Autor: Marek Nožka, marek <@t> tlapicka <d.t> net
# Licence: GNU/GPL
# Úloha:
############################################################################
from __future__ import division, print_function, unicode_literals
############################################################################
zasobnik = []
def fce2argumenty(funkce):
if len(zasobnik) >= 2:
b = zasobnik.pop()
a = zasobnik.pop()
try:
zasobnik.append(funkce(a, b))
except Exception as e:
print('Chyba:', e.__class__)
print('Chyba:', e.message)
zasobnik.extend([a, b])
else:
print('"' + token + '":',
'nelze provést, v zásobníku musí být alespoň dvě čísla')
def plus(a, b):
return a+b
def minus(a, b):
return a-b
while True:
try:
radek = raw_input(str(zasobnik) + ' >> ')
for token in radek.split():
try:
zasobnik.append(float(token))
except ValueError:
if token == '+':
fce2argumenty(plus)
elif token == '-':
fce2argumenty(minus)
elif token == '*':
fce2argumenty(lambda x, y: x * y)
elif token == '/':
fce2argumenty(lambda x, y: x / y)
except EOFError:
exit(0)
except KeyboardInterrupt:
exit(1)