Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unordered_map>
- #include <unordered_set>
- #include <vector>
- #include <string>
- #include <cmath>
- #include <fstream>
- #include <string_view>
- using namespace std;
- ofstream BusManager_log("log.txt");// Добавил логгер
- string_view RemoveSpaces(string_view line){
- while (!line.empty() && line[0] == ' '){
- line.remove_prefix(1);
- }
- while (!line.empty() && line.back() == ' '){
- line.remove_suffix(1);
- }
- return line;
- }
- enum class BusType{
- Reversive,
- Circular
- };
- struct TypeHolder{
- string_view name;
- vector<string_view> stops = vector<string_view>(0);
- BusType type = BusType::Reversive;
- };
- TypeHolder SplitStops(string_view line){
- vector<string_view> result;
- string_view name = line.substr(0, line.find_first_of(':'));
- line.remove_prefix(line.find_first_of(':') + 1);
- size_t pos = 0;
- size_t delim_pos;
- char ch = (line.find_first_of('-') == line.npos ? '>' : '-');
- while(line.find_first_of(ch, pos) != line.npos){
- delim_pos = line.find_first_of(ch, pos);
- result.push_back(RemoveSpaces(line.substr(pos, delim_pos - pos)));
- pos = delim_pos + 2;
- }
- result.push_back(RemoveSpaces(line.substr(pos, line.size() - pos)));
- BusType bt;
- bt = (ch == '-' ? BusType::Reversive : BusType::Circular);
- return {name, result, bt};
- }
- struct Coordinates{
- Coordinates(double lattitude, double longtitude) : lat(lattitude), lon(longtitude){}
- Coordinates() = default;
- double lat; // Широта
- double lon;// Долгота
- };
- double ComputeRoute(Coordinates lhs, Coordinates rhs){
- return acos(sin(lhs.lat) * sin(rhs.lat) +
- cos(lhs.lat) * cos(rhs.lat) *
- cos(abs(lhs.lon - rhs.lon))) * 6371000;
- }
- struct Stop{
- string_view name;
- Coordinates coordinates;
- };
- Stop SplitStopInfo(string_view line){
- string_view name_s = line.substr(0, line.find_first_of(':'));
- string_view lat = RemoveSpaces(line.substr(line.find_first_of(' ', line.find_first_of(':')),line.find_first_of(',')));
- string_view lon = RemoveSpaces(line.substr(line.find_first_of(',') + 1, line.size() - (line.find_first_of(',') + 1)));
- return {name_s, {stod(string(lat)), stod(string(lon))}};
- }
- class BusManager{
- public:
- void Stops_For_Bus(string_view bus) const {
- if(!buses.count(bus)){
- cout<<bus<<": not found\n";
- return;
- }
- ShowBuses(); //Логеры.
- ShowStops();
- ShowBase();
- Coordinates prev_coordinates = stops.at(base.at(bus).at(0));
- double route = 0;
- size_t vec_size = base.at(bus).size();
- for(size_t i = 1;i < vec_size;++i){
- route+= ComputeRoute(prev_coordinates, stops.at(base.at(bus).at(i)));
- prev_coordinates = stops.at(base.at(bus).at(i));
- }
- size_t stops_amount;
- size_t unique_stops_amount;
- BusType bt = buses.at(bus);
- if(bt == BusType::Reversive){
- route*= 2;
- stops_amount = base.at(bus).size() * 2 - 1;
- }
- else{
- stops_amount = base.at(bus).size();
- route += ComputeRoute(stops.at(base.at(bus).back()), stops.at(base.at(bus).at(0)));
- }
- unordered_set<string_view> stops_(base.at(bus).begin(), base.at(bus).end());
- unique_stops_amount = stops_.size();
- cout<<fixed<<bus<<": "<<stops_amount<<" stops on route, "<<unique_stops_amount
- <<" unique stops, "<<route<<" route length\n";
- }
- void Add_New_Bus(TypeHolder&& th){
- base[th.name].swap(th.stops);
- buses[th.name] = th.type;
- //Логирование
- BusManager_log<<"Added new bus '"<<th.name<<"' with type: '"<<static_cast<int>(th.type)<<"'\n";
- BusManager_log<<"Beginning show BASE"<<"\n";
- ShowBase();
- BusManager_log<<"Beginning show buses"<<"\n";
- ShowBuses();
- }
- void Add_New_Stop(const Stop& stop){
- stops[stop.name] = stop.coordinates;
- //Логирование
- BusManager_log<<"Added new stop '"<<stop.name<<"'\n";
- BusManager_log<<"Beginning show stops"<<"\n";
- ShowStops();
- }
- void ShowBuses() const{
- BusManager_log<<"--------------------Buses--------------------\n";
- for(const auto& b : buses){
- BusManager_log<<"name: '"<<b.first<<"', type: "<<static_cast<int>(b.second)<<"\n";
- }
- BusManager_log<<"--------------------Buses--------------------\n";
- }
- void ShowStops() const{
- BusManager_log<<"--------------------Stops--------------------\n";
- for(const auto& s : stops){
- BusManager_log<<"name: '"<<s.first<<"', coordinates: "<<s.second.lat<<" : "<<s.second.lon<<"\n";
- }
- BusManager_log<<"--------------------Stops--------------------\n";
- }
- void ShowBase() const{
- BusManager_log<<"--------------------BASE--------------------\n";
- for(const auto& b : base){
- //Логирование
- BusManager_log<<"Bus name: '"<<b.first<<"', stops: ";
- for(const auto& s : b.second){
- BusManager_log<<"'"<<s<<"' ";
- }
- BusManager_log<<"\n";
- }
- BusManager_log<<"--------------------BASE--------------------\n";
- }
- private:
- unordered_map<string_view, vector<string_view>> base;
- unordered_map<string_view, Coordinates> stops;
- unordered_map<string_view, BusType> buses;
- };
- void Perform_Modifying_Requests(BusManager& bm, const vector<string> base){
- BusManager_log<<"Began performing requests\n";
- for(size_t i = 0;i < base.size();++i){
- string_view sv(base.at(i));
- string_view type = sv.substr(0, sv.find_first_of(' '));
- if(type == "Stop"){
- //Логирование
- BusManager_log<<"Added STOP request\n";
- sv.remove_prefix(sv.find_first_of(' '));
- bm.Add_New_Stop(SplitStopInfo(RemoveSpaces(sv)));
- }
- else if (type == "Bus"){
- //Логирование
- BusManager_log<<"Added ADD BUS request\n";
- sv.remove_prefix(sv.find_first_of(' '));
- bm.Add_New_Bus(SplitStops(RemoveSpaces(sv)));
- }
- }
- //Логирование
- BusManager_log<<"Completed performing requests\n";
- }
- vector<string> Read_Requests(istream& is = cin){
- //Логирование
- BusManager_log<<"Began reading requests\n";
- size_t q;
- cin>>q;
- vector<string> result(q);
- string line;
- getline(is, line); //Фикс бага с пустой строкой
- for(size_t i = 0; i < q;++i){
- getline(is, line);
- result[i] = move(line);
- }
- //Логирование
- BusManager_log<<"Completed reading requests\n";
- return move(result);
- }
- void Perform_Bus_Requests(const BusManager& bm, string_view request){
- //Логирование
- BusManager_log<<"Began performing SHOW STOPS request for '"<<request<<"' bus\n";
- bm.Stops_For_Bus(RemoveSpaces(request));
- //Логирование
- BusManager_log<<"Completed performing SHOW STOPS request for '"<<request<<"' bus\n";
- }
- void Perform_Computing_Requests(const BusManager& bm, const vector<string> base){
- //Логирование
- BusManager_log<<"Began performing SHOW STOPS requests\n";
- for(size_t i = 0;i < base.size(); ++i){
- string_view sv(base.at(i));
- sv.remove_prefix(sv.find_first_of(' '));
- Perform_Bus_Requests(bm, sv);
- }
- //Логирование
- BusManager_log<<"Completed performing SHOW STOPS requests\n";
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.precision(10);
- BusManager bm;
- auto Modifying_requests = Read_Requests();
- Perform_Modifying_Requests(bm, Modifying_requests);
- auto Computing_Requests = Read_Requests();
- Perform_Computing_Requests(bm, Computing_Requests);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement