Advertisement
Xom9ik

Lab_9_Task2/9 var #2 (IIl semester)

Dec 13th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.95 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <iostream>
  4. #include <locale>
  5. #include <Windows.h>
  6. #include <fstream>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10.  
  11. //Класс-растение
  12. class TPlant
  13. {
  14. protected:
  15.     string Fname_plant;
  16.     int Fgectar;// гектары, общее количество
  17. public:
  18.     TPlant(); // Конструктор по умолчанию
  19.     TPlant(string Name, int gectar);
  20.     virtual ~TPlant(); // виртуальный деструктор
  21.     virtual void Print() = 0; // виртуальный метод вывода сведений о растении      
  22.     virtual void Input() = 0;// виртуальный метод ввода сведений о растении
  23.     string GetName() { return Fname_plant; }
  24.     int GetGectar() { return Fgectar; }
  25.     void SetName(string name) { Fname_plant = name; }
  26.     void SetGectar(int gectar) { Fgectar = gectar; }
  27. };
  28. //Класс-дерево
  29. class Tree :public TPlant
  30. {
  31. private:
  32.     int FPlantedTree;// сколько посажено деревьев всего
  33. public:
  34.     Tree(int PlantedTree, string Name, int gectar); // Конструктор с параметрами
  35.     Tree(); // Конструктор по умолчанию
  36.     Tree(Tree &A); // Конструктор копирования
  37.     ~Tree();// Деструктор по умолчанию
  38.     Tree& operator =(const Tree &A); // Перегруженная операция =
  39.     int GetPlantedTree() { return FPlantedTree; }
  40.     void SetPlantedTree(int PlantedTree) { FPlantedTree = PlantedTree; }
  41.     void Input(); // перегруженный виртуальный метод
  42.     void Print(); // перегруженный виртуальный метод
  43. };
  44. //Класс-зерновые
  45. class Cereals :public TPlant
  46. {
  47. private:
  48.     int FPlantedPerGectar;// гектары, общее количество
  49. public:
  50.     Cereals();// Конструктор по умолчанию
  51.     Cereals(int PlantedPerGectar, string Name, int gectar);// Конструктор инициализатор
  52.     Cereals(Cereals &A);// Конструктор копирования
  53.     ~Cereals();// Деструктор
  54.     Cereals& operator =(const Cereals &A);// Перегруженная операция =
  55.     int GetGectars() { return FPlantedPerGectar; }
  56.     void SetGectars(int PlantedPerGectar) { FPlantedPerGectar = PlantedPerGectar; }
  57.     void Input();// перегруженный виртуальный метод
  58.     void Print();// перегруженный виртуальный метод
  59. };
  60. TPlant::TPlant()
  61. {// Конструктор по умолчанию базового класса Растение
  62.     Fname_plant = "";
  63.     Fgectar = 0;
  64.     cout << "**** Конструктор по умолчанию базового класса Растение" << endl;
  65. }
  66. TPlant::TPlant(string Name, int gectar)
  67. {// Конструктор инициализатор базового класса Растение
  68.     Fname_plant = Name;
  69.     Fgectar = gectar;
  70.     cout << "**** Конструктор инициализатор базового класса Растение" << endl;
  71. }
  72. TPlant::~TPlant()
  73. {//Деструктор базового класса Растение
  74.     cout << "**** Деструктор базового класса Растение" << endl;
  75. }
  76. Tree::Tree(int PlantedTree, string Name, int gectar) :TPlant(Name, gectar)
  77. {// Конструктор инициализатор класса Дерево
  78.     FPlantedTree = PlantedTree;
  79.     cout << "**** Конструктор инициализатор класса Дерево" << endl;
  80. }
  81. Tree::Tree()
  82. {//Конструктор по умолчанию класса Дерево
  83.     FPlantedTree = 0;
  84.     Fname_plant = "";
  85.     Fgectar = 0;
  86.     cout << "**** Конструктор по умолчанию класса Дерево" << endl;
  87. }
  88. Cereals::Cereals(int PlantedPerGectar, string Name, int gectar) :TPlant(Name, gectar)
  89. {//Конструктор инициализатор класса Зерновые
  90.     FPlantedPerGectar = PlantedPerGectar;
  91.     cout << "**** Конструктор инициализатор класса Зерновые" << endl;
  92. }
  93. Cereals::Cereals()
  94. {//Конструктор по умолчанию класса Зерновые
  95.     FPlantedPerGectar = 0;
  96.     Fname_plant = "";
  97.     Fgectar = 0;
  98.     cout << "**** Конструктор по умолчанию класса Зерновые" << endl;
  99. }
  100. Tree::Tree(Tree &A)
  101. {//Конструктор копирования класса Дерево
  102.     FPlantedTree = A.FPlantedTree;
  103.     Fname_plant = A.Fname_plant;
  104.     Fgectar = A.Fgectar;
  105.     cout << "**** Конструктор копирования класса Дерево" << endl;
  106. }
  107. Cereals& Cereals::operator =(const Cereals &A)
  108. {//Перегруженная операция = класса Зерновые
  109.     FPlantedPerGectar = A.FPlantedPerGectar;
  110.     Fname_plant = A.Fname_plant;
  111.     Fgectar = A.Fgectar;
  112.     cout << "**** Перегруженная операция = класса Зерновые" << endl;
  113.     return *this;
  114. }
  115. Tree& Tree::operator =(const Tree &A)
  116. {//Перегруженная операция = класса Дерево
  117.     FPlantedTree = A.FPlantedTree;
  118.     Fname_plant = A.Fname_plant;
  119.     Fgectar = A.Fgectar;
  120.     cout << "**** Перегруженная операция = класса Дерево" << endl;
  121.     return *this;
  122. }
  123. Cereals::Cereals(Cereals &A)
  124. {//Конструктор копирования класса Зерновые
  125.     FPlantedPerGectar = A.FPlantedPerGectar;
  126.     Fname_plant = A.Fname_plant;
  127.     Fgectar = A.Fgectar;
  128.     cout << "****Конструктор копирования класса Зерновые" << endl;
  129. }
  130. void Tree::Input()
  131. {//Ввод сведений о Дереве
  132.     cout << "Ввод сведений о Дереве:" << endl;
  133.     cout << "Введите наименование дерева: ";
  134.     cin >> Fname_plant;
  135.     cout << "Введите количество деревьев: ";
  136.     cin >> FPlantedTree;
  137.     cout << "Введите общее количество гектар: ";
  138.     cin >> Fgectar;
  139.     cout << "**** Перегруженный виртуальный метод Input класса Дерево" << endl;
  140. }
  141. void Cereals::Input()
  142. {//Ввод сведений о Зерновой культуре
  143.     cout << "Ввод сведений о зерновых:" << endl;
  144.     cout << "Введите наименование зерновой культуры: ";
  145.     cin >> Fname_plant;
  146.     cout << "Введите количество зерновой культуры на 1 гектаре: ";
  147.     cin >> FPlantedPerGectar;
  148.     cout << "Введите общее количество гектар: ";
  149.     cin >> Fgectar;
  150.     cout << "**** Перегруженный виртуальный метод Input класса Зерновые" << endl;
  151. }
  152. void Tree::Print()
  153. {// Перегруженный виртуальный метод класса Дерево
  154.     cout << "\t" << Fname_plant << "\t" << Fgectar << "\t" << FPlantedTree << endl;
  155.     cout << "**** Перегруженный виртуальный метод Print класса Дерево" << endl;
  156. }
  157. void Cereals::Print()
  158. {// Перегруженный виртуальный метод Print класса Зерновые
  159.     cout << "\t" << Fname_plant << "\t" << Fgectar << "\t" << FPlantedPerGectar << endl;
  160.     cout << "**** Перегруженный виртуальный метод Print класса Зерновые" << endl;
  161. }
  162. Tree::~Tree()
  163. {//Перегруженный виртуальный Деструктор класса Дерево
  164.     cout << "**** Перегруженный виртуальный Деструктор класса Дерево" << endl;
  165. }
  166. Cereals::~Cereals()
  167. {//Перегруженный виртуальный Деструктор класса Зерновые
  168.     cout << "**** Перегруженный виртуальный Деструктор класса Зерновые" << endl;
  169. }
  170.  
  171. //Класс-сектор
  172. class TSector
  173. {
  174. private:
  175.     int FCountTrees; // Количество Деревьев  
  176.     int FCounCereals; //Количество Зерновой культуры
  177.     Tree *FTree; // Массив Деревьев
  178.     Cereals *FCereals;// Массив Зерновой культуры
  179. public:
  180.     TSector();// Конструктор по умолчанию
  181.     TSector(Tree* p, Cereals* g, int max)
  182.     {
  183.         FTree = p;
  184.         FCereals = g;
  185.         FCountTrees = max;
  186.         FCounCereals = max;
  187.     }
  188.     ~TSector();// Деструктор
  189.     void Print();
  190.     void AddTree(Tree &A);// Добавление Дерева
  191.     void AddCereals(Cereals &A);//Добавление Зерновой культуры
  192.     void DelTree(int c);//Удаление Дерева
  193.     void DelCereals(int c);//Удаление Зерновой культуры
  194.     Tree* GetTree(int c) { return &FTree[c]; }
  195.     Cereals* GetCereals(int c) { return &FCereals[c]; }
  196.     int GetCountTrees() { return FCountTrees; }
  197.     int GetCountCereals() { return FCounCereals; }
  198. };
  199. TSector::TSector()
  200. {//Конструктор по умолчанию класса Сектор
  201.     FCountTrees = 0;
  202.     FCounCereals = 0;
  203.     FTree = NULL;
  204.     FCereals = NULL;
  205.     cout << "**** Конструктор по умолчанию класса Сектор" << endl;
  206. }
  207. void TSector::AddTree(Tree &A)
  208. {//Метод добавления Дерева класса Сектор
  209.     int i;
  210.     Tree t;
  211.     Tree *tmp = new Tree[FCountTrees + 1];
  212.     for (i = 0; i<FCountTrees; i++)
  213.         tmp[i] = FTree[i];
  214.     tmp[FCountTrees] = A;
  215.     delete[] FTree;
  216.     FTree = tmp;
  217.     FCountTrees++;
  218.     cout << "**** Метод добавления Дерева класса Сектор" << endl;
  219. }
  220. void TSector::AddCereals(Cereals &A)
  221. {//Метод добавления Зерновой культуры класса Сектор
  222.     int i;
  223.     Cereals *tmp = new Cereals[FCounCereals + 1];
  224.     for (i = 0; i<FCounCereals; i++)
  225.         tmp[i] = FCereals[i];
  226.     tmp[FCounCereals] = A;
  227.     delete[] FCereals;
  228.     FCereals = tmp;
  229.     FCounCereals++;
  230.     cout << "**** Метод добавления Зерновой культуры класса Сектор" << endl;
  231. }
  232. void TSector::DelTree(int c)
  233. {//Метод удаления Дерева класса Сектор
  234.     int i;
  235.     Tree *tmp = new Tree[FCountTrees - 1];
  236.     for (i = 0; i<c; i++)
  237.         tmp[i] = FTree[i];
  238.     for (i = c + 1; i<FCountTrees; i++)
  239.         tmp[i - 1] = FTree[i];
  240.     delete[] FTree;
  241.     FTree = tmp;
  242.     FCountTrees--;
  243.     cout << "**** Метод удаления Дерева класса Сектор" << endl;
  244. }
  245. void TSector::DelCereals(int c)
  246. {//Метод удаления Зерновой культуры класса Сектор
  247.     int i;
  248.     Cereals *tmp = new Cereals[FCounCereals - 1];
  249.     for (i = 0; i<c; i++)
  250.         tmp[i] = FCereals[i];
  251.     for (i = c + 1; i<FCounCereals; i++)
  252.         tmp[i - 1] = FCereals[i];
  253.     delete[] FCereals;
  254.     FCereals = tmp;
  255.     FCounCereals--;
  256.     cout << "**** Метод удаления Зерновой культуры класса Сектор" << endl;
  257. }
  258. TSector::~TSector()
  259. {// Деструктор класса Сектор
  260.     if (FTree != NULL)
  261.         delete[] FTree;
  262.     if (FCereals != NULL)
  263.         delete[] FCereals;
  264.     cout << "**** Деструктор класса Сектор" << endl;
  265. }
  266.  
  267. class TabularOutput
  268. {//табличный вывод информации в файл и на экран
  269. public:
  270.     virtual void Print(TSector *S) = 0;
  271. };
  272. class StringOutput
  273. {//вывод информации в ленточной форме в файл и на экран
  274. public:
  275.     virtual void Print(TSector *S) = 0;
  276. };
  277. class Tabular_Output : public TabularOutput {
  278. public:
  279.     virtual void TO_Print(TSector *S) = 0;
  280.     void Print(TSector *S)
  281.     {
  282.         TO_Print(S);
  283.     }
  284. };
  285. class String_Output : public StringOutput {
  286. public:
  287.     virtual void SO_Print(TSector *S) = 0;
  288.     void Print(TSector *S)
  289.     {
  290.         SO_Print(S);
  291.     }
  292. };
  293.  
  294. //Класс-обработчик
  295. class Handler : public Tabular_Output, public String_Output
  296. {
  297. public:
  298.     Handler();
  299.     Handler(TSector *T);
  300.     TSector *Tpointer;
  301.     void TO_Print(TSector *S)
  302.     {
  303.         ofstream fout;
  304.         fout.open("TO.txt");
  305.         cout << "Tabular Output" << endl;
  306.         fout << "Tabular Output" << endl;
  307.         cout << left << setw(15) << "Name" << setw(10) << "Gektar" << setw(10) << "L per h" << setw(10) << "Productivity" << endl;
  308.         fout << left << setw(15) << "Name" << setw(10) << "Gektar" << setw(10) << "L per h" << setw(10) << "Productivity" << endl;
  309.         for (int i = 0; i < S[0].GetCountTrees(); i++)
  310.         {          
  311.             //примем, что на 1 дереве 111 фруктов
  312.             cout << left << setw(15) << S[0].GetTree(i)->GetName()
  313.                 << setw(10) << S[0].GetTree(i)->GetGectar()
  314.                 << setw(10) << S[0].GetTree(i)->GetPlantedTree()
  315.                 << setw(10) << S[0].GetTree(i)->GetPlantedTree() * S[0].GetTree(i)->GetGectar() * 111 << endl;
  316.             //GetPlantedTree = общему количеству деревьев определенного вида
  317.             fout << left << setw(15) << S[0].GetTree(i)->GetName()
  318.                 << setw(10) << S[0].GetTree(i)->GetGectar()
  319.                 << setw(10) << S[0].GetTree(i)->GetPlantedTree()
  320.                 << setw(10) << S[0].GetTree(i)->GetPlantedTree() * 111 << endl;
  321.         }
  322.         fout.close();
  323.     }
  324.     void SO_Print(TSector *S)
  325.     {
  326.         cout << "String Output" << endl;
  327.         ofstream fout;
  328.         fout.open("String.txt");
  329.         cout << "Name: ";
  330.         fout << "Name: ";
  331.         for (int i = 0; i < S[0].GetCountCereals(); i++)
  332.         {
  333.             cout << S[0].GetCereals(i)->GetName() << "; ";
  334.             fout << S[0].GetCereals(i)->GetName() << "; ";
  335.         }
  336.         cout << endl;
  337.         fout << endl;
  338.         cout << "L per h: ";
  339.         fout << "L per h: ";
  340.         for (int i = 0; i < S[0].GetCountCereals(); i++)
  341.         {
  342.             cout << S[0].GetCereals(i)->GetGectar() << "; ";
  343.             fout << S[0].GetCereals(i)->GetGectar() << "; ";
  344.         }
  345.         cout << endl;
  346.         fout << endl;
  347.         cout << "Gektars: ";
  348.         fout << "Gektars: ";
  349.         for (int i = 0; i < S[0].GetCountCereals(); i++)
  350.         {
  351.             cout << S[0].GetCereals(i)->GetGectars() << "; ";
  352.             fout << S[0].GetCereals(i)->GetGectars() << "; ";
  353.         }
  354.         cout << endl;
  355.         fout << endl;
  356.         cout << "Productivity: ";
  357.         fout << "Productivity: ";
  358.         for (int i = 0; i < S[0].GetCountCereals(); i++)
  359.         {
  360.             cout << S[0].GetCereals(i)->GetGectars() * S[0].GetCereals(i)->GetGectar() << "; ";
  361.             fout << S[0].GetCereals(i)->GetGectars() * S[0].GetCereals(i)->GetGectar() << "; ";
  362.         }
  363.         cout << endl;
  364.         fout << endl;
  365.         fout.close();
  366.     }
  367.     ~Handler();
  368. };
  369. Handler::Handler()
  370. {
  371.     cout << "Constructor class Handler" << endl;
  372. };
  373. Handler::Handler(TSector *T)
  374. {
  375.     Tpointer = T;
  376. }
  377. Handler::~Handler()
  378. {
  379.     cout << "Destructor class Handler" << endl;
  380. };
  381.  
  382. int main()
  383. {
  384.     setlocale(LC_ALL, "rus");
  385.     SetConsoleCP(1251);
  386.     SetConsoleOutputCP(1251);
  387.     int max = 0;
  388.     cout << "Введите количество деревьев и зерновых: ";
  389.     cin >> max;
  390.     Tree *A = new Tree[max];
  391.     for (int i = 0; i<max; i++)
  392.         A[i].Input();
  393.     Cereals *B = new Cereals[max];
  394.     for (int i = 0; i<max; i++)
  395.         B[i].Input();
  396.  
  397.     Tree *tree = new Tree[max];
  398.     Cereals *cereals = new Cereals[max];
  399.     TSector sector(A, B, max);
  400.  
  401.     Handler processor(&sector);
  402.     StringOutput* SO = &processor;
  403.     TabularOutput* TO = &processor;
  404.  
  405.     SO->Print(&sector);
  406.     TO->Print(&sector);
  407.  
  408.     system("pause");
  409.     return 0;
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement