Advertisement
Guest User

Code

a guest
Jan 23rd, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <unordered_set>
  4. #include <vector>
  5. #include <string>
  6. #include <cmath>
  7. #include <fstream>
  8. #include <string_view>
  9. using namespace std;
  10.  
  11. ofstream BusManager_log("log.txt");// Добавил логгер
  12.  
  13. string_view RemoveSpaces(string_view line){
  14.  
  15. while (!line.empty() && line[0] == ' '){
  16. line.remove_prefix(1);
  17. }
  18.  
  19. while (!line.empty() && line.back() == ' '){
  20. line.remove_suffix(1);
  21. }
  22.  
  23. return line;
  24. }
  25.  
  26. enum class BusType{
  27. Reversive,
  28. Circular
  29. };
  30.  
  31. struct TypeHolder{
  32. string_view name;
  33. vector<string_view> stops = vector<string_view>(0);
  34. BusType type = BusType::Reversive;
  35. };
  36.  
  37.  
  38. TypeHolder SplitStops(string_view line){
  39. vector<string_view> result;
  40.  
  41. string_view name = line.substr(0, line.find_first_of(':'));
  42. line.remove_prefix(line.find_first_of(':') + 1);
  43.  
  44. size_t pos = 0;
  45. size_t delim_pos;
  46. char ch = (line.find_first_of('-') == line.npos ? '>' : '-');
  47.  
  48. while(line.find_first_of(ch, pos) != line.npos){
  49. delim_pos = line.find_first_of(ch, pos);
  50. result.push_back(RemoveSpaces(line.substr(pos, delim_pos - pos)));
  51. pos = delim_pos + 2;
  52. }
  53.  
  54. result.push_back(RemoveSpaces(line.substr(pos, line.size() - pos)));
  55.  
  56. BusType bt;
  57. bt = (ch == '-' ? BusType::Reversive : BusType::Circular);
  58.  
  59. return {name, result, bt};
  60. }
  61.  
  62. struct Coordinates{
  63.  
  64. Coordinates(double lattitude, double longtitude) : lat(lattitude), lon(longtitude){}
  65.  
  66. Coordinates() = default;
  67.  
  68. double lat; // Широта
  69. double lon;// Долгота
  70. };
  71.  
  72. double ComputeRoute(Coordinates lhs, Coordinates rhs){
  73. return acos(sin(lhs.lat) * sin(rhs.lat) +
  74. cos(lhs.lat) * cos(rhs.lat) *
  75. cos(abs(lhs.lon - rhs.lon))) * 6371000;
  76. }
  77.  
  78. struct Stop{
  79. string_view name;
  80. Coordinates coordinates;
  81. };
  82.  
  83. Stop SplitStopInfo(string_view line){
  84.  
  85. string_view name_s = line.substr(0, line.find_first_of(':'));
  86.  
  87. string_view lat = RemoveSpaces(line.substr(line.find_first_of(' ', line.find_first_of(':')),line.find_first_of(',')));
  88.  
  89. string_view lon = RemoveSpaces(line.substr(line.find_first_of(',') + 1, line.size() - (line.find_first_of(',') + 1)));
  90.  
  91. return {name_s, {stod(string(lat)), stod(string(lon))}};
  92. }
  93.  
  94.  
  95. class BusManager{
  96. public:
  97.  
  98. void Stops_For_Bus(string_view bus) const {
  99.  
  100. if(!buses.count(bus)){
  101. cout<<bus<<": not found\n";
  102. return;
  103. }
  104.  
  105. ShowBuses(); //Логеры.
  106. ShowStops();
  107. ShowBase();
  108.  
  109. Coordinates prev_coordinates = stops.at(base.at(bus).at(0));
  110.  
  111. double route = 0;
  112. size_t vec_size = base.at(bus).size();
  113.  
  114. for(size_t i = 1;i < vec_size;++i){
  115. route+= ComputeRoute(prev_coordinates, stops.at(base.at(bus).at(i)));
  116. prev_coordinates = stops.at(base.at(bus).at(i));
  117. }
  118.  
  119. size_t stops_amount;
  120. size_t unique_stops_amount;
  121.  
  122. BusType bt = buses.at(bus);
  123.  
  124. if(bt == BusType::Reversive){
  125. route*= 2;
  126. stops_amount = base.at(bus).size() * 2 - 1;
  127. }
  128. else{
  129. stops_amount = base.at(bus).size();
  130. route += ComputeRoute(stops.at(base.at(bus).back()), stops.at(base.at(bus).at(0)));
  131. }
  132.  
  133. unordered_set<string_view> stops_(base.at(bus).begin(), base.at(bus).end());
  134. unique_stops_amount = stops_.size();
  135.  
  136. cout<<fixed<<bus<<": "<<stops_amount<<" stops on route, "<<unique_stops_amount
  137. <<" unique stops, "<<route<<" route length\n";
  138.  
  139. }
  140.  
  141. void Add_New_Bus(TypeHolder&& th){
  142.  
  143. base[th.name].swap(th.stops);
  144. buses[th.name] = th.type;
  145. //Логирование
  146. BusManager_log<<"Added new bus '"<<th.name<<"' with type: '"<<static_cast<int>(th.type)<<"'\n";
  147. BusManager_log<<"Beginning show BASE"<<"\n";
  148. ShowBase();
  149. BusManager_log<<"Beginning show buses"<<"\n";
  150. ShowBuses();
  151. }
  152.  
  153. void Add_New_Stop(const Stop& stop){
  154. stops[stop.name] = stop.coordinates;
  155. //Логирование
  156. BusManager_log<<"Added new stop '"<<stop.name<<"'\n";
  157. BusManager_log<<"Beginning show stops"<<"\n";
  158. ShowStops();
  159. }
  160. void ShowBuses() const{
  161. BusManager_log<<"--------------------Buses--------------------\n";
  162.  
  163. for(const auto& b : buses){
  164. BusManager_log<<"name: '"<<b.first<<"', type: "<<static_cast<int>(b.second)<<"\n";
  165. }
  166.  
  167. BusManager_log<<"--------------------Buses--------------------\n";
  168. }
  169.  
  170. void ShowStops() const{
  171. BusManager_log<<"--------------------Stops--------------------\n";
  172.  
  173. for(const auto& s : stops){
  174. BusManager_log<<"name: '"<<s.first<<"', coordinates: "<<s.second.lat<<" : "<<s.second.lon<<"\n";
  175. }
  176.  
  177. BusManager_log<<"--------------------Stops--------------------\n";
  178. }
  179. void ShowBase() const{
  180. BusManager_log<<"--------------------BASE--------------------\n";
  181.  
  182. for(const auto& b : base){
  183. //Логирование
  184. BusManager_log<<"Bus name: '"<<b.first<<"', stops: ";
  185.  
  186. for(const auto& s : b.second){
  187. BusManager_log<<"'"<<s<<"' ";
  188. }
  189.  
  190. BusManager_log<<"\n";
  191. }
  192.  
  193. BusManager_log<<"--------------------BASE--------------------\n";
  194. }
  195.  
  196. private:
  197. unordered_map<string_view, vector<string_view>> base;
  198. unordered_map<string_view, Coordinates> stops;
  199. unordered_map<string_view, BusType> buses;
  200. };
  201.  
  202.  
  203. void Perform_Modifying_Requests(BusManager& bm, const vector<string> base){
  204.  
  205. BusManager_log<<"Began performing requests\n";
  206.  
  207. for(size_t i = 0;i < base.size();++i){
  208. string_view sv(base.at(i));
  209. string_view type = sv.substr(0, sv.find_first_of(' '));
  210.  
  211. if(type == "Stop"){
  212. //Логирование
  213. BusManager_log<<"Added STOP request\n";
  214. sv.remove_prefix(sv.find_first_of(' '));
  215. bm.Add_New_Stop(SplitStopInfo(RemoveSpaces(sv)));
  216. }
  217. else if (type == "Bus"){
  218. //Логирование
  219. BusManager_log<<"Added ADD BUS request\n";
  220. sv.remove_prefix(sv.find_first_of(' '));
  221. bm.Add_New_Bus(SplitStops(RemoveSpaces(sv)));
  222. }
  223. }
  224. //Логирование
  225. BusManager_log<<"Completed performing requests\n";
  226. }
  227.  
  228. vector<string> Read_Requests(istream& is = cin){
  229. //Логирование
  230. BusManager_log<<"Began reading requests\n";
  231.  
  232. size_t q;
  233. cin>>q;
  234. vector<string> result(q);
  235. string line;
  236. getline(is, line); //Фикс бага с пустой строкой
  237.  
  238. for(size_t i = 0; i < q;++i){
  239. getline(is, line);
  240. result[i] = move(line);
  241. }
  242. //Логирование
  243. BusManager_log<<"Completed reading requests\n";
  244.  
  245. return move(result);
  246. }
  247.  
  248. void Perform_Bus_Requests(const BusManager& bm, string_view request){
  249. //Логирование
  250. BusManager_log<<"Began performing SHOW STOPS request for '"<<request<<"' bus\n";
  251.  
  252. bm.Stops_For_Bus(RemoveSpaces(request));
  253. //Логирование
  254. BusManager_log<<"Completed performing SHOW STOPS request for '"<<request<<"' bus\n";
  255. }
  256.  
  257. void Perform_Computing_Requests(const BusManager& bm, const vector<string> base){
  258. //Логирование
  259. BusManager_log<<"Began performing SHOW STOPS requests\n";
  260.  
  261. for(size_t i = 0;i < base.size(); ++i){
  262. string_view sv(base.at(i));
  263. sv.remove_prefix(sv.find_first_of(' '));
  264. Perform_Bus_Requests(bm, sv);
  265. }
  266. //Логирование
  267. BusManager_log<<"Completed performing SHOW STOPS requests\n";
  268. }
  269.  
  270. int main()
  271. {
  272.  
  273. ios_base::sync_with_stdio(false);
  274. cin.tie(nullptr);
  275. cout.precision(10);
  276.  
  277. BusManager bm;
  278.  
  279. auto Modifying_requests = Read_Requests();
  280. Perform_Modifying_Requests(bm, Modifying_requests);
  281.  
  282. auto Computing_Requests = Read_Requests();
  283. Perform_Computing_Requests(bm, Computing_Requests);
  284.  
  285. return 0;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement