Infix to Postfix

Program

#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
#define MAX 100
class stack{
 int top;
 char infix[MAX],postfix[MAX],s[MAX];
 public:
  stack()
  {
  top=-1;
}
  void read();
  char Push(char);
  char Pop();
  int conversion();
  char Prioity(char);
  void display();
};
char stack::Push(char a){
 if(top>MAX)
 cout<"STACK is over flow";
 else{
  top++;
  s[top]=a;
 }
}
char stack::Pop(){
 char item;
 if(top==-1)
    {
       cout<<"\n Stack is under flow"<<endl;
    }
    else
    { item=s[top];
     top--;
     return item;
}
}
char stack::Prioity(char x){
 switch(x){
     case'(':
  return 0;
  case'+':
  return 1;
  case'-':
  return 1;
  case'*':
  return 2;
  case'/':
  return 2;
}
}
int stack::conversion(){
 int k=0,i;char sp;
 for(i=0;infix[i]!='\0';i++&&k++)
 {
 if(infix[i]>='0'&&infix[i]<='9')
 postfix[k]=infix[i];
 else if(infix[i]>='A'&&infix[i]<='Z')
 postfix[k]=infix[i];
 else if(infix[i]>='a'&&infix[i]<='z')
 postfix[k]=infix[i];
 else if(infix[i]=='(')
 Push(infix[i]);
 else if(infix[i]==')'){
  while(sp=Pop()!='('){
   infix[i]=sp;}}
 else{
  while(top!=-1)
  {
   while(Prioity(infix[i])<=Prioity(sp=Pop())){
   postfix[k]=sp;
   }
}
   //if(Prioity(infix[i])>Prioity(sp)){
   Push(sp);
   Push(infix[i]);
   //}
  }
}
 while(top!=-1){
  sp=Pop();
  postfix[k]=sp;
  k++;}
 postfix[k-1]='\0';
 //for(i=0;postfix[i]='\0';i++)
  cout<<"Expression in Postfix :"<<postfix;
}
/*void stack::display(){
 for(int i=1;postfix[i]='\0';i++)
  cout<<"Expression in Postfix :"<<postfix[i];
 }*/

void stack::read(){
 cout<<"Enter expression in postfix:";
 cin>>infix;
 //cout<<"Expression in Postfix :"<<infix;
}
int main(){
 stack s;
 s.read();
 s.conversion();
// s.display();
}

Comments

Popular posts from this blog

Stack

Operation on Single Linked List

EXTRA INFORMATION ABOUT TECHNOLOGY