I was trying to write a code to convert infix expression into postfix expression , I have the source code here and when I compile and give the input say 'a+b' it returns 'ab?' as the answer it does not show the operator symbols at all. Can you guys please tell me where I have gone wrong.!!
It also shows this warning when I compile
infpos.c:74:1: warning: non-void function does not return a value in all control paths [-Wreturn-type]
PS: I got to know what is the error.. Thank you
(The error was in the line 58 I had to use prefix increment but I had used postfix increment)
#include<stdio.h>
#include<ctype.h>
#define max 30
void main()
{
char inf[30],post[30];
void convert(char [],char []);
printf("Enter the infix exxpression\n");
scanf("%s",inf);
convert(inf,post);
printf("Postfix expression is\n");
printf("%s\n",post);
}
void convert(char inf[],char post[])
{
int i,j=0,top=-1,f=1,test;
char stack[30],ch,x;
int check(char);
int pre(char);
for(i=0;inf[i]!='\0';i++)
{
ch = inf[i];
test = check(ch);
switch(test)
{
case 1:
post[j++] = ch;
break;
case 2:
stack[++top] = ch;
break;
case 3:
while((x=stack[top--])!='(')
post[j++] = x;
break;
case 4:
do
{
if(top==-1)
f = 1;
else if(stack[top] == '(')
f = 1;
else if(pre(ch)>pre(stack[top]))
f = 1;
else
{
post[j++] = stack[top--];
f = 0;
}
}while(f==0);
stack[top++] = ch;
break;
}
}
while(top!=-1)
post[j++] = stack[top--];
post[j] = '\0';
}
int pre(char op)
{
if(op == '+' || op=='-')
return 1;
else if(op == '/' || op=='*' || op =='%')
return 2;
else if(op=='^')
return 3;
}
int check(char ch)
{
if(isalnum(ch))
return 1;
else if(ch=='(')
return 2;
else if(ch == ')')
return 3;
else if(ch == '+' || ch =='-' || ch=='/' || ch =='*' || ch=='%' || ch=='^')
return 4;
else
return 5;
}
prereturn if none of the conditions are true? And debugging, debugging, debugging! Step through your code statement by statement while monitoring variables and their values, to see what's going on. Use pen and paper to keep track of the stack you have and other arrays.void main()There are only two valid signatures formain(). They are:int main( void )andint main( int argc, char *argv[] )predoesn't return a value in all cases