Advertisement
myname0

шаблоны V 2

Mar 16th, 2015
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. template < typename create, typename create1 >
  7. create* CreateFun (int &n, int &m, ifstream &inp)
  8. {
  9.         inp >> n >> m;
  10.         create *mus = new create [n];
  11.         for (int i = 0; i < n; i++)
  12.         {
  13.                 mus[i] = new create1[m];
  14.                 for ( int j=0; j < m; j++)
  15.                         inp >> mus[i][j];
  16.         }
  17.         return mus;
  18. }
  19.  
  20. template < typename print >
  21. void PrintFun (print *mus, int &n, int &m)
  22. {
  23.         for (int i = 0; i < n; i++)
  24.         {
  25.          cout << endl;
  26.          for ( int j = 0; j < m; j++)
  27.                  cout << mus[i][j] << " ";
  28.         }
  29. }
  30.  
  31. template < typename rep, typename rep1 >
  32. void replace ( rep a, rep1 *mus, int n, int m)
  33. {
  34.         for (int i=0; i < n; i++)
  35.                 for (int j = 0; j < m; j++)
  36.                 {
  37.                         if (mus[i][j] < a)
  38.                                 mus[i][j] = a;
  39.                 }
  40. }
  41.  
  42. int main()
  43.  
  44. {
  45.         ifstream inp ("massive.txt");
  46.         ifstream in ("input.txt");
  47.         int n, m;
  48.         int **mus1 = CreateFun<int*, int> (n, m, inp);
  49.         int a;
  50.         cout << "enter number: ";
  51.         cin >> a;
  52.         replace <int,int*>(a, mus1, n, m);
  53.         PrintFun <int*>(mus1, n, m);
  54.         double b;
  55.         cout << "enter number: ";
  56.         cin >> b;
  57.         double **mus2 = CreateFun<double*, double> (n, m, in);
  58.         replace <double,double*>(b, mus2, n, m);
  59.         PrintFun<double*>(mus2, n, m);
  60.         inp.close();
  61.         return 0;
  62. }
  63. 4 5
  64. 54 23 12 34 -9
  65. 32 12 3 -2 0
  66. 5 13 -56 0 18
  67. 67 87 2 1 90
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement