Advertisement
Xom9ik

Lab_2/6var (IIl semester)

Oct 20th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <iostream>
  4. #include <locale>
  5.  
  6. using namespace std;
  7. //6.  Классы маршрут и остановка.
  8. //Класс обработчик реализует сравнение маршрутов на идентичность,
  9. //расчет времени прохождения всего маршрута.
  10.  
  11. //Класс-остановка
  12. class TStation
  13. {
  14. private:
  15.     string Name;
  16.     int Time;
  17. public:
  18.     TStation();
  19.     int getTime()
  20.     {
  21.         return Time;
  22.     }
  23.     string getName()
  24.     {
  25.         return Name;
  26.     }
  27.     void setTime(int time)
  28.     {
  29.         Time = time;
  30.     }
  31.     void setName(string name)
  32.     {
  33.         Name = name;
  34.     }
  35.     ~TStation();
  36. };
  37. TStation::TStation()
  38. {
  39.     cout << "Constructor class TStation" << endl;
  40. }
  41. TStation::~TStation()
  42. {
  43.     cout << "Destructor class TStation" << endl;
  44. }
  45. //Класс-маршрут
  46. class TRoute
  47. {  
  48. public:
  49.     TRoute();
  50.     int Max;
  51.     int CurrentPos = 0;
  52.     TStation *Spointer;
  53.    
  54.     void setMaxAndPointer(TStation *S, int max)
  55.     {
  56.         Max = max;
  57.         Spointer = S;
  58.     }
  59.     int getMax()
  60.     {
  61.         return Max;
  62.     }
  63.     void addStation();
  64.     void print();
  65.     ~TRoute();
  66. };
  67. TRoute::TRoute()
  68. {
  69.     cout << "Constructor class TRoute" << endl;
  70. }
  71. TRoute::~TRoute()
  72. {
  73.     cout << "Destructor class TRoute" << endl;
  74. }
  75. void TRoute::addStation()
  76. {
  77.     string name;
  78.     int time;
  79.     cout << "Enter name: ";
  80.     cin >> name;
  81.     Spointer[CurrentPos].setName(name);
  82.     cout << "Enter time: ";
  83.     cin >> time;
  84.     Spointer[CurrentPos].setTime(time);
  85.     CurrentPos++;
  86. }
  87. void TRoute::print()
  88. {
  89.     cout << "Name\t" << "Time\t" << endl;
  90.     for (int i = 0; i < CurrentPos; i++)
  91.     {
  92.         cout << Spointer[i].getName() << "\t" << Spointer[i].getTime() << endl;
  93.     }
  94. }
  95. //Класс-обработчик
  96. class TProcessor
  97. {
  98. public:
  99.     TProcessor();
  100.     TProcessor(TRoute *R);
  101.     TRoute *Rpointer;
  102.     int CalculateTime(int routeNumber);
  103.     void IdentityCheck(int routeNumber1, int routeNumber2);
  104.     ~TProcessor();
  105. };
  106. int TProcessor::CalculateTime(int routeNumber)
  107. {
  108.     int summ = 0;
  109.     cout << "Travel time for route " << routeNumber << ": ";
  110.     routeNumber -= 1;
  111.     for (int i = 0; i < Rpointer[routeNumber].getMax(); i++)
  112.     {
  113.         summ += Rpointer[routeNumber].Spointer[i].getTime();
  114.         if(i<Rpointer[routeNumber].getMax()-1)
  115.             cout << Rpointer[routeNumber].Spointer[i].getTime() << " + ";
  116.         else
  117.             cout << Rpointer[routeNumber].Spointer[i].getTime() << " = ";
  118.     }
  119.     cout << summ << endl;
  120.     return summ;
  121. }
  122. void TProcessor::IdentityCheck(int routeNumber1, int routeNumber2)
  123. {
  124.     int one = CalculateTime(routeNumber1);
  125.     int two = CalculateTime(routeNumber2);
  126.     cout << "Time for the first route: " << one << endl;
  127.     cout << "Time for the second route: " << two << endl;
  128.     if (one == two)
  129.     {
  130.         cout << "Time for the first and second routes are the same: " << one << " min" << endl;
  131.     }
  132.     else if (one > two)
  133.     {
  134.         cout << "The time for the second route is less for: " << one-two << " min" << endl;
  135.     }
  136.     else if (one < two)
  137.     {
  138.         cout << "The time for the first route is less for: " << two - one << " min" << endl;
  139.     }
  140.  
  141. }
  142. TProcessor::TProcessor()
  143. {
  144.     cout << "Constructor class TProcessor" << endl;
  145. };
  146. TProcessor::TProcessor(TRoute *R)
  147. {
  148.     Rpointer = R;
  149. }
  150. TProcessor::~TProcessor()
  151. {
  152.     cout << "Destructor class TProcessor" << endl;
  153. };
  154.  
  155. int main()
  156. {
  157.     int countRoute = 0, countStation = 0, sortRow = 0;
  158.     string name = "";
  159.     int year = 0;
  160.     cout << "Enter the number of route: ";
  161.     cin >> countRoute;     
  162.     TRoute *route = new TRoute[countRoute];
  163.     //TRoute route(station, countStation);
  164.  
  165.     for (int i = 0; i < countRoute; i++)
  166.     {
  167.         cout << "Enter the number of station: ";
  168.         cin >> countStation;
  169.         TStation *station = new TStation[countStation];
  170.         route[i].setMaxAndPointer(station, countStation);
  171.         for (int j = 0; j < countStation; j++)
  172.         {
  173.             route[i].addStation();
  174.         }
  175.         route[i].print();
  176.     }
  177.  
  178.  
  179.     TProcessor processor(route);
  180.     while (1)
  181.     {
  182.         int option;
  183.         cout << "1.Calculation of transit time" << endl;
  184.         cout << "2.Comparison of identity" << endl;
  185.         cout << "3.Exit" << endl;
  186.         cin >> option;
  187.         switch (option)
  188.         {
  189.         case 1:
  190.         {
  191.             int routeNumber;
  192.             cout << "For which route is the time calculated: (1-" << countRoute << ")" << endl;
  193.             cin >> routeNumber;
  194.             processor.CalculateTime(routeNumber);
  195.             break;
  196.         }
  197.         case 2:
  198.         {
  199.             int routeNumber1, routeNumber2;
  200.             cout << "Which routes to compare: (1-" << countRoute << ")" << endl;
  201.             cout << "Enter the first route to compare: ";
  202.             cin >> routeNumber1;
  203.             cout << "Enter the second route to compare: ";
  204.             cin >> routeNumber2;   
  205.             processor.IdentityCheck(routeNumber1, routeNumber2);
  206.             break;
  207.         }
  208.         case 3:
  209.         {
  210.             exit(0);
  211.             break;
  212.         }
  213.         }
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement