shushus

Спринт 10. Транспортный справочник - пишем код. Задание 1.

Mar 25th, 2023
1,767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.34 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. main.cpp
  3. -----------------------------------------------------------------------
  4. #include "transport_catalogue.h"
  5. #include "input_reader.h"
  6. #include "stat_reader.h"
  7.  
  8. using namespace std;
  9.  
  10. int main() {    
  11.     TransportCatalogue catalogue;
  12.     input_(catalogue);
  13.     output_(catalogue);
  14. }
  15. -----------------------------------------------------------------------
  16. geo.h
  17. -----------------------------------------------------------------------
  18. #pragma once
  19. #include <cmath>
  20.  
  21. struct Coordinates {
  22.     double latitude;
  23.     double longitude;
  24.    
  25.     bool operator==(const Coordinates& other) const {
  26.         return latitude == other.latitude
  27.             && longitude == other.longitude;
  28.     }
  29. };
  30.  
  31. inline double ComputeDistance(Coordinates start, Coordinates end) {
  32.     using namespace std;
  33.     if (!(start == end)) {
  34.         const double dr = 3.1415926535 / 180.;
  35.         return acos(sin(start.latitude * dr) * sin(end.latitude * dr)
  36.                     + cos(start.latitude * dr) * cos(end.latitude * dr)
  37.                     * cos(abs(start.longitude - end.longitude) * dr)) * 6371000;
  38.     } else {
  39.         return 0.0;
  40.     }
  41. }
  42. -----------------------------------------------------------------------
  43. input_reader.h
  44. -----------------------------------------------------------------------
  45. #pragma once
  46. #include "transport_catalogue.h"
  47.  
  48. Stop split_stop(std::string_view str);
  49. Bus split_bus(TransportCatalogue& catalogue, std::string_view str);
  50. void input_(TransportCatalogue& catalogue);
  51. -----------------------------------------------------------------------
  52. stat_reader.h
  53. -----------------------------------------------------------------------
  54. #pragma once
  55. #include "transport_catalogue.h"
  56.  
  57. void query_(TransportCatalogue& catalogue, std::string_view str);
  58. void output_(TransportCatalogue& catalogue);
  59. -----------------------------------------------------------------------
  60. transport_catalogue.cpp
  61. -----------------------------------------------------------------------
  62. #include "transport_catalogue.h"
  63.  
  64. #include <execution>
  65.  
  66. void TransportCatalogue::add_stop(Stop&& stop) {
  67.     stops_to_buses_.insert(std::move(stop));
  68. }
  69.  
  70. void TransportCatalogue::add_bus(Bus&& bus) {
  71.     buses_.push_back(std::move(bus));
  72. }
  73.  
  74. const Bus* TransportCatalogue::get_bus(std::string_view bus) {  
  75.     for (const auto& bus_ : buses_) {
  76.         if (bus == bus_.name_) {
  77.             return &bus_;
  78.         }
  79.     }
  80.    
  81.     return {};
  82. }
  83.  
  84. const Stop* TransportCatalogue::get_stop(std::string_view stop) {
  85.     for (const auto& stop_ : stops_to_buses_) {
  86.         if (stop == stop_.name_) {
  87.             return &stop_;
  88.         }
  89.     }
  90.    
  91.     return {};
  92. }
  93.  
  94. int TransportCatalogue::get_uniq_stops(const Bus* bus) {
  95.     std::unordered_set<const Stop*, Bus__hash> unique_stops_;
  96.    
  97.     unique_stops_.insert(bus->stops_.begin(),
  98.                          bus->stops_.end());
  99.    
  100.     return int(unique_stops_.size());
  101. }
  102.  
  103. double TransportCatalogue::get_length(const Bus* bus) {
  104.     return transform_reduce(next(bus->stops_.begin()),
  105.                             bus->stops_.end(),
  106.                             bus->stops_.begin(),
  107.                             0.0,
  108.                             std::plus<>{},
  109.                             [](const Stop* lhs, const Stop* rhs) {
  110.                                 return ComputeDistance({(*lhs).latitude_,
  111.                                                         (*lhs).longitude_}, {(*rhs).latitude_,
  112.                                                                               (*rhs).longitude_});
  113.                             });
  114. }
  115. -----------------------------------------------------------------------
  116. transport_catalogue.h
  117. -----------------------------------------------------------------------
  118. #pragma once
  119. #include <deque>
  120. #include <string>
  121. #include <vector>
  122.  
  123. #include <iomanip>
  124. #include <iostream>
  125.  
  126. #include <unordered_set>
  127.  
  128. #include "geo.h"
  129.  
  130. struct Stop {    
  131.     std::string name_;
  132.     double latitude_;
  133.     double longitude_;
  134.    
  135.     auto tuple() const {
  136.         return tie(name_, latitude_, longitude_);
  137.     }
  138.  
  139.     bool operator==(const Stop& other) const {
  140.         return tuple() == other.tuple();
  141.     }
  142.    
  143.     size_t hash() const {
  144.         std::hash<double> hasher;
  145.         auto hasher_latitude = hasher(latitude_);
  146.         auto hasher_longitude = hasher(longitude_);
  147.         return size_t(hasher_latitude * 17 + hasher_longitude);
  148.     }  
  149. };
  150.  
  151. struct Bus {
  152.     std::string name_;
  153.     std::deque<const Stop*> stops_;
  154. };
  155.  
  156. struct Bus_hash {
  157.     size_t operator()(const Stop& stop) const {
  158.         return size_t(stop.hash());
  159.     }
  160. };
  161.  
  162. struct Bus__hash {
  163.     size_t operator()(const Stop* stop) const {
  164.         return size_t((*stop).hash());
  165.     }
  166. };
  167.  
  168. class TransportCatalogue {
  169. public:  
  170.     void add_bus(Bus&& bus);
  171.     void add_stop(Stop&& stop);
  172.  
  173.     const Bus* get_bus(std::string_view stop);//const?
  174.     const Stop* get_stop(std::string_view stop);
  175.  
  176.     int get_uniq_stops(const Bus* bus);
  177.     double get_length(const Bus* bus);
  178. private:
  179.     std::deque<Bus> buses_;
  180.     std::unordered_set<Stop, Bus_hash> stops_to_buses_;    
  181. };
  182. -----------------------------------------------------------------------
  183. input_reader.cpp
  184. -----------------------------------------------------------------------
  185. #include "input_reader.h"
  186. #include "stat_reader.h"
  187. #include <algorithm>
  188.  
  189. Stop split_stop(std::string& str) {
  190.     auto twopoint_pos = str.find(':');
  191.     auto comma_pos = str.find(',');
  192.     auto entry_length = 5;
  193.     auto distance = 2;
  194.    
  195.     std::string name = str.substr(entry_length,
  196.                                   twopoint_pos - entry_length);
  197.     double latitude = stod(str.substr(twopoint_pos + distance,
  198.                                       comma_pos - twopoint_pos - distance));
  199.     double longitude = stod(str.substr(comma_pos + distance));
  200.     return {name,
  201.             latitude,
  202.             longitude};
  203. }
  204.  
  205.  
  206. Bus split_bus(TransportCatalogue& catalogue, std::string& str) {
  207.     auto entry_length = 4;
  208.     auto distance = 2;
  209.     auto twopoint_pos = str.find(':');
  210.     Bus bus;
  211.     bus.name_ = str.substr(entry_length,
  212.                            twopoint_pos - entry_length);
  213.    
  214.     str = str.substr(twopoint_pos + distance);
  215.    
  216.     auto more_pos = str.find('>');
  217.     if (more_pos == std::string_view::npos) {
  218.         auto tire_pos = str.find('-');
  219.        
  220.         while (tire_pos != std::string_view::npos) {
  221.             bus.stops_.push_back(catalogue.get_stop(str.substr(0, tire_pos - 1)));
  222.            
  223.             str = str.substr(tire_pos + distance);
  224.             tire_pos = str.find('-');
  225.         }
  226.        
  227.         bus.stops_.push_back(catalogue.get_stop(str.substr(0, tire_pos - 1)));
  228.         size_t size_ = bus.stops_.size() - 1;
  229.        
  230.         for (size_t i = size_; i > 0; i--) {
  231.             bus.stops_.push_back(bus.stops_[i-1]);
  232.         }
  233.        
  234.     } else {
  235.         while (more_pos != std::string_view::npos) {
  236.             bus.stops_.push_back(catalogue.get_stop(str.substr(0, more_pos - 1)));
  237.            
  238.             str = str.substr(more_pos + distance);
  239.             more_pos = str.find('>');
  240.         }
  241.        
  242.         bus.stops_.push_back(catalogue.get_stop(str.substr(0, more_pos - 1)));
  243.     }
  244.     return bus;
  245. }
  246.  
  247.  
  248. void input_(TransportCatalogue& catalogue) {
  249.    
  250.     std::string count;
  251.     std::getline(std::cin, count);
  252.    
  253.     if (count != "") {
  254.         std::string str;
  255.         std::vector<std::string> buses;
  256.         int amount = stoi(count);
  257.         auto bus_distance = 3;
  258.        
  259.         for (int i = 0; i < amount; ++i) {
  260.             std::getline(std::cin, str);
  261.            
  262.             if (str != "") {
  263.                 auto space_pos = str.find_first_not_of(' ');
  264.                 str = str.substr(space_pos);
  265.  
  266.                 if (str.substr(0, bus_distance) != "Bus") {
  267.                     catalogue.add_stop(split_stop(str));
  268.                 } else {
  269.                     buses.push_back(str);
  270.                 }
  271.             }
  272.         }
  273.        
  274.         for (auto& bus : buses) {
  275.             catalogue.add_bus(split_bus(catalogue, bus));
  276.         }
  277.     }
  278. }
  279. -----------------------------------------------------------------------
  280. stat_reader.cpp
  281. -----------------------------------------------------------------------
  282. #include "stat_reader.h"
  283.  
  284. void query_(TransportCatalogue& catalogue, std::string_view str) {
  285.     auto entry = 4;
  286.     str = str.substr(entry);
  287.    
  288.     const Bus* bus = catalogue.get_bus(str);
  289.     if (bus != nullptr) {
  290.         std::cout << "Bus " << bus->name_ << ": "
  291.                   << bus->stops_.size() << " stops on route, "
  292.                   << catalogue.get_uniq_stops(bus) << " unique stops, "
  293.                   << std::setprecision(6) << catalogue.get_length(bus) << " route length" << std::endl;
  294.     } else {      
  295.         std::cout << "Bus " << str << ": not found" << std::endl;
  296.     }  
  297. }
  298.  
  299. void output_(TransportCatalogue& catalogue) {
  300.     std::string count;
  301.     std::getline(std::cin, count);
  302.    
  303.     std::string str;
  304.     std::vector<std::string> query;
  305.     auto amount = stoi(count);
  306.    
  307.     for (int i = 0; i < amount; ++i) {
  308.         std::getline(std::cin, str);
  309.         query.push_back(str);
  310.     }
  311.    
  312.     for (auto& strr : query) {
  313.         query_(catalogue, strr);
  314.     }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment