Advertisement
Josif_tepe

Untitled

Nov 26th, 2023
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. class Array {
  7. private:
  8.     int kapacitet;
  9.     int n;
  10.     int * niza;
  11.  
  12. public:
  13.     Array() {
  14.         kapacitet = 5;
  15.         niza = new int[kapacitet];
  16.         n = 0;
  17.     }
  18.     Array(int _kapacitet) {
  19.         kapacitet = _kapacitet;
  20.         niza = new int[kapacitet];
  21.         n = 0;
  22.     }
  23.     Array & operator += (int x) {
  24.         int tmp_niza[n + 1];
  25.         for(int i = 0; i < n; i++) {
  26.             tmp_niza[i] = niza[i];
  27.         }
  28.         tmp_niza[n] = x;
  29.         n++;
  30.         if(n > kapacitet) {
  31.             kapacitet *= 2;
  32.         }
  33.         niza = new int[kapacitet];
  34.         for(int i = 0; i < n; i++) {
  35.             niza[i] = tmp_niza[i];
  36.         }
  37.         return *this;
  38.     }
  39.     Array & operator -= (int x) {
  40.         int tmp_niza[n];
  41.         int j = 0;
  42.        
  43.         for(int i = 0; i < n; i++) {
  44.             if(niza[i] != x) {
  45.                 tmp_niza[j] = niza[i];
  46.                 j++;
  47.             }
  48.         }
  49.         n = j;
  50.         niza = new int[kapacitet];
  51.         for(int i = 0; i < n; i++) {
  52.             niza[i] = tmp_niza[i];
  53.         }
  54.         return *this;
  55.     }
  56.     bool operator == (Array tmp) {
  57.         if(kapacitet == tmp.kapacitet and n == tmp.n) {
  58.             bool ok = true;
  59.             for(int i = 0; i < n; i++) {
  60.                 if(niza[i] != tmp.niza[i]) {
  61.                     ok = false;
  62.                 }
  63.             }
  64.             return ok;
  65.         }
  66.         else {
  67.             return false;
  68.         }
  69.     }
  70.     int & operator [] (int i) {
  71.         return niza[i];
  72.     }
  73.     friend ostream & operator << (ostream & stream, Array tmp);
  74. };
  75.  
  76. ostream & operator << (ostream & stream, Array tmp) {
  77.     for(int i = 0; i < tmp.kapacitet; i++) {
  78.         if(i < tmp.n) {
  79.             stream << tmp.niza[i] << " ";
  80.         }
  81.         else {
  82.             stream << " - ";
  83.         }
  84.     }
  85.     stream << endl;
  86.     return stream;
  87. }
  88. int main()
  89. {
  90.     Array a;
  91.     a += (6);
  92.     a += (4);
  93.     a += (3);
  94.     a += (2);
  95.     a += (1);
  96.     Array b(a);
  97.     b -= (2);
  98.     b -= (3);
  99.    
  100.     a[0] = 9; //primena na operatorot []
  101.     cout << " a: " << a;
  102.     cout << " b: " << b;
  103.     if (a == b) cout << "Isti se";
  104.     else cout << "Ne se isti";
  105.     return 0;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement