Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include"stdafx.h"
- #include <iostream>
- #include <ctime>
- using namespace std;
- template <typename T>
- class Multiplicity
- {
- public:
- Multiplicity(int s, T k = 0)
- {
- size = s;
- data = new T[size];
- for (int i = 0; i<size; i++)
- data[i] = k;
- }
- //конструктор копирования
- Multiplicity(const Multiplicity<T>&a)
- {
- size = a.size;
- data = new int[size];
- for (int i = 0; i<size; i++)
- data[i] = a.data[i];
- }
- ~Multiplicity()
- {
- delete[]data;
- data = 0;
- }
- //метод получения количества элементов множества
- int len()
- {
- return size;
- }
- //оператор присваивания
- Multiplicity<T> &operator=(const Multiplicity<T> &a)
- {
- if (this == &a)
- return *this;
- size = a.size;
- if (data != 0)
- delete[]data;
- data = new int[size];
- for (int i = 0; i<size; i++)
- data[i] = a.data[i];
- return *this;
- }
- //операция доступа по индексу
- T &operator[](int index)
- {
- if (index < size)
- return data[index];
- else
- cout << "Error" << endl;
- }
- //операция для равности множества
- void operator==(const Multiplicity<T> &k)
- {
- for (int i = 0; i < size; i++)
- {
- cout << (data[i] == k.data[i] ? "Yes" : "No") << endl;
- }
- }
- //операция >
- void operator > (int value)
- {
- int index;
- bool flag = false;
- for (int i = 0; i < size; i++)
- {
- if (data[i] == value)
- {
- flag = true;
- index = i;
- break;
- }
- }
- if (flag == true)
- cout << "Множество содержит " << value << endl;
- else
- cout << "Множество не содержит " << value << endl;
- }
- //перегруженные операции ввода-вывода
- template <typename J>
- friend ostream& operator<<(ostream& out, Multiplicity<J> &a)
- {
- for (int i = 0; i<a.len(); ++i)
- out << a.data[i] << " ";
- return out;
- }
- template <typename J>
- friend istream& operator >> (istream& in, Multiplicity<J> &a)
- {
- for (int i = 0; i<a.len(); ++i)
- in >> a.data[i];
- return in;
- }
- private:
- int size;
- T *data;
- };
- template <class T1, class T2>
- class Pair
- {
- public:
- Pair() {};
- Pair(T1 f, T2 s)
- {
- first = f;
- second = s;
- }
- T1 first;
- T2 second;
- ~Pair() { first = 0; second = 0; };
- //перегруженные операции ввода-вывода
- template <class J1, class J2>
- friend ostream& operator << (ostream& out, Pair<J1, J2> &a)
- {
- out << a.first << ":" << a.second;
- return out;
- }
- template <class J1, class J2>
- friend istream& operator >> (istream& in, Pair<J1, J2> &a)
- {
- in >> a.first;
- in >> a.second;
- return in;
- }
- };
- int main()
- {
- setlocale(LC_ALL, "rus");
- srand(time(NULL));
- int size;
- cout << "\nведите размерность множеств A и B: ";
- cin >> size;
- Multiplicity<int> A(size);
- cout << "Множество A \n" << A << "\n";
- cout << "Введите " << size << " чисел множества A\n";
- cin >> A;
- cout << "Введенное множество A: " << A << endl;
- int index, value;
- cout << "Введите индекс элемента для замены(0-" << size << "): ";
- cin >> index;
- cout << "Введите новое значение: ";
- cin >> value;
- A[index] = value;
- cout << "Мћножество A после замены: " << A << endl;
- Multiplicity<int> B(size);
- cout << "Введите " << size << " чисел множества B\n";
- cin >> B;
- cout << "Введенное множество B: " << B << endl;
- cout << "Множество A после вычитания из него B: " << A << endl;
- cout << A << "\n=\n" << B << "\n-----------------" << endl;
- cout << "Проверка на равенство" << endl;
- A == B;
- cout << "Число - принадлежность числа множеству" << endl;
- cout << "Введите число для проверки: ";
- cin >> value;
- A > value;
- cout << "\n->Ввод данных для Pair(int, double): ";
- Pair<int, double> MyPair;
- cin >> MyPair;
- cout << MyPair;
- cout << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement