makrusak

t1

Mar 6th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <map>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. class TransportDB
  9. {
  10. protected:
  11.         enum trType {bus, tbus, tram};
  12.         struct RouteName
  13.         {
  14.                 int num; char let;
  15.                 RouteName(int a, char l=0) {num=a; let=l;}
  16.                 RouteName(){}
  17.                 bool operator < (RouteName& oth)
  18.                 {
  19.                         return num<oth.num;
  20.                 }
  21.                 bool operator == (RouteName& oth)
  22.                 {
  23.                         return (num == oth.num && let == oth.let);
  24.                 }
  25.         };
  26.         struct Route;
  27.         struct Stop
  28.         {
  29.                 vector<Route*> routes;
  30.                 string name;
  31.                 Stop (string n)
  32.                 {
  33.                         name=n;
  34.                 }
  35.         };
  36.         struct Route
  37.         {
  38.                 trType curType;
  39.                 RouteName name;
  40.                 vector<Stop*> stops;
  41.                 Route (RouteName rou, int cur)
  42.                 {
  43.                         name = rou;
  44.                         if (cur == 0)
  45.                                 curType = bus;
  46.                         if (cur == 1)
  47.                                 curType = tbus;
  48.                         if (cur == 2)
  49.                                 curType = tram;
  50.                 }
  51.         };
  52.         vector<Route*> routes;
  53.         vector<Stop*> stops;
  54. public:
  55.         friend class Interface;
  56. };
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. class Interface
  65. {
  66. private:
  67.         TransportDB* pointer;
  68.         int findStop(string stopName)
  69.         {
  70.                 for (int i = 0; i < pointer->stops.size(); i++)
  71.                         if (pointer->stops[i]->name == stopName)
  72.                                 return i;
  73.                 return -1;
  74.         }
  75.  
  76.         /////////////////////////////////////////////
  77.         TransportDB::RouteName convertStrToRName (string rName)
  78.         {
  79.                 int c=0, num=0;
  80.                 while (c < rName.size() && rName[c] >= '0' && rName[c] <= '9')
  81.                 {
  82.                         num = num * 10 + rName[c]-'0';
  83.                         c++;
  84.                 }
  85.                 if (((int)rName.size()-c) >= 2)
  86.                 {
  87.                         cout << "Wrong route name!" << endl;
  88.                         return TransportDB::RouteName(0, 0);
  89.                 }
  90.                 char symb = 0;
  91.                 if (c == (int)rName.size() - 1)
  92.                         symb = rName[c];
  93.                 TransportDB::RouteName retName(num, symb);
  94.                 return retName;
  95.         }
  96.  
  97.  
  98.  
  99.         int findRoute(string routeName, int trType)
  100.         {
  101.                 TransportDB::RouteName rName = convertStrToRName(routeName);
  102.                 if (rName.num==0) return -1;
  103.                 for (int i = 0; i < pointer->routes.size(); i++)
  104.                         if (pointer->routes[i]->name == rName && pointer->routes[i]->curType == trType)
  105.                         return i;
  106.                 return -1;
  107.         }
  108.  
  109.  
  110.  
  111.  
  112.         void disjoinRouteStopPoint (TransportDB::Route* route,TransportDB::Stop* stop)
  113.         {
  114.                 int pos = -1;
  115.                 for (int i=0; i<route->stops.size(); i++)
  116.                 {
  117.                         if (stop == route->stops[i])
  118.                         {
  119.                                 pos = i;
  120.                                 break;
  121.                         }
  122.                 }
  123.                 if (pos == -1) return;
  124.                 route->stops[pos] = route->stops.back();
  125.                 route->stops.pop_back();
  126.                 int posS = -1;
  127.                 for (int i=0; i < stop->routes.size(); i++)
  128.                         if (route == stop->routes[i])
  129.                         {
  130.                                 posS = i;
  131.                                 break;
  132.                         }
  133.                 if (posS == -1) return;
  134.                 stop->routes[posS] = stop->routes.back();
  135.                 stop->routes.pop_back();
  136.         }
  137.  
  138.  
  139.         void addRouteStopPoint (TransportDB::Route* route, TransportDB::Stop* stop)
  140.         {
  141.                 route->stops.push_back(stop);
  142.                 stop->routes.push_back(route);
  143.         }
  144.  
  145.  
  146.  
  147. public:
  148.         Interface()
  149.         {
  150.                 pointer = new TransportDB();
  151.         }
  152.         Interface(TransportDB* getpointer)
  153.         {
  154.                 pointer = getpointer;
  155.         }
  156.  
  157.  
  158.  
  159.         void addRouteStop (string rName, int trType, string sName)
  160.         {
  161.                 int posR = findRoute(rName, trType);
  162.                 int posS = findStop(sName);
  163.                 if (posR != -1 && posS != -1)
  164.                   addRouteStopPoint(pointer->routes[posR], pointer->stops[posS]);
  165.                 else cout << "No such route or stop to add!" << endl;
  166.         }
  167.  
  168.  
  169.  
  170.         void disjoinRouteStop (string rName, int trType, string sName)
  171.         {
  172.                 int posR = findRoute(rName, trType);
  173.                 int posS = findStop(sName);
  174.                 if (posR != -1 && posS != -1)
  175.                   disjoinRouteStopPoint(pointer->routes[posR], pointer->stops[posS]);
  176.                 else cout << "No such route or stop to remove!" << endl;
  177.         }
  178.  
  179.     void addStop(string stopName)
  180.         {
  181.                 TransportDB::Stop* firStop = new TransportDB::Stop (stopName);
  182.                 pointer->stops.push_back(firStop);
  183.         }
  184.         void addRoute(string routeName, int trType)
  185.         {      
  186.                 TransportDB::RouteName rName = convertStrToRName(routeName);
  187.                 if (rName.num==0) return;
  188.                 TransportDB::Route* firRoute = new TransportDB::Route(rName, trType);
  189.                 pointer->routes.push_back(firRoute);
  190.         }
  191.  
  192.         /////////////////////////////////////
  193.     void deleteRoute(string rName, int trType)
  194.         {
  195.                 int pos = findRoute(rName, trType);
  196.                 if (pos == -1)
  197.                         return;
  198.                 TransportDB::Route* cur = pointer->routes[pos];
  199.                 while (!cur->stops.empty())
  200.                         disjoinRouteStopPoint(cur, cur->stops[0]);
  201.                 delete pointer->routes[pos];
  202.                 pointer->routes[pos] = pointer->routes.back();
  203.                 pointer->routes.pop_back();
  204.         }
  205.  
  206.     void deleteStop(string stopName)
  207.         {
  208.                 int pos = findStop(stopName);
  209.                 if (pos == -1)
  210.                         return;
  211.                 TransportDB::Stop* cur = pointer->stops[pos];
  212.                 while (!cur->routes.empty())
  213.                         disjoinRouteStopPoint(cur->routes[0], cur);
  214.                 delete pointer->stops[pos];
  215.                 pointer->stops[pos] = pointer->stops.back();
  216.                 pointer->stops.pop_back();    
  217.         }
  218.         int routesCount()
  219.         {
  220.                 return pointer->routes.size();
  221.         }
  222.  
  223.     void printDB() {
  224.       cout << "Routes:\n";
  225.       for (int i=0;i<pointer->routes.size();i++) {
  226.         auto cur = pointer->routes[i];
  227.         cout << cur->name.num;
  228.         if (cur->name.let!=0) cout << cur->name.let;
  229.         cout << "\n";
  230.         if (cur->curType==0) cout << "  Bus";
  231.         else if (cur->curType==1) cout << "  Tbus";
  232.         else cout << "  Tram";
  233.         cout << "\n";
  234.         cout << "  st:  ";
  235.         for (int j=0;j<cur->stops.size();j++) {
  236.           cout << cur->stops[j]->name << " ";
  237.         }
  238.         cout << "\n";
  239.  
  240.       }
  241.       cout << "\n";
  242.       cout << "Stops\n";
  243.       for (int i=0;i<pointer->stops.size();i++) {
  244.         auto cur = pointer->stops[i];
  245.         cout << cur->name << "\n";
  246.         cout << "  ro:  ";
  247.         for (int j=0;j<cur->routes.size();j++) {
  248.           auto curr = cur->routes[j];
  249.           cout << curr->name.num;
  250.           if (curr->name.let!=0) cout << curr->name.let;
  251.           if (curr->curType==0) cout << ":Bus";
  252.           else if (curr->curType==1) cout << ":Tbus";
  253.           else cout << ":Tram";
  254.           cout << " ";
  255.         }
  256.         cout << "\n";
  257.  
  258.       }
  259.     }
  260.  
  261. };
  262.  
  263.  
  264. int main ()
  265. {
  266.         Interface a;
  267.         for (;;) {
  268.           int c;
  269.           cin >> c;
  270.           if (c == 0) {
  271.             string s; cin >> s;
  272.             a.addStop(s);
  273.           }
  274.           else if (c == 1) {
  275.             string s; cin >> s;
  276.             a.deleteStop(s);
  277.           }
  278.           else if (c == 2) {
  279.             string s; int q; cin >> s >> q;
  280.             a.addRoute(s, q);
  281.           }
  282.           else if (c == 3) {
  283.             string s; int q; cin >> s >> q;
  284.             a.deleteRoute(s, q);
  285.           }
  286.           else if (c == 4) {
  287.             string s1, s2; int q; cin >> s1 >> q >>  s2;
  288.             a.addRouteStop(s1, q,s2);
  289.           }
  290.           else {
  291.             string s1, s2; int q; cin >> s1 >> q >> s2;
  292.             a.disjoinRouteStop(s1, q, s2);
  293.           }
  294.           a.printDB();
  295.         }
  296.         return 0;
  297. }
Advertisement
Add Comment
Please, Sign In to add comment