@@ -26,7 +26,7 @@ def infix_2_postfix(infix: str) -> str:
26
26
"+" : 1 ,
27
27
"-" : 1 ,
28
28
} # Priority of each operator
29
-
29
+
30
30
print_width = len (infix ) if (len (infix ) > 7 ) else 7
31
31
32
32
# Print table header for output
@@ -44,18 +44,21 @@ def infix_2_postfix(infix: str) -> str:
44
44
elif x == "(" :
45
45
stack .append (x ) # if x is "(" push to Stack
46
46
elif x == ")" : # if x is ")" pop stack until "(" is encountered
47
- if len (stack ) == 0 : # close bracket without open bracket
47
+ if len (stack ) == 0 : # close bracket without open bracket
48
48
raise ValueError ("Invalid bracket position(s)" )
49
-
49
+
50
50
while stack [- 1 ] != "(" :
51
51
post_fix .append (stack .pop ()) # Pop stack & add the content to Postfix
52
52
stack .pop ()
53
53
else :
54
54
if len (stack ) == 0 :
55
55
stack .append (x ) # If stack is empty, push x to stack
56
56
else : # while priority of x is not > priority of element in the stack
57
- while len (stack ) > 0 and stack [- 1 ] != '(' \
58
- and priority [x ] <= priority [stack [- 1 ]]:
57
+ while (
58
+ len (stack ) > 0
59
+ and stack [- 1 ] != "("
60
+ and priority [x ] <= priority [stack [- 1 ]]
61
+ ):
59
62
post_fix .append (stack .pop ()) # pop stack & add to Postfix
60
63
stack .append (x ) # push x to stack
61
64
@@ -67,9 +70,9 @@ def infix_2_postfix(infix: str) -> str:
67
70
) # Output in tabular format
68
71
69
72
while len (stack ) > 0 : # while stack is not empty
70
- if stack [- 1 ] == '(' : # open bracket with no close bracket
73
+ if stack [- 1 ] == "(" : # open bracket with no close bracket
71
74
raise ValueError ("Invalid bracket position(s)" )
72
-
75
+
73
76
post_fix .append (stack .pop ()) # pop stack & add to Postfix
74
77
print (
75
78
" " .center (8 ),
@@ -86,32 +89,32 @@ def infix_2_prefix(infix: str) -> str:
86
89
>>> infix_2_prefix('a+b^c')
87
90
Symbol | Stack | Postfix
88
91
----------------------------
89
- c | | c
90
- ^ | ^ | c
91
- b | ^ | cb
92
- + | + | cb^
93
- a | + | cb^a
94
- | | cb^a+
92
+ c | | c
93
+ ^ | ^ | c
94
+ b | ^ | cb
95
+ + | + | cb^
96
+ a | + | cb^a
97
+ | | cb^a+
95
98
'+a^bc'
96
-
99
+
97
100
>>> infix_2_prefix('1*((-a)*2+b)')
98
- Symbol | Stack | Postfix
101
+ Symbol | Stack | Postfix
99
102
-------------------------------------------
100
- ( | ( |
101
- b | ( | b
102
- + | (+ | b
103
- 2 | (+ | b2
104
- * | (+* | b2
105
- ( | (+*( | b2
106
- a | (+*( | b2a
107
- - | (+*(- | b2a
108
- ) | (+* | b2a-
109
- ) | | b2a-*+
110
- * | * | b2a-*+
111
- 1 | * | b2a-*+1
112
- | | b2a-*+1*
103
+ ( | ( |
104
+ b | ( | b
105
+ + | (+ | b
106
+ 2 | (+ | b2
107
+ * | (+* | b2
108
+ ( | (+*( | b2
109
+ a | (+*( | b2a
110
+ - | (+*(- | b2a
111
+ ) | (+* | b2a-
112
+ ) | | b2a-*+
113
+ * | * | b2a-*+
114
+ 1 | * | b2a-*+1
115
+ | | b2a-*+1*
113
116
'*1+*-a2b'
114
-
117
+
115
118
>>> infix_2_prefix('')
116
119
Symbol | Stack | Postfix
117
120
----------------------------
@@ -128,7 +131,7 @@ def infix_2_prefix(infix: str) -> str:
128
131
ValueError: Invalid bracket position(s)
129
132
"""
130
133
infix = list (infix [::- 1 ]) # reverse the infix equation
131
-
134
+
132
135
for i in range (len (infix )):
133
136
if infix [i ] == "(" :
134
137
infix [i ] = ")" # change "(" to ")"
@@ -142,8 +145,9 @@ def infix_2_prefix(infix: str) -> str:
142
145
143
146
if __name__ == "__main__" :
144
147
from doctest import testmod
148
+
145
149
testmod ()
146
-
150
+
147
151
Infix = input ("\n Enter an Infix Equation = " ) # Input an Infix equation
148
152
Infix = "" .join (Infix .split ()) # Remove spaces from the input
149
153
print ("\n \t " , Infix , "(Infix) -> " , infix_2_prefix (Infix ), "(Prefix)" )
0 commit comments