Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Ground Controller Lvl 1
Message 1 of 3

program to evaluate postfix expression

Screenshot (97).pngScreenshot (98).png

i am trying to write a program to evaluate postfix expression. i can't find any error in the code.

but when run it is giving segmentation fault.

someone please explain.especially explain if statement of while loop.

2 Replies
Community Manager
Message 2 of 3

Re: program to evaluate postfix expression

Hi @saurabh2391,

This post was moved to a different board that fits your topic of discussion a bit better. This means you’ll get better engagement on your post, and it keeps our community organized so users can more easily find information.

As you’ll notice, your topic is now here in the Programming Help and Discussion board. No action is needed on your part; you can continue the conversation as normal here.

 


Thank you for your perspective and your contribution!
All the Best

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!

Highlighted
Commander Lvl 2
Message 3 of 3

Re: program to evaluate postfix expression

You didn't provide an example of a postfix expression you are trying to evaluate. So I can merely guess based on code review.

 

Problem 1: your numbers are limited to single characters. Line 22 pushes a single character, not a number. E.g.

if your code was working, this postfix expression: "12 4 +" would give you an answer of "6". The current code pushes '1', then '2', then '4', then pops '4' and '2' to add them.

 

Problem 2: you are mixing characters (e.g. '1', '2', '4') and numbers. Your stack is declared "stack<char>" but then you pop operand1 / operand2, or push result, and they are integers, not characters.  So lets say your postfix expression is "9 2 *". The character '9'

 

Both problems would be solved if you:

- change your operation variable to "stack<int>"

- when reading digits, convert them to ints

 

This looks like a good tutorial on postfix.

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!