# the stack class works like a stack
class stack:
def __init__(self):
[Link] = []
def add(self, item):
[Link](item)
def remove(self):
end = [Link][-1]
[Link](-1)
return end
# allows iteration backwards through the stack | Magic method
def __iter__(self):
for op in [Link]:
yield [Link][-1]
def __len__(self):
return len([Link])
def print(self):
pro = ", ".join([Link])
print(f"Stack: {pro}")
# the actual code
class converter:
def __init__(self, infix):
self.operator_stack = stack()
#self.b_depth = 0
[Link] = infix
[Link] = ["+", "-", "*", "/", "^","×", "÷", "−"]
[Link] = ""
# the func
def convert(self):
for char in [Link]:
if char == "(":
pass
#self.b_depth += 1
# how it works is when theres a ) it empties the stack onto the
# postfix expression if theres items in the stack
elif char == ")":
#self.b_depth -= 1
if len(self.operator_stack) > 0:
for i in range(len(self.operator_stack)):
[Link] = [Link] + " " +
self.operator_stack.remove()
# adds operator to the stack
elif char in [Link]:
self.operator_stack.add(char)
[Link] = [Link] + " "
elif char == " ":
pass
# if its an operand add to the postfix
else:
[Link] = [Link] + char
return [Link]
# place where its executed
if __name__ == "__main__":
# example input
# print(converter("((15 ÷ (7 − (1 + 1))) × 3) − (2 + (1 + 1))").convert())
print(converter(input("Enter Infix expression: ")).convert())
# Working one hundred billion percent correctly !!