Advertisement
Guest User

Untitled

a guest
May 17th, 2017
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. template<class T>
  6. T sum(T iA, T iB)
  7.   {
  8.   return iA + iB;
  9.   }
  10.  
  11. template<class T>
  12. T prod(T iA, T iB)
  13.   {
  14.   return iA * iB;
  15.   }
  16.  
  17. template<class T>
  18. T minus(T iA, T iB)
  19.   {
  20.   return iA - iB;
  21.   }
  22.  
  23. template<class T>
  24. void matrixes(T *tResult[5], int& tElements)
  25.   {
  26.   T (*fct[])(T a, T b) = {sum, minus, prod};
  27.   cout << "How many elements do you wish to process? > ";
  28.   cin >> tElements;
  29.  
  30.   for (int i = 0; i < 5; i++) {
  31.     tResult[i] = new T[tElements];
  32.     }
  33.   /*
  34.   cout << "Enter two elements on each line!" << endl;
  35.   for (int i = 0; i < tElements; i++) {
  36.     cout << "Enter 2 elements: > ";
  37.     cin >> tResult[0][i] >> tResult[1][i];
  38.     cin.clear();
  39.     }
  40.   */
  41.   srand(time(NULL));
  42.   for (int i = 0; i < tElements; i++) {
  43.     tResult[0][i] = rand() % 100 + 1;
  44.     tResult[1][i] = rand() % 100 + 1;
  45.     }
  46.  
  47.   for (int i = 0; i < 3; i++) {
  48.     for (int j = 0; j < tElements; j++) {
  49.       tResult[i+2][j] = fct[i](tResult[0][j], tResult[1][j]);
  50.       }
  51.     }
  52.   }
  53.  
  54. void matrix_calc()
  55.   {
  56.   double *d[5];
  57.   int iElements = 0;
  58.   char cOperator[] = "+-*";
  59.  
  60.   matrixes(d, iElements);
  61.   /*
  62.   for (int j = 0; j < 3; j++) {
  63.     for (int i = 0; i < iElements; i++) {
  64.       cout << setw(3) << d[0][i] << " " << cOperator[j] << " " << setw(3) << d[1][i] << " = " << setw(5) << d[j + 2][i] << endl;
  65.       }
  66.     cout << "-----------------" << endl;
  67.     }
  68.   */
  69.   cout << "#     op1 op2      +    -      *" << endl <<
  70.           "---------------------------------" << endl;
  71.   for (int i = 0; i < iElements; i++) {
  72.     cout << setw(4) << i + 1 << ") " << setw(3) << d[0][i] << " " << setw(3) << d[1][i] << " = " << setw(5) << d[2][i] <<  setw(5) << d[3][i] << setw(7) << d[4][i] << endl;
  73.     }
  74.  
  75.   // free allocated resources
  76.   for (int i = 0; i < 5; i++) {
  77.     delete [] d[i];
  78.     d[i] = NULL;
  79.     }
  80.   }
  81. int main()
  82.   {
  83.   //srand(time(NULL));
  84.   //Grades(78.0);
  85.   // date_calc();
  86.   matrix_calc();
  87.   system("pause");
  88.   return 0;
  89.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement