How to Evaluating Arithmetic Expression?
Data Structure > Stack Data Structure > Polish Notation
1277
Answer:
There are 3 different ways of representing the algebraic expression. They are
* INFIX NOTATION
* POSTFIX NOTATION
* PREFIX NOTATION
INFIX NOTATION
In Infix notation, the arithmetic operator appears between the two operands to which it is being applied. For example: - A / B + C
POSTFIX NOTATION
The arithmetic operator appears directly after the two operands to which it applies. also called reverse polish notation. ((A/B) + C)For example: - AB / C +
PREFIX NOTATION
The arithmetic operator is placed before the two operands to which it applies.
Also called as polish notation. ((A/B) + C)For example: - +/ABC
INFIX PREFIX (or) POLISH POSTFIX (or) REVERSEPOLISH
1. (A + B) / (C - D) /+AB - CD AB + CD - /
2. A + B*(C - D) +A*B - CD ABCD - * +
3. X * A / B - D - / * XABD X A*B/D-
4. X + Y * (A - B) / +X/*Y - AB - CD XYAB - *CD - / +(C - D)
5. A * B/C + D + / * ABCD AB * C / D
To evaluate an arithmetic expressions, first convert the given infix expression to postfix expression and then evaluate the postfix expression using stack.
Infix to Postfix Conversion
Read the infix expression one character at a time until it encounters the delimiter "#"
Step 1: If the character is an operand, place it on to the output.
Step 2: If the character is an operator, push it onto the stack. If the stack operator
has a higher or equal priority than input operator then pop that operator from the
stack and place it onto the output.
Step 3: If the character is a left parenthesis, push it onto the stack.
Step 4: If the character is a right parenthesis, pop all the operators from the stack
till item counters left parenthesis, discard both the parenthesis in the output.
Evaluating Postfix Expression
Read the postfix expression one character at a time until it encounters the
delimiter `#'.
Step 1: - If the character is an operand, push its associated value onto the stack.
Step 2: - If the character is an operator, POP two values from the stack, apply the
operator to them and push the result onto the stack.
This Particular section is dedicated to Question & Answer only. If you want learn more about Data Structure. Then you can visit below links to get more depth on this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.