-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
82 lines (74 loc) · 2.07 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
class activeInput:
def __init__(self):
pass
def enter(i):
try:
if i[0] == 'C':
storedInput.modify_stored(i)
elif i == 'E':
Output.eval_output(i)
elif i[-2] == 'M':
activeMemory.modify_memory(i)
else:
for x in i:
if x != ' ':
stored.append(x)
activeInput.enter(Output.general_output())
except IndexError: #deals with case of single operand or operator, where elif i[-2] == 'M' forces IndexError
stored.append(i)
activeInput.enter(Output.general_output())
class activeMemory:
def __init__(self):
pass
def modify_memory(x):
global MR #or will be assumed to be local as assigned a value in the function's body
global stored
if x == 'MS':
MR = stored.copy()
activeInput.enter(Output.general_output())
elif x == 'MC':
MR = []
activeInput.enter(Output.general_output())
elif x[-2:] == 'MR':
operand = str(eval(''.join(MR))) #assign to a new variable as appending to stored affects MR for some reason
stored.append(x[:-2]) #operator
stored.append(operand) #operand
activeInput.enter(Output.general_output())
class storedInput:
def __init__(self):
pass
def modify_stored(x):
if x == 'C':
stored.clear()
activeInput.enter(Output.general_output())
if x == 'CE':
del stored[-1]
activeInput.enter(Output.general_output())
class Output:
def __init__(self):
pass
def eval_output(x):
if x == 'E':
e = eval(''.join(stored))
return print(f'\nThe final value is {e}.')
def general_output():
os.system('clear')
try:
general_stored = eval(''.join(stored))
except SyntaxError:
general_stored = 0
try:
general_MR = eval(''.join(MR))
except SyntaxError:
general_MR = 0
return input(f"""
STORED is {stored} = {general_stored}.
[CE] [C]
MEMORY is {MR} = {general_MR}.
[MS] [MC] [+MR] [-MR] [*MR] [/MR] [**MR]
Type E to execute calculation.
ENTER: """)
MR = []
stored = []
activeInput.enter(Output.general_output())