Advertisement
ilyakanyshev

lab4 v7

Jan 20th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. /*
  6.     Задание:
  7.     Разработать класс для обработки объектов
  8.     в соответствии с требованием индивидуального
  9.     варианта задания.
  10.     1. Класс должен быть реализован на основе
  11.        статического массива. Деструктор не нужен.
  12.     2. Выполнить задание пункта 1
  13.        на основе динамического массива.
  14.        Предусмотреть использование деструктора
  15.  
  16.     Класс множество
  17.  
  18.     Данные. Размер множества, массив для хранения элементов множества.
  19.     Методы класса:
  20.     1. Конструктор, создающий множество (на основе вектора),
  21.        инициализированное случайными числами (исключить повтор элементов в
  22.        множестве).
  23.     2. Вывод элементов множества на экран.
  24.     3. Удаление элемента из множества.
  25.  
  26. */
  27.  
  28. class set
  29. {
  30. private:
  31.     int n = 0;
  32.     int *a;
  33. public:
  34.     set(int);
  35.     ~set();
  36.     void output();
  37.     bool add(int);
  38.     bool remove(int);
  39.     set operator+(int);
  40.     set operator-(int);
  41. };
  42.  
  43. int main() {
  44.     cout << "Enter n:";
  45.     int n; cin >> n;
  46.     set v(n);
  47.     cout << "Set: "; v.output(); cout << endl;
  48.     int number1;
  49.     cout << endl << "------------- Non overload class -------------" << endl;
  50.     cout << "Enter add number to set: ";
  51.     cin >> number1;
  52.     v.add(number1);
  53.     cout << "Set: "; v.output();
  54.     cout << endl << "----------------------------------------------" << endl;
  55.     cout << "Enter remove number from set: ";
  56.     cin >> number1;
  57.     v.remove(number1);
  58.     cout << "Set: "; v.output();
  59.     cout << endl << "--------------- Overload class ---------------" << endl;
  60.     cout << "Enter add number to set: ";
  61.     cin >> number1;
  62.     set v2 = v + number1;
  63.     cout << "Set: "; v2.output();
  64.     cout << endl << "----------------------------------------------" << endl;
  65.     cout << "Enter remove number from set: ";
  66.     cin >> number1;
  67.     set v3 = v2 - number1;
  68.     cout << "Set: "; v3.output();
  69.     return 0;
  70. }
  71.  
  72. set::~set() {
  73.     delete []a;
  74. }
  75.  
  76. set::set(int count) {
  77.     this->a = new int[count];
  78.     int tmp;
  79.     for (int i = 0; i < count; i++) {
  80.         bool flag = true;
  81.         cin >> tmp;
  82.         for (int j = 0; j < this->n; j++)
  83.             if (this->a[j] == tmp)
  84.                 flag = false;
  85.         if (flag) {
  86.             this->n++;
  87.             this->a[this->n - 1] = tmp;
  88.         }
  89.     }
  90.     //this->output();
  91. }
  92. bool set::add(int a) {
  93.     bool flag = true;
  94.     for (int j = 0; j < this->n; j++)
  95.         if (this->a[j] == a)
  96.             flag = false;
  97.     if (flag) {
  98.         this->n++;
  99.         this->a[this->n - 1] = a;
  100.     }
  101.     return flag;
  102. }
  103. bool set::remove(int a) {
  104.     int id = 0;
  105.     bool flag = false;
  106.     for (int j = 0; j < this->n; j++)
  107.         if (this->a[j] == a) {
  108.             flag = true;
  109.             id = j;
  110.         }
  111.     if (flag) {
  112.         for (int j = id + 1; j < (this->n); j++)
  113.             this->a[j - 1] = this->a[j];
  114.         this->a[this->n - 1] = 0;
  115.         this->n--;
  116.     }
  117.     return flag;
  118. }
  119. void set::output() {
  120.     cout << "{";
  121.     for (int i = 0; i < this->n; i++) {
  122.         cout << this->a[i] << " ";
  123.     }
  124.     cout << "}" << endl;
  125. }
  126. set set::operator+(int a) {
  127.     this->add(a);
  128.     return *this;
  129. }
  130. set set::operator-(int a) {
  131.     this->remove(a);
  132.     return *this;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement