#include<conio.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int precedence(char c)
{
switch(c)
{
case '*':
case '/':
return(2);
case '+':
case '-':
return(0);
}
}
char *to_postfix(char *infix)
{
char stack[30],postfix[30];
int i=0,j=0,top=-1;
while(infix[i]!=NULL)
{
if(isalpha(infix[i])||isdigit(infix[i]))
{
postfix[j++]=infix[i++];
}
else if(infix[i]=='(')
{
stack[++top]=infix[i++];
}
else if(infix[i]==')')
{
while(stack[top]!='(')
{
postfix[j++]=stack[top--];
}
top--;
i++;
}
else
{
while(top!=-1&&stack[top]!='('&&precedence(infix[i])<precedence(stack[top]))
{
postfix[j++]=stack[top--];
}
stack[++top]=infix[i++];
}
}
while(top>-1)
{
postfix[j++]=stack[top--];
}
postfix[j]=NULL;
return(postfix);
}
void main()
{
char infix[30];
gets(infix);
puts(to_postfix(infix));
}
abrarsyed 0 Newbie Poster
abrarsyed 0 Newbie Poster
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
abrarsyed 0 Newbie Poster
Trentacle 112 Junior Poster in Training
abrarsyed 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.