I'd very much appreciate it if you could help. I really don't understand what's the problem. The infix2postfix and evaluate functions work fine I guess. So maybe it really has something to do with main.
I'd very much appreciate it if you could help. I really don't understand what's the problem. The infix2postfix and evaluate functions work fine I guess. So maybe it really has something to do with main.
@quzah: I think I terminated the string right. :|
I would advise testing your functions one at a time, not just piling them all into the same program and hoping they work. If he's getting random odd characters in his output, then that typically you aren't terminating correctly. That's also why I included quotes around the string I was trying to display. That way if you see anything weird, like a quote down on a line by itself, you know you have a newline character in there.
Also, you can add some printf lines temporarily to help you troubleshoot. For example:Could be:Code:default: printf("\nInvalid Operator"); return 0; }Oh, also you never actually clear out your stack properly. You just set top to -1. Who knows what values it actually contains. So you have to also be sure you aren't using your junk filled stack incorrectly.Code:default: printf("\nInvalid Operator: %d \'%c\'", *p, *p ); printf("\nevaluate( \"%s\" )\n", postfix ); return 0; }
Quzah.
Last edited by quzah; 03-01-2011 at 08:53 PM.
Hope is the first step on the road to disappointment.
Ok, I still can't fix the problem. Help. T___T
Run this through a debugger, following line by line what's happening with every variable. If you don't know how to use a debugger, learn to ASAP. Debuggers are a godsend for programmers. In the mean time, put some print statements in your functions to display what push and pop are doing (or trying to do). Do likewise throughout infix2postfix and evaluate. I put some print statements in push and pop, and found your code to be popping from an empty stack. Your pop function returns a special value (EMPTY) for this, but you ignore it in a couple places. You should print a detailed error message when things like this happen.
Honestly I ran this through a debugger as well and the problem could be something as innocuous as incrementing p too many times.
Suggestion: follow quzah's advice about clearing the stack. Actually, fill it with something mundane when it's empty, so you can detect problems with it. The post variable is terminated through p, but p is messed up, so post is not terminated correctly. post has stuff like "1 2 3 + * 4 - \377\0007rfa8tu" in it after your infix2postfix function.*
That's a place to start. Good luck.
*Not an actual value of post from my debugger. The junk doesn't matter apart from being there.
If post is printed as "123*+34-" && we enter "123*+4-" as exp, it is safe to conclude that infix2postfix has no bug because it outputted a correct postfix expression. Now, when the program was used to evaluate exp (i.e. 123*+4-), it also outputted a correct answer (i.e. 3). So it is safe to conclude that evaluate also has no bug.
I think the real problem is this: what characteristic is in "exp" array that's not in the "post" array? The only difference I see is that, "exp" was entered by the user, while "post" (despite being displayed as the same with "exp" like I said in my example earlier) was taken using the infix2postfix function.
Maybe it has something to do with the gets() or the fflush()?
Ok whiteflags, I'll try that. Thanks for the advice.
Nope. While gets is a horrible function and should die, and fflush(stdin) is undefined (Salem made this clear in post #2 -- I sincerely hope you have corrected this by now), they're not your problem. Your problem is infix2postfix. As I pointed out, the string in post has an extra garbage character at the end. The fact that you're in Windows might mean you are using some character encoding that doesn't print this bogus character, so you don't see it, but it's there. Use the following print function on post to see what exactly is in it:
You'll see something like ffffffff near the end, which is the -1 that pop is returning that you're putting in post somewhere in infix2postfix.Code:void print_hex(char *s) { while (s && *s) { printf("%02x ", *s++); } }
.EDIT: You also have some trailing spaces in post that confuse evaluate(). Those trailing spaces don't exist in exp
Last edited by anduril462; 03-02-2011 at 01:37 AM.
I see. (Oh dear, I'm an idiot, lol)
Anyways, I don't know how to correctly terminate *p. Suggestions please....I thought that puttingwould do it.Code:*p = '\0';
Yes, *p = '\0' terminates your string, but it only terminates it, so C can deal with it as a string. It doesn't magically remove garbage at the end. You just put a null character in there after the garbage.
Your problem is somewhere in infix2postfix. Everywhere you use a pop in there, you need to make sure you're not putting the -1 (EMPTY) value returned by pop when the stack is empty into the post array (into *p).
That's all I got for now. Sleepy time...
Hey I tried this:
It works if there are more than 2 operands but if there are only 2, it doesn't work.Code:while(!isempty(&X)) { n1 = pop(&X); *p = n1; if(!isempty(&X)) //added this p++; } *p = '\0';
Can anyone help?
Why are you only incrementing if it's not empty? You should always increment, because your loop will stop when it's empty, which would properly null terminate it.Quzah.Code:while( !isempty( &X ) ) { *p = pop( &X ); p++; } *p = '\0';
Hope is the first step on the road to disappointment.
Ok, finally got it.
Here's what I did:
It's working fine in my IDE.Code:int j; for(j=0; !isempty(&X); j++) { if(j>0) { n1 = pop(&X); *p = n1; if(!isempty(&X)) p++; } else { n1 = pop(&X); *p = n1; p++; } } *p = '\0'; }