C Program to convert infix to postfix expression

C Program to convert infix to postfix expression 




#include
#include
char infix[100],post[100];
int top=0,stack[10];
void push(int);
char pop();
void postfix();
main()
{
char ch;
clrscr();
printf("\n Infix Expression ");
gets(infix);
postfix();
getch();
}


void postfix()
{
int i=0,j=0;
for(i=0;infix[i]!='\0';i++)
{
switch(infix[i])
{
case '+': while(stack[top]>=1)
                 post[j++]=pop();
                 push(1);
                 break;
case '-': while(stack[top]>=1)
                post[j++]=pop();
                push(2);
                break;
case '*': while(stack[top]>=3)
                post[j++]=pop();
                push(3);
                break;
case '/': while(stack[top]>=3)
                post[j++]=pop();
                push(4);
                break;
case '^': while(stack[top]>=4)
                post[j++]=pop();
                push(5);
                break;
case '(': push(0);
                break;
                top--;
                break;
default: post[j++]=infix[i];
}
while(top>0)
post[j++]=pop();
printf("\n The postfix expression is %s\n",post);
}


void push(int element )
{
top++;
stack[top]=element;
}
char pop()
{
int ele;
char ex;
ele=stack[top];
top--;
switch(ele)
{
case 1: ex='+';
break
case 2: ex='-';
break;
case 3: ex='*';
break;
case 4: ex='/';
break;
case 5= ex='^';
break;
}
return ex;
}




2 comments:

  1. If you really want to clear your IAS UPSC Exams then you should take admission in the best ias upsc coaching in India to chase your dream job and life.

    ReplyDelete
  2. I really enjoyed reading this post, I always appreciate topics like this being discussed to us. Private Tutor WELLESLEY Thanks for sharing.

    ReplyDelete