Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <sstream>
- #include <iostream>
- #include <map>
- #include <string>
- #include <vector>
- using namespace std;
- enum class QueryType {
- NewBus,
- BusesForStop,
- StopsForBus,
- AllBuses,
- };
- struct Query {
- QueryType type;
- string bus;
- string stop;
- vector<string> stops;
- };
- istream& operator>>(istream& is, Query& q) {
- string request;
- is >> request;
- if (request == "NEW_BUS") {
- q.type = QueryType::NewBus;
- int stop_count = 0;
- is >> q.bus;
- is >> stop_count;
- for (int i = 0; i < stop_count; ++i) {
- string stop;
- is >> stop;
- q.stops.push_back(stop);
- }
- }
- else if (request == "BUSES_FOR_STOP") {
- q.type = QueryType::BusesForStop;
- is >> q.stop;
- }
- else if (request == "STOPS_FOR_BUS") {
- q.type = QueryType::StopsForBus;
- is >> q.bus;
- }
- else if (request == "ALL_BUSES") {
- q.type = QueryType::AllBuses;
- };
- return is;
- }
- struct BusesForStopResponse {
- string stop;
- vector<string> buses;
- };
- ostream& operator<<(ostream& os, const BusesForStopResponse& r) {
- // Реализуйте эту функцию
- return os;
- }
- struct StopsForBusResponse {
- string bus;
- vector<string> stops;
- };
- ostream& operator<<(ostream& os, const StopsForBusResponse& r) {
- // Реализуйте эту функцию
- return os;
- }
- struct AllBusesResponse {
- map<string, vector<string>> buses;
- };
- ostream& operator<<(ostream& os, const AllBusesResponse& r) {
- // Реализуйте эту функцию
- return os;
- }
- class BusManager {
- public:
- void AddBus(const string& bus, const vector<string>& stops) {
- for (const string& stop : stops) {
- buses_[bus].push_back(stop);
- stops_[stop].push_back(bus);
- }
- }
- BusesForStopResponse GetBusesForStop(const string& stop) const {
- if (stops_.count(stop)) {
- return { stop, stops_.at(stop) };
- }
- return {};
- }
- StopsForBusResponse GetStopsForBus(const string& bus) const {
- if (buses_.count(bus)) {
- return { bus, buses_.at(bus) };
- }
- return {};
- }
- AllBusesResponse GetAllBuses() const {
- return { buses_ };
- }
- private:
- map<string, vector<string>> buses_;
- map<string, vector<string>> stops_;
- };
- // Не меняя тела функции main, реализуйте функции и классы выше
- void TestInputData() {
- Query q;
- int query_count = 1;
- istringstream input;
- input.str("NEW_BUS golden_ring 4 sergiev_posad rostov ivanovo vladimir");
- input >> q;
- assert(q.bus == "golden_ring");
- assert(q.stops.size() == 4);
- assert(q.type == QueryType::NewBus);
- //assert(q.stops.at(-1) == "vladimir");
- cout << "Test Input Data is OK" << endl;
- }
- void TestBusesForStop() {
- Query q;
- istringstream input;
- input.str("BUSES_FOR_STOP rostov");
- input >> q;
- assert(q.type == QueryType::BusesForStop);
- assert(q.stop == "rostov");
- assert(q.stops.empty() == true);
- assert(q.bus == "");
- cout << "Test BusesForStop is OK" << endl;
- }
- void TestStopsForBus() {
- Query q;
- istringstream input;
- input.str("STOPS_FOR_BUS golden_ring");
- input >> q;
- assert(q.bus == "golden_ring");
- assert(q.stops.empty() == true);
- assert(q.stop == "");
- cout << "Test Stops For Bus is OK" << endl;
- }
- void TestClassData() {
- BusManager b_manager;
- BusesForStopResponse bfs;
- b_manager.AddBus("golden_ring", { "sergiev_posad", "rostov", "ivanovo", "vladimir" });
- b_manager.GetBusesForStop("rostov");
- assert(bfs.stop == "rostov");
- }
- int main() {
- TestInputData();
- TestBusesForStop();
- TestStopsForBus();
- TestClassData();
- /*
- int query_count;
- Query q;
- cin >> query_count;
- BusManager bm;
- for (int i = 0; i < query_count; ++i) {
- cin >> q;
- switch (q.type) {
- case QueryType::NewBus:
- bm.AddBus(q.bus, q.stops);
- break;
- case QueryType::BusesForStop:
- cout << bm.GetBusesForStop(q.stop) << endl;
- break;
- case QueryType::StopsForBus:
- cout << bm.GetStopsForBus(q.bus) << endl;
- break;
- case QueryType::AllBuses:
- cout << bm.GetAllBuses() << endl;
- break;
- }
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement