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

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...
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...
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)
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 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.
USING C++ Study the scenario and complete the question(s) that follow: Postfix using Stacks The rules...
USING C++ Study the scenario and complete the question(s) that follow: Postfix using Stacks The rules to convert an infix expression into an equivalent postfix expression are as follows: Suppose infx represents the infix expression and pfx represents the postfix expression. The rules to convert infx into pfx are as follows: 1. Initialize pfx to an empty expression and also initialize the stack. 2. Get the next symbol, sym, from infx. a. If sym is an operand, append sym to...
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,
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT