Reputation: 1247
I had to build a C program which convert's infix notation into postfix notation using STACK. That went well and it's working in some way. It was long ago when I used last time C language so I'm probably dont use char[] variables very well.
So problem is that when I give input like this :
A+B*(C*E-D)
My program returns this :
ABCE*D-*+ĚĚĚĚĚĚĚĚĚĚĚ
So as you see my program did postfix conversion very well but I have bunch of "garbage" chars at my result (ĚĚĚĚĚĚĚĚĚĚĚ).
Here is snippet of my code (only part that I think is not correct, maybe something with char[] and way how I assing value to postfix[] variable:
int main()
{
char infix[20], postfix[20];
int len, tip, i, p=0;
STACK pom;
MAKE_NULL(&pom);
printf ("Unesi izraz.\n");
scanf ("%s", infix);
len = strlen(infix);
for(i=0; i<len; i++)
{
tip = nadi_tip(infix[i]);
if (tip == Lijeva)
{
PUSH (infix[i], &pom);
}
if (tip == Operand)
{
postfix[p] = infix[i];
p++;
}
if (tip == Desna)
{
while (!EMPTY(pom) && (TOP(pom)!= '('))
{
postfix[p++] = TOP(pom);
POP (&pom);
}
POP (&pom);
}
if (tip == Operator)
{
while (!EMPTY(pom) && TOP(pom)!= '(')
{
if(prioritet(infix[i]) <= prioritet(TOP(pom)))
{
postfix[p++] = TOP(pom);
POP (&pom);
}
else break;
}
PUSH(infix[i], &pom);
}
}
while (EMPTY(pom) != 1)
{
postfix[p++] = TOP(pom);
POP(&pom);
}
printf("Izlaz: %s", postfix);
return 0;
}
infix[] is my input and postfix[] is my output. What did I do wrong I why am I having ĚĚĚĚĚĚĚĚĚĚĚ characters. Thank you in advance!
Upvotes: 2
Views: 11993
Reputation: 4897
YOU DIDN'T NULL TERMINATE YOUR STRINGS! Kidding. Did you get that message yet from the thirty or so other people that told you? Just to add some information on top of that, the garbage characters are an attempt to interpret whatever happens to be in memory beyond the end of your char[]
as chars. It grabs all it can of whatever is in memory until it hits a null terminating character and spits it all out, and the reason it's the same every time is that your char[]
and ĚĚĚĚĚĚĚĚĚĚĚ
get allocated next to each other every time the program runs. If you already knew all of this, then I apologize for wasting your time with another superfluous answer.
Upvotes: 4
Reputation: 17557
As others have said you should initialize your array.
Or, at any time in the program, you can use,
memset (infix ,0, 20);
memset (postfix, 0, 20);
That will set all the values of array elements to zero.
Upvotes: 3
Reputation: 44240
My guess is that you are confusing characters and pointers to strings. Where are the definitions for POP() and PUSH() ? What is ndi_tip() Why don't you use a switch inside the forloop, that is much easier to read and maintain.
Upvotes: 0
Reputation: 9031
char infix[20], postfix[20];
You do not initialize these, nor append '\0' at the end of algorithm.
Upvotes: 3
Reputation: 490108
It looks like you don't have a NUL terminator on your postfix
string. You could either change the definition to char postfix[20] = {0};
, or you just before the printf
, you could add postfix[p] = '\0';
Upvotes: 4
Reputation: 182619
You need to NUL-terminate postfix
.
postfix[p] = 0;
printf...
A simpler (but slightly less efficient) method would be to initialize your array to {0}
or to memset
it to 0
.
Upvotes: 3