Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "settemplate.h"
- template <typename T>
- Set<T>::Set()
- {
- length = 0;
- }
- template <typename T>
- int Set<T>::GetLength()
- {
- return length;
- }
- template <typename T>
- void Set<T>::AddEl(T el)
- {
- if (length == MAX)
- {
- std::cout << "Множество переполнено!\n";
- std::cout << "Попробуйте удалить элемент из множества прежде чем добавить новый.\n";
- }
- else
- {
- set[length] = el;
- length++;
- }
- }
- template <typename T>
- int Set<T>::IsConsist(T el) //если элемент содержится, то возвращаем его индекс, если нет, то -1
- {
- int i;
- for (i = 0; i < length; i++)
- if (set[i] == el)
- return i;
- return -1;
- }
- template <typename T>
- void Set<T>::DelEl(T el)
- {
- int i, j;
- int n = this->IsConsist(el);
- if (n != -1)
- {
- Set<T> nset;
- for (i = 0; i < n; i++)
- {
- nset.set[i] = set[i];
- nset.length++;
- }
- for (i = n + 1, j = n; i < length; i++, j++)
- {
- nset.set[j] = set[i];
- nset.length++;
- }
- *this = nset;
- }
- }
- template <typename T>
- void Set<T>::Show()
- {
- for (int i = 0; i < length; i++)
- std::cout << set[i] << " ";
- std::cout << "\n";
- }
- template <typename T>
- Set<T> Set<T>::operator+(Set<T> a)
- {
- Set<T> res;
- int i, j, n;
- res = *this;
- for (i = 0, j = res.length; i < a.length && j < MAX; i++)
- {
- n = this->IsConsist(a.set[i]);
- if (n == -1)
- {
- res.set[j] = a.set[i];
- res.length++;
- j++;
- }
- }
- return res;
- }
- template <typename T>
- Set<T> Set<T>::operator*(Set<T> a)
- {
- Set<T> res;
- int i, j = 0;
- for (i = 0; i < length; i++)
- {
- if (a.IsConsist(set[i]) != -1)
- {
- res.set[j] = set[i];
- res.length++;
- j++;
- }
- }
- return res;
- }
- template <typename T>
- Set<T> Set<T>::operator-(Set<T> a)
- {
- Set<T> res;
- int i, j = 0;
- for (i = 0; i < length; i++)
- {
- if (a.IsConsist(set[i]) == -1)
- {
- res.set[j] = set[i];
- res.length++;
- j++;
- }
- }
- return res;
- }
- template <typename T>
- bool Set<T>::include(Set<T> a) //проверяет: cодержится ли множество А в вызываемом объекте
- {
- int i, j, l;
- bool k;
- if (a.length <= length)
- {
- for (i = 0; i < length; i++)
- {
- j = 1;
- if (set[i] == a.set[0])
- {
- k = true;
- l = i + 1;
- while (j < a.length)
- {
- if (a.set[j] != set[l])
- {
- k = false;
- break;
- }
- j++;
- l++;
- }
- if (k) return true;
- }
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement