#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define MAX 20
void push(char op);
char pop();
int Priority(char op);
void Conversion(char infix[]);
char stack[MAX];
int top=-1;
int j=0;
char temp;
int main()
{ char infix[20];
scanf("%s",infix);
// printf("%s",infix);
Conversion(infix);
}
void push(char op)
{
if(top==MAX-1)
exit(0);
stack[++top]=op;
}
int getPriority(char ch)
{
if(ch=='^')
return 3;
else if(ch=='*'||ch=='/'||ch=='%')
return 2;
else if(ch=='+'||ch=='-')

return 1;

else if (ch=='(')
return 0;
}
void Conversion(char infix[])
{
int i;
char ch,postfix[20];
for(i=0;i<strlen(infix);i++)
{ ch=infix[i];
if(isalnum(ch))
{
postfix[j++]=ch;
}
else
if(ch=='+'||ch=='*'||ch=='-'||ch=='/'||ch=='%'||ch=='^')
{
while(getPriority(ch)<=getPriority(stack[top]))
{
postfix[j++]=pop();
}
push(ch);
}
else if(ch=='(')
{
push(ch);
}

else if(ch==')')

{
while(stack[top]!='(')
{
postfix[j++]=pop();
}
temp=pop();
}
else
{
printf("Incorrect Infix Expression - Invalid Symbols");
exit(0);
}
}
while(top!=-1)
{
if(stack[top]!='(')
postfix[j++]=pop();
else
{
printf("Invalid Infix Expression - Imbalanced braces");
exit(0);
}
}

postfix[j]='\0';

if(top==-1)
{
printf("\nGiven infix expression is : %s\nPostfix expression is : %s",infix,postfix);
}
else
{
printf("Invalid Infix Expression - Imbalanced braces");
}
}
char pop()
{
if(top==-1)
{
printf("Invalid Infix Expression - Imbalanced braces");
exit(0);
}
return stack[top--];
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: