Monday, October 20, 2014

Checking Parenthesis code - in C++

#include<iostream>
#include<string>
#include<cassert>

using namespace std;

template<class T> class StackADT
{
public:

    virtual void initialstack() = 0;
    virtual bool isEmpty() const = 0;
    virtual bool isFull() const = 0;
    virtual void push(const T& otheritem) = 0;
    virtual T top() const = 0;
    virtual void pop() = 0;
};

template <class T> class Stack : public StackADT<T>{
    int size, tos;
    T *list;

public:
    Stack(int size)
    {
        if(size <= 0)
        {
            cout << "Size of the array must be positive" << endl;
            cout << "By default it is set to 100" << endl;
            this->size = 100;
        }
        else
        {
            this->size = size;
        }

        tos = 0;
        list = new T[this->size];
    }
    void initialstack()
    {
        tos = 0;
    }
    bool isFull() const
    {
        return(tos == size);
    }
    bool isEmpty() const
    {
        return(tos == 0);
    }
    void push(const T& otheritem)
    {
        if(!isFull())
        {
            list[tos] = otheritem;
            tos++;
        }
        else
        {
            cout << "Can not add t a full stack" << endl;
        }
    }
    void pop()
    {
        if(!isEmpty())
        {
            tos--;
        }
        else
        {
            cout << "Canno remove from a empty stack" << endl;
        }
    }
    T top() const
    {
        assert( tos != 0);
        return list[tos-1];
    }
    void copystack(const Stack<T>& otherstack)
    {
        delete[] list;
        size = otherstack.size;
        tos = otherstack.tos;

        list = new T[size];
        for(int j=0; j<tosl j++)
        {
            list[j] = otherstack.list[j];
        }
    }
    ~Stack()
    {
        delete []list;
    }

};


int main()
{

   int i, l;
   Stack<char> stack(100);
   char str[100] = {"{(A+B-C}"};
  
   l=strlen(str);
   for(i=0; i<l; i++)
   {
      
       if((str[i] == '(') ||  (str[i] == '{') || (str[i] == '['))
       {
            stack.push(str[i]);
       }
       else if((str[i] == ')') ||  (str[i] == '}') || (str[i] == ']'))
       {
             if(str[i] == ')')
           {
             if(stack.top() == '(')
                    {
                         stack.pop();
                    }
                    else
                    {
                          cout << "The parenthesis are not balanced "<< endl;
                    }
             }
             else if(str[i] == '}')
             {
                 if(stack.top() == '{')
                 {
                     stack.pop();
                 }
                 else
                 {
                     cout << "The parenthesis are not balanced "<< endl;
                 }
             }
             if(str[i] == ']')
             {
                 if(stack.top() == '[')
                 {
                     stack.pop();
                 }
                 else
                 {
                     cout << "The parenthesis are not balanced "<< endl;
                 }
             }
         }
     }
   
   if(stack.isEmpty())
   {
       cout << "The parenthesis are balanced " << endl;
   }
  

 
   return 0;
}

No comments:

Post a Comment