Advertisement
Xom9ik

Lab_9_Task2/9 var (IIl semester)

Dec 11th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.86 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <iostream>
  4. #include <windows.h>
  5. #include <fstream>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9. //************************класс часть***************************
  10. class Plant
  11. {
  12.     string name_plant;
  13.     int number;//код cтроки
  14.     int gectar;// сколько посажено на 1 гектар
  15.     int gectars;// гектары, общее количество
  16. public:
  17.     Plant();// Конструктор по умолчанию
  18.     Plant(int i);// Конструкторы инициализаторы
  19.     Plant(string Name, int num, int Gect, int gect);// Конструкторы инициализаторы
  20.     ~Plant();// Деструктор
  21.     void Input();//ввод данных
  22.     void Print();
  23.     // Методы для работы с атрибутами класса
  24.     int GetNumber() { return number; };//получение номера
  25.     int GetGectar() { return gectar; };
  26.     int GetGectars() { return gectars; };
  27.  
  28.     string GetName() { return name_plant; };
  29.     void SetPlant()
  30.     {
  31.         cout << "Input the name of Plant\n";
  32.         getline(cin, name_plant);
  33.         cout << "Input Gectar(number of landings per hectare)\n";
  34.         cin >> gectar;
  35.         cout << "Input Gectars\n";
  36.         cin >> gectars;
  37.         cin.get();//пропускает "\n" из потока ввода
  38.     }
  39. };
  40. Plant::Plant()
  41. {// Конструктор по умолчанию
  42.     name_plant = "undefined";
  43.     number = 1;
  44.     gectars = 0;
  45.     gectar = 0;
  46.     cout << "Default constructor for class Plant\n";
  47. }
  48. Plant::Plant(int i)
  49. {// Конструктор инициализатор
  50.     if (i == 1 || i == 2)//если указана категория
  51.     {
  52.         SetPlant();
  53.         number = i;
  54.     }
  55.     else
  56.         Input();
  57.     cout << "Constructor initializer class of Plant\n";
  58. }
  59. Plant::Plant(string Name, int num, int Gect, int gect)
  60. {// Конструктор инициализатор (перегруженный)
  61.     name_plant = Name;
  62.     number = num;
  63.     gectar = Gect;
  64.     gectars = gect;
  65.     cout << "Constructor initializer (overloaded) class of Plant\n";
  66. }
  67. void Plant::Input()
  68. {// Метод ввода данных
  69.     cin.sync();//очистка буфера
  70.     cout << "Input name of the Plant\n";
  71.     getline(cin, name_plant);
  72.     cout << "Input Gectar(number of landings per hectare)\n";
  73.     cin >> gectar;
  74.     cout << "Input Gectars\n";
  75.     cin >> gectars;
  76.     cout << "Input Number" << endl;
  77.     do {
  78.         cin >> number;
  79.     } while (number != 1 && number != 2);
  80. }
  81. void Plant::Print()
  82. {// Метод вывода
  83.     cout << "Name of Plant: \"" << name_plant << "\". His productivity is: " << gectar * gectars << ".\n";
  84. }
  85. Plant::~Plant()
  86. {// Деструктор
  87.     cout << "Destructor class Plant\n";
  88. }
  89.  
  90. class Sector
  91. {
  92.     Plant **FPlantArray;
  93.     int FPlantCount;
  94.     int FCurrentIndex;
  95. public:
  96.     Sector(); // Конструктор по умолчанию
  97.     Sector(int n, int f); // Конструктор инициализатор
  98.     void PrintAll();
  99.     void Print_i() { FPlantArray[FCurrentIndex]->Print(); }
  100.     virtual ~Sector(); // Деструктор класса
  101.     void AddPlant(Plant* PlantBuf);
  102.     // Методы для работы с атрибутами класса
  103.     void SetN(int n) { FPlantCount = n; };//кол-во растений
  104.     int GetN() { return FPlantCount; }
  105.     // Методы для работы с «частью»
  106.     Plant* GetMas(int i) { return FPlantArray[i]; }
  107.     void SetMas(int i, Plant *f) { FPlantArray[i] = f; }
  108.  
  109. };
  110. Sector::Sector()
  111. {// Конструктор по умолчанию
  112.     FPlantCount = 0;
  113.     FCurrentIndex = 0;
  114.     FPlantArray = NULL;
  115.     cout << "Default constructor Computation\n";
  116. }
  117. Sector::Sector(int n, int f)
  118. {// Конструктор инициализатор
  119.     int i;
  120.     FPlantCount = n;
  121.     FPlantArray = new Plant*[n];// присвоили массиву новую переменную с адресом на массив
  122.     for (i = 0; i < FPlantCount; i++)
  123.         FPlantArray[i] = new Plant(f);//пока не заполнится массив, можно добавлять новые растения
  124.     cout << "Constructor initializer class Sector\n";
  125. }
  126. void Sector::AddPlant(Plant* PlantBuf)
  127. {// Метод добавления растения
  128.     int j, k = 0;
  129.     Plant **PlantArray;//указатель на указатель
  130.     PlantArray = new Plant*[this->GetN() + 1];//инициализация массива
  131.     for (j = 0; j <this->GetN(); j++)
  132.         PlantArray[j] = this->GetMas(j);
  133.     PlantArray[j] = PlantBuf;
  134.     delete[] FPlantArray;
  135.     FPlantArray = new Plant*[this->GetN() + 1];
  136.     this->SetN(this->GetN() + 1);//установка параметров(устанавливает указатель)
  137.     for (j = 0; j <this->GetN(); j++)
  138.         FPlantArray[j] = PlantArray[j];
  139. }
  140. void Sector::PrintAll()
  141. {// Метод вывода перечня растений
  142.     for (int i = 0; i < FPlantCount; i++)
  143.         FPlantArray[i]->Print();
  144. }
  145. Sector::~Sector()
  146. {// Деструктор
  147.     for (int i = 0; i < FPlantCount; i++)
  148.         delete FPlantArray[i];
  149.     delete[] FPlantArray;
  150.     cout << "Destructor class Sector\n";
  151. }
  152.  
  153. class TableOutput
  154. {//табличный вывод информации в файл и на экран
  155. public:
  156.     virtual void Print(Sector *S) {};
  157. };
  158. class StringOutput
  159. {//вывод информации в ленточной форме в файл и на экран
  160. public:
  161.     virtual void Print(Sector *S) {};
  162. };
  163. class TableOutput_helper : public TableOutput {
  164. public:
  165.     virtual void TableOutput_Print(Sector *S) = 0;
  166.     void Print(Sector *S)
  167.     {
  168.         TableOutput_Print(S);
  169.     }
  170. };
  171. class StringOutput_helper : public StringOutput {
  172. public:
  173.     virtual void StringOutput_Print(Sector *S) = 0;
  174.     void Print(Sector *S)
  175.     {
  176.         StringOutput_Print(S);
  177.     }
  178. };
  179.  
  180. class Handler : public TableOutput_helper, public StringOutput_helper
  181. {
  182. private:
  183.     int N;
  184.     Sector *Desk;
  185. public:
  186.     Handler();
  187.     Handler(Sector *S);
  188.     void SetInTable(Sector *A, Sector *B, Plant *LineInTable);//установка растения в 1 или 2 категорию
  189.     void TableOutput_Print(Sector *S)
  190.     {
  191.         cout << "Table Output" << endl;
  192.         ofstream fout;
  193.         fout.open("Table.txt");
  194.         cout << left << setw(15) << "Name" << setw(10) << "L per h" << setw(10) << "Gektars" << setw(10) << "Productivity" << endl;
  195.         fout << left << setw(15) << "Name" << setw(10) << "L per h" << setw(10) << "Gektars" << setw(10) << "Productivity" << endl;
  196.         for (int i = 0; i < S[0].GetN(); i++)
  197.         {
  198.             cout << left << setw(15) << S[0].GetMas(i)->GetName() << setw(10) << S[0].GetMas(i)->GetGectar() << setw(10)
  199.                 << S[0].GetMas(i)->GetGectars() << setw(10) << S[0].GetMas(i)->GetGectars() * S[0].GetMas(i)->GetGectar() << endl;
  200.             fout << left << setw(15) << S[0].GetMas(i)->GetName() << setw(10) << S[0].GetMas(i)->GetGectar() << setw(10)
  201.                 << S[0].GetMas(i)->GetGectars() << setw(10) << S[0].GetMas(i)->GetGectars() * S[0].GetMas(i)->GetGectar() << endl;
  202.         }
  203.         fout.close();
  204.     }
  205.     void StringOutput_Print(Sector *S)
  206.     {
  207.         cout << "String Output" << endl;
  208.         ofstream fout;
  209.         fout.open("String.txt");
  210.         cout << "Name: ";
  211.         fout << "Name: ";
  212.         for (int i = 0; i < S[0].GetN(); i++)
  213.         {
  214.             cout << S[0].GetMas(i)->GetName() << "; ";
  215.             fout << S[0].GetMas(i)->GetName() << "; ";
  216.         }
  217.         cout << endl;
  218.         fout << endl;
  219.         cout << "L per h: ";
  220.         fout << "L per h: ";
  221.         for (int i = 0; i < S[0].GetN(); i++)
  222.         {
  223.             cout << S[0].GetMas(i)->GetGectar() << "; ";
  224.             fout << S[0].GetMas(i)->GetGectar() << "; ";
  225.         }
  226.         cout << endl;
  227.         fout << endl;
  228.         cout << "Gektars: ";
  229.         fout << "Gektars: ";
  230.         for (int i = 0; i < S[0].GetN(); i++)
  231.         {
  232.             cout << S[0].GetMas(i)->GetGectars() << "; ";
  233.             fout << S[0].GetMas(i)->GetGectars() << "; ";
  234.         }
  235.         cout << endl;
  236.         fout << endl;
  237.         cout << "Productivity: ";
  238.         fout << "Productivity: ";
  239.         for (int i = 0; i < S[0].GetN(); i++)
  240.         {
  241.             cout << S[0].GetMas(i)->GetGectars() * S[0].GetMas(i)->GetGectar() << "; ";
  242.             fout << S[0].GetMas(i)->GetGectars() * S[0].GetMas(i)->GetGectar() << "; ";
  243.         }
  244.         cout << endl;
  245.         fout << endl;
  246.         fout.close();
  247.     }
  248.     ~Handler();
  249. };
  250. Handler::Handler()
  251. {
  252.     cout << "Constructor class Handler" << endl;
  253. };
  254. Handler::Handler(Sector *C)
  255. {
  256.     Desk = C;
  257.     N = Desk->GetN();
  258. }
  259. void Handler::SetInTable(Sector *A, Sector *B, Plant *LineInTable)
  260. {// установка растения по номеру
  261.     if (LineInTable->GetNumber() == 1)
  262.         A->AddPlant(LineInTable);
  263.     else
  264.         B->AddPlant(LineInTable);
  265. }
  266. Handler::~Handler()
  267. {
  268.     cout << "Destructor class Handler" << endl;
  269. }
  270.  
  271. int main()
  272. {
  273.     cout << "\tInput 2 Plants for FIRST sector\n";
  274.     Sector *A = new Sector(2, 1);//создаем 2 категории
  275.     cout << "\tInput 2 Plants for SECOND second\n";
  276.     Sector *B = new Sector(2, 2);
  277.     cout << "\n";
  278.     cout << "Plants in first Sector:\n";
  279.     A->PrintAll();
  280.     cout << "\n";
  281.     cout << "PLants in second Sector:\n";
  282.     B->PrintAll();
  283.  
  284.     Handler handler(A);
  285.     StringOutput* SO = &handler;
  286.     TableOutput* TO = &handler;
  287.     SO->Print(A);
  288.     TO->Print(B);
  289.  
  290.     delete A;
  291.     delete B;
  292.     system("pause");
  293.     return 0;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement