Question

In: Computer Science

Run Python code  List as Stack  and verify the following calculations; submit screen shots in a single file....

Run Python code  List as Stack  and verify the following calculations; submit screen shots in a single file.

Postfix Expression                Result

4 5 7 2 + - * = -16

3 4 + 2  * 7 / = 2

5 7 + 6 2 -  * = 48

4 2 3 5 1 - + * + = 18   

List as Stack

"""

File: pyStackPostfix.py

Author: JD

"""

# 5 7 + 6 2 -  * = 48

print("Postfix Calculator\n")

stack = []              # Empty stack

y = int(0)

z = int(0)

w = int(0)

while True:

   x = input("Enter a postfix expression one by one:")

   if x >= '0' and x<= '9':

      x = int(x)

      stack.append(x)      # Push on top of stack

   elif x == '+':          # Got an operator

      if len(stack) >= 2:

         y = stack.pop()   # Pop from top of stack

         z = stack.pop()

         w = y + z

         stack.append(w)   # Push result back

      else:

         print("Stack error") # Not enough operhand A + B

         break

   elif x == '-':

      if len(stack) >= 2:

         y = stack.pop()

         z = stack.pop()

         w = y - z

         stack.append(w)   

      else:

         print("Stack error")

         break

   elif x == '*':

      if len(stack) >= 2:

         y = stack.pop()

         z = stack.pop()

         w = y * z

         stack.append(w)   

      else:

         print("Stack error")

         break

   elif x == '/':

      if len(stack) >= 2:

         y = stack.pop()

         z = stack.pop()

         w = y / z

         stack.append(w)   

      else:

         print("Stack error")

         break

   elif x == '=':             # Equal operator

      if len(stack) == 1:

         z = stack.pop()      # Pop the result

         print (z)

         break

      else:

         print("Stack error")

         break

Postfix Calculator

Enter a postfix expression one by one:5

Enter a postfix expression one by one:7

Enter a postfix expression one by one:+

Enter a postfix expression one by one:6

Enter a postfix expression one by one:2

Enter a postfix expression one by one:-

Enter a postfix expression one by one:*

Enter a postfix expression one by one:=

48

Press any key to continue . . .

its python

Solutions

Expert Solution

code:

print("Postfix Calculator\n")

stack = []              # Empty stack

y = int(0)

z = int(0)

w = int(0)

while True:

   x = input("Enter a postfix expression one by one:")

   if x >= '0' and x<= '9':

      x = int(x)

      stack.append(x)      # Push on top of stack

   elif x == '+':          # Got an operator

      if len(stack) >= 2:

         y = stack.pop()   # Pop from top of stack

         z = stack.pop()

         w = z + y            #here u have to change it to operand2 operator operand1

         stack.append(w)   # Push result back

      else:

         print("Stack error") # Not enough operhand A + B

         break

   elif x == '-':

      if len(stack) >= 2:

         y = stack.pop()

         z = stack.pop()

         w = z - y      #here u have to change it to operand2 operator operand1

         stack.append(w)

      else:

         print("Stack error")

         break

   elif x == '*':

      if len(stack) >= 2:

         y = stack.pop()

         z = stack.pop()

         w = z * y              #here u have to change it to operand2 operator operand1

         stack.append(w)

      else:

         print("Stack error")

         break

   elif x == '/':

      if len(stack) >= 2:

         y = stack.pop()

         z = stack.pop()

         w = z // y    #here u have to change it to operand2 operator operand1,extra "/" operator for floor division.

         stack.append(w)

      else:

         print("Stack error")

         break

   elif x == '=':             # Equal operator

      if len(stack) == 1:

         z = stack.pop()      # Pop the result

         print (z)

         break

      else:

         print("Stack error")

         break


output:


Related Solutions

Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Run the commands used for following tasks and paste the screen shots here: a) Search/find a...
Run the commands used for following tasks and paste the screen shots here: a) Search/find a specified word in a file and display all the lines containing that word. b) List all the files and directories in the current directory (including the hidden files). c) Change to the root directory / and show a complete long listing of it. d) Append the output of file1 to file2. [1 Mark] e) Count how many lines, words and characters in file1. f)...
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
In the PDF file that will contain your screen shots, explain what is wrong with the...
In the PDF file that will contain your screen shots, explain what is wrong with the code below. Do not just state thing like “line 3 should come after line 7,” you must explain why it needs to be changed. This shows that you are fully understanding what we are going over. // This program uses a switch-case statement to assign a // letter grade (A, B, C, D, or F) to a numeric test score. #include <iostream> using namespace...
In java, write a class that tests the following code. Add screen shots as well. public...
In java, write a class that tests the following code. Add screen shots as well. public class DoubleStack<E> {    private E[] elements;    private int top1, top2;    private static int DEFAULT_SIZE = 10;       public DoubleStack() {        elements = (E[])new Object[DEFAULT_SIZE];        top1 = 0;        top2 = elements.length - 1;    }       public boolean isFull(int stack) {        if(top2 == top1 + 1)            return true;   ...
Run the following ANOVA analysis in excel and interpret your findings. Upload your screen shots of...
Run the following ANOVA analysis in excel and interpret your findings. Upload your screen shots of the steps you took in running the analysis in excel. Treatment 1 Treatment 2 Treatment 3 0 1 6 1 4 5 0 1 8 3 2 5
Run the following ANOVA analysis in excel and interpret your findings. Upload your screen shots of...
Run the following ANOVA analysis in excel and interpret your findings. Upload your screen shots of the steps you took in running the analysis in excel. Treatment 1 Treatment 2 Treatment 3 0 1 6 1 4 5 0 1 8 3 2 5
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Name your program file warmup.py Submit your working Python code to your CodePost.io account. In this...
Name your program file warmup.py Submit your working Python code to your CodePost.io account. In this challenge, establish if a given integer num is a Curzon number. If 1 plus 2 elevated to num is exactly divisible by 1 plus 2 multiplied by num, then num is a Curzon number. Given a non-negative integer num, implement a function that returns True if num is a Curzon number, or False otherwise. Examples is_curzon(5) ➞ True 2 ** 5 + 1 =...
Marielab1 input your age and display what your age would be next year. submit screen shots...
Marielab1 input your age and display what your age would be next year. submit screen shots from Marie. im 24 and going to be 25 next year.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT