I have written a code for Infix to Postfix in c in it I have not yet written any conditions for for the final push of the stack but the problem in my program is that when I am giving equation AxB+C the sol should be ABxC (according to the program I have written ) but the output coming is ABC. The operator part is not getting pushed in the stack.
x–>*
#include <stdio.h>
char a[100];//input stack
char b[100];//output stack
char c[10];//operator stack
int top=-1;
int n=0,j;
int push(char g[])//pushing the characters into final stack
{
if(top==99)
{
printf("\n OVER FLOW!!! \n");
}
else
{
top+=1;
b[top]=g;
printf("%c\n",b[top]);
}
}
int pop()//poping the element from stack
{
if(top==-1)
{
printf("\n UNDER FLOW!!! \n");
}
else
{
printf(“the element getting deleted is : %c”,b[top]);
top–;
}
}
int pri()//checking the priority of the different operators
{
int o=0;
{
if(o==0) //storing the first operator in c[o]
{
c[o]=a[j];
o++;
}
else if(c[o-1]==43)//checking priority if first element is +
{
if(a[j]==43||a[j]==45)
{
push(c[o-1]);
c[o-1]=a[j];
}
else if(a[j]==42||a[j]==47)
{
c[o]=a[j];
o++;
}
}
else if(c[o-1]==45)//checking priority if first element is -
{
if(a[j]==43||a[j]==45)
{
push(c[o-1]);
c[o-1]=a[j];
}
else if(a[j]==42||a[j]==47)
{
c[o]=a[j];
o++;
}
}
else if(c[o-1]==42)//checking priority if first element is *
{
if(a[j]==42||a[j]==47||a[j]==43||a[j]==45)
{
push(c[o-1]);//the problem is here when i am pushing c[o-1] it is getting stored in output stack
c[o-1]=a[j];
}
}
else if(c[o-1]==47)//checking priority if first element is /
{
if(a[j]==42||a[j]==47||a[j]==43||a[j]==45)
{
push(c[o-1]);
c[o-1]=a[j];
}
}
}
}
int main()
{
printf("\nEnter the equation you want to change : “);
gets(a);
printf(”\nYour given equation is : ");
puts(a);
int i;
for (i=0;a[i]!='\0';i++)
{
n++;
}
for (j=0;j<=n+1;j++)
{
if(j==n+1)
{
}
else
{
if(a[j]==42||a[j]==43||a[j]==45||a[j]==47)
{
pri();
}
else
{
push(a[j]);
}
}
}
puts(b);
puts©;
return 0;
}
I have not yet written the condition where all the last remaining elements of the stack c are pushed into final stack(output stack) b so please ignore that part