Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <string>
- #include <iostream>
- #include <locale>
- #include <Windows.h>
- #include <fstream>
- #include <iomanip>
- using namespace std;
- //Класс-растение
- class TPlant
- {
- protected:
- string Fname_plant;
- int Fgectar;// гектары, общее количество
- public:
- TPlant(); // Конструктор по умолчанию
- TPlant(string Name, int gectar);
- virtual ~TPlant(); // виртуальный деструктор
- virtual void Print() = 0; // виртуальный метод вывода сведений о растении
- virtual void Input() = 0;// виртуальный метод ввода сведений о растении
- string GetName() { return Fname_plant; }
- int GetGectar() { return Fgectar; }
- void SetName(string name) { Fname_plant = name; }
- void SetGectar(int gectar) { Fgectar = gectar; }
- };
- //Класс-дерево
- class Tree :public TPlant
- {
- private:
- int FPlantedTree;// сколько посажено деревьев всего
- public:
- Tree(int PlantedTree, string Name, int gectar); // Конструктор с параметрами
- Tree(); // Конструктор по умолчанию
- Tree(Tree &A); // Конструктор копирования
- ~Tree();// Деструктор по умолчанию
- Tree& operator =(const Tree &A); // Перегруженная операция =
- int GetPlantedTree() { return FPlantedTree; }
- void SetPlantedTree(int PlantedTree) { FPlantedTree = PlantedTree; }
- void Input(); // перегруженный виртуальный метод
- void Print(); // перегруженный виртуальный метод
- };
- //Класс-зерновые
- class Cereals :public TPlant
- {
- private:
- int FPlantedPerGectar;// гектары, общее количество
- public:
- Cereals();// Конструктор по умолчанию
- Cereals(int PlantedPerGectar, string Name, int gectar);// Конструктор инициализатор
- Cereals(Cereals &A);// Конструктор копирования
- ~Cereals();// Деструктор
- Cereals& operator =(const Cereals &A);// Перегруженная операция =
- int GetGectars() { return FPlantedPerGectar; }
- void SetGectars(int PlantedPerGectar) { FPlantedPerGectar = PlantedPerGectar; }
- void Input();// перегруженный виртуальный метод
- void Print();// перегруженный виртуальный метод
- };
- TPlant::TPlant()
- {// Конструктор по умолчанию базового класса Растение
- Fname_plant = "";
- Fgectar = 0;
- cout << "**** Конструктор по умолчанию базового класса Растение" << endl;
- }
- TPlant::TPlant(string Name, int gectar)
- {// Конструктор инициализатор базового класса Растение
- Fname_plant = Name;
- Fgectar = gectar;
- cout << "**** Конструктор инициализатор базового класса Растение" << endl;
- }
- TPlant::~TPlant()
- {//Деструктор базового класса Растение
- cout << "**** Деструктор базового класса Растение" << endl;
- }
- Tree::Tree(int PlantedTree, string Name, int gectar) :TPlant(Name, gectar)
- {// Конструктор инициализатор класса Дерево
- FPlantedTree = PlantedTree;
- cout << "**** Конструктор инициализатор класса Дерево" << endl;
- }
- Tree::Tree()
- {//Конструктор по умолчанию класса Дерево
- FPlantedTree = 0;
- Fname_plant = "";
- Fgectar = 0;
- cout << "**** Конструктор по умолчанию класса Дерево" << endl;
- }
- Cereals::Cereals(int PlantedPerGectar, string Name, int gectar) :TPlant(Name, gectar)
- {//Конструктор инициализатор класса Зерновые
- FPlantedPerGectar = PlantedPerGectar;
- cout << "**** Конструктор инициализатор класса Зерновые" << endl;
- }
- Cereals::Cereals()
- {//Конструктор по умолчанию класса Зерновые
- FPlantedPerGectar = 0;
- Fname_plant = "";
- Fgectar = 0;
- cout << "**** Конструктор по умолчанию класса Зерновые" << endl;
- }
- Tree::Tree(Tree &A)
- {//Конструктор копирования класса Дерево
- FPlantedTree = A.FPlantedTree;
- Fname_plant = A.Fname_plant;
- Fgectar = A.Fgectar;
- cout << "**** Конструктор копирования класса Дерево" << endl;
- }
- Cereals& Cereals::operator =(const Cereals &A)
- {//Перегруженная операция = класса Зерновые
- FPlantedPerGectar = A.FPlantedPerGectar;
- Fname_plant = A.Fname_plant;
- Fgectar = A.Fgectar;
- cout << "**** Перегруженная операция = класса Зерновые" << endl;
- return *this;
- }
- Tree& Tree::operator =(const Tree &A)
- {//Перегруженная операция = класса Дерево
- FPlantedTree = A.FPlantedTree;
- Fname_plant = A.Fname_plant;
- Fgectar = A.Fgectar;
- cout << "**** Перегруженная операция = класса Дерево" << endl;
- return *this;
- }
- Cereals::Cereals(Cereals &A)
- {//Конструктор копирования класса Зерновые
- FPlantedPerGectar = A.FPlantedPerGectar;
- Fname_plant = A.Fname_plant;
- Fgectar = A.Fgectar;
- cout << "****Конструктор копирования класса Зерновые" << endl;
- }
- void Tree::Input()
- {//Ввод сведений о Дереве
- cout << "Ввод сведений о Дереве:" << endl;
- cout << "Введите наименование дерева: ";
- cin >> Fname_plant;
- cout << "Введите количество деревьев: ";
- cin >> FPlantedTree;
- cout << "Введите общее количество гектар: ";
- cin >> Fgectar;
- cout << "**** Перегруженный виртуальный метод Input класса Дерево" << endl;
- }
- void Cereals::Input()
- {//Ввод сведений о Зерновой культуре
- cout << "Ввод сведений о зерновых:" << endl;
- cout << "Введите наименование зерновой культуры: ";
- cin >> Fname_plant;
- cout << "Введите количество зерновой культуры на 1 гектаре: ";
- cin >> FPlantedPerGectar;
- cout << "Введите общее количество гектар: ";
- cin >> Fgectar;
- cout << "**** Перегруженный виртуальный метод Input класса Зерновые" << endl;
- }
- void Tree::Print()
- {// Перегруженный виртуальный метод класса Дерево
- cout << "\t" << Fname_plant << "\t" << Fgectar << "\t" << FPlantedTree << endl;
- cout << "**** Перегруженный виртуальный метод Print класса Дерево" << endl;
- }
- void Cereals::Print()
- {// Перегруженный виртуальный метод Print класса Зерновые
- cout << "\t" << Fname_plant << "\t" << Fgectar << "\t" << FPlantedPerGectar << endl;
- cout << "**** Перегруженный виртуальный метод Print класса Зерновые" << endl;
- }
- Tree::~Tree()
- {//Перегруженный виртуальный Деструктор класса Дерево
- cout << "**** Перегруженный виртуальный Деструктор класса Дерево" << endl;
- }
- Cereals::~Cereals()
- {//Перегруженный виртуальный Деструктор класса Зерновые
- cout << "**** Перегруженный виртуальный Деструктор класса Зерновые" << endl;
- }
- //Класс-сектор
- class TSector
- {
- private:
- int FCountTrees; // Количество Деревьев
- int FCounCereals; //Количество Зерновой культуры
- Tree *FTree; // Массив Деревьев
- Cereals *FCereals;// Массив Зерновой культуры
- public:
- TSector();// Конструктор по умолчанию
- TSector(Tree* p, Cereals* g, int max)
- {
- FTree = p;
- FCereals = g;
- FCountTrees = max;
- FCounCereals = max;
- }
- ~TSector();// Деструктор
- void Print();
- void AddTree(Tree &A);// Добавление Дерева
- void AddCereals(Cereals &A);//Добавление Зерновой культуры
- void DelTree(int c);//Удаление Дерева
- void DelCereals(int c);//Удаление Зерновой культуры
- Tree* GetTree(int c) { return &FTree[c]; }
- Cereals* GetCereals(int c) { return &FCereals[c]; }
- int GetCountTrees() { return FCountTrees; }
- int GetCountCereals() { return FCounCereals; }
- };
- TSector::TSector()
- {//Конструктор по умолчанию класса Сектор
- FCountTrees = 0;
- FCounCereals = 0;
- FTree = NULL;
- FCereals = NULL;
- cout << "**** Конструктор по умолчанию класса Сектор" << endl;
- }
- void TSector::AddTree(Tree &A)
- {//Метод добавления Дерева класса Сектор
- int i;
- Tree t;
- Tree *tmp = new Tree[FCountTrees + 1];
- for (i = 0; i<FCountTrees; i++)
- tmp[i] = FTree[i];
- tmp[FCountTrees] = A;
- delete[] FTree;
- FTree = tmp;
- FCountTrees++;
- cout << "**** Метод добавления Дерева класса Сектор" << endl;
- }
- void TSector::AddCereals(Cereals &A)
- {//Метод добавления Зерновой культуры класса Сектор
- int i;
- Cereals *tmp = new Cereals[FCounCereals + 1];
- for (i = 0; i<FCounCereals; i++)
- tmp[i] = FCereals[i];
- tmp[FCounCereals] = A;
- delete[] FCereals;
- FCereals = tmp;
- FCounCereals++;
- cout << "**** Метод добавления Зерновой культуры класса Сектор" << endl;
- }
- void TSector::DelTree(int c)
- {//Метод удаления Дерева класса Сектор
- int i;
- Tree *tmp = new Tree[FCountTrees - 1];
- for (i = 0; i<c; i++)
- tmp[i] = FTree[i];
- for (i = c + 1; i<FCountTrees; i++)
- tmp[i - 1] = FTree[i];
- delete[] FTree;
- FTree = tmp;
- FCountTrees--;
- cout << "**** Метод удаления Дерева класса Сектор" << endl;
- }
- void TSector::DelCereals(int c)
- {//Метод удаления Зерновой культуры класса Сектор
- int i;
- Cereals *tmp = new Cereals[FCounCereals - 1];
- for (i = 0; i<c; i++)
- tmp[i] = FCereals[i];
- for (i = c + 1; i<FCounCereals; i++)
- tmp[i - 1] = FCereals[i];
- delete[] FCereals;
- FCereals = tmp;
- FCounCereals--;
- cout << "**** Метод удаления Зерновой культуры класса Сектор" << endl;
- }
- TSector::~TSector()
- {// Деструктор класса Сектор
- if (FTree != NULL)
- delete[] FTree;
- if (FCereals != NULL)
- delete[] FCereals;
- cout << "**** Деструктор класса Сектор" << endl;
- }
- class TabularOutput
- {//табличный вывод информации в файл и на экран
- public:
- virtual void Print(TSector *S) = 0;
- };
- class StringOutput
- {//вывод информации в ленточной форме в файл и на экран
- public:
- virtual void Print(TSector *S) = 0;
- };
- class Tabular_Output : public TabularOutput {
- public:
- virtual void TO_Print(TSector *S) = 0;
- void Print(TSector *S)
- {
- TO_Print(S);
- }
- };
- class String_Output : public StringOutput {
- public:
- virtual void SO_Print(TSector *S) = 0;
- void Print(TSector *S)
- {
- SO_Print(S);
- }
- };
- //Класс-обработчик
- class Handler : public Tabular_Output, public String_Output
- {
- public:
- Handler();
- Handler(TSector *T);
- TSector *Tpointer;
- void TO_Print(TSector *S)
- {
- ofstream fout;
- fout.open("TO.txt");
- cout << "Tabular Output" << endl;
- fout << "Tabular Output" << endl;
- cout << left << setw(15) << "Name" << setw(10) << "Gektar" << setw(10) << "L per h" << setw(10) << "Productivity" << endl;
- fout << left << setw(15) << "Name" << setw(10) << "Gektar" << setw(10) << "L per h" << setw(10) << "Productivity" << endl;
- for (int i = 0; i < S[0].GetCountTrees(); i++)
- {
- //примем, что на 1 дереве 111 фруктов
- cout << left << setw(15) << S[0].GetTree(i)->GetName()
- << setw(10) << S[0].GetTree(i)->GetGectar()
- << setw(10) << S[0].GetTree(i)->GetPlantedTree()
- << setw(10) << S[0].GetTree(i)->GetPlantedTree() * S[0].GetTree(i)->GetGectar() * 111 << endl;
- //GetPlantedTree = общему количеству деревьев определенного вида
- fout << left << setw(15) << S[0].GetTree(i)->GetName()
- << setw(10) << S[0].GetTree(i)->GetGectar()
- << setw(10) << S[0].GetTree(i)->GetPlantedTree()
- << setw(10) << S[0].GetTree(i)->GetPlantedTree() * 111 << endl;
- }
- fout.close();
- }
- void SO_Print(TSector *S)
- {
- cout << "String Output" << endl;
- ofstream fout;
- fout.open("String.txt");
- cout << "Name: ";
- fout << "Name: ";
- for (int i = 0; i < S[0].GetCountCereals(); i++)
- {
- cout << S[0].GetCereals(i)->GetName() << "; ";
- fout << S[0].GetCereals(i)->GetName() << "; ";
- }
- cout << endl;
- fout << endl;
- cout << "L per h: ";
- fout << "L per h: ";
- for (int i = 0; i < S[0].GetCountCereals(); i++)
- {
- cout << S[0].GetCereals(i)->GetGectar() << "; ";
- fout << S[0].GetCereals(i)->GetGectar() << "; ";
- }
- cout << endl;
- fout << endl;
- cout << "Gektars: ";
- fout << "Gektars: ";
- for (int i = 0; i < S[0].GetCountCereals(); i++)
- {
- cout << S[0].GetCereals(i)->GetGectars() << "; ";
- fout << S[0].GetCereals(i)->GetGectars() << "; ";
- }
- cout << endl;
- fout << endl;
- cout << "Productivity: ";
- fout << "Productivity: ";
- for (int i = 0; i < S[0].GetCountCereals(); i++)
- {
- cout << S[0].GetCereals(i)->GetGectars() * S[0].GetCereals(i)->GetGectar() << "; ";
- fout << S[0].GetCereals(i)->GetGectars() * S[0].GetCereals(i)->GetGectar() << "; ";
- }
- cout << endl;
- fout << endl;
- fout.close();
- }
- ~Handler();
- };
- Handler::Handler()
- {
- cout << "Constructor class Handler" << endl;
- };
- Handler::Handler(TSector *T)
- {
- Tpointer = T;
- }
- Handler::~Handler()
- {
- cout << "Destructor class Handler" << endl;
- };
- int main()
- {
- setlocale(LC_ALL, "rus");
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- int max = 0;
- cout << "Введите количество деревьев и зерновых: ";
- cin >> max;
- Tree *A = new Tree[max];
- for (int i = 0; i<max; i++)
- A[i].Input();
- Cereals *B = new Cereals[max];
- for (int i = 0; i<max; i++)
- B[i].Input();
- Tree *tree = new Tree[max];
- Cereals *cereals = new Cereals[max];
- TSector sector(A, B, max);
- Handler processor(§or);
- StringOutput* SO = &processor;
- TabularOutput* TO = &processor;
- SO->Print(§or);
- TO->Print(§or);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement