-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
128 lines (104 loc) · 3.12 KB
/
calculator.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
K = {}
operands = {'+': 1,
'-': 1,
'*': 2,
'/': 2,
'(': 0,
')': 0}
def convert_infix(infix):
infix = infix.replace('(', '( ').replace(')', ' )')
if infix.count('(') != infix.count(')') or infix.count('*') > 2 or infix.count('//') > 1:
return print('Invalid expression')
Stack = []
postfix = ''
for i in infix.split():
if i in operands.keys():
# № 2,3
if not Stack or Stack[-1] == '(' or operands[i] > operands[Stack[-1]]:
Stack.append(i)
# № 4
while operands[i] < operands[Stack[-1]]:
postfix += Stack.pop() + ' '
if not Stack or Stack[-1] == ')':
Stack.append(i)
break
# № 5, 6
elif i == '(':
Stack.append(i)
elif i == ')':
while Stack[-1] != '(':
postfix += Stack.pop() + ' '
Stack.remove('(')
else:
postfix += i + ' '
while Stack:
postfix += Stack.pop() + ' '
return postfix
def calculate_post(postfix):
postfix = str(postfix)
Stack = []
for i in postfix.split():
if i.isdigit():
Stack.append(i)
elif i in K:
Stack.append(K[i])
elif i in operands.keys():
sec, first = Stack.pop(), Stack.pop()
result = eval(str(first) + i + str(sec))
Stack.append(result)
return round(Stack[-1]) if Stack else 0
def assignment(income):
income = income.replace(' ', '')
t = income.split('=')
if not t[0].isalpha():
return print('Invalid identifier')
elif t[1] not in K and t[1].isalpha():
return print('Unknown variable')
elif not t[1].isalpha() and not t[1].isdigit() or len(t) > 2:
return print('Invalid assignment')
if t[1] in K:
K[t[0]] = K[t[1]]
else:
K[t[0]] = t[1]
def printer(income):
t = income.split()
try:
print(K[t[0]])
except KeyError:
print('Unknown variable')
def replace(income):
for i in income:
if i.isalpha():
try:
income = income.replace(i, K[i])
except KeyError:
return print("Unknown variable")
return print(round(eval(income)))
while True:
inp = input()
operation = ["+", "-", "*", "/"]
if not inp:
continue
elif '=' in inp:
assignment(inp)
elif '+' in inp or '-' in inp or '*' in inp or '/' in inp:
if '/help' in inp:
print('The program calculates the sum, subtraction, multiplication of numbers')
continue
elif '/exit' in inp:
print("Bye!")
break
elif '/start' in inp:
print("Unknown command")
continue
try:
if inp.count('/') > 1:
print('Invalid expression')
continue
print(eval(inp))
except NameError:
replace(inp)
except SyntaxError:
calculate_post(convert_infix(inp))
else:
printer(inp)