CONVERT 2*3/(2-1)+5*3 TO POSTFIX AND EVALUATE IT USING STACK
Answers
Answer:
While reading the expression from left to right, push the element in the stack if it is an operand. Pop the two operands from the stack, if the element is an operator and then evaluate it. Push back the result of the evaluation.Jun 19, 2017
Explanation:
try google answer understand
Answer:
The Postfix Expression of the given Infix Expression is
2 3 2 1 - 5 3 * + / *
Explanation:
- The given Infix 2*3/(2-1)+5*3
- Conversion of Infix to Postfix using Stack.
Symbol postfix string Stack
2 2
* 2 *
3 2 3 *
/ 2 3 * /
( 2 3 * / (
2 2 3 2 * / (
- 2 3 2 * / ( -
1 2 3 2 1 * / ( -
) 2 3 2 1 - * /
+ 2 3 2 1 - * / +
5 2 3 2 1 - 5 * / +
* 2 3 2 1 - 5 * / + *
3 2 3 2 1 - 5 3 * / + *
2 3 2 1 - 5 3 * * / +
2 3 2 1 - 5 3 * + * /
2 3 2 1 - 5 3 * + / *
2 3 2 1 - 5 3 * + / *
- An Algorithm to convert an Infix string to Postfix String:
1. opstk = the empty stack
2. while (not end of input) {
3. symb = next input character;
4. if (symb is an operand)
add symb to the postfix string
5. else {
6. while (!empty(opstk) && prcd(stacktop(opstk) , symb)) {
7. topsymb = pop (opstk);
8. add topsymb to the postfix string;
} /* end while */
9. if (empty (opstk) || symb != ')' )
push (opstk , symb)
else /* pop the open parenthesis and discard it * /
topsymb = pop(opstk);
}
}
10. while (!empty (opstk)) {
11. topsymb = pop (opstk);
12. add topsymb to the postfix string;
} /*end while */
- In the above Algorithm, the prcd(op1, op2) where op1 and op2 are characters representing operators. This function returns TRUE if op1 has precedence over op2 when op1 appears to the left of op2 (other than a left parenthesis ) in an infix expression.
- It returns FALSE otherwise.