123 Eng

Engineering the engineers™


Latest Jobs   Forum Map

 


Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

   Computer Science / IT
   Electronics
   Electrical
   Mechanical
   Chemical
   Civil

   CAT / MBA

   GMAT / Foreign MBA
Latest Jobs

Engineering Jobs / Technical Jobs
Management Jobs

Sitemap
Terms of use

Displaying  Source Code(s)  
 

 
Stack With OOP and Exception Handling Concept.

--------------------------------------------------------------------------------

Description : Stack With OOP and Exception Handling Concept.

#ifndef STACK_H
#define STACK_H

template <class A>
class Stack
{
public:
virtual void makeNewStack(int) = 0 ;
virtual void push(A) = 0 ;
virtual A pop() = 0 ;
virtual bool isEmpty() = 0 ;
virtual bool isFull() = 0 ;
virtual A topOfStack() = 0 ;
virtual void display() = 0 ;
virtual ~Stack(){}
virtual A traverse() = 0 ;
virtual bool endTrav() = 0 ;
virtual void resetTrav() = 0 ;
} ;
#endif

#ifndef STATICSTACK_H
#define STATICSTACK_H

#include <stdlib.h>
#include "Stack.h"
#include "StackExceptions.h"
#include <iostream>
#include<string>
using namespace std ;

template <class T>
class StaticStack : public Stack<T>
{
private:
int top ;
int size ;
T* array ;
int trav;

public:

StaticStack()
{
top = -1 ;
size = 0 ;
array = NULL ;
trav = 0 ;

}

StaticStack(int s)
{
top = -1 ;
size = s ;
array = new T[size] ;
trav = 0 ;
}

virtual void makeNewStack(int s)
{
delete[] array ;
top = -1 ;
trav = 0 ;
size = s ;
array = new T[size] ;

}

virtual void push(T arg)
{
if(isFull())
{
throw StackFull("The Stack is Full") ;
}
else
{
if(top == -1)
{
top = top + 1 ;
array[top] = arg;
top++;
}
else
{
array[top] = arg ;
top++ ;
}
}
}

virtual T pop()
{
if(isEmpty())
{
throw StackEmpty("The Stack is Empty") ;
}

else
{
top-- ;
if(top == -1)
{
throw StackEmpty("The Stack is Empty") ;
}
else
{

return array[top] ;
}
}
}

virtual T traverse()
{
/* if( trav == top )
{
char ch;
cout<<"

The value of Traverse reach at it`s Peak";
cout<<"
May you Want to Reset The Traverse (y/n) : ";
cin>>ch;
if(ch=='y')
resetTrav();*/
}
if(endTrav())
{
throw CantTrav("Transverse Cant Proceed");
}
else
{
if( trav > top )
{
trav = trav - 2 ;
if(trav == -1)
throw CantTrav("Transverse Cant Proceed Because The Stack Is Empty");
else
{
return array[trav++];
}
}
else if(trav == top)
{
trav = trav - 2 ;
if(trav == -1)
throw CantTrav("Transverse Cant Proceed Because The Stack Is Empty");
else
{
return array[trav];
}

}
else
{
if(trav == -1)
throw CantTrav("Transverse Cant Proceed Because The Stack Is Empty");
else
{
return array[trav++];
}
}
}
}

virtual bool endTrav()
{
if(top == -1)
{
return (trav == top + 1) ;
}
else
return (trav == top) ;
}

virtual void resetTrav()
{
trav = 0;
}

virtual bool isEmpty()
{
return (top == -1) ;
}

virtual bool isFull()
{
return (top == size) ;
}

virtual T topOfStack()
{
if(isEmpty())
{
throw StackEmpty("The Stack is Empty") ;
}
else
{
return (array[top - 1]) ;
}
}

virtual void display()
{
int i = -1;
cout << endl ;
for(i = i+1 ; i < top ; i++)
{
cout << array[i] << ' ' ;
}
cout << endl ;
}

virtual ~StaticStack()
{

delete[] array ;
top = 0 ;
size = 0 ;
array = NULL ;
}
} ;

#endif

#ifndef STACKEXCEPTIONS_H
#define STACKEXCEPTIONS_H

#include <exception>
using namespace std ;

class StackEmpty : public exception
{
public:
StackEmpty(char* c) : exception(c)
{}
} ;

class StackFull : public exception
{
public:
StackFull(char* c) : exception(c)
{}
} ;
class CantTrav : public exception
{
public:
CantTrav(char* c) : exception(c)
{}
};

#endif
#include "StaticStack.h"
//#include "StackExceptions.h"
#include <iostream>
using namespace std ;

void main()
{
int size;
cout<<"
Enter the Maximum Size of Stack : ";
cin>>size;
Stack<int>* S = new StaticStack<int>(size) ;

int choice ;
int data ;
bool cont = true ;

while(cont)
{
try
{

cout<< endl << "Enter 1 push()"
<< endl << "Enter 2 to pop()"
<< endl << "Enter 3 to display()"
<< endl << "Enter 4 to exit"
<< endl << "Enter 5 to transverse"
<< endl << "Enter 6 to reset the transverse" << endl;

cin >> choice ;
switch(choice)
{
case 1:
cout << endl << "Enter value to push in stack <BR> ;
cin >> data ;
S->push(data) ;
break ;
case 2:
data = S->pop() ;
cout << "The value returned is : " << data ;
break ;

case 3:
S->display() ;
break ;

case 4:
cont = false ;
break ;
case 5:
data = S->traverse();
cout<< "The value returned is : "<<data << endl << endl;
break;
case 6:
S->resetTrav();
break;
default:
cout <<endl<< "wrong value entered, try again" << endl ;
}// switch ends

}
catch(exception e)
{
cout << endl << e.what() << endl ;
}
} // while loop ends

delete S ;
}
 

 

 

Contribute content or training reports / feedback / Comments
job placement papers
All rights reserved © copyright 123ENG