hi guys !

i had this code snippet from the net...so i m trying to understand the code but m not getting those points plz help me

1.what does return 2,return 4,return 6 means in this code?
2.why we have case '#' ,why '#' opreator is assigned to stack[top];
3.if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}
4.and i have to use multiple brackets alse like [,{,(,),},] according to prcedence

thanks in advance

#include<stdio.h>
 
#include<conio.h>
 
#include<string.h>
 
#define MAX 20
 
char stack[MAX];
 
int top=-1;
 
char pop();
 
void push(char item);
 
int prcd(char symbol)
 
{
 
switch(symbol)
 
{
 
case '+':
 
case '-':return 2;
 
break;
 
case '*':
 
case '/':return 4;
 
break;
 
case '^':
 
case '$':return 6;
 
break;
 
case '(':
 
case ')':
 
case '#':return 1;
 
break;
 
}
 
}
 
int isoperator(char symbol)
 
{
 
switch(symbol)
 
{
 
case '+':
 
case '-':
 
case '*':
 
case '/':
 
case '^':
 
case '$':
 
case '(':
 
case ')':return 1;
 
break;
 
default:return 0;
 
}
 
}
 
void convertip(char infix[],char postfix[])
 
{
 
int i,symbol,j=0;
 
stack[++top]='#';
 
for(i=0;i<strlen(infix);i++)
 
{
 
symbol=infix[i];
 
if(isoperator(symbol)==0)
 
{
 
postfix[j]=symbol;
 
j++;
 
}
 
else{
 
if(symbol=='(')push(symbol);
 
else if(symbol==')')
 
{
 
while(stack[top]!='(')
 
{
 
postfix[j]=pop();
 
j++;
 
}
 
pop();//pop out (.
 
}
 
else{
 
if(prcd(symbol)>prcd(stack[top]))
 
push(symbol);
 
else{
 
while(prcd(symbol)<=prcd(stack[top]))
 
{
 
postfix[j]=pop();
 
j++;
 
}
 
push(symbol);
 
}//end of else.
 
}//end of else.
 
}//end of else.
 
}//end of for.
 
while(stack[top]!='#')
 
{
 
postfix[j]=pop();
 
j++;
 
}
 
postfix[j]='\0';//null terminate string.
 
}
 
void main()
 
{
 
char infix[20],postfix[20];
 
clrscr();
 
printf("Enter the valid infix string:\n");
 
gets(infix);
 
convertip(infix,postfix);
 
printf("The corresponding postfix string is:\n");
 
puts(postfix);
 
getch();
 
}
 
void push(char item)
 
{
 
top++;
 
stack[top]=item;
 
}
 
char pop()
 
{
 
char a;
 
a=stack[top];
 
top--;
 
return a;
 
}

1.what does return 2,return 4,return 6 means in this code?

Ans:

The function prcd(char) returns these values. These are the values used to indicate a precedence of various operators used.
ie, here the precedence order is
+,- has precedence 2;
*,/ has precedence 4 and is greater than that of +and-
and so on...

2.why we have case '#' ,why '#' operator is assigned to stack[top];

Ans:
'#' is used here to find the end point of stack: because stack is just represented here as an array.

3.if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}

Ans:
our input: infix[]
output: postfix[]

if the symbol is not an operator,
add this symbol directly to the postfix[] array, the output of the program

thnks chikkupa .it really helped me alot.i had modified the code so that i can convert the barackets also..but a problem occurs here that my programm runs fine but whenever i enter "[]" the square brackets it dont convert the expression...

and one thing more how could i have validation on the expression?

#include<stdio.h>
 
#include<conio.h>
 
#include<string.h>
 
#define MAX 40
 
char stack[MAX];
 
int top=-1;
 
char pop();
 
void push(char item);

int prcd(char symbol)
 
{

switch(symbol)
 
{

case '+':

case '-':return 2;

break;

case '*':

case '/':return 5;

break;

case '^':

case '$':

return 6;

break;


case '{':

case '}':

return 4;

break;


case '[':

case ']':

return 3;

break;


break;

case '(':

case ')':

case '#':

return 1;

break;

}

}

int isoperator(char symbol)

{

switch(symbol)

{

case '+':

case '-':

case '*':

case '/':

case '^':

case '$':




case '{':

case '}':

case '(':

case ')':

case '[':

case ']':



return 1;

break;

default:return 0;

}

}

void convertip(char infix[],char postfix[])

{

int i,symbol,j=0;

stack[++top]='#';

for(i=0;i<strlen(infix);i++)

{

symbol=infix[i];

if(isoperator(symbol)==0)

{

postfix[j]=symbol;

j++;

}

else{

if(symbol=='(' || symbol =='{' || symbol == 91)

push(symbol);

else if(symbol==')')

{

while(stack[top]!='(')

{

postfix[j]=pop();

j++;

}

pop();//pop out (.

} // else iof ends here

else if (symbol==93)

{
while (stack[top]!=91)

{

postfix[j]=pop();

j++;

} //2nd else if ends here

pop();

}

else if(symbol=='}')
{
while (stack[top]!='{')
{

postfix[j]=pop();

j++;

}

pop();
} // end of else if ([])

else{

if(prcd(symbol)>prcd(stack[top]))

push(symbol);

else{

while(prcd(symbol)<=prcd(stack[top]))

{

postfix[j]=pop();

j++;

}

push(symbol);

}//end of else.

}//end of else.

}//end of else.
}//end of for.

while(stack[top]!='#')

{

postfix[j]=pop();

j++;

}

postfix[j]='\0';//null terminate string.

 }

void main()

{

char infix[50],postfix[50];

clrscr();

printf("Enter the valid infix string:\n");

gets(infix);

convertip(infix,postfix);

printf("The corresponding postfix string is:\n");

puts(postfix);

getch();

}

void push(char item)
 
{
 
top++;
 
stack[top]=item;
 
}
 
char pop()
 
{
 
char a;
 
a=stack[top];
 
top--;
 
return a;
 
}
This article has been dead for over six months. Start a new discussion instead.