Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab_6.cpp
- #include "stdafx.h"
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <iostream>
- #include <locale.h>
- #include <string>
- using namespace std;
- class TOstanPunkt
- {
- protected:
- string FName;
- public:
- TOstanPunkt(); // Конструктор по умолчанию
- TOstanPunkt(string Name);
- virtual ~TOstanPunkt(); // виртуальный деструктор
- virtual void Print() = 0; // виртуальный метод вывода сведений о остановочный пункте
- virtual void Input() = 0;// виртуальный метод ввода сведений о остановочный пункте
- string GetName() { return FName; }
- void SetName(string name) { FName = name; }
- };
- class TStanciya :public TOstanPunkt
- {
- private:
- int FTime;
- public:
- TStanciya(string Name, int Time); // Конструктор с парамметрами
- TStanciya(); // Конструктор по умолчанию
- TStanciya(TStanciya &A); // Конструктор копирования
- ~TStanciya();// Деструктор по умолчанию
- TStanciya& operator =(const TStanciya &A); // Перегруженная операция =
- int GetTime() { return FTime; }
- void SetTime(int Time)
- {
- FTime = Time;
- }
- void Input(); // перегруженный виртуальный метод
- void Print(); // перегруженный виртуальный метод
- };
- class TPolustanok :public TOstanPunkt
- {
- private:
- int FTime;
- public:
- TPolustanok();// Конструктор по умолчанию
- TPolustanok(string Name, int Time);// Конструктор инициализатор
- TPolustanok(TPolustanok &A);// Конструктор копирования
- ~TPolustanok();// Деструктор
- TPolustanok& operator =(const TPolustanok &A);// Перегруженная операция =
- int GetTime() { return FTime; }
- void SetTime(int Time) { FTime = Time; }
- void Input();// перегруженный виртуальный метод
- void Print();// перегруженный виртуальный метод
- };
- TOstanPunkt::TOstanPunkt()
- {// Конструктор по умолчанию базового класса остановочный пункт
- FName = "";
- cout << "**** Конструктор по умолчанию базового класса остановочный пункт" << endl;
- }
- TOstanPunkt::TOstanPunkt(string Name)
- {// Конструктор инициализатор базового класса остановочный пункт
- FName = Name;
- cout << "**** Конструктор инициализатор базового класса остановочный пункт" << endl;
- }
- TOstanPunkt::~TOstanPunkt()
- {//Деструктор базового класса остановочный пункт
- cout << "**** Деструктор базового класса остановочный пункт" << endl;
- }
- TStanciya::TStanciya(string Name, int Time) :TOstanPunkt(Name)
- {// Конструктор инициализатор класса станция
- FTime = Time;
- cout << "**** Конструктор инициализатор класса станция" << endl;
- }
- TStanciya::TStanciya()
- {//Конструктор по умолчанию класса станция
- FName = "";
- FTime = 0;
- cout << "**** Конструктор по умолчанию класса станция" << endl;
- }
- TPolustanok::TPolustanok(string Name, int Time) :TOstanPunkt(Name)
- {//Конструктор инициализатор класса полустанок
- FTime = Time;
- cout << "**** Конструктор инициализатор класса полустанок" << endl;
- }
- TPolustanok::TPolustanok()
- {//Конструктор по умолчанию класса полустанок
- FName = "";
- FTime = 0;
- cout << "**** Конструктор по умолчанию класса полустанок" << endl;
- }
- TStanciya::TStanciya(TStanciya &A)
- {//Конструктор копирования класса станция
- FName = A.FName;
- FTime = A.FTime;
- cout << "**** Конструктор копирования класса станция" << endl;
- }
- TPolustanok& TPolustanok::operator =(const TPolustanok &A)
- {//Перегруженная операция = класса полустанок
- FName = A.FName;
- FTime = A.FTime;
- cout << "**** Перегруженная операция = класса полустанок" << endl;
- return *this;
- }
- TStanciya& TStanciya::operator =(const TStanciya &A)
- {//Перегруженная операция = класса станция
- FName = A.FName;
- FTime = A.FTime;
- cout << "**** Перегруженная операция = класса станция" << endl;
- return *this;
- }
- TPolustanok::TPolustanok(TPolustanok &A)
- {//Конструктор копирования класса полустанок
- FName = A.FName;
- FTime = A.FTime;
- cout << "****Конструктор копирования класса полустанок" << endl;
- }
- void TStanciya::Input()
- {//Ввод сведений о станции:
- cout << "Ввод сведений о станции:" << endl;
- cout << "Введите имя станции: ";
- cin >> FName;
- cout << "Введите время: ";
- cin >> FTime;
- cout << "**** Перегруженный виртуальный метод Input класса станция" << endl;
- }
- void TPolustanok::Input()
- {//Ввод сведений о полустанке:
- cout << "Ввод сведений о полустанке:" << endl;
- cout << "Введите имя полустанка: ";
- cin >> FName;
- cout << "Введите время: ";
- cin >> FTime;
- cout << "**** Перегруженный виртуальный метод Input класса полустанок" << endl;
- }
- void TStanciya::Print()
- {// Перегруженный виртуальный метод класса станция
- cout << "\t" << FName << "\t" << FTime << endl;
- cout << "**** Перегруженный виртуальный метод Print класса станция" << endl;
- }
- void TPolustanok::Print()
- {// Перегруженный виртуальный метод Print класса полустанок
- cout << "\t" << FName << "\t" << FTime << endl;
- cout << "**** Перегруженный виртуальный метод Print класса полустанок" << endl;
- }
- TStanciya::~TStanciya()
- {//Перегруженный виртуальный Деструктор класса станция
- cout << "**** Перегруженный виртуальный Деструктор класса станция" << endl;
- }
- TPolustanok::~TPolustanok()
- {//Перегруженный виртуальный Деструктор класса полустанок
- cout << "**** Перегруженный виртуальный Деструктор класса полустанок" << endl;
- }
- //-----------------------------------------------
- class TRoute
- {
- private:
- int FCounTStanciya; // Количество станций
- int FCounTPolustanok; //Количество полустанок
- TStanciya *FStanciya; // Массив станций
- TPolustanok *FPolustanok;// Массив полустанок
- public:
- TRoute();// Конструктор по умолчанию
- ~TRoute();// Деструктор
- void Print();
- void AddStanciya(TStanciya &A);// Добавление станций
- void AddPolustanok(TPolustanok &A);//Добавление полустанок
- void DelStanciya(int c);//Удаление станций
- void DelPolustanok(int c);//Удаление полустанок
- TStanciya* GeTStanciya(int c) { return &FStanciya[c]; }
- TPolustanok* GeTPolustanok(int c) { return &FPolustanok[c]; }
- int GetCounTStanciya() { return FCounTStanciya; }
- int GetCounTPolustanok() { return FCounTPolustanok; }
- };
- TRoute::TRoute()
- {//Конструктор по умолчанию класса Маршрут
- FCounTStanciya = 0;
- FCounTPolustanok = 0;
- FStanciya = NULL;
- FPolustanok = NULL;
- cout << "**** Конструктор по умолчанию класса Маршрут" << endl;
- }
- void TRoute::AddStanciya(TStanciya &A)
- {//Метод добавления станций класса Командa
- int i;
- TStanciya t;
- TStanciya *tmp = new TStanciya[FCounTStanciya + 1];
- for (i = 0; i<FCounTStanciya; i++)
- tmp[i] = FStanciya[i];
- tmp[FCounTStanciya] = A;
- delete[] FStanciya;
- FStanciya = tmp;
- FCounTStanciya++;
- cout << "**** Метод добавления станций класса Маршрут" << endl;
- }
- void TRoute::AddPolustanok(TPolustanok &A)
- {//Метод добавления полустанок класса Маршрут
- int i;
- TPolustanok *tmp = new TPolustanok[FCounTPolustanok + 1];
- for (i = 0; i<FCounTPolustanok; i++)
- tmp[i] = FPolustanok[i];
- tmp[FCounTPolustanok] = A;
- delete[] FPolustanok;
- FPolustanok = tmp;
- FCounTPolustanok++;
- cout << "**** Метод добавления полустанок класса Маршрут" << endl;
- }
- void TRoute::DelStanciya(int c)
- {//Метод удаления станций класса Маршрут
- int i;
- TStanciya *tmp = new TStanciya[FCounTStanciya - 1];
- for (i = 0; i<c; i++)
- tmp[i] = FStanciya[i];
- for (i = c + 1; i<FCounTStanciya; i++)
- tmp[i - 1] = FStanciya[i];
- delete[] FStanciya;
- FStanciya = tmp;
- FCounTStanciya--;
- cout << "**** Метод удаления станций класса Маршрут" << endl;
- }
- void TRoute::DelPolustanok(int c)
- {//Метод удаления полустанок класса Маршрут
- int i;
- TPolustanok *tmp = new TPolustanok[FCounTPolustanok - 1];
- for (i = 0; i<c; i++)
- tmp[i] = FPolustanok[i];
- for (i = c + 1; i<FCounTPolustanok; i++)
- tmp[i - 1] = FPolustanok[i];
- delete[] FPolustanok;
- FPolustanok = tmp;
- FCounTPolustanok--;
- cout << "**** Метод удаления полустанок класса Маршрут" << endl;
- }
- TRoute::~TRoute()
- {// Деструктор класса Маршрут
- if (FStanciya != NULL)
- delete[] FStanciya;
- if (FPolustanok != NULL)
- delete[] FPolustanok;
- cout << "**** Деструктор класса Маршрут" << endl;
- }
- void TRoute::Print()
- {// Метод вывод сведений о маршруте класса Маршрут
- int i;
- cout << "станции:" << endl;
- for (i = 0; i<FCounTStanciya; i++)
- FStanciya[i].Print();
- cout << "полустанки:" << endl;
- for (i = 0; i<FCounTPolustanok; i++)
- FPolustanok[i].Print();
- cout << "**** Метод вывод сведений о маршруте класса Маршрут" << endl;
- }
- //-----------------------------------------------------
- class TProcessor
- {// Класс обработчик
- private:
- TRoute* t; // Маршрут
- int max1, max2, max3, imax1, imax2, imax3;
- int min1, min2, min3, imin1, imin2, imin3;
- public:
- TProcessor();// Конструктор по умолчанию
- ~TProcessor();//Деструктор
- void Sravnenie();
- void CalculateTime();
- void AddStanciya(TStanciya &A) { t->AddStanciya(A); } // Добавить станций
- void AddPolustanok(TPolustanok &A)
- {
- t->AddPolustanok(A);
- }// Добавить полустанок
- void DelStanciya(int c) { t->DelStanciya(c); }//Удалить станций
- void DelPolustanok(int c)
- {
- t->DelPolustanok(c);
- }//Удалить полустанок
- void Print() { t->Print(); }// Вывод сведений о маршруте
- };
- TProcessor::TProcessor()
- {// Конструктор по умолчанию класса Обработчик
- t = new TRoute();
- cout << "**** Конструктор по умолчанию класса Обработчик" << endl;
- }
- TProcessor::~TProcessor()
- {// Деструктор класса Обработчик
- delete t;
- cout << "**** Деструктор класса Обработчик" << endl;
- };
- void TProcessor::Sravnenie()
- {
- cout << "Какие маршруты сравнить?" << endl;
- int one, two;
- cout << "Введите первый: ";
- cin >> one;
- cout << "Введите второй: ";
- cin >> two;
- cout << "Сравнение станций" << endl;
- if (t->GeTStanciya(one - 1)->GetTime() == t->GeTStanciya(two - 1)->GetTime())
- cout << "Маршруты " << one << " & " << two << " равны" << endl;
- else
- cout << "Маршруты " << one << " & " << two << " НЕ равны" << endl;
- cout << "Сравнение полустанок" << endl;
- if (t->GeTPolustanok(one - 1)->GetTime() == t->GeTPolustanok(two - 1)->GetTime())
- cout << "Маршруты " << one << " & " << two << " равны" << endl;
- else
- cout << "Маршруты " << one << " & " << two << " НЕ равны" << endl;
- }
- void TProcessor::CalculateTime()
- {
- int time = 0;
- for (int i = 0; i<t->GetCounTStanciya(); i++)
- time += t->GeTStanciya(i)->GetTime();
- for (int i = 0; i<t->GetCounTPolustanok(); i++)
- time += t->GeTPolustanok(i)->GetTime();
- cout << "Общее время для прохождения маршрута = " << time << endl;
- }
- int main()
- {
- setlocale(LC_ALL, "Rus");
- int CounTStanciya, CounTPolustanok;
- int i;
- cout << "Введите количество станций: ";
- cin >> CounTStanciya;
- cout << "Введите количество полустанок: ";
- cin >> CounTPolustanok;
- TStanciya *A = new TStanciya[CounTStanciya];
- for (i = 0; i<CounTStanciya; i++)
- A[i].Input();
- TPolustanok *B = new TPolustanok[CounTPolustanok];
- for (i = 0; i<CounTPolustanok; i++)
- B[i].Input();
- // Работа с классом Обработчик
- cout << "\n Работа с классом Обработчик" << endl;
- TProcessor *p = new TProcessor;
- for (i = 0; i<CounTStanciya; i++)
- p->AddStanciya(A[i]);
- for (i = 0; i<CounTPolustanok; i++)
- p->AddPolustanok(B[i]);
- cout << " Все Маршруты: \n";
- p->Print();
- p->Sravnenie();
- p->CalculateTime();
- system("pause");
- delete[] A;
- delete[] B;
- delete p;
- // Работа с массивом указателей на тип базового класса
- cout << "\n Работа с массивом указателей на тип базового класса\n" << endl;
- TOstanPunkt **array_route;
- array_route = new TOstanPunkt*[CounTStanciya + CounTPolustanok];
- for (i = 0; i<CounTStanciya; i++)
- array_route[i] = new TStanciya;
- for (i = CounTStanciya; i<CounTStanciya + CounTPolustanok; i++)
- array_route[i] = new TPolustanok;
- cout << "\n Ввод сведений о маршруте:\n" << endl;
- for (i = 0; i<CounTStanciya + CounTPolustanok; i++)
- array_route[i]->Input();
- cout << "\n В маршруте присутствуют следующие остановки:\n" << endl;
- for (i = 0; i<CounTStanciya + CounTPolustanok; i++)
- array_route[i]->Print();
- for (i = 0; i<CounTStanciya + CounTPolustanok; i++)
- delete array_route[i];
- delete[] array_route;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment