#pragma once
typedef unsigned long Item;
#include <iostream>
class stack
{
enum { max = 3 } ;
Item * pitem;
int size;
int top;
public :
stack ( int n = max) ;
stack ( const stack& st) ;
~ stack ( ) ;
bool isempty ( ) const ;
bool isfull ( ) const ;
bool push ( const Item& item) ;
bool pop ( Item& item) ;
stack& operator = ( const stack& st) ;
friend std:: ostream & operator << ( std:: ostream & os, const stack & a) ;
} ;
```cpp
#include "stack.h"
stack:: stack ( int n)
{
pitem = new Item[ n] ;
int size = 0 ;
top = 0 ;
}
stack:: stack ( const stack& st)
{
pitem = new Item[ st. size] ;
for ( int i = 0 ; i < st. top; i++ )
{
pitem [ i] = st. pitem[ i] ;
}
size = st. size;
top = st. top;
}
bool stack:: isempty ( ) const
{
return top == 0 ;
}
bool stack:: isfull ( ) const
{
return top == max;
}
bool stack:: push ( const Item& item)
{
if ( top == max)
{
std:: cout << "full" ;
return false ;
}
else
{
pitem[ top++ ] = item;
return true ;
}
}
bool stack:: pop ( Item& item)
{
if ( top == 0 )
{
std:: cout << "empty" ;
return false ;
}
else
{
item = pitem[ -- top] ;
return true ;
}
}
stack & stack:: operator = ( const stack& st)
{
if ( this == & st)
return * this ;
delete [ ] pitem;
pitem = new Item[ st. size] ;
size = st. size;
top = st. top;
for ( int i = 0 ; i < top; i++ )
{
pitem[ i] = st. pitem[ i] ;
}
return * this ;
}
std:: ostream & operator << ( std:: ostream & os, const stack & a)
{
for ( int i = 0 ; i < a. top; i++ )
{
os << a. pitem[ i] ;
}
return os;
}
stack:: ~ stack ( )
{
delete [ ] pitem;
}
```cpp
#include<iostream>
#include"stack.h"
using namespace std;
int main()
{
char ch;
stack s1;
unsigned long po;
cout << "enter a ch;";
while (cin >> ch && ch != 'q')
{
while (cin.get() != '\n')
continue;
switch (ch)
{
case 'a':
if (s1.isempty())
{
cout << "empty is u";
}
else
{
s1.pop(po);
cout << "pop is" << po;
}
break;
case'b':
if (s1.isfull())
{
cout << "full u cant";
}
else
{
cout << "enter a number";
cin >> po;
s1.push(po);
}
break;
default:
break;
}
cout << "enter a ch;";
}
cout << s1;
stack st2;
st2 = s1;
cout << "stack2 = stack is:\n" << st2;
system("pause");
return 0;
}