Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- /*
- Задание:
- Разработать класс для обработки объектов
- в соответствии с требованием индивидуального
- варианта задания.
- 1. Класс должен быть реализован на основе
- статического массива. Деструктор не нужен.
- 2. Выполнить задание пункта 1
- на основе динамического массива.
- Предусмотреть использование деструктора
- Класс множество
- Данные. Размер множества, массив для хранения элементов множества.
- Методы класса:
- 1. Конструктор, создающий множество (на основе вектора),
- инициализированное случайными числами (исключить повтор элементов в
- множестве).
- 2. Вывод элементов множества на экран.
- 3. Удаление элемента из множества.
- */
- class set
- {
- private:
- int n = 0;
- int *a;
- public:
- set(int);
- ~set();
- void output();
- bool add(int);
- bool remove(int);
- set operator+(int);
- set operator-(int);
- };
- int main() {
- cout << "Enter n:";
- int n; cin >> n;
- set v(n);
- cout << "Set: "; v.output(); cout << endl;
- int number1;
- cout << endl << "------------- Non overload class -------------" << endl;
- cout << "Enter add number to set: ";
- cin >> number1;
- v.add(number1);
- cout << "Set: "; v.output();
- cout << endl << "----------------------------------------------" << endl;
- cout << "Enter remove number from set: ";
- cin >> number1;
- v.remove(number1);
- cout << "Set: "; v.output();
- cout << endl << "--------------- Overload class ---------------" << endl;
- cout << "Enter add number to set: ";
- cin >> number1;
- set v2 = v + number1;
- cout << "Set: "; v2.output();
- cout << endl << "----------------------------------------------" << endl;
- cout << "Enter remove number from set: ";
- cin >> number1;
- set v3 = v2 - number1;
- cout << "Set: "; v3.output();
- return 0;
- }
- set::~set() {
- delete []a;
- }
- set::set(int count) {
- this->a = new int[count];
- int tmp;
- for (int i = 0; i < count; i++) {
- bool flag = true;
- cin >> tmp;
- for (int j = 0; j < this->n; j++)
- if (this->a[j] == tmp)
- flag = false;
- if (flag) {
- this->n++;
- this->a[this->n - 1] = tmp;
- }
- }
- //this->output();
- }
- bool set::add(int a) {
- bool flag = true;
- for (int j = 0; j < this->n; j++)
- if (this->a[j] == a)
- flag = false;
- if (flag) {
- this->n++;
- this->a[this->n - 1] = a;
- }
- return flag;
- }
- bool set::remove(int a) {
- int id = 0;
- bool flag = false;
- for (int j = 0; j < this->n; j++)
- if (this->a[j] == a) {
- flag = true;
- id = j;
- }
- if (flag) {
- for (int j = id + 1; j < (this->n); j++)
- this->a[j - 1] = this->a[j];
- this->a[this->n - 1] = 0;
- this->n--;
- }
- return flag;
- }
- void set::output() {
- cout << "{";
- for (int i = 0; i < this->n; i++) {
- cout << this->a[i] << " ";
- }
- cout << "}" << endl;
- }
- set set::operator+(int a) {
- this->add(a);
- return *this;
- }
- set set::operator-(int a) {
- this->remove(a);
- return *this;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement