Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab_8_4var
- #include "stdafx.h"
- #include <iostream>
- #include <ctime>
- using namespace std;
- //4. Задан массив из k чисел. Отсортировать элементы массива по возрастанию.
- template <typename T> class Element {
- public:
- T elem;
- Element() {}
- Element(T e)
- {
- elem = e;
- }
- template <typename T> T operator *(T &e)
- {
- return elem * e.elem;
- }
- template <typename T> T operator +=(T &e)
- {
- return elem += e.elem;
- }
- operator T()
- {
- return elem;
- }
- Element(const Element &base)
- {
- elem = base.elem;
- }
- };
- //явное инстанцирование шаблона функции
- template int* SortDefaultType(int*, int);
- int* SortDefaultType(int* arrayA, int size)
- {
- for (int i = 0; i < size; i++)
- for (int j = size - 1; j > i; j--)
- if (arrayA[j] < arrayA[j - 1])
- swap(arrayA[j], arrayA[j - 1]);
- return arrayA;
- }
- template double* SortDefaultType(double*, int);
- double* SortDefaultType(double* arrayA, double size)
- {
- for (int i = 0; i < size; i++)
- for (int j = size - 1; j > i; j--)
- if (arrayA[j] < arrayA[j - 1])
- swap(arrayA[j], arrayA[j - 1]);
- return arrayA;
- }
- template float* SortDefaultType(float*, int);
- float* SortDefaultType(float* arrayA, int size)
- {
- for (int i = 0; i < size; i++)
- for (int j = size - 1; j > i; j--)
- if (arrayA[j] < arrayA[j - 1])
- swap(arrayA[j], arrayA[j - 1]);
- return arrayA;
- }
- template <typename T> T* SortDefaultType(T* arrayA, int size); //шаблон функции для стандартных типов данных (int, float, double)
- template <typename T> T* SortDefaultType(T* arrayA, int size)
- {
- for (int i = 0; i < size; i++)
- for (int j = size - 1; j > i; j--)
- if (arrayA[j] < arrayA[j - 1])
- swap(arrayA[j], arrayA[j - 1]);
- return arrayA;
- }
- template <class T> T* SortAbstractType(T arrayA[], int size); //шаблон функции для абстрактного типа данных
- template <class T> T* SortAbstractType(T arrayA[], int size)
- {
- for (int i = 0; i < size; i++)
- for (int j = size - 1; j > i; j--)
- if (arrayA[j] < arrayA[j - 1])
- swap(arrayA[j], arrayA[j - 1]);
- return arrayA;
- }
- int main()
- {
- int size = 0;
- srand(time(NULL));
- cout << "Enter size array: ";
- cin >> size;
- cout << "template <typename T>" << endl;
- float *arrayA = new float[size];
- cout << "Array A: " << endl;
- for (int i = 0; i < size; i++)
- {
- arrayA[i] = (rand() % 100) / 2.3;
- cout << arrayA[i] << " ";
- }
- cout << "\nSortDefaultType A: " << endl;
- SortDefaultType(arrayA, size);
- for (int i = 0; i < size; i++)
- cout << arrayA[i] << " ";
- cout << "\ntemplate <class T>" << endl;
- Element<double> *AbstractArrayA = new Element<double>[size];
- cout << "\nArray A(AbstractType) double: " << endl;
- for (int i = 0; i < size; i++)
- {
- AbstractArrayA[i].elem = (rand() % 100) / 3.14;
- cout << AbstractArrayA[i].elem << " ";
- }
- cout << "\nSortAbstractType A: " << endl;
- SortAbstractType(AbstractArrayA, size);
- for (int i = 0; i < size; i++)
- cout << AbstractArrayA[i].elem << " ";
- Element<int> *AbstractArrayAInt = new Element<int>[size];
- cout << "\nArray A(AbstractType) int: " << endl;
- for (int i = 0; i < size; i++)
- {
- AbstractArrayAInt[i].elem = (rand() % 100);
- cout << AbstractArrayAInt[i].elem << " ";
- }
- cout << "\nSortAbstractType A: " << endl;
- SortAbstractType(AbstractArrayAInt, size);
- for (int i = 0; i < size; i++)
- cout << AbstractArrayAInt[i].elem << " ";
- cout << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment