-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCalculator - Command Line Application.py
84 lines (67 loc) · 2.45 KB
/
Calculator - Command Line Application.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
#Calculator - A Command Line Application that does basic Arithmetic Opertions.(Divison, Multiplication, Addition, Subtraction)
'''Postfix evaluation'''
def postfix_evaluation(st, n):
'''Evaluates a Postfix Expression and returns the answer(an Integer).'''
#Operand Stack - Only contains the Operands
operand_stack = []
for i in range(n):
if st[i] not in ('/', '*', '-', '+'):
operand_stack.append(st[i])
else:
a = float(operand_stack.pop())
b = float(operand_stack.pop())
if st[i] == '/':
val = b / a
elif st[i] == '*':
val = a * b
elif st[i] == '+':
val = a + b
elif st[i] == '-':
val = b - a
operand_stack.append(val)
#print('OS: {}'.format(operand_stack))
return operand_stack[-1]
'''Infix to Postfix conversion'''
def infix_to_postfix(s, n):
'''Converts a given Infix expression to Postfix Expression. Returns the Postfix Expression as a String.'''
#Operator Stack - Only contains Operators
stack = []
#Precedence of Arithmetic Operators
operator_precedence = {'/' : 4, '*' : 3, '+' : 2, '-' : 1}
#To store the postfix notation as a list
res = []
for i in range(n):
if s[i] not in operator_precedence:
if i == 0:
res.append(s[i])
else:
if s[i-1] not in operator_precedence:
res[-1] = res[-1] + s[i]
else:
res.append(s[i])
else:
if len(stack) == 0:
stack.append(s[i])
else:
if operator_precedence.get(stack[-1]) > operator_precedence.get(s[i]):
res.append(stack[-1])
stack.pop()
stack.append(s[i])
else:
stack.append(s[i])
#Adding the remaining operators to the result
while len(stack) != 0:
res.append(stack[-1])
stack.pop()
print(res)
return postfix_evaluation(res, len(res))
#Driver Code
'''Taking mathematical expression as a String'''
s = input('Expression: ').strip()
print(infix_to_postfix(s, len(s)))
'''
To Do:
---------------
Failing to handle Associativity ----> Left to Right Associativity
Left Most Operation should be evaluated first.
'''