-
infix to postfix
supposed i am doing a program to convert from infix to postfix
here is my code , i donno whats wrong with the stack of operators and the periorties
can anyone help me?
Code:
void OperatorProcessing(char inputchar,PtrToNode S,PtrToList L)
{
int op;
if(inputchar==')')
{
while(inputchar!='(') // 23mlha while mesh do while
{
op=pop(S);
InsertLastchar(op,L);
}
}
else if(inputchar=='+')
{
op=pop(S);
if(op=='*'||op=='/'||op=='%'||op=='-'||op=='+')
{
InsertLastchar(op,L);
push(inputchar,S);
}
}
else if(inputchar=='-')
{
op=pop(S);
if(op=='*'||op=='/'||op=='%'||op=='-'||op=='+')
{
InsertLastchar(op,L);
push(inputchar,S);
}
}
else if(inputchar=='*')
{
op=pop(S);
if(op=='/'||op=='%'||op=='*')
{
InsertLastchar(op,L);
push(inputchar,S);
}
else // + or -
{
push(op,S);
push(inputchar,S);
}
}
else if(inputchar=='/')
{
op=pop(S);
if(op=='/'||op=='%'||op=='*')
{
InsertLastchar(op,L);
push(inputchar,S);
}
else // + or -
{
push(op,S);
push(inputchar,S);
}
}
else if(inputchar=='%')
{
op=pop(S);
if(op=='/'||op=='%'||op=='*')
{
InsertLastchar(op,L);
push(inputchar,S);
}
else // + or -
{
push(op,S);
push(inputchar,S);
}
}
}
Code:
void popandcheckchar(PtrToNode S,int flag,PtrToList L)
{
int ipchar,sum,flag2;
CharStack Operators; // there is something wrong with this stack
Operators=new struct charnode;//
while(!IsEmpty(S))
{
ipchar=pop(S);
if(ipchar>47 && ipchar<60)
{
printf("\n ipchar = %d its a number",ipchar);
if(flag==0)
{
sum=ipchar-48;
printf("\n\t\t 1st time number SUM = %d",sum);
flag=1;
}
else
{
sum=sum*10+ipchar-48;
printf("\n\t\t Nth time number SUM= %d",sum);
}
}
else
{
printf("\n \n");
printf("\n ipchar = %c its an Operator",ipchar);
flag=0;
InsertLast(sum,L);
sum=0;
if(!IsEmpty(Operators))
{
//the stack is empty
printf("\nthe stack is empty , i am gonna push %c into",ipchar);
push(ipchar,Operators);
}
else
{
// there is an operator in the stack
// go check periorties
OperatorProcessing(ipchar,Operators,L);
}
}
}
// after the end of the while loop
// the equation stack should be empty
// then the last # in sum should be inserted
// and the rest of operators
InsertLast(sum,L);
InsertLastchar(pop(Operators),L);
}