Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctime>
  4. #include <map>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. class SuppBase
  11. {
  12. public:
  13.     virtual int Get() //базовый класс
  14.     {
  15.         cout << "Base" << endl;
  16.         return 0;
  17.     }
  18. };
  19.  
  20.  
  21. class SuppFile : public SuppBase    //класс-поставщик данных считанных из файла
  22. {
  23. private:
  24.     ifstream *file;
  25.     int buff;
  26.  
  27. public:
  28.     SuppFile()
  29.     {
  30.         file = new ifstream("test.txt");
  31.     }
  32.  
  33.     int Get()
  34.     {
  35.         while (file->is_open())
  36.         {
  37.             if (!(*file >> buff))
  38.                 return -1;
  39.             cout << buff << endl;
  40.             return buff;
  41.         }
  42.     }
  43.  
  44.     ~SuppFile()
  45.     {
  46.         if (file->is_open())
  47.             file->close();
  48.         delete[] file;
  49.     }
  50. };
  51.  
  52.  
  53. class SuppKbrd : public SuppBase    //класс-поставщик данных считанных с клавиатуры
  54. {
  55. private:
  56.     int buff;
  57.  
  58. public:
  59.     int Get()
  60.     {
  61.         cin >> buff;
  62.         return buff;
  63.     }
  64. };
  65.  
  66.  
  67. class SuppQueue : public SuppBase   //класс-поставщик данных из массива случайных чисел
  68. {
  69. private:
  70.     int length;
  71.     int i = 0;
  72.     int *arr;
  73.  
  74. public:
  75.     SuppQueue() //конструктор без параметра
  76.     {
  77.         length = rand() % 30 + 5;
  78.         arr = new int[length];
  79.         for (int j = 0; j < length; j++)
  80.         {
  81.             arr[j] = rand() % 11;
  82.             cout << arr[j] << endl;
  83.         }
  84.     }
  85.  
  86.     SuppQueue(int len) //конструктор с параметром
  87.     {
  88.         length = len;
  89.         arr = new int[length];
  90.         for (int j = 0; j < length; j++)
  91.         {
  92.             arr[j] = rand() % 11;
  93.             cout << arr[j] << endl;
  94.         }
  95.     }
  96.  
  97.     int Get()
  98.     {
  99.         if (i < length)
  100.             return arr[i++];
  101.         return -1;
  102.     }
  103. };
  104.  
  105.  
  106. class Freq
  107. {
  108. private:
  109.     map<int, int> num;
  110.  
  111. public:
  112.     virtual void Calc(SuppBase &b)  ////метод для вычисления частоты повторения различных чисел
  113.     {
  114.         while (true)
  115.         {
  116.             int i = b.Get();
  117.             if (i < 0)
  118.                 break;
  119.             num[i]++;
  120.         }
  121.     }
  122.  
  123.     friend ostream& operator<<(ostream& ost, Freq &s);
  124. };
  125.  
  126. ostream& operator<<(ostream& ost, Freq &s)  //перегруженный оператор << для Freq
  127. {
  128.     for (auto a : s.num)
  129.         ost << a.first << " - " << a.second << endl;
  130.     return ost;
  131. }
  132.  
  133.  
  134. class Diap : public Freq
  135. {
  136. private:
  137.     int min, max = 0, sum = 0;
  138.  
  139. public:
  140.     void Calc(SuppBase &b)  //метод для поиска max, min, sum
  141.     {
  142.         int i = b.Get();
  143.         min = i;
  144.         while (true)
  145.         {
  146.             if (i < 0)
  147.                 break;
  148.             if (i > max)
  149.                 max = i;
  150.             else if (i < min)
  151.                 min = i;
  152.             sum += i;
  153.             i = b.Get();
  154.         }
  155.     }
  156.  
  157.     friend ostream& operator<<(ostream& ost, Diap &s);
  158. };
  159.  
  160. ostream& operator<<(ostream& ost, Diap &s)  //перегруженный оператор << для Diap
  161. {
  162.     cout << "Min = " << s.min << " Max = " << s.max << " Sum = " << s.sum << endl;
  163.     return ost;
  164. }
  165.  
  166.  
  167. int main()
  168. {
  169.     srand(time(NULL));
  170.  
  171.     SuppFile file;
  172.     Freq example1;
  173.     //Diap example1;
  174.     example1.Calc(file);
  175.     cout << endl << "FROM FILE" << endl;
  176.     cout << example1 << endl << endl;
  177.  
  178.     SuppKbrd kbrd;
  179.     Freq example2;
  180.     //Diap example2;
  181.     cout << "Input numbers from keyboard" << endl;
  182.     example2.Calc(kbrd);
  183.     cout << endl << "FROM KEYBOARD" << endl;
  184.     cout << example2 << endl << endl;
  185.  
  186.     SuppQueue queue(10);
  187.     Freq example3;
  188.     //Diap example3;
  189.     example3.Calc(queue);
  190.     cout << endl << "FROM QUEUE" << endl;
  191.     cout << example3 << endl << endl;
  192.  
  193.     system("Pause");
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement