Evaluating postfix expression in c program


















If the character is left parathesis then push it on the stack. If the character found is right parantheses then pop all the operators from the stack upto the first left parantheses and add all these operators to the postfix string. Now, delete the right and left parantheses. If the character is an operator then pop the operators that have the greater or equal precedence than the character operator and shift these popped operators to the postfix string.

Improve this answer. Paul Ogilvie Paul Ogilvie How can I rewrite this program so that it can compute more than single digit numbers, because honestly my current one is pretty much useless. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science.

Stack Gives Back Viewed 2k times. Improve this question. Andrew Marshall Add a comment. Active Oldest Votes. Improve this answer. I did as you suggested and got rid of the do - while loop in place for a regular while loop and increment postfixStr at the end of the loop. After doing that however, the application crashes, and I can't quite figure out why.

I modified the code to destroy the stack at the end of the function as I thought perhaps unfree memory was causing a problem Any thoughts perhaps?

What error message do you receive when it crashes? Have you run the code through a debugger or added debug print statements to find where the crash is occurring? Just out of curiosity, what IDE are you using? JackDingler 2-Jul pm. A fundamental skill of development is being proficient in using a debugger. Those pop commands are suspect BTW. What happens when you call pop and you have no data? What happens when you call pop and top is -1?

Then you go on stack under flow! Top Rated Most Recent. Accept Solution Reject Solution. You need to suffix your input string with. Posted 2-Jul pm Fredrik Bornander. That is very likely the problem. And in addition, I would recommend to check for reaching the string end in form of the NULL-character; just in case the user forgets to put the at the end. Actually, this terminating is a bad idea anyhow.

Just use the string end to detect the end of the expression. I hope OP reads my hint. Fredrik Bornander 2-Jul pm. Thank you. I agree with you on the parsing comment, the whole part that reads and parses the string is a bit dodgy to say the least. Thank you! That worked.



0コメント

  • 1000 / 1000