I am solving this problem on evaluating reverse polish notation expressions or postfix expressions involving integers between -2000000 and 2000000. The procedure is pretty straightforward:
if x is a number, put the number in stack
if x is the operator +, pop two values b and a, and push a+b in stack
if x is the operator -, pop two values b and a, and push a-b in stack
if x is the operator *, pop two values b and a, and push a*b in stack
if x is the operator /, pop two values b and a, and push a/b in stack
My program is able to print correct results for the given sample inputs. However, when I submitted it to an online judge, it was judged as Incorrect. Here's the code:
I really feel my algorithm is right. But what could be wrong in my code? Can anyone point the flaws and how I can fix it? Providing critical test input can also help. Thanks in advance!
I think it would be really educational to learn how to use the debugger. If you are using an IDE there should be a built in one that is easy to use. If not, then it is a bit trickier, but not impossible to use one from a command line. For example gdb, can be used from the cmd line, or incorporated into an IDE.
You can set breakpoints, have a watchlist of variable values, and step through the code 1 line at a time.
The whole process can be quicker and less boring than what one might expect initially.
Hope all goes well.
Edit:
Here is a link to K&R C Programming - it has a RPN program.
Sometimes, it is not necessary - on my system a long is 64bits.
I tend to use the stdint.h header, it has types like uint64_t which is an unsigned 64 bit quantity. It has others like int8_t which is a signed 8 bit - I find this less confusing than using a char as a small int.