Friday, August 27, 2010
C Program to convert infix to postfix expression
#include<stdio.h>
#include<conio.h>
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;
}
Subscribe to:
Post Comments (Atom)
Checkout my version of Infix to Postfix Conversion :)
ReplyDeletehttp://msumca2012.blogspot.in/2013/02/infix-to-postfix-conversion.html