Edsger Dijkstra invented the Shunting-yard algorithm to convert infix expressions to postfix (RPN), so named because its operation resembles that of a railroad shunting yard.
There are other ways of producing postfix expressions from infix notation. Most Operator-precedence parsers can be modified to produce postfix expressions; in particular, once an abstract syntax tree has been constructed, the corresponding postfix expression is given by a simple post-order traversal of that tree.
----------------------------------------------------------------------------------------------
A C PROGRAM TO IMPLEMENT INFIX TO POSTFIX EXPRESSION CONVERSION - STACK APPLICATION
COMPILER EMPLOYED: DEV C++ COMPILER-4.9.9.2
SOURCE FILE SIZE :3 kb
EXE FILE SIZE :23 kb
NOTE: PLEASE INCLUDE THE DESIRED HEADER FILE
---------------------------------------------------------------------------------------------
C PROGRAM SOURCE & EXE DOWNLOAD:
Click download button to download
DISCLAIMER: The following program cannot be ensured of perfection.so any flaws in the program can be notified in the comments section.
----------------------------------------------------------------------------------------------
CODE:
#include
#define SIZE 10
char STACK[SIZE];
int TOP=0,ELE;
void PUSH();
char POP();
void SHOW();
int ISEMPTY();
int ISFULL();
char INFIX[30],OUTPUT[30];
int prec(char);
int main()
{
int i=0,j=0,k=0,LENGTH;
char TEMP;
printf("\n CONVERSION OF INFIX TO POSTFIX EXPRESSION");
printf("\n\n ENTER AN INFIX EXPRESSION:");
scanf("%s",INFIX);
printf("\n INFIX EXPRESSION:\t %s",INFIX);
LENGTH=strlen(INFIX);
for(i=0;i
//Numbers are added to the out put QUE
if(INFIX[i]!='+' && INFIX[i]!='-' && INFIX[i]!='*' && INFIX[i]!='/' && INFIX[i]!='^' && INFIX[i]!=')' && INFIX[i]!='(' )
{
OUTPUT[j++]=INFIX[i];
printf("\nTHE ELEMENT ADDED TO QUEUE:%c",INFIX[i]);
}
//If an operator or a bracket is encountered...
else
{
if(TOP==0) //If there are no elements in the STACK, the operator is added to it
{
PUSH(INFIX[i]);
printf("\nELEMENT PUSHED:%c",INFIX[i]);
}
else
{ //Operators or pushed or poped based on the order of precedence
if(INFIX[i]!=')' && INFIX[i]!='(')
{
if( prec(INFIX[i]) <= prec(STACK[TOP-1]) )
{
TEMP=POP();
printf("\n POPPED ELEMENT:%c",TEMP);
OUTPUT[j++]=TEMP;
PUSH(INFIX[i]);
printf("\n PUSHED ELEMENT:%c",INFIX[i]);
SHOW();
}
else
{
PUSH(INFIX[i]);
printf("\n INFIX EXPRESSION:%c",INFIX[i]);
SHOW();
}
}
else
{
if(INFIX[i]=='(')
{
PUSH(INFIX[i]);
printf("\nPUSHED ELEMENT:%c",INFIX[i]);
}
if(INFIX[i]==')')
{
TEMP=POP();
while(TEMP!='(')
{OUTPUT[j++]=TEMP;
printf("\n ELEMENT ADDED TO QUEUE:%c",TEMP);
//TEMP=POP();
printf("\n POPPED ELEMENT:%c",TEMP);
TEMP=POP();}
}
}
}
}
printf("\n INFIX EXPRESSION:%s",OUTPUT);
}
while(TOP!=0)
{
OUTPUT[j++]=POP();
}
printf("\n INFIX EXPRESSION: %s\n",OUTPUT);
system("pause");
return 0;
}
//Functions for operations on STACK
void PUSH(int ELE)
{
STACK[TOP]=ELE;
TOP++;
}
char POP()
{
TOP--;
return(STACK[TOP]);
}
void SHOW()
{
int x=TOP;
printf("STACK ELEMENTS:");
while(x!=0)
printf("%c, ",STACK[--x]);
}
//Function to get the precedence of an operator
int prec(char symbol)
{
if(symbol== '(')
return 0;
if(symbol== ')')
return 0;
if(symbol=='+' || symbol=='-')
return 1;
if(symbol=='*' || symbol=='/')
return 2;
if(symbol=='^')
return 3;
return 0;
}
Your's friendly,
[MOHANRAM.G],
ADMIN...
0 comments:
Post a Comment