Xom9ik

Lab_9_Task2/6 var (IIl semester)

Nov 26th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.38 KB | None | 0 0
  1. //Lab_9_Task1_var6
  2. #include "stdafx.h"
  3. #include <string>
  4. #include <iostream>
  5. #include <locale>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9. //6.  Классы маршрут и остановка.
  10. //Класс обработчик реализует сравнение маршрутов на идентичность,
  11. //расчет времени прохождения всего маршрута.
  12.  
  13. //Класс-остановка
  14. class TStation
  15. {
  16. private:
  17.     string Name;
  18.     int Time;
  19. public:
  20.     TStation();
  21.     int getTime()
  22.     {
  23.         return Time;
  24.     }
  25.     string getName()
  26.     {
  27.         return Name;
  28.     }
  29.     void setTime(int time)
  30.     {
  31.         Time = time;
  32.     }
  33.     void setName(string name)
  34.     {
  35.         Name = name;
  36.     }
  37.     ~TStation();
  38. };
  39. TStation::TStation()
  40. {
  41.     cout << "Constructor class TStation" << endl;
  42. }
  43. TStation::~TStation()
  44. {
  45.     cout << "Destructor class TStation" << endl;
  46. }
  47. //Класс-маршрут
  48. class TRoute
  49. {
  50. public:
  51.     TRoute();
  52.     int Max;
  53.     int CurrentPos = 0;
  54.     TStation *Spointer;
  55.  
  56.     void setMaxAndPointer(TStation *S, int max)
  57.     {
  58.         Max = max;
  59.         Spointer = S;
  60.     }
  61.     int getMax()
  62.     {
  63.         return Max;
  64.     }
  65.     void addStation();
  66.     ~TRoute();
  67. };
  68. TRoute::TRoute()
  69. {
  70.     cout << "Constructor class TRoute" << endl;
  71. }
  72. TRoute::~TRoute()
  73. {
  74.     cout << "Destructor class TRoute" << endl;
  75. }
  76. void TRoute::addStation()
  77. {
  78.     string name;
  79.     int time;
  80.     cout << "Enter name: ";
  81.     cin >> name;
  82.     Spointer[CurrentPos].setName(name);
  83.     cout << "Enter time: ";
  84.     cin >> time;
  85.     Spointer[CurrentPos].setTime(time);
  86.     CurrentPos++;
  87. }
  88. class TabularOutput
  89. {//табличный вывод информации в файл и на экран
  90. public:
  91.     virtual void Print(TRoute *T) = 0;
  92.  
  93. };
  94. class RibbonOutput
  95. {//вывод информации в ленточной форме в файл и на экран
  96. public:
  97.     virtual void Print(TRoute *T) = 0;
  98.  
  99. };
  100. class TabularOutput_helper : public TabularOutput {
  101. public:
  102.     virtual void TabularOutput_Print(TRoute *T) = 0;
  103.     void Print(TRoute *T)
  104.     {
  105.         TabularOutput_Print(T);
  106.     }
  107. };
  108. class RibbonOutput_helper : public RibbonOutput {
  109. public:
  110.     virtual void RibbonOutput_Print(TRoute *T) = 0;
  111.     void Print(TRoute *T)
  112.     {
  113.         RibbonOutput_Print(T);
  114.     }
  115. };
  116. //Класс-обработчик
  117. class TProcessor : public TabularOutput_helper, public RibbonOutput_helper
  118. {
  119. public:
  120.     TProcessor();
  121.     TProcessor(TRoute *R);
  122.     TRoute *Spointer;
  123.     int CalculateTime(int routeNumber);
  124.     void IdentityCheck(int routeNumber1, int routeNumber2);
  125.     void TabularOutput_Print(TRoute *T)
  126.     {
  127.         string out = "";
  128.         cout << "Tabular Output" << endl;
  129.         out += "Tabular Output\n";
  130.         cout << "Name\t" << "Time\t" << endl;
  131.         out += "Name\tTime\n";
  132.         for (int i = 0; i < T[0].CurrentPos; i++)
  133.         {
  134.             out += T[0].Spointer[i].getName() + "\t";
  135.             out += to_string(T[0].Spointer[i].getTime()) + "\n";
  136.             cout << T[0].Spointer[i].getName() << "\t" << T[0].Spointer[i].getTime() << endl;
  137.         }
  138.         ofstream fout;
  139.         fout.open("Tabular.txt");
  140.         fout << out;
  141.         fout.close();
  142.     }
  143.     void RibbonOutput_Print(TRoute *T)
  144.     {
  145.         string out = "";
  146.         out += "Ribbon Output\n";
  147.         cout << "Ribbon Output" << endl;
  148.         cout << "Name: ";
  149.         out += "Name: ";
  150.         for (int i = 0; i < T[0].CurrentPos; i++)
  151.         {
  152.             out += T[0].Spointer[i].getName() + ", ";
  153.             cout << T[0].Spointer[i].getName() << ", ";
  154.         }
  155.         cout << endl;
  156.         cout << "Time: ";
  157.         out += "\nTime: ";
  158.         for (int i = 0; i < T[0].CurrentPos; i++)
  159.         {
  160.             out += T[0].Spointer[i].getTime() + ", ";
  161.             cout << T[0].Spointer[i].getTime() << ", ";
  162.         }
  163.         cout << endl;
  164.         out += "\n";
  165.         ofstream fout;
  166.         fout.open("Ribbon.txt");
  167.         fout << out;
  168.         fout.close();
  169.     }
  170.     ~TProcessor();
  171. };
  172. int TProcessor::CalculateTime(int routeNumber)
  173. {
  174.     int summ = 0;
  175.     cout << "Travel time for route " << routeNumber << ": ";
  176.     routeNumber -= 1;
  177.     for (int i = 0; i < Spointer[routeNumber].getMax(); i++)
  178.     {
  179.         summ += Spointer[routeNumber].Spointer[i].getTime();
  180.         if (i<Spointer[routeNumber].getMax() - 1)
  181.             cout << Spointer[routeNumber].Spointer[i].getTime() << " + ";
  182.         else
  183.             cout << Spointer[routeNumber].Spointer[i].getTime() << " = ";
  184.     }
  185.     cout << summ << endl;
  186.     return summ;
  187. }
  188. void TProcessor::IdentityCheck(int routeNumber1, int routeNumber2)
  189. {
  190.     int one = CalculateTime(routeNumber1);
  191.     int two = CalculateTime(routeNumber2);
  192.     cout << "Time for the first route: " << one << endl;
  193.     cout << "Time for the second route: " << two << endl;
  194.     if (one == two)
  195.     {
  196.         cout << "Time for the first and second routes are the same: " << one << " min" << endl;
  197.     }
  198.     else if (one > two)
  199.     {
  200.         cout << "The time for the second route is less for: " << one - two << " min" << endl;
  201.     }
  202.     else if (one < two)
  203.     {
  204.         cout << "The time for the first route is less for: " << two - one << " min" << endl;
  205.     }
  206.  
  207. }
  208. TProcessor::TProcessor()
  209. {
  210.     cout << "Constructor class TProcessor" << endl;
  211. };
  212. TProcessor::TProcessor(TRoute *R)
  213. {
  214.     Spointer = R;
  215. }
  216. TProcessor::~TProcessor()
  217. {
  218.     cout << "Destructor class TProcessor" << endl;
  219. };
  220.  
  221. int main()
  222. {
  223.     int countRoute = 0, countStation = 0, sortRow = 0;
  224.     string name = "";
  225.     int year = 0;
  226.     cout << "Enter the number of route: ";
  227.     cin >> countRoute;
  228.     TRoute *route = new TRoute[countRoute];
  229.     //TRoute route(station, countStation);
  230.     TProcessor processor(route);
  231.     TabularOutput* TO = &processor;
  232.     RibbonOutput* RO = &processor;
  233.     for (int i = 0; i < countRoute; i++)
  234.     {
  235.         cout << "Enter the number of station: ";
  236.         cin >> countStation;
  237.         TStation *station = new TStation[countStation];
  238.         route[i].setMaxAndPointer(station, countStation);
  239.         for (int j = 0; j < countStation; j++)
  240.         {
  241.             route[i].addStation();
  242.         }
  243.         RO->Print(route);
  244.     }  
  245.     while (1)
  246.     {
  247.         int option;
  248.         cout << "1.Calculation of transit time" << endl;
  249.         cout << "2.Comparison of identity" << endl;
  250.         cout << "3.Exit" << endl;
  251.         cin >> option;
  252.         switch (option)
  253.         {
  254.         case 1:
  255.         {
  256.             TO->Print(route);
  257.             int routeNumber;
  258.             cout << "For which route is the time calculated: (1-" << countRoute << ")" << endl;
  259.             cin >> routeNumber;
  260.             processor.CalculateTime(routeNumber);
  261.             break;
  262.         }
  263.         case 2:
  264.         {
  265.             int routeNumber1, routeNumber2;
  266.             cout << "Which routes to compare: (1-" << countRoute << ")" << endl;
  267.             cout << "Enter the first route to compare: ";
  268.             cin >> routeNumber1;
  269.             cout << "Enter the second route to compare: ";
  270.             cin >> routeNumber2;
  271.             processor.IdentityCheck(routeNumber1, routeNumber2);
  272.             break;
  273.         }
  274.         case 3:
  275.         {
  276.             exit(0);
  277.             break;
  278.         }
  279.         }
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment