Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Slack {
  5.     int size, quantity;
  6.     bool if_exist;
  7.     int* tab;
  8. public:
  9.     Slack(int x = 10) {
  10.         cout << "To jest konstruktor!: "<<this << endl;
  11.         init(x);
  12.     }
  13.     ~Slack() {
  14.         cout << "To jest destruktor!: " <<this << endl;
  15.         destroy();
  16.     }
  17.     void init(int x) {
  18.         if_exist = true;
  19.         tab = new int[x];
  20.         size = x;
  21.         quantity = -1;
  22.     }
  23.     void push(int x) {
  24.         if (if_exist == false) { cout << "There is no slack!" << endl; return; }
  25.         else if (quantity < size - 1) {
  26.             quantity++;
  27.             tab[quantity] = x;
  28.             if_exist = true;
  29.         }
  30.         else cout << "There is no space!" << endl;
  31.     }
  32.     void pop() {
  33.         if (if_exist == false) { cout << "There is no slack!" << endl; return; }
  34.         else if (quantity < 0) cout << "There is nothing to pop!" << endl;
  35.         else {
  36.             tab[quantity] = 0;
  37.             quantity--;
  38.         }
  39.     }
  40.     int top() {
  41.         if (if_exist == false) { cout << "There is no slack!" << endl; return -1; }
  42.         else if (quantity < 0) { cout << "There is nothing to show!" << endl; return -1; }
  43.         else {
  44.             return tab[quantity];
  45.         }
  46.     }
  47.     bool empty() {
  48.         if (if_exist == false) { cout << "There is no slack!" << endl; return false; }
  49.         if (quantity < 0) {cout << "It's empty, True!" << endl; return true;}
  50.         else cout << "It's NOT empty, False!" << endl; return false;
  51.     }
  52.     bool full() {
  53.         if (if_exist == false) {cout << "There is no slack!" << endl; return false;}
  54.         if (quantity >= size - 1) {cout << "It is full, True!" << endl; return true;}
  55.         else cout << "It is NOT full, False!" << endl; return false;
  56.     }
  57.     void destroy() {
  58.         size = 0;
  59.         quantity = 0;
  60.         if_exist = false;
  61.         delete[] tab;
  62.     }
  63. };
  64. void f(Slack s, int a) {
  65.     s.push(a);
  66. }
  67. int main() {
  68.     Slack s;
  69.     s.push(0);
  70.     f(s, 1);
  71.     f(s, 2);
  72.     while (!s.empty()) {
  73.         cout <<
  74.             s.top();
  75.         s.pop();
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement