+ 6

Infix to Postfix. How to do this correctly?

PS - This is not related to programming. I'm sorry, but I had a doubt and I wished to make a program for this, so... I recently learned conversion of infix to postfix expressions. Thus a*(b+c) will be converted to abc+* using stacks. I made a program for the same and it works ( for many, but not all). Consider a+b+c. Me and my program both generate the final expression as abc++. But on checking the net, I found that it was ab+c+. Here is my approach to this : https://code.sololearn.com/cvA1wSM5pSqh/?ref

30th Aug 2017, 1:00 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
11 Respuestas
+ 13
from reading that article that krishna posted it would seem abc++ is correct, ab+c+ seems to be unnecessary (unless I am missing something) as order is always read left to right..
31st Aug 2017, 10:04 AM
jay
jay - avatar
+ 12
yeah best to do what the teacher wants
31st Aug 2017, 10:26 AM
jay
jay - avatar
+ 11
Once again you have exceeded my knowledge base. Wish I could help.
31st Aug 2017, 9:43 AM
jay
jay - avatar
+ 8
both are correct. but if she says to use "abc++" then use that. check your text book, ask some other professor then go with that. while you may use "ab+c+" & may be even right, the person checking your exam paper may not know this is correct & may deduct marks or flat out mark it wrong( worst case scenario).
31st Aug 2017, 10:03 AM
Lord Krishna
Lord Krishna - avatar
+ 8
@Kinshuk Vasisht do what works best for you. i tried the online converter this was the infix expression ((15 ÷ (7 − (1 + 1))) × 3) − (2 + (1 + 1)) i got this result 15 ÷ 7 − 1 1 + × 3 − 2 1 1 + + but when i gave it in postfix-infix it shows it as invalid
31st Aug 2017, 10:30 AM
Lord Krishna
Lord Krishna - avatar
+ 7
@jay you are not alone here read the definition http://www.cs.man.ac.uk/~pjj/cs212/fix.html for more results: https://duckduckgo.com/?q=infix+and+postfix&t=h_&ia=web
31st Aug 2017, 9:55 AM
Lord Krishna
Lord Krishna - avatar
+ 7
@jay more read on polish notation or simple postfix https://en.m.wikipedia.org/wiki/Reverse_Polish_notation
31st Aug 2017, 10:04 AM
Lord Krishna
Lord Krishna - avatar
+ 7
http://scanftree.com/Data_Structure/prefix-postfix-infix-online-converter the previous example throws error on most converter's online. another example infix: 7+8*12/9 postfix: 7 8 12 * 9 / + //the operator multiplication was considered according to operator precedence.
31st Aug 2017, 10:42 AM
Lord Krishna
Lord Krishna - avatar
+ 6
Well, let me try a postfix to infix calculator now. That will clear my doubts. If both yield a same answer, then Ill use abc++. Else, Ill show them why it isn't abc++ (Ill convert it back to infix and display the result).
31st Aug 2017, 10:07 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 6
@Lord Krishna && @ jay scanftree.com/Data_Structure/prefix-postfix-infix-online-converter Checking both statements here makes me conclude that both are the same. Both yield a+b+c, and so Ill write according to the education board (After all, now I can' argue any more).
31st Aug 2017, 10:24 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 5
I have read more on this and found that : The expression a+b+c is evaluated as (a+b)+c as the + operator is left to right associative. This is why the postfix comes out to be ab+c+ and this is useful in a-b-c. But a+b+c is also equal to a+(b+c), then why isn't this used, and why is the associativity fixed like this? I'm from India, and my teacher has told me to use only abc++, even when I explained to her the logic and the real answer comes out to be ab+c+. Her explanation was that this is what our education system wants and in the end, this will only fetch you marks... What am I to do? Please explain me which is correct - abc++ or ab+c+ ?
31st Aug 2017, 9:56 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar