-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexe_131_infix_to_postfix.py
118 lines (95 loc) · 3.5 KB
/
exe_131_infix_to_postfix.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
"""
The program receives a MATHEMATICAL EXPRESSION from the USER
and return the same expression EXPRESSED in POSTFIX FORM (RPN).
"""
# START Definition of the FUNCTIONS
def checkEntry(math_exp): # possible evolution -> check entry
pass
def tokenizingString(string): # possible evolution -> import
# LIST of the MATHEMATICAL OPERATORS
tokens = ["+", "-", "*", "/", "^", "(", ")"]
tokens_list = []
i = 0
while i < len(string):
# Check MATHEMATICAL OPERATORS
if string[i] in tokens:
tokens_list.append(string[i])
# Check INTEGERS
elif string[i].isdigit():
tmp = ""
# LOOP for NUMBERS > 1 DIGITS
while i < len(string):
if string[i].isdigit():
tmp += string[i]
i += 1
else:
i -= 1
break
tokens_list.append(tmp)
i += 1
return tokens_list # RETURN LIST of TOKENS
def operatorUnaryBinary(original_tokens_list): # possible evolution -> import
tokens_list = original_tokens_list[:] # copy list
operators = ["+", "-"]
if tokens_list[0] in operators:
# duplicates accepted
tokens_list[0] = "u" + tokens_list[0]
for i in range(1, len(tokens_list)):
if tokens_list[i] in operators and tokens_list[i-1] == "(":
# duplicates accepted
tokens_list[i] = "u" + tokens_list[i]
return tokens_list
def operatorPrecedence(operator): # fuction updated
if (operator == "+" or operator == "-"):
return 1
elif (operator == "*" or operator == "/"):
return 2
elif (operator == "u+" or operator == "u-"):
return 3
elif (operator == "^"):
return 4
def infixToPostfix(list_infix_expression):
token_operators = ["+", "u+", "-", "u-",
"*", "/", "^"] # without parentheses
operators = []
postfix = []
for token in list_infix_expression:
# CHECK INTEGER
if token.isdigit():
postfix.append(token)
# CHECK OPERATORS
if token in token_operators:
while operators != [] and operators[-1] != "(" and \
operatorPrecedence(token) <= operatorPrecedence(operators[-1]):
postfix.append(operators.pop())
operators.append(token)
# CHECK PARENTHESES
if token == "(":
operators.append(token)
if token == ")":
while operators[-1] != "(":
postfix.append(operators.pop())
operators.remove("(")
while operators != []:
postfix.append(operators.pop())
return postfix
# END Definition of FUNCTIONS
# START MAIN PROGRAM
def main():
# Acquisition of DATA entered by the USER
math_exp = input("Enter the MATHEMATICAL EXPRESSION: ")
# STRING TESTING
# math_exp = "-1*(-2 + (6 * (3-1)) / 844 + (74 - (-3)) / 121-10+10"
# Generation of the TOKENS LIST
tokens_list = tokenizingString(math_exp)
# Generation of the TOKENS LIST with the identification of UNARY and BINARY operators
tokens_list_updated = operatorUnaryBinary(tokens_list)
# Generation of the POSTFIX LIST
postfix_expression = infixToPostfix(tokens_list_updated)
# Displaying the EXPRESSIONS
print("ORIGINAL EXPRESSION -> {}".format(math_exp))
print("POSTFIX EXPRESSION -> ", end="")
for token in postfix_expression:
print(token, end=" ")
if __name__ == "__main__":
main()