M
manohar
//Program to convert infix to postfix
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
class infix
{
char infix[20],stack[10],postfix[20];
public:
infix(); //To initialize private members
void accept(); //To accept input expression
int check(); //This is the function to push elements to stack and
postfix
int check1(char ); //To check whether operator or operand
int diff(int ,int ); //To check whether input expression is valid or
not
void push2stack(char ); //To push elements to stack
int precedence(char ); //Evaluating the precedence of operators
char poppreced(); //To pop elements from stack in case of precedence
void popfstack(char ); //To pop elements from stack
void push2post(char ); //To push the elements to postfix expression
void display(); //To display postfix expression
};
infix::infix()
{
memset(stack,0,sizeof(stack));
memset(postfix,0,sizeof(postfix));
stack[0]='#'; //setting first element of array stack as '#'
}
void infix::accept()
{
cout<<"\nEnter the expression: ";
gets(infix);
infix[strlen(infix)]='#'; //adding '#' at end of array infix
}
int infix::check()
{
char *ptr=infix;
int i=0,cnt=0,cnt1=0,j;
for(i=0;i<strlen(infix);i++)
{
j=check1(*ptr);
switch(j)
{
case 0:
{
cnt++;
//cout<<"\n1."<<*ptr;
push2stack(*ptr);
ptr++;
}
break;
case 1:
{
char *ptr1=&stack[strlen(stack)-1];
cnt++;
//cout<<"\n2."<<*ptr;
popfstack(*ptr1);
ptr++;
}
break;
case 2:
popfstack(*ptr);
break;
case 3:
{
cnt1++;
//cout<<"\n3."<<*ptr;
push2post(*ptr);
ptr++;
}
break;
default:
;
};
}
if(diff(cnt,cnt1)%2==0)
return 0;
else return 1;
}
int infix::check1(char ch)
{
if(ch=='(') return 0;
else if(ch=='+') return 0;
else if(ch=='-') return 0;
else if(ch=='^') return 0;
else if(ch=='*') return 0;
else if(ch=='/') return 0;
else if(ch==')') return 1;
else if(ch=='#') return 2;
else return 3;
}
int infix::diff(int cnt,int cnt1)
{
if(cnt>cnt1)
return (cnt-cnt1);
else if(cnt<cnt1)
return (cnt1-cnt);
else return 0;
}
void infix:ush2stack(char ch)
{
if(stack[0]!=NULL)
if(precedence(ch)<=precedence(stack[strlen(stack)-1]))
{
char ch1=poppreced();
push2post(ch1);
}
else
stack[strlen(stack)]=ch;
}
int infix:recedence(char ch)
{
if(ch=='+' || '-')
return 0;
else if(ch=='/' || '*')
return 1;
else if(ch=='^')
return 2;
else
return 3;
}
char infix:oppreced()
{
char ch=stack[strlen(stack)-1];
stack[strlen(stack)-1]=0;
return(ch);
}
void infix:opfstack(char ch)
{
char *ptr=&ch;
if(ch!='#')
{
while(*ptr=='(')
{
push2post(*ptr);
stack[strlen(stack)-1]=0;
ptr--;
}
stack[strlen(stack)-1]=0;
}
else
{
ptr=&stack[strlen(stack)-1];
while(*ptr!='#')
{
push2post(*ptr);
stack[strlen(stack)-1]=0;
ptr--;
}
stack[strlen(stack)-1]=0;
_exit(0);
}
}
void infix:ush2post(char ch)
{
postfix[strlen(postfix)]=ch;
cout<<endl;
for(int i=0;i<strlen(postfix);i++)
cout<<postfix;
}
void infix::display()
{
cout<<endl;
for(int i=0;i<strlen(postfix);i++)
cout<<postfix;
}
void main()
{
infix a1;
clrscr();
a1.accept();
if(!a1.check())
cout<<"\nInvalid expression";
else
a1.display();
}
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
class infix
{
char infix[20],stack[10],postfix[20];
public:
infix(); //To initialize private members
void accept(); //To accept input expression
int check(); //This is the function to push elements to stack and
postfix
int check1(char ); //To check whether operator or operand
int diff(int ,int ); //To check whether input expression is valid or
not
void push2stack(char ); //To push elements to stack
int precedence(char ); //Evaluating the precedence of operators
char poppreced(); //To pop elements from stack in case of precedence
void popfstack(char ); //To pop elements from stack
void push2post(char ); //To push the elements to postfix expression
void display(); //To display postfix expression
};
infix::infix()
{
memset(stack,0,sizeof(stack));
memset(postfix,0,sizeof(postfix));
stack[0]='#'; //setting first element of array stack as '#'
}
void infix::accept()
{
cout<<"\nEnter the expression: ";
gets(infix);
infix[strlen(infix)]='#'; //adding '#' at end of array infix
}
int infix::check()
{
char *ptr=infix;
int i=0,cnt=0,cnt1=0,j;
for(i=0;i<strlen(infix);i++)
{
j=check1(*ptr);
switch(j)
{
case 0:
{
cnt++;
//cout<<"\n1."<<*ptr;
push2stack(*ptr);
ptr++;
}
break;
case 1:
{
char *ptr1=&stack[strlen(stack)-1];
cnt++;
//cout<<"\n2."<<*ptr;
popfstack(*ptr1);
ptr++;
}
break;
case 2:
popfstack(*ptr);
break;
case 3:
{
cnt1++;
//cout<<"\n3."<<*ptr;
push2post(*ptr);
ptr++;
}
break;
default:
;
};
}
if(diff(cnt,cnt1)%2==0)
return 0;
else return 1;
}
int infix::check1(char ch)
{
if(ch=='(') return 0;
else if(ch=='+') return 0;
else if(ch=='-') return 0;
else if(ch=='^') return 0;
else if(ch=='*') return 0;
else if(ch=='/') return 0;
else if(ch==')') return 1;
else if(ch=='#') return 2;
else return 3;
}
int infix::diff(int cnt,int cnt1)
{
if(cnt>cnt1)
return (cnt-cnt1);
else if(cnt<cnt1)
return (cnt1-cnt);
else return 0;
}
void infix:ush2stack(char ch)
{
if(stack[0]!=NULL)
if(precedence(ch)<=precedence(stack[strlen(stack)-1]))
{
char ch1=poppreced();
push2post(ch1);
}
else
stack[strlen(stack)]=ch;
}
int infix:recedence(char ch)
{
if(ch=='+' || '-')
return 0;
else if(ch=='/' || '*')
return 1;
else if(ch=='^')
return 2;
else
return 3;
}
char infix:oppreced()
{
char ch=stack[strlen(stack)-1];
stack[strlen(stack)-1]=0;
return(ch);
}
void infix:opfstack(char ch)
{
char *ptr=&ch;
if(ch!='#')
{
while(*ptr=='(')
{
push2post(*ptr);
stack[strlen(stack)-1]=0;
ptr--;
}
stack[strlen(stack)-1]=0;
}
else
{
ptr=&stack[strlen(stack)-1];
while(*ptr!='#')
{
push2post(*ptr);
stack[strlen(stack)-1]=0;
ptr--;
}
stack[strlen(stack)-1]=0;
_exit(0);
}
}
void infix:ush2post(char ch)
{
postfix[strlen(postfix)]=ch;
cout<<endl;
for(int i=0;i<strlen(postfix);i++)
cout<<postfix;
}
void infix::display()
{
cout<<endl;
for(int i=0;i<strlen(postfix);i++)
cout<<postfix;
}
void main()
{
infix a1;
clrscr();
a1.accept();
if(!a1.check())
cout<<"\nInvalid expression";
else
a1.display();
}