Advertisement
Guest User

Untitled

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