Problem with Conversion from Infix to Postfix Notation

  • Thread starter zak100
  • Start date
  • Tags
    Notation
In summary, the problem with the infix to posfix conversion algorithm is that multiplication and division have the same precedence, which means that * is done after seeing the / character and before the terms to the right of the / character are parsed.
  • #1
zak100
462
11

Homework Statement


I am trying to convert following from infix to post fix:

(4+8)*(6-5)/((3-2)*(2+2))

I am facing problem at character 20 which is a ‘(‘ i.e. opening bracket.

Homework Equations


There is no eq but the algorihm is given below:
I got following algorithm from internet.

1. if (t is an operand) output t.

2. else if (t is a right parentheses){ POP and output tokens until a left parentheses is popped(but do not output) }

3. else { POP and output tokens until one of lower priority than t is encountered or a left parentheses is encountered. or the stack is empty. PUSH t. }

The Attempt at a Solution


I am facing problem at character 20 which is a ‘(‘ i.e. opening bracket. At character 19 i.e. at '*' i.e. (asterisk) I have following status:

Character = *

Stack = /(*

Output = 4 8 + 6 5 - * 3 2 –

At character 20 i.e. ( , as per above algorithm, I would come to step 3. so I am popping tokens & I have following status after character 20:

Character = (

Stack = /((

Output = 4 8 + 6 5 - * 3 2 – *

But the above is wrong. Some body please guide me what is my mistake?Zulfi.
 
Last edited by a moderator:
Physics news on Phys.org
  • #3
Hi,
I have got a problem with precedence. According to BODMAS rule, division has precedence over multiplication but the infix to posfix conversion algorithm is working opposite to this rule & giving precedence to division over multiplication. For instance in the expression:
(4+8)*(6-5)/((3-2)*(2+2))

At character# 12 which is /, we have following situation:
Character: /
Stack: *
Output: 4 8 + 6 5 -
I have seen that many solutions show:
Stack: /
Output: 4 8 + 6 5 - *

I think this is not right because / has more precedence over *.

Some body please guide me.
Zulfi.
 
  • #4
zak100 said:
According to BODMAS rule, division has precedence over multiplication but the infix to posfix conversion algorithm is working opposite to this rule & giving precedence to division over multiplication
From Wikipedia:
https://en.wikipedia.org/wiki/Order_of_operations
The order of operations used throughout mathematics, science, technology and many computer programming languages is expressed here:[3]

exponents[1] and roots
multiplication[1] and division[1]
addition[1] and subtraction[1]
I'm no expert on the Order of Precedence, but multiplication and division have the same precedence, I believe. So in your equation, without more brackets in the equation, the usual factor that decides whether * or / is done first is left-to-right. So in your equation, the * would be applied after seeing the / character and before the terms to the right of the / character are parsed.
 
  • #5
Hi,
Thanks for removing my confusion. I got the same answer from another forum & they removed my BODMAS confusion also.
https://forum.allaboutcircuits.com/threads/problem-with-conversion-from-infix-to-postfix-notation.132914/

Thanks all for providing me help and related algorithm. This problem is solved now.

Zulfi.
 
  • Like
Likes berkeman

1. What is infix notation and postfix notation?

Infix notation is a mathematical notation in which operators are placed between the operands. For example, 2 + 3. Postfix notation, also known as Reverse Polish Notation, is a mathematical notation in which operators are placed after the operands. For example, 2 3 +.

2. Why do we need to convert from infix to postfix notation?

Infix notation can be difficult to read and evaluate, especially for complex mathematical expressions. Postfix notation is more efficient for computers to evaluate, making it a preferred notation for programming and mathematical calculations.

3. What is the problem with converting from infix to postfix notation?

The main problem with converting from infix to postfix notation is determining the correct order of operations. In infix notation, the order of operations is determined by parentheses, but in postfix notation, the order is determined by the placement of operators.

4. How do you convert from infix to postfix notation?

To convert from infix to postfix notation, you can use the shunting-yard algorithm. This algorithm involves using a stack to store operators and then rearranging the operands and operators based on their precedence and associativity.

5. Are there any advantages to using postfix notation?

Yes, there are several advantages to using postfix notation. It eliminates the need for parentheses, making expressions easier to read and evaluate. It also allows for easier implementation in computer programs and avoids the potential for ambiguity in mathematical expressions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Nuclear Engineering
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
39K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
911
  • Programming and Computer Science
Replies
2
Views
313
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top