Advertisement
Guest User

Probni test 2

a guest
Jan 29th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #define MAX 5
  4.  
  5. class NizBrojeva {
  6. public:
  7.     int* niz;
  8. public:
  9.     NizBrojeva();
  10.     NizBrojeva(int*, int);
  11.     NizBrojeva& operator-=(const NizBrojeva&);
  12.     friend NizBrojeva operator+(const NizBrojeva&, const NizBrojeva&);
  13.     friend ostream& operator<<(ostream&, const NizBrojeva&);
  14. };
  15.  
  16. NizBrojeva::NizBrojeva() {
  17.     niz = new int[MAX];
  18.     for (int i = 0; i < MAX; i++)
  19.         niz[i] = 0;
  20. }
  21.  
  22. NizBrojeva::NizBrojeva(int* pok, int n) {
  23.     niz = new int[MAX];
  24.     for (int i = 0; i < MAX; i++)
  25.         if (i < n)
  26.             niz[i] = pok[i];
  27.         else
  28.             niz[i] = 0;
  29. }
  30.  
  31. NizBrojeva& NizBrojeva::operator-=(const NizBrojeva& n) {
  32.     for (int i = 0; i < MAX; i++)
  33.         niz[i] -= n.niz[i];
  34.  
  35.     return *this;
  36. }
  37.  
  38. NizBrojeva operator+(const NizBrojeva& n1, const NizBrojeva& n2) {
  39.     NizBrojeva n;
  40.     int br = 0;
  41.  
  42.     for (int i = 0; i < MAX; i++)
  43.         if (n1.niz[i] == n2.niz[i])
  44.             n.niz[br++] = 1;
  45.         else
  46.             n.niz[br++] = 0;
  47.  
  48.     return n;
  49. }
  50.  
  51. ostream& operator<<(ostream& out, const NizBrojeva& n) {
  52.     for (int i = 0; i < MAX; i++)
  53.         out << n.niz[i] << "    ";
  54.  
  55.     return out;
  56. }
  57.  
  58. int main()
  59. {
  60.     int niz1[] = { 3, 5, 6, 8, 9, 1 };
  61.     int niz2[] = { 5, 2, 6, 0, 9, 1 };
  62.     int niz3[] = { 5, 2, 6 };
  63.  
  64.     NizBrojeva n1, n2(niz1, 5), n3(niz2, 5), n4(niz3, 3);
  65.  
  66.     cout << "n1:    " << n1 << endl;
  67.     cout << "n2:    " << n2 << endl;
  68.     cout << "n3:    " << n3 << endl;
  69.     cout << "n4:    " << n4 << endl << endl;
  70.  
  71.     cout << "n1 + n2:    " << n1 + n2 << endl;
  72.     cout << "n2 + n3:    " << n2 + n3 << endl;
  73.     cout << "n3 + n4:    " << n3 + n4 << endl << endl;
  74.  
  75.     cout << "n1 -= n2" << endl;
  76.     n1 -= n2;
  77.     cout << "n1:    " << n1 << endl << endl;
  78.  
  79.     cout << "n2 -= n3" << endl;
  80.     n2 -= n3;
  81.     cout << "n2:    " << n2 << endl << endl;
  82.  
  83.     cout << "n3 -= n4" << endl;
  84.     n3 -= n4;
  85.     cout << "n3:    " << n3 << endl << endl;
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement