Xom9ik

Lab_8/16 var (IIl semester)

Nov 12th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. //Lab_8
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7. //16.   Даны два массива найти их скалярное произведение
  8.  
  9. template <typename T> class Element {
  10. public:
  11.     T elem;
  12.     Element() {}
  13.     Element(T e)
  14.     { //cout << "Constructor " << endl;
  15.         elem = e;
  16.     }
  17.     template <typename T> T operator *(T &e)
  18.     { //cout << "Operation *" << endl;
  19.         return elem * e.elem;
  20.     }
  21.     template <typename T> T operator +=(T &e)
  22.     { //cout << "Operation +=" << endl;
  23.         return elem += e.elem;
  24.     }
  25.     operator T()
  26.     { //cout << "operator T" << endl;
  27.         return elem;
  28.     }
  29.     Element(const Element &base)
  30.     { //cout << "Copy constructor" << endl;
  31.         elem = base.elem;
  32.     }
  33. };
  34. //явное инстанцирование шаблона функции
  35. template int ScalarDefaultType(int*, int*, int);
  36. int ScalarDefaultType(int* arrayA, int* arrayB, int size)
  37. {
  38.     int result = 0;
  39.     for (int i = 0; i < size; i++)
  40.         result += arrayA[i] * arrayB[i];
  41.     cout << "\nint";
  42.     return result;
  43. }
  44. template double ScalarDefaultType(double*, double*, int);
  45. double ScalarDefaultType(double* arrayA, double* arrayB, double size)
  46. {
  47.     double result = 0;
  48.     for (int i = 0; i < size; i++)
  49.         result += arrayA[i] * arrayB[i];
  50.     cout << "\ndouble";
  51.     return result;
  52. }
  53. template float ScalarDefaultType(float*, float*, int);
  54. float ScalarDefaultType(float* arrayA, float* arrayB, int size)
  55. {
  56.     float result = 0;
  57.     for (int i = 0; i < size; i++)
  58.         result += arrayA[i] * arrayB[i];
  59.     cout << "\nfloat";
  60.     return result;
  61. }
  62. template <typename T> T ScalarDefaultType(T* arrayA, T* arrayB, int size); //шаблон функции для стандартных типов данных (int, float, double)
  63. template <typename T> T ScalarDefaultType(T* arrayA, T* arrayB, int size)
  64. {
  65.     T result = 0;
  66.     for (int i = 0; i < size; i++)
  67.         result += arrayA[i] * arrayB[i];
  68.     return result;
  69. }
  70.  
  71.  
  72.  
  73. template <class T> T ScalarAbstractType(T arrayA[], T arrayB[], int size); //шаблон функции для абстрактного типа данных
  74. template <class T> T ScalarAbstractType(T arrayA[], T arrayB[], int size)
  75. {
  76.     T result[1];
  77.     result[0] = 0;
  78.     for (int i = 0; i < size; i++)
  79.         result[0] += arrayA[i] * arrayB[i];
  80.     return result[0];
  81. }
  82.  
  83. int main()
  84. {
  85.     int size = 0;
  86.     srand(time(NULL));
  87.     cout << "Enter size arrays: ";
  88.     cin >> size;
  89.     cout << "template <typename T>" << endl;
  90.     float *arrayA = new float[size];
  91.     cout << "Array A: " << endl;
  92.     for (int i = 0; i < size; i++)
  93.     {
  94.         arrayA[i] = (rand() % 100)/2.3;
  95.         cout << arrayA[i] << " ";
  96.     }
  97.     float *arrayB = new float[size];
  98.     cout << "\nArray B: " << endl;
  99.     for (int i = 0; i < size; i++)
  100.     {
  101.         arrayB[i] = rand() % 100;
  102.         cout << arrayB[i] << " ";
  103.     }
  104.     cout << "\nScalarDefaultType Product A & B = " << ScalarDefaultType(arrayA, arrayB, size) << endl;
  105.     cout << "\ntemplate <class T>" << endl;
  106.     Element<double> *AbstractArrayA = new Element<double>[size];
  107.     Element<double> *AbstractArrayB = new Element<double>[size];
  108.     cout << "\nArray A(AbstractType) double: " << endl;
  109.     for (int i = 0; i < size; i++)
  110.     {
  111.         AbstractArrayA[i].elem = (rand() % 100) / 3.14;
  112.         cout << AbstractArrayA[i].elem << " ";
  113.     }
  114.     cout << "\nArray B(AbstractType) double: " << endl;
  115.     for (int i = 0; i < size; i++)
  116.     {
  117.         AbstractArrayB[i].elem = (rand() % 100) / 3.14;
  118.         cout << AbstractArrayB[i].elem << " ";
  119.     }
  120.     cout << "\nScalarAbstractType Product A & B = " << ScalarAbstractType(AbstractArrayA, AbstractArrayB, size) << endl;
  121.  
  122.     Element<int> *AbstractArrayAInt = new Element<int>[size];
  123.     Element<int> *AbstractArrayBInt = new Element<int>[size];
  124.     cout << "\nArray A(AbstractType) int: " << endl;
  125.     for (int i = 0; i < size; i++)
  126.     {
  127.         AbstractArrayAInt[i].elem = (rand() % 100);
  128.         cout << AbstractArrayAInt[i].elem << " ";
  129.     }
  130.     cout << "\nArray B(AbstractType) int: " << endl;
  131.     for (int i = 0; i < size; i++)
  132.     {
  133.         AbstractArrayBInt[i].elem = (rand() % 100);
  134.         cout << AbstractArrayBInt[i].elem << " ";
  135.     }
  136.     cout << "\nScalarAbstractType Product A & B = " << ScalarAbstractType(AbstractArrayAInt, AbstractArrayBInt, size) << endl;
  137.     system("pause");
  138. }
Advertisement
Add Comment
Please, Sign In to add comment