Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Звіт до лабораторної роботи 2.docx
Скачиваний:
4
Добавлен:
01.03.2025
Размер:
163.65 Кб
Скачать

3. Вихідний текст програми розв’язку задачі

Головний файл бібліотеки:

Set.h

#include <iostream>

using namespace std;

class Set

{

friend istream& operator>>(istream & stream, Set & с);

friend ostream& operator<<(ostream & stream, Set & с);

private:

int size;

int* array;

public:

Set() {int cnt = 0; size = 0; array = new int[size]; } // конструктор

Set(int s) { size = s; array = new int[size]; // конструктор с аргументами

for (int i = 0; i < size; i++)

array[i] = 0; }

Set(const Set &ob) // конструктор копии

{ size = ob.size;

array = new int[size];

for ( int i = 0; i < size; i++)

array[i] = ob.array[i]; }

~Set() { delete [] array; } // деструктор

void Enter(); // ввод

void Show(); // вывод

void Union(Set, Set); // объединение

void intersection(Set, Set); // пересечение

void add(); // добавка элемента

void extract(); // извлечение

void check(); // проверка

void operator = (Set); // перегрузки

int operator [] (int i);

Set *Set :: operator -> ()

{

cnt++;

return this;

}

};

Файл бібліотеки:

Set.cpp

#include <iostream>

#include "Set.h"

using namespace std;

ostream& operator << (ostream& stream, Set &c)

{

c.Show();

return stream;

}

istream& operator >> (istream& stream, Set &c)

{

c.Enter();

for (int i = 0; i < c.size; i++)

stream >> c.array[i];

return stream;

}

void Set::Enter()

{

cout << "Введите размер " << endl;

cin >> size;

array = new int[size];

for (int i = 0; i < size; i++)

{

cout << "Введите элемент " << i + 1 << ") ";

cin >> array[i];

}

}

void Set::Show()

{

cout << "Множество " << endl;

for ( int i = 0; i < size; i++)

cout << array[i] << " ";

cout << endl;

}

void Set::Union(Set a, Set b)

{

this->size = a.size + b.size;

this->array = new int[size];

int i, j, count = 0;

int n = a.size;

for ( i = 0; i < a.size; i++)

array[i] = a.array[i];

for ( i = 0; i < b.size; i++)

{

for ( j = 0; j < a.size; j++)

if(b.array[i] == a.array[j])

continue;//пропускаем равные элементы

else

count++;//

if(count == a.size)

array[n++] = b.array[i];

count = 0;

}

size = n;

}

void Set::intersection(Set a, Set b)

{

size = a.size + b.size;

array = new int[size];

int i, j, n = 0;

for ( i = 0; i < a.size; i++)

for ( j = 0; j < b.size; j++)

{

if(a.array[i] == b.array[j])

{

array[n++] = a.array[i];

continue;

}

}

size = n;

}

void Set::add()

{

int a[100];

int i;

int element;

for ( i = 0; i < size; i++)

a[i] = array[i];

delete [] array;

size = size + 1;

cout << "Введите новый элемент " << endl;

cin >> element;

a[i] = element;

array = new int[size];

for ( i = 0; i < size; i++)

array[i] = a[i];

}

void Set::extract()

{

int number, n, i, j;

n = size;

cout << "Введите номер элемента от 1 до " << size << endl;

cin >> number;

number--;

if( number < 0 || number > size)

cout << "Ошибка " << endl;

else

{

for ( i = 0; i < n; i++)

{

if( i == number)

{

for ( j = i; j < n - 1; j++)

array[j] = array[j+1];

n--;

}

}

size--;

}

}

void Set::check()

{

int element;

cout << "Введите элемент для проверки " << endl;

cin >> element;

int i, count = 0;

for ( i = 0; i < size; i++)

if( array[i] == element)

cout << "Да такой элемент найден " << endl;

else

count++;

if(count == size)

cout << "Элемент не найден " << endl;

}

void Set::operator = (Set a)

{

int i;

delete[] this->array;

this->size = a.size;

this->array = new int[size];

for ( i = 0; i < size; i++)

array[i] = a.array[i];

}

Головний модуль:

main.cpp

#include <iostream>

#include "Set.h"

using namespace std;

int main()

{

setlocale(0, "rus");

Set a;

cout << "Ввод и вывод 1 множества " << endl;

a.Enter();

a.Show();

Set b = a;

cout << "инициализация множества 2 с помощью конструктора копии "<< endl;

b.Show();

cout << "Ввод множества 2 " << endl;

b.Enter();

Set c;

cout << "Объединение множеств " << endl;

c.Union(a, b);

c.Show();

Set d;

d.intersection(a, b);

cout << "Пересечение множеств" << endl;

d.Show();

cout << "Добавка элемента "<< endl;

d.add();

d.Show();

cout << "Проверка " << endl;

d.check();

d.Show();

cout << "Извлечение " << endl;

d.extract();

d.Show();

system("PAUSE");

}