Advertisement
RobertDeMilo

Декомпозиция и отладка кода (мое решение)

Nov 6th, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.54 KB | None | 0 0
  1. #include <cassert>
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5. #include <vector>
  6. #include <sstream>
  7. #include <fstream>
  8.  
  9. using namespace std;
  10.  
  11. //string path = "C:/Users/musta/Downloads/nova.txt";
  12. //fstream f(path);
  13.  
  14. enum class QueryType
  15. {
  16.     NewBus,
  17.     BusesForStop,
  18.     StopsForBus,
  19.     AllBuses,
  20. };
  21.  
  22. struct Query
  23. {
  24.     QueryType type;
  25.     string bus;
  26.     string stop;
  27.     vector<string> stops;
  28. };
  29.  
  30. istream& operator>>(istream& is, QueryType& query)
  31. {
  32.     string type;
  33.     is >> type;
  34.  
  35.     if (type == "NEW_BUS")
  36.     {
  37.         query = QueryType::NewBus;
  38.     }
  39.     else if (type == "BUSES_FOR_STOP")
  40.     {
  41.         query = QueryType::BusesForStop;
  42.     }
  43.     else if (type == "STOPS_FOR_BUS")
  44.     {
  45.         query = QueryType::StopsForBus;
  46.     }
  47.     else
  48.     {
  49.         query = QueryType::AllBuses;
  50.     }
  51.  
  52.     return is;
  53. }
  54.  
  55. istream& operator>>(istream& is, vector<string>& q)
  56. {
  57.     int count = 0;
  58.     cin >> count;
  59.     q.resize(count);
  60.  
  61.     for (int i = 0; i < count; ++i)
  62.     {
  63.         is >> q[i];
  64.     }
  65.  
  66.     return is;
  67. }
  68.  
  69. istream& operator>>(istream& is, Query& q)
  70. {
  71.  
  72.     is >> q.type;
  73.  
  74.     switch (q.type)
  75.     {
  76.     case QueryType::NewBus:
  77.         is >> q.bus;
  78.         is >> q.stops;
  79.         break;
  80.  
  81.     case QueryType::BusesForStop:
  82.         is >> q.stop;
  83.         break;
  84.  
  85.     case QueryType::StopsForBus:
  86.         is >> q.bus;
  87.         break;
  88.  
  89.     default:
  90.         break;
  91.     }
  92.  
  93.     return is;
  94. }
  95.  
  96. struct BusesForStopResponse
  97. {
  98.     string response;
  99. };
  100.  
  101. ostream& operator<<(ostream& os, const BusesForStopResponse& r)
  102. {
  103.    
  104.     istringstream ir(r.response);
  105.     bool first = true;
  106.     for (string str; getline(ir, str, ' ');)
  107.     {
  108.         if (first)
  109.         {
  110.             os << str;
  111.             first = false;
  112.         }
  113.         else
  114.         {
  115.             os << " " << str;
  116.         }
  117.        
  118.     }
  119.  
  120.     return os;
  121. }
  122.  
  123.  
  124. struct StopsForBusResponse
  125. {
  126.     vector <string> response;
  127. };
  128.  
  129. ostream& operator<<(ostream& os, const StopsForBusResponse& r)
  130. {
  131.     int count = 1;
  132.     for (const auto& text : r.response)
  133.     {
  134.         istringstream ir(text);
  135.         bool first = true;
  136.         for (string str; getline(ir, str, ' ');)
  137.         {
  138.             if (first)
  139.             {
  140.                 os << str;
  141.                 first = false;
  142.             }
  143.  
  144.             else
  145.             {
  146.                 os << " " << str;
  147.             }
  148.  
  149.         }
  150.  
  151.         if (count < r.response.size())
  152.         {
  153.             os << endl;
  154.             ++count;
  155.         }
  156.     }
  157.    
  158.     return os;
  159. }
  160.  
  161. struct AllBusesResponse
  162. {
  163.     // Наполните полями эту структуру
  164.     vector <string> response;
  165. };
  166.  
  167. ostream& operator<<(ostream& os, const AllBusesResponse& r)
  168. {
  169.     int count = 1;
  170.     for (const auto& text : r.response)
  171.     {
  172.         istringstream ir(text);
  173.         bool first = true;
  174.  
  175.         for (string str; getline(ir, str, ' ');)
  176.         {
  177.             if (first)
  178.             {
  179.                 os << str;
  180.                 first = false;
  181.             }
  182.             else
  183.             {
  184.                 os << " " << str;
  185.             }
  186.  
  187.         }
  188.        
  189.         if (count < r.response.size())
  190.         {
  191.             os << endl;
  192.             ++count;
  193.         }
  194.     }
  195.  
  196.     return os;
  197. }
  198.  
  199. class BusManager
  200. {
  201. public:
  202.     void AddBus(const string& bus, const vector<string>& stops)
  203.     {
  204.         buses_to_stops[bus] = stops;
  205.  
  206.         for (const string& stop : stops) {
  207.  
  208.             stops_to_buses[stop].push_back(bus);
  209.         }
  210.     }
  211.  
  212.     BusesForStopResponse GetBusesForStop(const string& stop) const
  213.     {
  214.         BusesForStopResponse bs;
  215.         stringstream ss;
  216.  
  217.         if (stops_to_buses.count(stop) == 0) {
  218.             ss << "No stop"s;
  219.         }
  220.         else {
  221.             bool is_first = true;
  222.             for (const string& bus : stops_to_buses.at(stop))
  223.             {
  224.                 if (!is_first)
  225.                 {
  226.                     ss << " "s;
  227.                 }
  228.                 is_first = false;
  229.                 ss << bus;
  230.             }
  231.         }
  232.         bs.response = ss.str();
  233.         return bs;
  234.     }
  235.  
  236.     StopsForBusResponse GetStopsForBus(const string& bus) const
  237.     {
  238.         StopsForBusResponse rs;
  239.         vector<string> v;
  240.         stringstream ss;
  241.  
  242.         if (buses_to_stops.count(bus) == 0)
  243.         {
  244.             ss << "No bus"s;
  245.             v.push_back(ss.str());
  246.         }
  247.         else {
  248.             for (const string& stop : buses_to_stops.at(bus))
  249.             {
  250.                 ss << "Stop "s << stop << ":"s;
  251.  
  252.                 if (stops_to_buses.at(stop).size() == 1)
  253.                 {
  254.                     ss << " no interchange"s;
  255.                 }
  256.                 else {
  257.                     for (const string& other_bus : stops_to_buses.at(stop))
  258.                     {
  259.                         if (bus != other_bus)
  260.                         {
  261.                             ss << " "s << other_bus;
  262.                         }
  263.                     }
  264.                 }
  265.            
  266.                 v.push_back(ss.str());
  267.                 ss.str(string());
  268.  
  269.             }
  270.         }
  271.         rs.response = v;
  272.         return rs;
  273.     }
  274.  
  275.     AllBusesResponse GetAllBuses() const
  276.     {
  277.         AllBusesResponse ar;
  278.         vector<string> result;
  279.         stringstream ss;
  280.  
  281.         if (buses_to_stops.empty())
  282.         {
  283.             ss << "No buses"s;
  284.             result.push_back(ss.str());
  285.         }
  286.         else
  287.         {
  288.             for (const auto& bus_item : buses_to_stops)
  289.             {
  290.                 ss << "Bus "s << bus_item.first << ":"s;
  291.  
  292.                 for (const string& stop : bus_item.second)
  293.                 {
  294.                     ss << " "s << stop;
  295.                 }
  296.                
  297.                 result.push_back(ss.str());
  298.                 ss.str(string());
  299.             }
  300.         }
  301.         ar.response = result;
  302.         return ar;
  303.     }
  304. private:
  305.     map<string, vector<string>> buses_to_stops;  // автобус и остановки через которые он проходит
  306.     map<string, vector<string>> stops_to_buses;  // остановка и какие автобусы проходят, через эту остановку
  307. };
  308.  
  309. // Реализуйте функции и классы, объявленные выше, чтобы эта функция main
  310. // решала задачу "Автобусные остановки"
  311.  
  312. int main() {
  313.  
  314.     int query_count;
  315.     Query q;
  316.  
  317.     cin >> query_count;
  318.  
  319.     BusManager bm;
  320.  
  321.     for (int i = 0; i < query_count; ++i) {
  322.         cin >> q;
  323.         switch (q.type)
  324.         {
  325.         case QueryType::NewBus:
  326.             bm.AddBus(q.bus, q.stops);
  327.             break;
  328.  
  329.         case QueryType::BusesForStop:
  330.             cout << bm.GetBusesForStop(q.stop) << endl;
  331.             break;
  332.  
  333.         case QueryType::StopsForBus:
  334.             cout << bm.GetStopsForBus(q.bus) << endl;
  335.             break;
  336.  
  337.         case QueryType::AllBuses:
  338.             cout << bm.GetAllBuses() << endl;
  339.             break;
  340.         }
  341.     }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement