Computers and Technology

The Stack As mentioned in class, Python 3 provides the LifoQueue class as part of the Queue library. To see how this is really just a wrapper around the basic list class below is our own Stack class that uses a list This code has been provided to you below in a module called stackclass. py You MAY NOT change the structure of the provided Stack class in ANY way. Your post-fix logic should go in your main script. If you find yourself wanting to add more methods to the class, it means you are writing redundant code and should stop. The Stack class has the following public interface: _init_ #creates an empty stack _str_#returns a string representation of the stack • push(data) #adds a new element (data) to the top of the stack • pop() #removes and returns the top element from the stack .top() #returns what on top of the stack, but doesn't remove it • isEmpty() #Returns True if the stack is empty and False otherwise. Postfix Math You are probably most familiar with math written in infix notation. For example, "4+3 - 7". In infix notation, operators are placed between their inputs. This is a very nice way to read math, but it is nontrivial for a computer to parse. An alternative representation is called postfix notation. In postfix we write the two inputs followed by the operator. The previous example would be "4 3 + 7-". This is much easier to parse because we have both inputs before the operator. We also don't have to worry about parenthesis Note: You may assume all symbols will have spaces between them. This makes it easy to split the expression. You will never be given "2 3+" instead of "23 + You may also assume that the only operators supported are +, -*./. Examples Postfix Infix Result 21+ 2+1 3 54- 5-4 1 72* 7*2 14 93/ 9/3 3 25+7* (2+5)*7 49 Postfix notation works well with a Stack. Here's how you can do it: 1. Push Numbers onto Stack as read from input. 2. When operator seen: 1. Pop top 2 items on stack. Since the second item popped off the top came before the top item in the postfix statement, it is the left operand. 2. Complete Operation. 3. Push result onto Stack. 3. Repeat until end of input. 4. Final Result will be on top of Stack. Within your main script, implement a function postfix(exp) that takes a string exp containing a postfix math expression as input and returns the result as a floating point number. You MUST use the provided Stack class to implement this function. Main Program In your main. py script (which should now also include your postfix(exp) function) ask the user to enter a postfix expression and print the result. Repeat this process until the user enters "exit". Here is an example, which should demonstrated the expected I/O (Note: All the tests for this lab are exact output tests). Welcome to Postfix Calculator Enter exit to quit Enter Expression 1 2 + Result: 3.0 Enter Expression 2 3 + Result: 5.0 Enter Expression 4 5 + Result: 9.0 Enter Expression 6 7 + Result: 13.0 Enter Expression 10 12 + Result: 22.0 Enter Expression 99 1 + Result: 100.0 Enter Expression -9 -8 + Result: -17.0 Enter Expression 1 2 3 4 5 6 + + + + + Result: 21.0 Enter Expression 3 4 * Result: 12.0 Enter Expression 6 5 * Result: 30.0 Enter Expression 9 7 * Result: 63.0 Enter Expression 10 9 * Result: 90.0 * Enter Expression 100 -9 Result: -900.0 Enter Expression 90 3 * Result: 270.0 Enter Expression 5 5 5 5 * * * Result: 625.0 Enter Expression 4 3 - Result: 1.0 Enter Expression 3 4 - Result: -1.0 Enter Expression 9 8 - Result: 1.0 Enter Expression 12 3 - Result: 9.0 Enter Expression 3 12 - Result: -9.0 Enter Expression 10 9 8 7 6 - - - Result: 8.0 Enter Expression 1 2 / Result: 0.5 Enter Expression 4 2 / Result: 2.0 Enter Expression 9 3 / Result: 3.0 Enter Expression 25 5 / Result: 5.0 Enter Expression 125 5 / Result: 25.0 Enter Expression 1 7 / Result: 0.14285714285714285 Enter Expression 100 2 2 2 /// Result: 50.0 Enter Expression 100 2 2 2 ** / Result: 12.5 Enter Expression 4 -1 9 5 2 3 + * - */ Result: 0.25 Enter Expression 100 2 / 2 / 2 / Result: 12.5 Enter Expression 1 2 * 7 + 9 * 11 + Result: 92.0 Enter Expression exit class Stack: #Create a New Empty Stack definit (self): self. __S = [] #Display the Stack def __str__(self): return str(self. __S) #Add a new element to top of stack def push(self, x): self. - S. append(x) #Remove the top element from stack def pop (self): return self.__S. pop #See what element is on top of stack #Leaves stack unchanged def top (self): return self.__S[-1] def isEmpty (self): return len(self. __S)==0

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 01:30, gabriieldelacruz16
What “old fashioned” features of checking accounts is p2p replacing
Answers: 3
image
Computers and Technology, 23.06.2019 09:30, blake2001
Which of the following tasks is an audio technician most likely to perform while working on a nature documentary? (select all that apply). eliminating potentially distracting background noise adding sound effects making sure the lighting is adequate for a particular scene changing the narration to better match the mood of the documentary
Answers: 3
image
Computers and Technology, 23.06.2019 23:30, cam961
What are "open-loop" and "closed-loop" systems
Answers: 1
image
Computers and Technology, 24.06.2019 00:40, ndurairajownkpq
What social factors affect your health
Answers: 3
Do you know the correct answer?
The Stack As mentioned in class, Python 3 provides the LifoQueue class as part of the Queue library....

Questions in other subjects:

Konu
Mathematics, 26.05.2021 20:50