Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <iomanip>
- using namespace std;
- template<class T>
- T sum(T iA, T iB)
- {
- return iA + iB;
- }
- template<class T>
- T prod(T iA, T iB)
- {
- return iA * iB;
- }
- template<class T>
- T minus(T iA, T iB)
- {
- return iA - iB;
- }
- template<class T>
- void matrixes(T *tResult[5], int& tElements)
- {
- T (*fct[])(T a, T b) = {sum, minus, prod};
- cout << "How many elements do you wish to process? > ";
- cin >> tElements;
- for (int i = 0; i < 5; i++) {
- tResult[i] = new T[tElements];
- }
- /*
- cout << "Enter two elements on each line!" << endl;
- for (int i = 0; i < tElements; i++) {
- cout << "Enter 2 elements: > ";
- cin >> tResult[0][i] >> tResult[1][i];
- cin.clear();
- }
- */
- srand(time(NULL));
- for (int i = 0; i < tElements; i++) {
- tResult[0][i] = rand() % 100 + 1;
- tResult[1][i] = rand() % 100 + 1;
- }
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < tElements; j++) {
- tResult[i+2][j] = fct[i](tResult[0][j], tResult[1][j]);
- }
- }
- }
- void matrix_calc()
- {
- double *d[5];
- int iElements = 0;
- char cOperator[] = "+-*";
- matrixes(d, iElements);
- /*
- for (int j = 0; j < 3; j++) {
- for (int i = 0; i < iElements; i++) {
- cout << setw(3) << d[0][i] << " " << cOperator[j] << " " << setw(3) << d[1][i] << " = " << setw(5) << d[j + 2][i] << endl;
- }
- cout << "-----------------" << endl;
- }
- */
- cout << "# op1 op2 + - *" << endl <<
- "---------------------------------" << endl;
- for (int i = 0; i < iElements; i++) {
- 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;
- }
- // free allocated resources
- for (int i = 0; i < 5; i++) {
- delete [] d[i];
- d[i] = NULL;
- }
- }
- int main()
- {
- //srand(time(NULL));
- //Grades(78.0);
- // date_calc();
- matrix_calc();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement