Friday, 18 January 2013

Infix expression in c++

                /*infix notation*/


                                    #include <iostream>
                                    #include <conio>



        class infix
        {


private:
char sy;
int operand[10];
int op1,op2,value,top,ch;
                          public: /*constructor*/
                          infix()
            {            top=0;              }




            void input()      /*Member function*/
{
            cout<<"Enter for infix Experession\n\n" ;
            cout<<"1=operand\n";
            cout<<"2=operator\n";
            cin>>ch;
            switch(ch)
       {
         case 1:
         push();
         break;
         case 2:
         pop();
         break;
      }
}
        void push()   /*Menber function*/
  {

       if(top==10)
                    cout<<"Stack is over flow\n\n";

                    else
       {
       top++;
                   cout<<"Enter operand\n\n";
                   cin>>operand[top];
       }
   }



                              void pop()  /*Member function*/
  {


                        if(top==0)

                                 cout<<"stack is under flow\n\n";
                        else
                 {
                        cout<<"Enter oprator\n\n";
                        cin>>sy;

                        op1=operand[top];
                        push();
                        op2=operand[top];
                        cout<<"Has poped="<<op1<<endl;
                        top--;
                        cout<<"Has poped="<<op2<<endl;
                        top--;
                    switch(sy)
                    {
                    case '+':
                    value=op2+op1;
                    top++;

                    operand[top]=value;
                     cout<<value<<endl;
                     break;
                     case '-':
                     value=op2-op1;
                     top++;
                     operand[top]=value;
                     cout<<value<<endl;
                     break;
                     case '*':
                     value=op2*op1;
                     top++;
                     operand[top]=value;
                     cout<<value<<endl;
                     break;
                     case '/':
                           value=op2/op1;
                     top++;
                     operand[top]=value;
                     cout<<value<<endl;

                      break;
                      }    /*ending switch*/
                 } /*ending else statement*/

       }
        /*ending of member function*/
       };                                  /*ending of class*/
       void main()
   {
      infix m;
      for(int  i=0 ; i<30; i++)
      {
      m.input();  }
      getch();
      }           /*ending of main function*/

0 comments:

Post a Comment