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!