Advertisement
Guest User

decomposuition

a guest
Dec 11th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <cassert>
  4. #include <vector>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. enum class QueryType {
  10.   NewBus,
  11.   BusesForStop,
  12.   StopsForBus,
  13.   AllBuses
  14. };
  15.  
  16. struct Query {
  17.   QueryType type;
  18.   string bus;
  19.   string stop;
  20.   vector<string> stops;
  21. };
  22.  
  23. istream& operator >> (istream& is, Query& q) {
  24.   // Реализуйте эту функцию
  25.   string command;
  26.   is >> command;
  27.   if (command == "NewBus"){
  28.       q.type = QueryType::NewBus;
  29.       is >> q.bus;
  30.       int stop_count;
  31.       is >> stop_count;
  32.       q.stops.resize(stop_count);
  33.       for (string& stop:q.stops){
  34.           is >> stop;
  35.       }
  36.   } else if (command == "BusesForStop"){
  37.       q.type = QueryType::BusesForStop;
  38.       is >> q.stop;
  39.   } else if (command == "StopsForBus"){
  40.       q.type = QueryType::StopsForBus;
  41.       is >> q.bus;
  42.   } else if (command == "AllBuses"){
  43.       q.type = QueryType::AllBuses;
  44.   }
  45.   return is;
  46. }
  47.  
  48. struct BusesForStopResponse {
  49.   // Наполните полями эту структуру
  50.   vector <string> buses;
  51. };
  52.  
  53. ostream& operator << (ostream& os, const BusesForStopResponse& r) {
  54.   // Реализуйте эту функцию
  55.   if (r.buses.empty()){
  56.     os << "No stop" << endl;
  57.   } else {
  58.     for(const string& bus:r.buses){
  59.       os << bus << " ";
  60.     }
  61.     os << endl;
  62.   }
  63.   return os;
  64. }
  65.  
  66. struct StopsForBusResponse {
  67.   // Наполните полями эту структуру
  68.     vector <string> stops;
  69.     map<string, vector<string>> stb;
  70. };
  71.  
  72. ostream& operator << (ostream& os, const StopsForBusResponse& r) {
  73.   // Реализуйте эту функцию
  74.   if (r.stops.empty()){
  75.     os << "No bus" << endl;
  76.   } else{
  77.     for(const string& stop:r.stops){
  78.       os << "Stop" << stop << ": ";
  79.       if(r.stb.at(stop).size() == 1){
  80.         os << "no interchange";
  81.       } else {
  82.         for (const string& other_bus:r.stb.at(stop)){
  83.           if(r.bus != other_bus){
  84.             os << other_bus << " ";
  85.           }
  86.         }
  87.       }
  88.       os << endl;
  89.     }
  90.   }
  91.   return os;
  92. }
  93.  
  94. struct AllBusesResponse {
  95.   // Наполните полями эту структуру
  96.   map<string, vector<string>> bts;
  97. };
  98.  
  99. ostream& operator << (ostream& os, const AllBusesResponse& r) {
  100.   // Реализуйте эту функцию
  101.   if(r.bts.empty()){
  102.     os << "No buses" << endl;
  103.   } else {
  104.     for (const auto& bus_item : r.bts){
  105.       os << "Bus" << bus_item.first << ": ";
  106.       for (const string& stop:bus_item.second){
  107.         os << stop << " ";
  108.       }
  109.       os << endl;
  110.     }
  111.   }
  112.   return os;
  113. }
  114.  
  115. class BusManager {
  116. public:
  117.   void AddBus(const string& bus, const vector<string>& stops) {
  118.     // Реализуйте этот метод
  119.  
  120.   }
  121.  
  122.   BusesForStopResponse GetBusesForStop(const string& stop) const {
  123.     // Реализуйте этот метод
  124.   }
  125.  
  126.   StopsForBusResponse GetStopsForBus(const string& bus) const {
  127.     // Реализуйте этот метод
  128.   }
  129.  
  130.   AllBusesResponse GetAllBuses() const {
  131.     // Реализуйте этот метод
  132.   }
  133.  
  134. private:
  135.  
  136.  
  137. };
  138.  
  139. // Не меняя тела функции main, реализуйте функции и классы выше
  140.  
  141. int main() {
  142.   int query_count;
  143.   Query q;
  144.  
  145.   cin >> query_count;
  146.  
  147.   BusManager bm;
  148.   for (int i = 0; i < query_count; ++i) {
  149.     cin >> q;
  150.     switch (q.type) {
  151.     case QueryType::NewBus:
  152.       bm.AddBus(q.bus, q.stops);
  153.       break;
  154.     case QueryType::BusesForStop:
  155.       cout << bm.GetBusesForStop(q.stop) << endl;
  156.       break;
  157.     case QueryType::StopsForBus:
  158.       cout << bm.GetStopsForBus(q.bus) << endl;
  159.       break;
  160.     case QueryType::AllBuses:
  161.       cout << bm.GetAllBuses() << endl;
  162.       break;
  163.     }
  164.   }
  165.  
  166.   return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement