Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. class Stos{
  8.  
  9.     int *stackT;
  10.     int maxValue, index;
  11.  
  12.  
  13. private:
  14.  
  15.     int random_num(int scope, int limit){
  16.         return rand()%scope + limit;
  17.     }
  18.  
  19. public:
  20.  
  21.     Stos(int rozmiar){
  22.         init();
  23.         cout<<"Adres obiektu: "<<this<<endl;
  24.         cout<<"Wywolany zostal konstruktor z 1 parametrem"<<endl;
  25.     }
  26.  
  27.     Stos(){
  28.         init();
  29.         cout<<"Adres obiektu: "<<this<<endl;
  30.         cout<<"Wywolany zostal konstrktor domyslny"<<endl;
  31.     }
  32.  
  33.     void init(){
  34.         index = 0;
  35.         maxValue = random_num(100, 0);
  36.         stackT = new int[maxValue];
  37.     }
  38.  
  39.     bool empty(){
  40.         if(index == 0)
  41.             return true;
  42.         else
  43.             return false;
  44.     }
  45.  
  46.     bool full(){
  47.         if(index == maxValue)
  48.             return true;
  49.         else
  50.             return false;
  51.     }
  52.  
  53.     void push(int value){
  54.         if(!full())
  55.             stackT[index++] = value;
  56.         else
  57.             cout<<"Stos jest pelny";
  58.     }
  59.  
  60.     void pop(){
  61.         if(!empty()){
  62.             int *tmp = new int[maxValue];
  63.             index--;
  64.             for(int i = 0; i < index; i++){
  65.                 tmp[i] = stackT[i];
  66.             }
  67.             delete [] stackT;
  68.             stackT = tmp;
  69.         }
  70.     }
  71.  
  72.     int top(){
  73.         if(!empty())
  74.             return stackT[index-1];
  75.     }
  76.  
  77.     void destroy(){
  78.         delete [] stackT;
  79.         index = 0;
  80.     }
  81.  
  82.     ~Stos(){
  83.         destroy();
  84.         cout<<"Adres obiektu: "<<this<<endl;
  85.     }
  86.  
  87. };
  88.  
  89. void dodaj(Stos s, int a) {
  90.    s.push(a);
  91. }
  92. main() {
  93.    Stos s;
  94.    s.push(0);
  95.    dodaj(s, 1);
  96.    dodaj(s, 2);
  97.    while (!s.empty()) {
  98.       cout <<
  99.       s.top();
  100.       s.pop();
  101.    }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement