Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------------------------------------
- main.cpp
- -----------------------------------------------------------------------
- #include "transport_catalogue.h"
- #include "input_reader.h"
- #include "stat_reader.h"
- using namespace std;
- int main() {
- TransportCatalogue catalogue;
- input_(catalogue);
- output_(catalogue);
- }
- -----------------------------------------------------------------------
- geo.h
- -----------------------------------------------------------------------
- #pragma once
- #include <cmath>
- struct Coordinates {
- double latitude;
- double longitude;
- bool operator==(const Coordinates& other) const {
- return latitude == other.latitude
- && longitude == other.longitude;
- }
- };
- inline double ComputeDistance(Coordinates start, Coordinates end) {
- using namespace std;
- if (!(start == end)) {
- const double dr = 3.1415926535 / 180.;
- return acos(sin(start.latitude * dr) * sin(end.latitude * dr)
- + cos(start.latitude * dr) * cos(end.latitude * dr)
- * cos(abs(start.longitude - end.longitude) * dr)) * 6371000;
- } else {
- return 0.0;
- }
- }
- -----------------------------------------------------------------------
- input_reader.h
- -----------------------------------------------------------------------
- #pragma once
- #include "transport_catalogue.h"
- Stop split_stop(std::string_view str);
- Bus split_bus(TransportCatalogue& catalogue, std::string_view str);
- void input_(TransportCatalogue& catalogue);
- -----------------------------------------------------------------------
- stat_reader.h
- -----------------------------------------------------------------------
- #pragma once
- #include "transport_catalogue.h"
- void query_(TransportCatalogue& catalogue, std::string_view str);
- void output_(TransportCatalogue& catalogue);
- -----------------------------------------------------------------------
- transport_catalogue.cpp
- -----------------------------------------------------------------------
- #include "transport_catalogue.h"
- #include <execution>
- void TransportCatalogue::add_stop(Stop&& stop) {
- stops_to_buses_.insert(std::move(stop));
- }
- void TransportCatalogue::add_bus(Bus&& bus) {
- buses_.push_back(std::move(bus));
- }
- const Bus* TransportCatalogue::get_bus(std::string_view bus) {
- for (const auto& bus_ : buses_) {
- if (bus == bus_.name_) {
- return &bus_;
- }
- }
- return {};
- }
- const Stop* TransportCatalogue::get_stop(std::string_view stop) {
- for (const auto& stop_ : stops_to_buses_) {
- if (stop == stop_.name_) {
- return &stop_;
- }
- }
- return {};
- }
- int TransportCatalogue::get_uniq_stops(const Bus* bus) {
- std::unordered_set<const Stop*, Bus__hash> unique_stops_;
- unique_stops_.insert(bus->stops_.begin(),
- bus->stops_.end());
- return int(unique_stops_.size());
- }
- double TransportCatalogue::get_length(const Bus* bus) {
- return transform_reduce(next(bus->stops_.begin()),
- bus->stops_.end(),
- bus->stops_.begin(),
- 0.0,
- std::plus<>{},
- [](const Stop* lhs, const Stop* rhs) {
- return ComputeDistance({(*lhs).latitude_,
- (*lhs).longitude_}, {(*rhs).latitude_,
- (*rhs).longitude_});
- });
- }
- -----------------------------------------------------------------------
- transport_catalogue.h
- -----------------------------------------------------------------------
- #pragma once
- #include <deque>
- #include <string>
- #include <vector>
- #include <iomanip>
- #include <iostream>
- #include <unordered_set>
- #include "geo.h"
- struct Stop {
- std::string name_;
- double latitude_;
- double longitude_;
- auto tuple() const {
- return tie(name_, latitude_, longitude_);
- }
- bool operator==(const Stop& other) const {
- return tuple() == other.tuple();
- }
- size_t hash() const {
- std::hash<double> hasher;
- auto hasher_latitude = hasher(latitude_);
- auto hasher_longitude = hasher(longitude_);
- return size_t(hasher_latitude * 17 + hasher_longitude);
- }
- };
- struct Bus {
- std::string name_;
- std::deque<const Stop*> stops_;
- };
- struct Bus_hash {
- size_t operator()(const Stop& stop) const {
- return size_t(stop.hash());
- }
- };
- struct Bus__hash {
- size_t operator()(const Stop* stop) const {
- return size_t((*stop).hash());
- }
- };
- class TransportCatalogue {
- public:
- void add_bus(Bus&& bus);
- void add_stop(Stop&& stop);
- const Bus* get_bus(std::string_view stop);//const?
- const Stop* get_stop(std::string_view stop);
- int get_uniq_stops(const Bus* bus);
- double get_length(const Bus* bus);
- private:
- std::deque<Bus> buses_;
- std::unordered_set<Stop, Bus_hash> stops_to_buses_;
- };
- -----------------------------------------------------------------------
- input_reader.cpp
- -----------------------------------------------------------------------
- #include "input_reader.h"
- #include "stat_reader.h"
- #include <algorithm>
- Stop split_stop(std::string& str) {
- auto twopoint_pos = str.find(':');
- auto comma_pos = str.find(',');
- auto entry_length = 5;
- auto distance = 2;
- std::string name = str.substr(entry_length,
- twopoint_pos - entry_length);
- double latitude = stod(str.substr(twopoint_pos + distance,
- comma_pos - twopoint_pos - distance));
- double longitude = stod(str.substr(comma_pos + distance));
- return {name,
- latitude,
- longitude};
- }
- Bus split_bus(TransportCatalogue& catalogue, std::string& str) {
- auto entry_length = 4;
- auto distance = 2;
- auto twopoint_pos = str.find(':');
- Bus bus;
- bus.name_ = str.substr(entry_length,
- twopoint_pos - entry_length);
- str = str.substr(twopoint_pos + distance);
- auto more_pos = str.find('>');
- if (more_pos == std::string_view::npos) {
- auto tire_pos = str.find('-');
- while (tire_pos != std::string_view::npos) {
- bus.stops_.push_back(catalogue.get_stop(str.substr(0, tire_pos - 1)));
- str = str.substr(tire_pos + distance);
- tire_pos = str.find('-');
- }
- bus.stops_.push_back(catalogue.get_stop(str.substr(0, tire_pos - 1)));
- size_t size_ = bus.stops_.size() - 1;
- for (size_t i = size_; i > 0; i--) {
- bus.stops_.push_back(bus.stops_[i-1]);
- }
- } else {
- while (more_pos != std::string_view::npos) {
- bus.stops_.push_back(catalogue.get_stop(str.substr(0, more_pos - 1)));
- str = str.substr(more_pos + distance);
- more_pos = str.find('>');
- }
- bus.stops_.push_back(catalogue.get_stop(str.substr(0, more_pos - 1)));
- }
- return bus;
- }
- void input_(TransportCatalogue& catalogue) {
- std::string count;
- std::getline(std::cin, count);
- if (count != "") {
- std::string str;
- std::vector<std::string> buses;
- int amount = stoi(count);
- auto bus_distance = 3;
- for (int i = 0; i < amount; ++i) {
- std::getline(std::cin, str);
- if (str != "") {
- auto space_pos = str.find_first_not_of(' ');
- str = str.substr(space_pos);
- if (str.substr(0, bus_distance) != "Bus") {
- catalogue.add_stop(split_stop(str));
- } else {
- buses.push_back(str);
- }
- }
- }
- for (auto& bus : buses) {
- catalogue.add_bus(split_bus(catalogue, bus));
- }
- }
- }
- -----------------------------------------------------------------------
- stat_reader.cpp
- -----------------------------------------------------------------------
- #include "stat_reader.h"
- void query_(TransportCatalogue& catalogue, std::string_view str) {
- auto entry = 4;
- str = str.substr(entry);
- const Bus* bus = catalogue.get_bus(str);
- if (bus != nullptr) {
- std::cout << "Bus " << bus->name_ << ": "
- << bus->stops_.size() << " stops on route, "
- << catalogue.get_uniq_stops(bus) << " unique stops, "
- << std::setprecision(6) << catalogue.get_length(bus) << " route length" << std::endl;
- } else {
- std::cout << "Bus " << str << ": not found" << std::endl;
- }
- }
- void output_(TransportCatalogue& catalogue) {
- std::string count;
- std::getline(std::cin, count);
- std::string str;
- std::vector<std::string> query;
- auto amount = stoi(count);
- for (int i = 0; i < amount; ++i) {
- std::getline(std::cin, str);
- query.push_back(str);
- }
- for (auto& strr : query) {
- query_(catalogue, strr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment