Friday, 7 December 2012

Postfix evaluation using CPP

CPP Program to implement post fix evaluation

#include<iostream.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>

const int MAX=50;

class polish
{ private:
           char *stack;
          int top;
  public:
           polish(void);
           void push(char c);
          char pop(void);
           int priority(char c);
           int convert(char *,char *);
           int calculate(char *);
           int IsValid(char *,char *);
};

polish::polish()
{        top=-1;
           stack=new char[MAX];
}


void polish::push(char c)
{        top++;
           stack[top]=c;
}

char polish::pop(void)
{        char item=stack[top];
          top--;
          return item;
}

int polish::IsValid(char *s,char *t)
{        polish temp;
          if(*s=='\0')
 {       strcpy(t,"Null expression");
                   return 0;
 }
          while(*s)
 {       if(!((*s=='*')||(*s=='+')||(*s=='/')||(*s=='%')||(*s=='-')||(*s=='^')||
                    (*s=='(')||(*s==')')||(*s==' ')||((*s>='0')&&(*s<='9'))||
                     ((*s>='A')&&(*s<='Z'))||((*s>='a')&&(*s<='z'))))
                   {        strcpy(t,"Invalid character in expression");
                             return 0;
                    }
                   if(*s=='(')
                              temp.push(*s);
                   if(*s==')')
                              temp.pop();
                    s++;

                   }
 if(temp.top!=-1)
{        strcpy(t,"Parentheses mismatch in expression");
                             return 0;
                   }
 return 1;
}
int polish::priority(char c)
{        if(c=='^')
                    return 3;
 else
                    if((c=='*')||(c=='/')||(c=='%'))
                     return 2;
                   else
                    if(c=='+'||c=='-')
                              return 1;
                   else
                                return 0;
}

int polish::convert(char *s,char *t)
{        if(!IsValid(s,t))return 0;
                   while(*s)
{        if(*s==' '||*s=='\t')
                              {       s++;
                                      continue;
                             }
                    while(isdigit(*s)||isalpha(*s))
                   {        *t=*s;
                              s++;
                              t++;
                    }
                    if(*s=='(')
                   {        push(*s);
                              s++;
                    }
           char opr;
           if((*s=='*')||(*s=='+')||(*s=='/')||(*s=='%')||(*s=='-')||(*s=='^'))
          {        if(top>-1)
                    {       opr=pop();
                    while(priority(opr)>=priority(*s))
                    {       *t=opr;
                              t++;
                             opr=pop();
                    }
           push(opr);
           push(*s);
          }
            else
                     push(*s);
                     s++;
          }
          if(*s==')')
          {        opr=pop();
                   while(opr!='(')
                    {       *t=opr;
                              t++;
                             opr=pop();
                   }
                     s++;
             }
}
while(top!=-1)
{        char opr=pop();
                   *t=opr;
  t++;
}
          *t='\0';
           return 1;
}

int polish::calculate(char *t)
{          int n1,n2,n3=0;
          while(*t)
          {        if(isdigit(*t))
                   {         int temp=*t-'0';
                               push(temp);
                   }
                    else
                    {       n1=pop();
                             n2=pop();
                   switch(*t)
           {       case '+':
                              n3=n2+n1;
                             break;
                   case '-':
                              n3=n2-n1;
                              break;
                   case '/':
                               n3=n2/n1;
                             break;
                   case '*':
                               n3=n2*n1;
                               break;
                   case '%':
                             n3=n2%n1;
                             break;
case '^':
                             n3=pow(n2,n1);
                             break;
                   default:
                             cout<<"Unknown operater";
                              exit(1);
                   }
                    push(n3);
           }
           t++;
          }
          return n3;
  }
void main()
{        int c;
          polish q,p;
          char infixexp[MAX],postfixexp[MAX];
           while(1)
           {       clrscr();
cout<<"\nPerform the Following Operations on Numeric Expressions";
cout<<"\n_________________________________________"<<endl;
                   cout<<"\n1.Input an Infix Expression";
                   cout<<"\n2.Display the Infix Expression";
                   cout<<"\n3.Convert Infix to Postfix Expression";
                   cout<<"\n4.Evaluate the Postfix Expression";
                   cout<<"\n5.Exit from Program";
                    do
                    {
                             cout<<"Select Your Choice(1:5):";
                             cin>>c;
                              cout<<" ";
                    }
                    while((c<1)||(c>5));
           if(c==5)exit(1);
                    clrscr();
                    switch(c)
                   { case 1:
                             clrscr();
                             cin.ignore(1);
                             cout<<"\nEnter an expression in infix form:";
                             cin.getline(infixexp,MAX);
                             char*temp;
                             if(!q.IsValid(infixexp,temp))
                              cout<<"Error!"<<temp;
                             break;
                   case 2:
                             clrscr();
                             cout<<"The infix expression you entered is:"<<infixexp;
                             break;
                   case 3:
                              if(q.convert(infixexp,postfixexp))
                                       cout<<"\nThe postfix form of the infix expression"
                                        <<infixexp<<"is:";
                              else
                                       cout<<"Error!";
                                         cout<<postfixexp;
                              break;
                   case 4:
cout<<"Evaluation of "<<postfixexp<<" result is "<<p.calculate(postfixexp);
                             break;
                    }
          cout<<endl<<"Press Any Key to Continue...";
           getch();
          }
  }

No comments:

Post a Comment