lab_OOP_2(1)
mysliutchka
plain_text
2 years ago
3.9 kB
3
Indexable
Never
#include <iostream> #include <algorithm> using namespace std; class Set_of_integers { public: Set_of_integers() = default; Set_of_integers(int *&arr, int length) { int *new_array = new int[length]; int i, j, k = 0; for (i = 0; i < length; i++) { for (j = 0; j < k; j++) { if ( arr[i] == new_array[j]) break; } if (j == k) { new_array[k] = arr[i]; k++; } } /*cout << "Repeated elements after deletion : ";*/ this->size = k; for (i = 0; i < k; i++) { this->p_int_set[i] = new_array[i]; } delete[] new_array; } void Display() { cout << "Set:" << endl; for (int a = 0; a < this->size; a++) { cout << this->p_int_set[a] << "\t"; cout << endl; } } int Union(Set_of_integers set2) { sort(this->p_int_set,this->p_int_set + this->size); sort(set2.p_int_set, set2.p_int_set + set2.size); int* result = new int[this->size + set2.size]; int index = 0; int left = 0, right = 0; while (left < this->size && right < set2.size) { if (this->p_int_set[left] < set2.p_int_set[right]) { if (index != 0 && p_int_set[left] == result[index - 1]) { left++; } else { result[index] = p_int_set[left]; left++; index++; } } else { if (index != 0 && this->p_int_set [right] == result[index - 1]) { right++; } else { result[index] = this->p_int_set[right]; right++; index++; } } } while (left < this->size) { if (index != 0 && p_int_set[left] == result[index - 1]) { left++; } else { result[index] = p_int_set[left]; left++; index++; } } while (right < set2.size) { if (index != 0 && this->p_int_set[right] == result[index - 1]) { right++; } else { result[index] = this->p_int_set[right]; right++; index++; } } cout << "Union: "; for (int k = 0; k < index; k++) cout << result[k] << " "; cout << endl; delete[] result; return 0; }; int Intersection(Set_of_integers set2) { int i = 0, j = 0, k = 0; int* result = new int[this->size + set2.size]; while (i < this->size && j < set2.size) { if (this->p_int_set[i] < set2.p_int_set[j]) i++; else if (this->p_int_set[i] > set2.p_int_set[j]) j++; else { if (k != 0 && this->p_int_set[i] == result[k - 1]) { i++; j++; } else { result[k] = this->p_int_set[i]; i++; j++; k++; } } } cout << "Intersection: "; for (int x = 0; x < k; x++) cout << result[x] << " "; cout << endl; delete[]result; } ~Set_of_integers() { delete[] this->p_int_set; }; private: int size; int* p_int_set = new int[size]; }; int main() { int length1; int length2; cout << "Enter the lenght of a first set" << endl; cin >> length1; int* array1 = new int[length1]; for (int i = 0; i < length1; i++) { cout << i << ": "; cin >> array1[i]; } cout << endl; cout << "Entered array of numbers "; for (int i = 0; i < length1; i++) { cout << array1[i] << "\t"; } cout <<endl; cout << "Enter the lenght of a second set" << endl; cin >> length2; int* array2 = new int[length2]; for (int i = 0; i < length2; i++) { cout << i << ": "; cin >> array2[i]; } cout << endl; cout << "Entered array of numbers "; for (int i = 0; i < length2; i++) { cout << array2[i] << "\t"; } cout << endl; Set_of_integers setint1(array1, length1); Set_of_integers setint2(array2, length2); /*Set_of_integers *setint1 = new Set_of_integers(array1, length1); Set_of_integers *setint2 = new Set_of_integers(array2, length2);*/ setint1.Display(); setint2.Display(); setint1.Union(setint2); delete[] array1; delete[] array2; }