Advertisement
VinnRonn

busstops

Jun 15th, 2022 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. #include <cassert>
  2. #include <sstream>
  3. #include <iostream>
  4. #include <map>
  5. #include <string>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. enum class QueryType {
  11. NewBus,
  12. BusesForStop,
  13. StopsForBus,
  14. AllBuses,
  15. };
  16.  
  17. struct Query {
  18. QueryType type;
  19. string bus;
  20. string stop;
  21. vector<string> stops;
  22. };
  23.  
  24. istream& operator>>(istream& is, Query& q) {
  25. string request;
  26. is >> request;
  27.  
  28. if (request == "NEW_BUS") {
  29. q.type = QueryType::NewBus;
  30. int stop_count = 0;
  31.  
  32. is >> q.bus;
  33.  
  34. is >> stop_count;
  35.  
  36. for (int i = 0; i < stop_count; ++i) {
  37. string stop;
  38. is >> stop;
  39. q.stops.push_back(stop);
  40. }
  41. }
  42. else if (request == "BUSES_FOR_STOP") {
  43. q.type = QueryType::BusesForStop;
  44. is >> q.stop;
  45. }
  46. else if (request == "STOPS_FOR_BUS") {
  47. q.type = QueryType::StopsForBus;
  48. is >> q.bus;
  49. }
  50. else if (request == "ALL_BUSES") {
  51. q.type = QueryType::AllBuses;
  52. };
  53. return is;
  54. }
  55.  
  56. struct BusesForStopResponse {
  57. string stop;
  58. vector<string> buses;
  59. };
  60.  
  61. ostream& operator<<(ostream& os, const BusesForStopResponse& r) {
  62. // Реализуйте эту функцию
  63. return os;
  64. }
  65.  
  66. struct StopsForBusResponse {
  67. string bus;
  68. vector<string> stops;
  69. };
  70.  
  71. ostream& operator<<(ostream& os, const StopsForBusResponse& r) {
  72. // Реализуйте эту функцию
  73. return os;
  74. }
  75.  
  76. struct AllBusesResponse {
  77. map<string, vector<string>> buses;
  78. };
  79.  
  80. ostream& operator<<(ostream& os, const AllBusesResponse& r) {
  81. // Реализуйте эту функцию
  82. return os;
  83. }
  84.  
  85. class BusManager {
  86. public:
  87. void AddBus(const string& bus, const vector<string>& stops) {
  88.  
  89. for (const string& stop : stops) {
  90. buses_[bus].push_back(stop);
  91. stops_[stop].push_back(bus);
  92. }
  93. }
  94.  
  95. BusesForStopResponse GetBusesForStop(const string& stop) const {
  96.  
  97. if (stops_.count(stop)) {
  98. return { stop, stops_.at(stop) };
  99. }
  100. return {};
  101. }
  102.  
  103. StopsForBusResponse GetStopsForBus(const string& bus) const {
  104.  
  105. if (buses_.count(bus)) {
  106. return { bus, buses_.at(bus) };
  107. }
  108. return {};
  109. }
  110.  
  111. AllBusesResponse GetAllBuses() const {
  112. return { buses_ };
  113. }
  114. private:
  115. map<string, vector<string>> buses_;
  116. map<string, vector<string>> stops_;
  117. };
  118.  
  119. // Не меняя тела функции main, реализуйте функции и классы выше
  120.  
  121. void TestInputData() {
  122. Query q;
  123. int query_count = 1;
  124. istringstream input;
  125.  
  126. input.str("NEW_BUS golden_ring 4 sergiev_posad rostov ivanovo vladimir");
  127. input >> q;
  128.  
  129. assert(q.bus == "golden_ring");
  130. assert(q.stops.size() == 4);
  131. assert(q.type == QueryType::NewBus);
  132. //assert(q.stops.at(-1) == "vladimir");
  133. cout << "Test Input Data is OK" << endl;
  134. }
  135.  
  136. void TestBusesForStop() {
  137. Query q;
  138. istringstream input;
  139. input.str("BUSES_FOR_STOP rostov");
  140. input >> q;
  141. assert(q.type == QueryType::BusesForStop);
  142. assert(q.stop == "rostov");
  143. assert(q.stops.empty() == true);
  144. assert(q.bus == "");
  145.  
  146. cout << "Test BusesForStop is OK" << endl;
  147. }
  148.  
  149. void TestStopsForBus() {
  150. Query q;
  151. istringstream input;
  152.  
  153. input.str("STOPS_FOR_BUS golden_ring");
  154. input >> q;
  155. assert(q.bus == "golden_ring");
  156. assert(q.stops.empty() == true);
  157. assert(q.stop == "");
  158.  
  159. cout << "Test Stops For Bus is OK" << endl;
  160. }
  161. void TestClassData() {
  162.  
  163. BusManager b_manager;
  164. BusesForStopResponse bfs;
  165.  
  166.  
  167. b_manager.AddBus("golden_ring", { "sergiev_posad", "rostov", "ivanovo", "vladimir" });
  168. b_manager.GetBusesForStop("rostov");
  169. assert(bfs.stop == "rostov");
  170.  
  171. }
  172.  
  173.  
  174. int main() {
  175. TestInputData();
  176. TestBusesForStop();
  177. TestStopsForBus();
  178. TestClassData();
  179. /*
  180. int query_count;
  181. Query q;
  182.  
  183. cin >> query_count;
  184.  
  185. BusManager bm;
  186. for (int i = 0; i < query_count; ++i) {
  187. cin >> q;
  188. switch (q.type) {
  189. case QueryType::NewBus:
  190. bm.AddBus(q.bus, q.stops);
  191. break;
  192. case QueryType::BusesForStop:
  193. cout << bm.GetBusesForStop(q.stop) << endl;
  194. break;
  195. case QueryType::StopsForBus:
  196. cout << bm.GetStopsForBus(q.bus) << endl;
  197. break;
  198. case QueryType::AllBuses:
  199. cout << bm.GetAllBuses() << endl;
  200. break;
  201. }
  202. }
  203. */
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement