Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <ctime>
- #include <map>
- #include <fstream>
- using namespace std;
- class SuppBase
- {
- public:
- virtual int Get() //базовый класс
- {
- cout << "Base" << endl;
- return 0;
- }
- };
- class SuppFile : public SuppBase //класс-поставщик данных считанных из файла
- {
- private:
- ifstream *file;
- int buff;
- public:
- SuppFile()
- {
- file = new ifstream("test.txt");
- }
- int Get()
- {
- while (file->is_open())
- {
- if (!(*file >> buff))
- return -1;
- cout << buff << endl;
- return buff;
- }
- }
- ~SuppFile()
- {
- if (file->is_open())
- file->close();
- delete[] file;
- }
- };
- class SuppKbrd : public SuppBase //класс-поставщик данных считанных с клавиатуры
- {
- private:
- int buff;
- public:
- int Get()
- {
- cin >> buff;
- return buff;
- }
- };
- class SuppQueue : public SuppBase //класс-поставщик данных из массива случайных чисел
- {
- private:
- int length;
- int i = 0;
- int *arr;
- public:
- SuppQueue() //конструктор без параметра
- {
- length = rand() % 30 + 5;
- arr = new int[length];
- for (int j = 0; j < length; j++)
- {
- arr[j] = rand() % 11;
- cout << arr[j] << endl;
- }
- }
- SuppQueue(int len) //конструктор с параметром
- {
- length = len;
- arr = new int[length];
- for (int j = 0; j < length; j++)
- {
- arr[j] = rand() % 11;
- cout << arr[j] << endl;
- }
- }
- int Get()
- {
- if (i < length)
- return arr[i++];
- return -1;
- }
- };
- class Freq
- {
- private:
- map<int, int> num;
- public:
- virtual void Calc(SuppBase &b) ////метод для вычисления частоты повторения различных чисел
- {
- while (true)
- {
- int i = b.Get();
- if (i < 0)
- break;
- num[i]++;
- }
- }
- friend ostream& operator<<(ostream& ost, Freq &s);
- };
- ostream& operator<<(ostream& ost, Freq &s) //перегруженный оператор << для Freq
- {
- for (auto a : s.num)
- ost << a.first << " - " << a.second << endl;
- return ost;
- }
- class Diap : public Freq
- {
- private:
- int min, max = 0, sum = 0;
- public:
- void Calc(SuppBase &b) //метод для поиска max, min, sum
- {
- int i = b.Get();
- min = i;
- while (true)
- {
- if (i < 0)
- break;
- if (i > max)
- max = i;
- else if (i < min)
- min = i;
- sum += i;
- i = b.Get();
- }
- }
- friend ostream& operator<<(ostream& ost, Diap &s);
- };
- ostream& operator<<(ostream& ost, Diap &s) //перегруженный оператор << для Diap
- {
- cout << "Min = " << s.min << " Max = " << s.max << " Sum = " << s.sum << endl;
- return ost;
- }
- int main()
- {
- srand(time(NULL));
- SuppFile file;
- Freq example1;
- //Diap example1;
- example1.Calc(file);
- cout << endl << "FROM FILE" << endl;
- cout << example1 << endl << endl;
- SuppKbrd kbrd;
- Freq example2;
- //Diap example2;
- cout << "Input numbers from keyboard" << endl;
- example2.Calc(kbrd);
- cout << endl << "FROM KEYBOARD" << endl;
- cout << example2 << endl << endl;
- SuppQueue queue(10);
- Freq example3;
- //Diap example3;
- example3.Calc(queue);
- cout << endl << "FROM QUEUE" << endl;
- cout << example3 << endl << endl;
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement