Question

In: Computer Science

Problem 2: Evaluate the following postfix expression, using the rules given in Section I of Lab...

Problem 2: Evaluate the following postfix expression, using the rules given in Section I of Lab 10: 1 5 4 – 3 + * 3.  

Computer Science

Solutions

Expert Solution

`Hey,

Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

1 5 4 – 3 + * 3.  

Scan ‘1’, it’s a number, so push it to stack. Stack contains ‘1’

Scan ‘5’, again a number, push it to stack, stack now contains ‘1 5’ (from bottom to top)

Scan ‘4’, again a number, push it to stack, stack now contains ‘1 5 4’ (from bottom to top)

Scan ‘-’, it’s an operator, pop two operands from stack, apply the - operator on operands, we get 5-4 which results in 1. We push the result ‘1’ to stack. Stack now becomes ‘1 1’.

Scan ‘3’, again a number, push it to stack, stack now contains ‘1 1 3’ (from bottom to top)

Scan ‘+’, it’s an operator, pop two operands from stack, apply the + operator on operands, we get 1+3 which results in 4. We push the result ‘2’ to stack. Stack now becomes ‘1 4’.

Scan ‘*’, it’s an operator, pop two operands from stack, apply the * operator on operands, we get 1*4 which results in 4. We push the result ‘4’ to stack. Stack now becomes ‘4’.

So, answer is 4

Kindly revert for any queries

Thanks.


Related Solutions

2. Convert the following infix form expression into the postfix form expression by using a stack:...
2. Convert the following infix form expression into the postfix form expression by using a stack: A+(B*(C-D)+E)/F-G*H Show the stack after each push/pop.
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)....
(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...
The following postfix expression 2 6 + 3 5 - / evaluates to Group of answer...
The following postfix expression 2 6 + 3 5 - / evaluates to Group of answer choices -1 1 -4 4 this is not a postfix expression In an array implementation of the StackInterface where push() and pop() are O(1), the top of the stack is Group of answer choices the first entry in the array the last occupied entry in the array the second entry in the array the entry before the last ocuppied entry in the array the...
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)
this is my code I want the opposite i want to convert a postfix expression to...
this is my code I want the opposite i want to convert a postfix expression to infix expression #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c; } char pop(Stack *st) {   char c = st->s[st->top];   st->top--;   //(A+B)*(C+D)   return c; } /* function to check whether a character is an operator or not. this function...
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2...
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2 + 5 * 8 4 / - The program should read a postfix expression consisting of single digits and operators into a StringBuilder, The program should read the expression and evaluate it (assume it's valid). The algorithm to evaluate a postfix expression is shown below. Use +, -, *, /, and ^. ^ is the exponent. Append a right parenthesis ') ' to the...
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 -   * +
Using java.util.Stack and java.util.StringTokenizer to write a program that converts an infix expression to a postfix...
Using java.util.Stack and java.util.StringTokenizer to write a program that converts an infix expression to a postfix expression using data from infix.dat. Make sure your program can handle negative number. If the input expression can’t be converted, display error message.(1.5 points) (Should remove parentheses) infix.dat 5 * 6 + -10 3 - 2 + 4 7 ( 3 * 4 - (2 + 5)) * 4 / 2 10 + 6 * 11 -(3 * 2 + 14) / 2 2...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT