Advertisement
Josif_tepe

Untitled

Nov 26th, 2023
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 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.     friend ostream & operator << (ostream & stream, Array tmp);
  57. };
  58.  
  59. ostream & operator << (ostream & stream, Array tmp) {
  60.     for(int i = 0; i < tmp.kapacitet; i++) {
  61.         if(i < tmp.n) {
  62.             stream << tmp.niza[i] << " ";
  63.         }
  64.         else {
  65.             stream << " - ";
  66.         }
  67.     }
  68.     stream << endl;
  69.     return stream;
  70. }
  71. int main()
  72. {
  73.     Array a;
  74.     a += (6);
  75.     a += (4);
  76.     a += (3);
  77.     a += (2);
  78.     a += (1);
  79.     Array b(a);
  80.     b -= (2);
  81.     b -= (3);
  82.    
  83.     cout << a << endl;
  84.     cout << b << endl;
  85.     return 0;
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement