Featured
- Get link
- X
- Other Apps
C++ program to convert infix express to postfix expression
Infix Expression: Any expression in the standard form like "A*B-C/D" is an Infix(Inorder) expression.Postfix Expression: The Postfix form of the above expression is "AB*CD/-".
here is the program to convert infix to postfix expression
#include <iostream.h>#include <string.h>#include <ctype.h>
#define MAX 10#define EMPTY -1
struct stack{ char data[MAX]; int top;};
int isempty(struct stack *s){ return (s->top == EMPTY) ? 1 : 0;}
void emptystack(struct stack* s){ s->top=EMPTY;}
void push(struct stack* s,int item){ if(s->top == (MAX-1)) { cout<<"\nSTACK FULL"; } else { ++s->top; s->data[s->top]=item; }}
char pop(struct stack* s){ char ret; if(!isempty(s)) { ret= s->data[s->top]; --s->top; } return ret;}
void display(struct stack s){ while(s.top != EMPTY) { cout<<"\n"<<s.data[s.top]; s.top--; }}
int isoperator(char e){ if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%') return 1; else return 0;}
int priority(char e){ int pri = 0;
if(e == '*' || e == '/' || e =='%') pri = 2; else { if(e == '+' || e == '-') pri = 1; } return pri;}
void infix2postfix(char* infix, char * postfix){ char *i,*p; struct stack X; char n1; emptystack(&X); i = &infix[0]; p = &postfix[0];
while(*i) { while(*i == ' ' || *i == '\t') { i++; }
if( isdigit(*i) || isalpha(*i) ) { while( isdigit(*i) || isalpha(*i)) { *p = *i; p++; i++; } /*SPACE CODE*/ *p = ' '; p++; /*END SPACE CODE*/ }
if( *i == '(' ) { push(&X,*i); i++; }
if( *i == ')') { n1 = pop(&X); while( n1 != '(' ) { *p = n1; p++; /*SPACE CODE*/ *p = ' '; p++; /*END SPACE CODE*/ n1 = pop(&X); } i++; }
if( isoperator(*i) ) { if(isempty(&X)) push(&X,*i); else { n1 = pop(&X); while(priority(n1) >= priority(*i)) { *p = n1; p++; /*SPACE CODE*/ *p = ' '; p++;
/*END SPACE CODE*/ n1 = pop(&X); } push(&X,n1); push(&X,*i); } i++; } } while(!isempty(&X)) { n1 = pop(&X); *p = n1; p++; /*SPACE CODE*/ *p = ' '; p++;
/*END SPACE CODE*/ } *p = '\0';}
int main(){ char in[50]; char post[50];
cout<<"Enter Infix Expression : "; cin.getline(in, 50); infix2postfix(&in[0],&post[0]); cout<<"Postfix Expression is : \n"<<post;
return 0;}
Postfix Expression: The Postfix form of the above expression is "AB*CD/-".
here is the program to convert infix to postfix expression
#include <iostream.h>
#include <string.h>
#include <ctype.h>
#define MAX 10
#define EMPTY -1
struct stack
{
char data[MAX];
int top;
};
int isempty(struct stack *s)
{
return (s->top == EMPTY) ? 1 : 0;
}
void emptystack(struct stack* s)
{
s->top=EMPTY;
}
void push(struct stack* s,int item)
{
if(s->top == (MAX-1))
{
cout<<"\nSTACK FULL";
}
else
{
++s->top;
s->data[s->top]=item;
}
}
char pop(struct stack* s)
{
char ret;
if(!isempty(s))
{
ret= s->data[s->top];
--s->top;
}
return ret;
}
void display(struct stack s)
{
while(s.top != EMPTY)
{
cout<<"\n"<<s.data[s.top];
s.top--;
}
}
int isoperator(char e)
{
if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%')
return 1;
else
return 0;
}
int priority(char e)
{
int pri = 0;
if(e == '*' || e == '/' || e =='%')
pri = 2;
else
{
if(e == '+' || e == '-')
pri = 1;
}
return pri;
}
void infix2postfix(char* infix, char * postfix)
{
char *i,*p;
struct stack X;
char n1;
emptystack(&X);
i = &infix[0];
p = &postfix[0];
while(*i)
{
while(*i == ' ' || *i == '\t')
{
i++;
}
if( isdigit(*i) || isalpha(*i) )
{
while( isdigit(*i) || isalpha(*i))
{
*p = *i;
p++;
i++;
}
/*SPACE CODE*/
*p = ' ';
p++;
/*END SPACE CODE*/
}
if( *i == '(' )
{
push(&X,*i);
i++;
}
if( *i == ')')
{
n1 = pop(&X);
while( n1 != '(' )
{
*p = n1;
p++;
/*SPACE CODE*/
*p = ' ';
p++;
/*END SPACE CODE*/
n1 = pop(&X);
}
i++;
}
if( isoperator(*i) )
{
if(isempty(&X))
push(&X,*i);
else
{
n1 = pop(&X);
while(priority(n1) >= priority(*i))
{
*p = n1;
p++;
/*SPACE CODE*/
*p = ' ';
p++;
/*END SPACE CODE*/
n1 = pop(&X);
}
push(&X,n1);
push(&X,*i);
}
i++;
}
}
while(!isempty(&X))
{
n1 = pop(&X);
*p = n1;
p++;
/*SPACE CODE*/
*p = ' ';
p++;
/*END SPACE CODE*/
}
*p = '\0';
}
int main()
{
char in[50];
char post[50];
cout<<"Enter Infix Expression : ";
cin.getline(in, 50);
infix2postfix(&in[0],&post[0]);
cout<<"Postfix Expression is : \n"<<post;
return 0;
}
- Get link
- X
- Other Apps
Comments
Popular Posts
C++ program to display numbers which get reversed after multiplying by 4
- Get link
- X
- Other Apps
Very Helpful Program!!!!
ReplyDelete