Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <string>
- #include <iostream>
- #include <windows.h>
- #include <fstream>
- #include <iomanip>
- using namespace std;
- //************************класс часть***************************
- class Plant
- {
- string name_plant;
- int number;//код cтроки
- int gectar;// сколько посажено на 1 гектар
- int gectars;// гектары, общее количество
- public:
- Plant();// Конструктор по умолчанию
- Plant(int i);// Конструкторы инициализаторы
- Plant(string Name, int num, int Gect, int gect);// Конструкторы инициализаторы
- ~Plant();// Деструктор
- void Input();//ввод данных
- void Print();
- // Методы для работы с атрибутами класса
- int GetNumber() { return number; };//получение номера
- int GetGectar() { return gectar; };
- int GetGectars() { return gectars; };
- string GetName() { return name_plant; };
- void SetPlant()
- {
- cout << "Input the name of Plant\n";
- getline(cin, name_plant);
- cout << "Input Gectar(number of landings per hectare)\n";
- cin >> gectar;
- cout << "Input Gectars\n";
- cin >> gectars;
- cin.get();//пропускает "\n" из потока ввода
- }
- };
- Plant::Plant()
- {// Конструктор по умолчанию
- name_plant = "undefined";
- number = 1;
- gectars = 0;
- gectar = 0;
- cout << "Default constructor for class Plant\n";
- }
- Plant::Plant(int i)
- {// Конструктор инициализатор
- if (i == 1 || i == 2)//если указана категория
- {
- SetPlant();
- number = i;
- }
- else
- Input();
- cout << "Constructor initializer class of Plant\n";
- }
- Plant::Plant(string Name, int num, int Gect, int gect)
- {// Конструктор инициализатор (перегруженный)
- name_plant = Name;
- number = num;
- gectar = Gect;
- gectars = gect;
- cout << "Constructor initializer (overloaded) class of Plant\n";
- }
- void Plant::Input()
- {// Метод ввода данных
- cin.sync();//очистка буфера
- cout << "Input name of the Plant\n";
- getline(cin, name_plant);
- cout << "Input Gectar(number of landings per hectare)\n";
- cin >> gectar;
- cout << "Input Gectars\n";
- cin >> gectars;
- cout << "Input Number" << endl;
- do {
- cin >> number;
- } while (number != 1 && number != 2);
- }
- void Plant::Print()
- {// Метод вывода
- cout << "Name of Plant: \"" << name_plant << "\". His productivity is: " << gectar * gectars << ".\n";
- }
- Plant::~Plant()
- {// Деструктор
- cout << "Destructor class Plant\n";
- }
- class Sector
- {
- Plant **FPlantArray;
- int FPlantCount;
- int FCurrentIndex;
- public:
- Sector(); // Конструктор по умолчанию
- Sector(int n, int f); // Конструктор инициализатор
- void PrintAll();
- void Print_i() { FPlantArray[FCurrentIndex]->Print(); }
- virtual ~Sector(); // Деструктор класса
- void AddPlant(Plant* PlantBuf);
- // Методы для работы с атрибутами класса
- void SetN(int n) { FPlantCount = n; };//кол-во растений
- int GetN() { return FPlantCount; }
- // Методы для работы с «частью»
- Plant* GetMas(int i) { return FPlantArray[i]; }
- void SetMas(int i, Plant *f) { FPlantArray[i] = f; }
- };
- Sector::Sector()
- {// Конструктор по умолчанию
- FPlantCount = 0;
- FCurrentIndex = 0;
- FPlantArray = NULL;
- cout << "Default constructor Computation\n";
- }
- Sector::Sector(int n, int f)
- {// Конструктор инициализатор
- int i;
- FPlantCount = n;
- FPlantArray = new Plant*[n];// присвоили массиву новую переменную с адресом на массив
- for (i = 0; i < FPlantCount; i++)
- FPlantArray[i] = new Plant(f);//пока не заполнится массив, можно добавлять новые растения
- cout << "Constructor initializer class Sector\n";
- }
- void Sector::AddPlant(Plant* PlantBuf)
- {// Метод добавления растения
- int j, k = 0;
- Plant **PlantArray;//указатель на указатель
- PlantArray = new Plant*[this->GetN() + 1];//инициализация массива
- for (j = 0; j <this->GetN(); j++)
- PlantArray[j] = this->GetMas(j);
- PlantArray[j] = PlantBuf;
- delete[] FPlantArray;
- FPlantArray = new Plant*[this->GetN() + 1];
- this->SetN(this->GetN() + 1);//установка параметров(устанавливает указатель)
- for (j = 0; j <this->GetN(); j++)
- FPlantArray[j] = PlantArray[j];
- }
- void Sector::PrintAll()
- {// Метод вывода перечня растений
- for (int i = 0; i < FPlantCount; i++)
- FPlantArray[i]->Print();
- }
- Sector::~Sector()
- {// Деструктор
- for (int i = 0; i < FPlantCount; i++)
- delete FPlantArray[i];
- delete[] FPlantArray;
- cout << "Destructor class Sector\n";
- }
- class TableOutput
- {//табличный вывод информации в файл и на экран
- public:
- virtual void Print(Sector *S) {};
- };
- class StringOutput
- {//вывод информации в ленточной форме в файл и на экран
- public:
- virtual void Print(Sector *S) {};
- };
- class TableOutput_helper : public TableOutput {
- public:
- virtual void TableOutput_Print(Sector *S) = 0;
- void Print(Sector *S)
- {
- TableOutput_Print(S);
- }
- };
- class StringOutput_helper : public StringOutput {
- public:
- virtual void StringOutput_Print(Sector *S) = 0;
- void Print(Sector *S)
- {
- StringOutput_Print(S);
- }
- };
- class Handler : public TableOutput_helper, public StringOutput_helper
- {
- private:
- int N;
- Sector *Desk;
- public:
- Handler();
- Handler(Sector *S);
- void SetInTable(Sector *A, Sector *B, Plant *LineInTable);//установка растения в 1 или 2 категорию
- void TableOutput_Print(Sector *S)
- {
- cout << "Table Output" << endl;
- ofstream fout;
- fout.open("Table.txt");
- cout << left << setw(15) << "Name" << setw(10) << "L per h" << setw(10) << "Gektars" << setw(10) << "Productivity" << endl;
- fout << left << setw(15) << "Name" << setw(10) << "L per h" << setw(10) << "Gektars" << setw(10) << "Productivity" << endl;
- for (int i = 0; i < S[0].GetN(); i++)
- {
- cout << left << setw(15) << S[0].GetMas(i)->GetName() << setw(10) << S[0].GetMas(i)->GetGectar() << setw(10)
- << S[0].GetMas(i)->GetGectars() << setw(10) << S[0].GetMas(i)->GetGectars() * S[0].GetMas(i)->GetGectar() << endl;
- fout << left << setw(15) << S[0].GetMas(i)->GetName() << setw(10) << S[0].GetMas(i)->GetGectar() << setw(10)
- << S[0].GetMas(i)->GetGectars() << setw(10) << S[0].GetMas(i)->GetGectars() * S[0].GetMas(i)->GetGectar() << endl;
- }
- fout.close();
- }
- void StringOutput_Print(Sector *S)
- {
- cout << "String Output" << endl;
- ofstream fout;
- fout.open("String.txt");
- cout << "Name: ";
- fout << "Name: ";
- for (int i = 0; i < S[0].GetN(); i++)
- {
- cout << S[0].GetMas(i)->GetName() << "; ";
- fout << S[0].GetMas(i)->GetName() << "; ";
- }
- cout << endl;
- fout << endl;
- cout << "L per h: ";
- fout << "L per h: ";
- for (int i = 0; i < S[0].GetN(); i++)
- {
- cout << S[0].GetMas(i)->GetGectar() << "; ";
- fout << S[0].GetMas(i)->GetGectar() << "; ";
- }
- cout << endl;
- fout << endl;
- cout << "Gektars: ";
- fout << "Gektars: ";
- for (int i = 0; i < S[0].GetN(); i++)
- {
- cout << S[0].GetMas(i)->GetGectars() << "; ";
- fout << S[0].GetMas(i)->GetGectars() << "; ";
- }
- cout << endl;
- fout << endl;
- cout << "Productivity: ";
- fout << "Productivity: ";
- for (int i = 0; i < S[0].GetN(); i++)
- {
- cout << S[0].GetMas(i)->GetGectars() * S[0].GetMas(i)->GetGectar() << "; ";
- fout << S[0].GetMas(i)->GetGectars() * S[0].GetMas(i)->GetGectar() << "; ";
- }
- cout << endl;
- fout << endl;
- fout.close();
- }
- ~Handler();
- };
- Handler::Handler()
- {
- cout << "Constructor class Handler" << endl;
- };
- Handler::Handler(Sector *C)
- {
- Desk = C;
- N = Desk->GetN();
- }
- void Handler::SetInTable(Sector *A, Sector *B, Plant *LineInTable)
- {// установка растения по номеру
- if (LineInTable->GetNumber() == 1)
- A->AddPlant(LineInTable);
- else
- B->AddPlant(LineInTable);
- }
- Handler::~Handler()
- {
- cout << "Destructor class Handler" << endl;
- }
- int main()
- {
- cout << "\tInput 2 Plants for FIRST sector\n";
- Sector *A = new Sector(2, 1);//создаем 2 категории
- cout << "\tInput 2 Plants for SECOND second\n";
- Sector *B = new Sector(2, 2);
- cout << "\n";
- cout << "Plants in first Sector:\n";
- A->PrintAll();
- cout << "\n";
- cout << "PLants in second Sector:\n";
- B->PrintAll();
- Handler handler(A);
- StringOutput* SO = &handler;
- TableOutput* TO = &handler;
- SO->Print(A);
- TO->Print(B);
- delete A;
- delete B;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement