Postfix expression program in c
Connect and share knowledge within a single location that is structured and easy to search. Wanting help with breaking apart postfix expressions and outputting each individual operation.
My issue is NOT evaluating the actual expression, but rather printing each individual operation before evaluating. Now I would like to write a function that outputs each individual operation.
For the example above, it would look something like this:. It would need to output the operations in order of precedence. Since A and B are in parentheses, they would need to be done first, and so on. I have tried to write a function that takes a postfix expression as input and returns nothing. The function creates a stack of type char and loops through the expression from beginning to end.
If the character is an operand, it is pushed to the stack. If the character is an operator, the two top values of the stack are stored in char variables and then outputted with their respective operator. I am not sure what's happening with the last line, but I think it has something to do with the way I am pushing the characters into the stack. I know I am going horribly wrong somewhere, and I am having difficulty understanding.
Any help is greatly appreciated. Thank you. The bug in the program comes from the s stack variable using type char and not type string , which is needed to store multiple characters. Forgot your password? Ask a Question. Please Sign up or sign in to vote. For some reason, this program runs but compiler is terminating and sends me to debug and termination. Any idea why?
I'm stuck like chuck. I commented in the code what it's supposed to do. Copy Code. Posted 2-Jul am lotussilva. Add a Solution. Sergey Alexandrovich Kryukov 2-Jul am. Maybe, it's your code is terminating, not a compiler? Then run it under debugger. I don't know how to debug in Code::Blocks. Please reply to the above post, otherwise SA will never know you left a message for him. 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.
Write a C Program to convert prefix to postfix using stack. Stack is an abstract data type with a bounded predefined capacity. It is a simple data structure that allows adding and removing elements in a particular order. Every time an element is added, it goes on the top of the stack, the only element that can be removed is the element that was at the top of the stack, just like a pile of objects.
0コメント