Question

In: Computer Science

Find is the final result of evaluating the following postfix expression using a stack. Show each...

Find is the final result of evaluating the following postfix expression using a stack. Show each push and pop operation.

85 5 / 4 * 5   6 +   10    5 -   * +

Solutions

Expert Solution

Let stack be s

=>85 is an operand, we push it in stack, s =85

=>5 is an operand, we push it in stack, s = 5,85(where5 is the top element)

=>/ is an operator, we pop 5 and 85, s becomes empty, 85/5 = 17 and we push 17 in stack, s = 17

=>4 is an operand, we push it in stack, s = 4,17(where 4 is the top element)

=>* is an operator, we pop 4 and 17, s becomes empty, 17*4 = 68 and we push 68 in stack, s = 68

=>5 is an operand, we push it in stack, s = 5,68(where 5 is the top element)

=>6 is an operand, we push it in stack, s = 6,5,68(where 6 is the top element)

=>+ is an operator, we pop 6 and 5, s now holds only 68, 5+6 = 11 and we push 11 in stack, s = 11,68(where 11 is the top element)

=>10 is an operand, we push it in stack, s = 10,11,68(where 10 is the top element)

=>5 is an operand, we push it in stack, s = 5,10,11,68(where 5 is the top element)

=>- is an operator, we pop 5 and 10, stack becomes s =11,68(where 11 is the top element), 10-5 = 5, we push 5 in the stack, s = 5,11,68

=>* is an operator, we pop 5 and 11, stack becomes s = 68, 11*5 = 55 an we push it in the stack, s = 55,68(where 55 is the top element

=>+ is an operator, we pop 55 and 68, stack becomes empty, 68+55 = 123

Therefore, final result is 123

Please give an upvote if you liked my soution.

Thank you :)


Related Solutions

Using a stack, write a program that turns a simple infix arithmetic expression into a postfix...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix expression. For example, 1 + 2 * 3 becomes 2 3 * 1 +. Also, evaluate the expression to ensure the expression is correct.
Python 1) Show the result of evaluating each expression. Be sure that the value is in...
Python 1) Show the result of evaluating each expression. Be sure that the value is in the proper form to indicate its type (int or float). If the expression is illegal, explain why. a) 4.0/10.0 + 3.5 *2 b)10%4 + 6/2 c)abs(4-20//3)**3 d) sqrt(4.5-5.0) + 7*3 e) 3*10//3 + 10%3 f) 3**3 2) Show the sequence of numbers that would be generated by each of the following range experssions. a) range(5) b) range(3,10) c)range (4, 13, 3) d)range (15, 5,...
GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW...
GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW STACK CONFIGURATION DETAILS 20 3 18 8 - * 10 + +10 12 - /
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
CS 400 Assignment 4 Stack application: postfix expression evaluation. Description: - The stack data structure can...
CS 400 Assignment 4 Stack application: postfix expression evaluation. Description: - The stack data structure can be used to evaluate postfix expressions. Please refer to the first 14 pages of this tutorial for postfix expression evaluation: http://www.cs.nthu.edu.tw/~wkhon/ds/ds10/tutorial/tutorial2.pdf Requirement: - deal with single digit positive integers only. Operands and operators are fully separated by space. - learn and use STL stack: http://www.cplusplus.com/reference/stack/stack/ - learn and use isdigit(): http://www.cplusplus.com/reference/cctype/isdigit/ - take in a postfix arithmetic expression from cin, and evaluate its value....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11 12 + 13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators : ( ) + - / * Write a...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression tree and prints the expression in prefix and infix notation and evaluates the expression. (Hint use a stack)
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack • When an operand is read, push it on statck • When an operator is read i.e +, *. /, - – Pop two from the top of the stack and apply the operator and push the result on stack if there is one value instead of two display an error message • Keep repeating until an equal sign, = is read; pop from...
Your assignment for this program is to evaluate a numeric expression in postfix notation using a...
Your assignment for this program is to evaluate a numeric expression in postfix notation using a dynamic (pointer based) stack. As stated in the handout, in order to evaluate a numeric expression, a compiler converts an infix numeric expression to postfix notation and then it uses an algorithm and a stack to evaluate the expression. Your program should implement the pseudocode algorithm described in the attached handout. Your program will read and evaluate expressions stored in an input file (infile.txt)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT