subject

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

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 14:10
Dean wants a quick way to look up staff members by their staff id. in cell q3, nest the existing vlookup function in an iferror function. if the vlookup function returns an error result, the text “invalid staff id” should be displayed by the formula. (hint: you can test that this formula is working by changing the value in cell q2 to 0, but remember to set the value of cell q2 back to 1036 when the testing is complete.)
Answers: 3
question
Computers and Technology, 22.06.2019 22:00
Consider the following declarations (1, 2, 3, 5, 7)class bagtype{public: void set(string, double, double, double, double); void print() const; string getstyle() const; double getprice() const; void get(double, double, double, double); bagtype(); bagtype(string, double, double, double, double); private: string style: double l; double w; double h; double price; }; a.) write the definition of the number function set so that private members are set according to the parametersb.) write the definition of the member function print that prints the values of the data membersc.) write the definition of the default constructor of the class bagtype so that the private member variables are initialized to "", 0.0, 0.0, 0.0, 0.0, respectively d.) write a c++ statement that prints the value of the object newbag.e.) write a c++ statement that declares the object tempbag of type bagtype, and initialize the member variables of tempbag to "backpack", 15, 8, 20 and 49.99, respectively
Answers: 3
question
Computers and Technology, 23.06.2019 03:00
State 7 common key's for every keyboard
Answers: 1
question
Computers and Technology, 23.06.2019 13:30
Me ! evelyn is a manager in a retail unit. she wants to prepare a report on the projected profit for the next year. which function can she use? a. pmt b. round c. division d. what-if analysis
Answers: 2
You know the right answer?
The Stack As mentioned in class, Python 3 provides the LifoQueue class as part of the Queue library....
Questions
question
Mathematics, 29.06.2019 12:10
Questions on the website: 13722363