Question
I am having to write a Stack class that uses postfix notation for calculating problems entered from a text file using Python. I have posted
I am having to write a Stack class that uses postfix notation for calculating problems entered from a text file using Python. I have posted pictures of my code, the text file, and my resulting execution as well as what the correct execution should be. Any advice would be appreciated, thanks.
Written code:
class Stack: def __init__(self): self.__data = [None] * 10 self.__length = 0 def __destroy__(self): self.__data = [] return def is_stack_empty(self): if len(self.__data) == 0: return True else: return False
def is_stack_full(self): if self.__length == 10: return True else: return False
def push(self, item): self.__data.append(item) self.__length += 1 #return
def pop(self): try: return self.__data.pop() #self.__length -= 1 return except Exception as ex: print("ERROR: Cannot pop from an empty list.")
def top(self): return self.__data[-1] def __len__(self): return len(self.__data)
def __str__(self): return str(self.__data)
def main(): file_name = input("Enter the name of the file containing postfix expressions: ") #expression = input("Enter a mathmatical expression: ") #e_list = expression.split() #validates file found = False
while not found: try: in_file = open(file_name) found = True except Exception as ex: print(file_name, " is not found.") file_name = input("Please re-enter a valid text file name: ") stack = Stack() for each in in_file: try: value = int(each) except Exception as ex: second = stack.pop() first = stack.pop()
if each == "+": try: answer = first + second stack.push(answer) except Exception as ex: print("ERROR: "+ expression + " is an invalid postfix expression.") elif each == "-": try: answer = first - second stack.push(answer) except Exception as ex: print("ERROR: "+ expression + " is an invalid postfix expression.") elif each == "*": try: answer = first * second stack.push(answer) except Exception as ex: print("ERROR: "+ expression + " is an invalid postfix expression.") elif each == "/": try: answer = first / second stack.push(answer) except Exception as ex: print("ERROR: "+ expression + " is an invalid postfix expression.") else: stack.push(value) print("Answer: ", stack.pop())
return
main()
class Stack: def init_ (self): self. data - [None] 10 self. length 0 def destroy(self): self. data-[ return def is stack empty (self): if len (self. data) 0: return True else: return False def is_stackfull (self): if self. length10: return True else: return Ealse def push (self, item) self. data.append (item) self. length 1 #return def pop (self) try return self. data.pop( #self. length- 1 return except Exception as ex: print ("ERROR: Cannot pop from an empty list.") def top (self) class Stack: def init_ (self): self. data - [None] 10 self. length 0 def destroy(self): self. data-[ return def is stack empty (self): if len (self. data) 0: return True else: return False def is_stackfull (self): if self. length10: return True else: return Ealse def push (self, item) self. data.append (item) self. length 1 #return def pop (self) try return self. data.pop( #self. length- 1 return except Exception as ex: print ("ERROR: Cannot pop from an empty list.") def top (self)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started