Friday, 18 January 2013

Balancing symbol in c++ using stacks

   /*Algorithm*/
               /*1. Make an Empty stack*/
               /*2.Read symbol untll the end of input & repeat steps 3 & 4*/
               /*3.If the read symbol is an opening symbol then push it into the stack*/
               /*4.If  the read symbol is closly symbol then do following */
                        /*i.Check if stack is empty then report error else pop the stack*/
                        /*ii.Check If symbol read & symbol poped corresponding symbol else
                        report error*/
               /*5.At the end of file if the stack  is non.emnpty then also give error report*/


/**********implementation of Balancing symbols*****************/

#include <iostream>
#include <conio>
class app
{
private:
char stack[6],sy;
int top;
public:
    app()         /*constructor*/
{
top=0;
}


void input()    /*Make a mamber function which is taking a symbol*/
{
cout<<"Enter symbol\n";
cin>>sy;
}
                    void push() /*member function*/
{
        if(top==6)

                cout<<"stack is over flow\n\n";
        else

        if(sy=='('||sy=='{'||sy=='[')
         { top++;
          stack[top]=sy;
          cout<<"has been push into stack\n="<<stack[top]<<endl; }
         

 }




             void pop()          /*member function*/
{
             if(top==0)

          cout<<"stack is under flow\n\n";

          else
          {
                 if(sy==')'||sy=='}'||sy==']')
                    {

   if(stack[top]=='('&&sy==')'||stack[top]=='{'&&sy=='}'||stack[top]=='['&&sy==']')
   {
   cout<<"has been poped"<<stack[top]<<endl;
   top--;
   }

   else

   cout<<"Error Detection"<<endl;
   }
   }

}
   void Report()
   {
   if(top!=0)
   {cout<<"Error detection\n\n";}

   }





  };





                         void main()  /*main function*/
               {


                   app    s;
                   int   ch;

for(int i=0;i<100;i++)                        /*using for loop  control shit again and again the member function*/
{
       s.input();

        s.push();

        s.pop();

       cout<<"1=select 1 goto again enter a sybol"<<endl;
       cin>>ch;
       }

                s.Report();

       getch();
       }

0 comments:

Post a Comment