Advertisement
Guest User

task2

a guest
Mar 29th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template<typename T> class Array
  4. {
  5. private:
  6.     int size;
  7.     T* array;
  8.  
  9. public:
  10.     Array(int n)
  11.     {
  12.         size = n;
  13.         array = new T[size];
  14.     }
  15.  
  16.     void set_value(int n, T value)
  17.     {
  18.         array[n] = value;
  19.     }
  20.  
  21.     T get_min()
  22.     {
  23.         T temp = array[0];
  24.         for (int i = 0; i < size; i++)
  25.         {
  26.             if (array[i] < temp)
  27.             {
  28.                 temp = array[i];
  29.             }
  30.         }
  31.         return temp;
  32.     }
  33.  
  34.     T get_max()
  35.     {
  36.         T temp = array[0];
  37.         for (int i = 0; i < size; i++)
  38.         {
  39.             if (array[i] > temp)
  40.             {
  41.                 temp = array[i];
  42.             }
  43.         }
  44.         return temp;
  45.     }
  46. };
  47.  
  48.  
  49. template<> class Array<std::string>
  50. {
  51. private:
  52.     int size;
  53.     std::string* array;
  54.  
  55. public:
  56.     Array(int n)
  57.     {
  58.         size = n;
  59.         array = new std::string[size];
  60.     }
  61.  
  62.     void set_value(int n, std::string value)
  63.     {
  64.         array[n] = value;
  65.     }
  66.  
  67.     std::string get_min()
  68.     {
  69.         std::string temp = array[0];
  70.         for (int i = 0; i < size; i++)
  71.         {
  72.             if (array[i].length() < temp.length())
  73.             {
  74.                 temp = array[i];
  75.             }
  76.         }
  77.         return temp;
  78.     }
  79.  
  80.     std::string get_max()
  81.     {
  82.         std::string temp = array[0];
  83.         for (int i = 0; i < size; i++)
  84.         {
  85.             if (array[i].length() > temp.length())
  86.             {
  87.                 temp = array[i];
  88.             }
  89.         }
  90.         return temp;
  91.     }
  92.  
  93. };
  94.  
  95.  
  96.  
  97. int main()
  98. {
  99.     Array<int> int_array(3);
  100.     int_array.set_value(0, 1);
  101.     int_array.set_value(1, 2);
  102.     int_array.set_value(2, 3);
  103.     std::cout << int_array.get_min() << "\n";
  104.     std::cout << int_array.get_max() << "\n";
  105.  
  106.  
  107.     Array<char> char_array(6);
  108.     char_array.set_value(0, 'A');
  109.     char_array.set_value(1, 'E');
  110.     char_array.set_value(2, 'Z');
  111.     char_array.set_value(3, 'K');
  112.     char_array.set_value(4, 'M');
  113.     char_array.set_value(5, 'I');
  114.     std::cout << char_array.get_min() << "\n";
  115.     std::cout << char_array.get_max() << "\n";
  116.  
  117.     Array<std::string> string_array(2);
  118.     string_array.set_value(0, "aaaaa");
  119.     string_array.set_value(1, "zzz");
  120.     std::cout << string_array.get_min() << "\n";
  121.     std::cout << string_array.get_max() << "\n";
  122.  
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement