david_Yakhin_312 IterCalc 5173
Алмаз Сейтхазин (КФ МГУ, кафедра НДС) IterCalc 5205
t1def itercalc():t1def itercalc():
2    stack = []2    stack = []
3    result = None3    result = None
4    while True:4    while True:
5        cmd = (yield result)5        cmd = (yield result)
6        result = None6        result = None
7        if cmd == '?':7        if cmd == '?':
8            if not stack:8            if not stack:
9                print('Insufficient stack')9                print('Insufficient stack')
10            else:10            else:
11                result = stack[-1]11                result = stack[-1]
12        elif cmd in ['+', '-', '*', '/']:12        elif cmd in ['+', '-', '*', '/']:
13            if len(stack) < 2:13            if len(stack) < 2:
14                print('Insufficient stack')14                print('Insufficient stack')
15            else:15            else:
16                b = stack.pop()16                b = stack.pop()
17                a = stack.pop()17                a = stack.pop()
18                try:18                try:
19                    if cmd == '+':19                    if cmd == '+':
20                        stack.append(a + b)20                        stack.append(a + b)
21                    elif cmd == '-':21                    elif cmd == '-':
22                        stack.append(a - b)22                        stack.append(a - b)
23                    elif cmd == '*':23                    elif cmd == '*':
24                        stack.append(a * b)24                        stack.append(a * b)
25                    elif cmd == '/':25                    elif cmd == '/':
26                        if b == 0:26                        if b == 0:
27                            print('Zero division')27                            print('Zero division')
28                            stack.append(a)28                            stack.append(a)
29                            stack.append(b)29                            stack.append(b)
30                        else:30                        else:
31                            stack.append(a // b)31                            stack.append(a // b)
32                except:32                except:
33                    stack.append(a)33                    stack.append(a)
34                    stack.append(b)34                    stack.append(b)
35                    print('Operation failed')35                    print('Operation failed')
36        else:36        else:
37            try:37            try:
38                num = int(cmd)38                num = int(cmd)
39                stack.append(num)39                stack.append(num)
40            except ValueError:40            except ValueError:
41                print('Unknown command')41                print('Unknown command')
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op