Advertisement
Guest User

Untitled

a guest
Feb 14th, 2012
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1.  #include <iostream.h>
  2. class StackException
  3. {
  4.   char * message;
  5. public:
  6.   StackException(char *msg)
  7.     : message(msg)
  8.   {
  9.   }
  10.   inline friend ostream & operator << (ostream & out, StackException & e)
  11.   {
  12.     return out << "StackException: " << e.message;
  13.   }
  14. };
  15.  #define STACK_SIZE 1024
  16. class StackMemento
  17. {
  18.   friend class ArrayStack;
  19.   int top;
  20.   char buf[STACK_SIZE];
  21.   StackMemento(char * curBuf, int curTop)
  22.   {
  23.     top = curTop;
  24.     for ( int i = 0; i < curTop; ++i )
  25.       buf[i] = curBuf[i];
  26.   }
  27.   void restore( char * curBuf, int & curTop )
  28.   {
  29.     curTop = top;
  30.     for ( int i = 0; i < top; ++i )
  31.       curBuf[i] = buf[i];
  32.   }
  33. };
  34. class StackIter;
  35. class ArrayStack
  36. {
  37.   friend class StackIter;
  38.   // static const int STACK_SIZE = 1024;
  39.   char buf[STACK_SIZE];
  40.   int top;
  41. public:
  42.   ArrayStack()
  43.     : top(0)
  44.   {
  45.   }
  46.   void push(char c)
  47.     throw (StackException)
  48.   {
  49.     if ( isFull() )
  50.       throw StackException("Full");
  51.     buf[top++] = c;
  52.   }
  53.   char pop()
  54.     throw (StackException)
  55.   {
  56.     if ( isEmpty() )
  57.       throw StackException("Empty");
  58.     return buf[--top];
  59.   }
  60.   bool isEmpty()
  61.   {
  62.     return top <= 0;
  63.   }
  64.   bool isFull()
  65.   {
  66.     return top >= STACK_SIZE;
  67.   }
  68.   StackMemento * createMemento()
  69.   {
  70.     return new StackMemento(buf, top);
  71.   }
  72.   void restoreState( StackMemento * m )
  73.   {
  74.     m->restore(buf, top);
  75.   }
  76.   StackIter * createIter();
  77. };
  78. class StackIter
  79. {
  80.   ArrayStack * stk;
  81.   int index;
  82. public:
  83.   StackIter( ArrayStack * s )
  84.   {
  85.     stk = s;
  86.     reset();
  87.   }
  88.   void reset()
  89.   {
  90.     index = 0;
  91.   }
  92.   void advance()
  93.   {
  94.     ++index;
  95.   }
  96.   bool hasMore()
  97.   {
  98.     return index < stk->top;
  99.   }
  100.   char current()
  101.   {
  102.     return stk->buf[index];
  103.   }
  104. };
  105. StackIter * ArrayStack::createIter()
  106. {
  107.   return new StackIter( this );
  108. }
  109. ostream & operator << (ostream & out, ArrayStack & stk)
  110. {
  111.   for ( StackIter & i = *stk.createIter(); i.hasMore(); i.advance() )
  112.     out << i.current();
  113.   return out;
  114. }
  115. int main()
  116. {
  117.   ArrayStack & stk = *new ArrayStack();
  118.   try
  119.   {
  120.     for ( char c = 'A'; c <= 'Z'; ++c )
  121.       stk.push(c);
  122.     cout << "Stack: " << stk << endl;
  123.     StackMemento * m = stk.createMemento();
  124.     cout << "Stack: " << stk << endl;
  125.     cout << "Stack: ";
  126.     while ( !stk.isEmpty() )
  127.       cout << stk.pop();
  128.     cout << endl;
  129.     cout << "Stack: " << stk << endl;
  130.     stk.restoreState( m );
  131.     cout << "Stack: " << stk << endl;
  132.   }
  133.   catch (StackException & e)
  134.   {
  135.     cout << e << endl;
  136.   }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement