- Write a program to convert an infix expression to a postfix expression?

// made by vijay NITJ

#include<iostream.h>

#include<string.h>

#include<stdio.h>

#include<math.h>

int precedence(char);

void main()

{

char q[20];

char p[20];

char stack[20];

int tp=-1,ts=0;

stack[0]='(';

cout<<stack;

int i=0, f=1;

cout<<"enter the prefix expression ";

gets(q);

int l=strlen(q);

q[l]=')';

while(ts!=-1)

{

if(q[i]>=47 && q[i]<=58 q[i]<=65 && q[i]<=90 q[i]<=97 && q[i]>=122)

{

p[++tp]=q[i];

}

else if(q[i]=='(')

stack[++ts]=q[i];

else if(q[i]=='+' q[i]=='-' q[i]=='*' q[i]=='/')

{

if(stack[ts]=='(')

stack[++ts]=q[i];

else

{

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

{

int p1=precedence(q[i]);

int p2=precedence(stack[ts]);

if(p2>=p1)

p[++tp]=stack[ts--];

else

{

f=0;

break;

}

}

if(f==0)

stack[++ts]=q[i];

}

}

else if(q[i]==')')

{

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

{

p[++tp]=stack[ts--];

}

ts--;

}

i++;

}

for(int j=0; j<=tp;j++)

{

cout<<p[j];

}

//precedence program

int precedence(char a)

{

if(a=='+' a=='-')

return 1;

else if(a=='*' a=='/')

return 2;

}