Xom9ik

Lab_6/6 var (IIl semester)

Dec 9th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.99 KB | None | 0 0
  1. //Lab_6.cpp
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <iostream>
  8. #include <locale.h>
  9. #include <string>
  10. using namespace std;
  11.  
  12. class TOstanPunkt
  13. {
  14. protected:
  15.     string FName;
  16. public:
  17.     TOstanPunkt(); // Конструктор по умолчанию
  18.     TOstanPunkt(string Name);
  19.     virtual ~TOstanPunkt(); // виртуальный деструктор
  20.     virtual void Print() = 0; // виртуальный метод вывода сведений о остановочный пункте      
  21.     virtual void Input() = 0;// виртуальный метод ввода сведений о остановочный пункте
  22.     string GetName() { return FName; }
  23.     void SetName(string name) { FName = name; }
  24. };
  25. class TStanciya :public TOstanPunkt
  26. {
  27. private:
  28.     int FTime;
  29. public:
  30.     TStanciya(string Name, int Time); // Конструктор с парамметрами
  31.     TStanciya(); // Конструктор по умолчанию
  32.     TStanciya(TStanciya &A); // Конструктор копирования
  33.     ~TStanciya();// Деструктор по умолчанию
  34.     TStanciya& operator =(const TStanciya &A); // Перегруженная операция =
  35.     int GetTime() { return FTime; }
  36.     void SetTime(int Time)
  37.     {
  38.         FTime = Time;
  39.     }
  40.     void Input(); // перегруженный виртуальный метод
  41.     void Print(); // перегруженный виртуальный метод
  42. };
  43. class TPolustanok :public TOstanPunkt
  44. {
  45. private:
  46.     int FTime;
  47. public:
  48.     TPolustanok();// Конструктор по умолчанию
  49.     TPolustanok(string Name, int Time);// Конструктор инициализатор
  50.     TPolustanok(TPolustanok &A);// Конструктор копирования
  51.     ~TPolustanok();// Деструктор
  52.     TPolustanok& operator =(const TPolustanok &A);// Перегруженная операция =
  53.     int GetTime() { return FTime; }
  54.     void SetTime(int Time) { FTime = Time; }
  55.     void Input();// перегруженный виртуальный метод
  56.     void Print();// перегруженный виртуальный метод
  57. };
  58. TOstanPunkt::TOstanPunkt()
  59. {// Конструктор по умолчанию базового класса остановочный пункт
  60.     FName = "";
  61.     cout << "**** Конструктор по умолчанию базового класса остановочный пункт" << endl;
  62. }
  63. TOstanPunkt::TOstanPunkt(string Name)
  64. {// Конструктор инициализатор базового класса остановочный пункт
  65.     FName = Name;
  66.     cout << "**** Конструктор инициализатор базового класса остановочный пункт" << endl;
  67. }
  68. TOstanPunkt::~TOstanPunkt()
  69. {//Деструктор базового класса остановочный пункт
  70.     cout << "**** Деструктор базового класса остановочный пункт" << endl;
  71. }
  72. TStanciya::TStanciya(string Name, int Time) :TOstanPunkt(Name)
  73. {// Конструктор инициализатор класса станция
  74.     FTime = Time;
  75.     cout << "**** Конструктор инициализатор класса станция" << endl;
  76. }
  77. TStanciya::TStanciya()
  78. {//Конструктор по умолчанию класса станция
  79.     FName = "";
  80.     FTime = 0;
  81.     cout << "**** Конструктор по умолчанию класса станция" << endl;
  82. }
  83. TPolustanok::TPolustanok(string Name, int Time) :TOstanPunkt(Name)
  84. {//Конструктор инициализатор класса полустанок
  85.     FTime = Time;
  86.     cout << "**** Конструктор инициализатор класса полустанок" << endl;
  87. }
  88. TPolustanok::TPolustanok()
  89. {//Конструктор по умолчанию класса полустанок
  90.     FName = "";
  91.     FTime = 0;
  92.     cout << "**** Конструктор по умолчанию класса полустанок" << endl;
  93. }
  94. TStanciya::TStanciya(TStanciya &A)
  95. {//Конструктор копирования класса станция
  96.     FName = A.FName;
  97.     FTime = A.FTime;
  98.     cout << "**** Конструктор копирования класса станция" << endl;
  99. }
  100. TPolustanok& TPolustanok::operator =(const TPolustanok &A)
  101. {//Перегруженная операция = класса полустанок
  102.     FName = A.FName;
  103.     FTime = A.FTime;
  104.     cout << "**** Перегруженная операция = класса полустанок" << endl;
  105.     return *this;
  106. }
  107. TStanciya& TStanciya::operator =(const TStanciya &A)
  108. {//Перегруженная операция = класса станция
  109.     FName = A.FName;
  110.     FTime = A.FTime;
  111.     cout << "**** Перегруженная операция = класса станция" << endl;
  112.     return *this;
  113. }
  114. TPolustanok::TPolustanok(TPolustanok &A)
  115. {//Конструктор копирования класса полустанок
  116.     FName = A.FName;
  117.     FTime = A.FTime;
  118.     cout << "****Конструктор копирования класса полустанок" << endl;
  119. }
  120. void TStanciya::Input()
  121. {//Ввод сведений о станции:
  122.     cout << "Ввод сведений о станции:" << endl;
  123.     cout << "Введите имя станции: ";
  124.     cin >> FName;
  125.     cout << "Введите время: ";
  126.     cin >> FTime;
  127.     cout << "**** Перегруженный виртуальный метод Input класса станция" << endl;
  128. }
  129. void TPolustanok::Input()
  130. {//Ввод сведений о полустанке:
  131.     cout << "Ввод сведений о полустанке:" << endl;
  132.     cout << "Введите имя полустанка: ";
  133.     cin >> FName;
  134.     cout << "Введите время: ";
  135.     cin >> FTime;
  136.     cout << "**** Перегруженный виртуальный метод Input класса полустанок" << endl;
  137. }
  138. void TStanciya::Print()
  139. {// Перегруженный виртуальный метод класса станция
  140.     cout << "\t" << FName << "\t" << FTime << endl;
  141.     cout << "**** Перегруженный виртуальный метод Print класса станция" << endl;
  142. }
  143. void TPolustanok::Print()
  144. {// Перегруженный виртуальный метод Print класса полустанок
  145.     cout << "\t" << FName << "\t" << FTime << endl;
  146.     cout << "**** Перегруженный виртуальный метод Print класса полустанок" << endl;
  147. }
  148. TStanciya::~TStanciya()
  149. {//Перегруженный виртуальный Деструктор класса станция
  150.     cout << "**** Перегруженный виртуальный Деструктор класса станция" << endl;
  151. }
  152. TPolustanok::~TPolustanok()
  153. {//Перегруженный виртуальный Деструктор класса полустанок
  154.     cout << "**** Перегруженный виртуальный Деструктор класса полустанок" << endl;
  155. }
  156. //-----------------------------------------------
  157. class TRoute
  158. {
  159. private:
  160.     int FCounTStanciya; // Количество станций
  161.     int FCounTPolustanok; //Количество полустанок
  162.     TStanciya *FStanciya; // Массив станций
  163.     TPolustanok *FPolustanok;// Массив полустанок
  164. public:
  165.     TRoute();// Конструктор по умолчанию
  166.     ~TRoute();// Деструктор
  167.     void Print();
  168.     void AddStanciya(TStanciya &A);// Добавление станций
  169.     void AddPolustanok(TPolustanok &A);//Добавление полустанок
  170.     void DelStanciya(int c);//Удаление станций
  171.     void DelPolustanok(int c);//Удаление полустанок
  172.     TStanciya* GeTStanciya(int c) { return &FStanciya[c]; }
  173.     TPolustanok* GeTPolustanok(int c) { return &FPolustanok[c]; }
  174.     int GetCounTStanciya() { return FCounTStanciya; }
  175.     int GetCounTPolustanok() { return FCounTPolustanok; }
  176. };
  177. TRoute::TRoute()
  178. {//Конструктор по умолчанию класса Маршрут
  179.     FCounTStanciya = 0;
  180.     FCounTPolustanok = 0;
  181.     FStanciya = NULL;
  182.     FPolustanok = NULL;
  183.     cout << "**** Конструктор по умолчанию класса Маршрут" << endl;
  184. }
  185. void TRoute::AddStanciya(TStanciya &A)
  186. {//Метод добавления станций класса Командa
  187.     int i;
  188.     TStanciya t;
  189.     TStanciya *tmp = new TStanciya[FCounTStanciya + 1];
  190.     for (i = 0; i<FCounTStanciya; i++)
  191.         tmp[i] = FStanciya[i];
  192.     tmp[FCounTStanciya] = A;
  193.     delete[] FStanciya;
  194.     FStanciya = tmp;
  195.     FCounTStanciya++;
  196.     cout << "**** Метод добавления станций класса Маршрут" << endl;
  197. }
  198. void TRoute::AddPolustanok(TPolustanok &A)
  199. {//Метод добавления полустанок класса Маршрут
  200.     int i;
  201.     TPolustanok *tmp = new TPolustanok[FCounTPolustanok + 1];
  202.     for (i = 0; i<FCounTPolustanok; i++)
  203.         tmp[i] = FPolustanok[i];
  204.     tmp[FCounTPolustanok] = A;
  205.     delete[] FPolustanok;
  206.     FPolustanok = tmp;
  207.     FCounTPolustanok++;
  208.     cout << "**** Метод добавления полустанок класса Маршрут" << endl;
  209. }
  210. void TRoute::DelStanciya(int c)
  211. {//Метод удаления станций класса Маршрут
  212.     int i;
  213.     TStanciya *tmp = new TStanciya[FCounTStanciya - 1];
  214.     for (i = 0; i<c; i++)
  215.         tmp[i] = FStanciya[i];
  216.     for (i = c + 1; i<FCounTStanciya; i++)
  217.         tmp[i - 1] = FStanciya[i];
  218.     delete[] FStanciya;
  219.     FStanciya = tmp;
  220.     FCounTStanciya--;
  221.     cout << "**** Метод удаления станций класса Маршрут" << endl;
  222. }
  223. void TRoute::DelPolustanok(int c)
  224. {//Метод удаления полустанок класса Маршрут
  225.     int i;
  226.     TPolustanok *tmp = new TPolustanok[FCounTPolustanok - 1];
  227.     for (i = 0; i<c; i++)
  228.         tmp[i] = FPolustanok[i];
  229.     for (i = c + 1; i<FCounTPolustanok; i++)
  230.         tmp[i - 1] = FPolustanok[i];
  231.     delete[] FPolustanok;
  232.     FPolustanok = tmp;
  233.     FCounTPolustanok--;
  234.     cout << "**** Метод удаления полустанок класса Маршрут" << endl;
  235. }
  236. TRoute::~TRoute()
  237. {// Деструктор класса Маршрут
  238.     if (FStanciya != NULL)
  239.         delete[] FStanciya;
  240.     if (FPolustanok != NULL)
  241.         delete[] FPolustanok;
  242.     cout << "**** Деструктор класса Маршрут" << endl;
  243. }
  244. void TRoute::Print()
  245. {// Метод вывод сведений о маршруте класса Маршрут
  246.     int i;
  247.     cout << "станции:" << endl;
  248.     for (i = 0; i<FCounTStanciya; i++)
  249.         FStanciya[i].Print();
  250.     cout << "полустанки:" << endl;
  251.     for (i = 0; i<FCounTPolustanok; i++)
  252.         FPolustanok[i].Print();
  253.     cout << "**** Метод вывод сведений о маршруте класса Маршрут" << endl;
  254. }
  255. //-----------------------------------------------------
  256. class TProcessor
  257. {// Класс обработчик
  258. private:
  259.     TRoute* t; // Маршрут
  260.     int max1, max2, max3, imax1, imax2, imax3;
  261.     int min1, min2, min3, imin1, imin2, imin3;
  262. public:
  263.     TProcessor();// Конструктор по умолчанию
  264.     ~TProcessor();//Деструктор
  265.     void Sravnenie();
  266.     void CalculateTime();
  267.     void AddStanciya(TStanciya &A) { t->AddStanciya(A); } // Добавить станций
  268.     void AddPolustanok(TPolustanok &A)
  269.     {
  270.         t->AddPolustanok(A);
  271.     }// Добавить полустанок
  272.     void DelStanciya(int c) { t->DelStanciya(c); }//Удалить станций
  273.     void DelPolustanok(int c)
  274.     {
  275.         t->DelPolustanok(c);
  276.     }//Удалить полустанок
  277.     void Print() { t->Print(); }// Вывод сведений о маршруте
  278. };
  279. TProcessor::TProcessor()
  280. {// Конструктор по умолчанию класса Обработчик
  281.     t = new TRoute();
  282.     cout << "**** Конструктор по умолчанию класса Обработчик" << endl;
  283. }
  284. TProcessor::~TProcessor()
  285. {// Деструктор класса Обработчик
  286.     delete t;
  287.     cout << "**** Деструктор класса Обработчик" << endl;
  288. };
  289. void TProcessor::Sravnenie()
  290. {
  291.     cout << "Какие маршруты сравнить?" << endl;
  292.     int one, two;
  293.     cout << "Введите первый: ";
  294.     cin >> one;
  295.     cout << "Введите второй: ";
  296.     cin >> two;
  297.  
  298.     cout << "Сравнение станций" << endl;
  299.     if (t->GeTStanciya(one - 1)->GetTime() == t->GeTStanciya(two - 1)->GetTime())
  300.         cout << "Маршруты " << one << " & " << two << " равны" << endl;
  301.     else
  302.         cout << "Маршруты " << one << " & " << two << " НЕ равны" << endl;
  303.  
  304.     cout << "Сравнение полустанок" << endl;
  305.     if (t->GeTPolustanok(one - 1)->GetTime() == t->GeTPolustanok(two - 1)->GetTime())
  306.         cout << "Маршруты " << one << " & " << two << " равны" << endl;
  307.     else
  308.         cout << "Маршруты " << one << " & " << two << " НЕ равны" << endl;
  309. }
  310. void TProcessor::CalculateTime()
  311. {
  312.     int time = 0;
  313.     for (int i = 0; i<t->GetCounTStanciya(); i++)
  314.         time += t->GeTStanciya(i)->GetTime();      
  315.     for (int i = 0; i<t->GetCounTPolustanok(); i++)
  316.         time += t->GeTPolustanok(i)->GetTime();
  317.     cout << "Общее время для прохождения маршрута = " << time << endl;
  318. }
  319.  
  320. int main()
  321. {
  322.     setlocale(LC_ALL, "Rus");
  323.     int CounTStanciya, CounTPolustanok;
  324.     int i;
  325.     cout << "Введите количество станций: ";
  326.     cin >> CounTStanciya;
  327.     cout << "Введите количество полустанок: ";
  328.     cin >> CounTPolustanok;
  329.     TStanciya *A = new TStanciya[CounTStanciya];
  330.     for (i = 0; i<CounTStanciya; i++)
  331.         A[i].Input();
  332.     TPolustanok *B = new TPolustanok[CounTPolustanok];
  333.     for (i = 0; i<CounTPolustanok; i++)
  334.         B[i].Input();
  335.     // Работа с классом Обработчик
  336.     cout << "\n Работа с классом Обработчик" << endl;
  337.     TProcessor *p = new TProcessor;
  338.     for (i = 0; i<CounTStanciya; i++)
  339.         p->AddStanciya(A[i]);
  340.     for (i = 0; i<CounTPolustanok; i++)
  341.         p->AddPolustanok(B[i]);
  342.     cout << " Все Маршруты: \n";
  343.     p->Print();
  344.     p->Sravnenie();
  345.     p->CalculateTime();
  346.     system("pause");
  347.     delete[] A;
  348.     delete[] B;
  349.     delete p;
  350.     // Работа с массивом указателей на тип базового класса
  351.     cout << "\n Работа с массивом указателей на тип базового класса\n" << endl;
  352.     TOstanPunkt **array_route;
  353.     array_route = new TOstanPunkt*[CounTStanciya + CounTPolustanok];
  354.     for (i = 0; i<CounTStanciya; i++)
  355.         array_route[i] = new TStanciya;
  356.     for (i = CounTStanciya; i<CounTStanciya + CounTPolustanok; i++)
  357.         array_route[i] = new TPolustanok;
  358.     cout << "\n Ввод сведений о маршруте:\n" << endl;
  359.     for (i = 0; i<CounTStanciya + CounTPolustanok; i++)
  360.         array_route[i]->Input();
  361.     cout << "\n В маршруте присутствуют следующие остановки:\n" << endl;
  362.     for (i = 0; i<CounTStanciya + CounTPolustanok; i++)
  363.         array_route[i]->Print();
  364.     for (i = 0; i<CounTStanciya + CounTPolustanok; i++)
  365.         delete array_route[i];
  366.     delete[] array_route;
  367.     system("pause");
  368.     return 0;
  369. }
Advertisement
Add Comment
Please, Sign In to add comment