Skip to content

Instantly share code, notes, and snippets.

@vlaleli
Created April 22, 2026 16:05
Show Gist options
  • Select an option

  • Save vlaleli/de74f3daf9657c6312e615f4cf4145e8 to your computer and use it in GitHub Desktop.

Select an option

Save vlaleli/de74f3daf9657c6312e615f4cf4145e8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
using namespace std;
class RouteStrategy {
public:
virtual string buildRoute(const string& from, const string& to) = 0;
virtual ~RouteStrategy() = default;
};
class RoadStrategy : public RouteStrategy {
public:
string buildRoute(const string& from, const string& to) override {
return "Маршрут для автомобіля: " + from + " -> вул. Центральна -> просп. Миру -> " + to;
}
};
class WalkingStrategy : public RouteStrategy {
public:
string buildRoute(const string& from, const string& to) override {
return "Пішохідний маршрут: " + from + " -> парк -> пішохідна зона -> " + to;
}
};
class PublicTransportStrategy : public RouteStrategy {
public:
string buildRoute(const string& from, const string& to) override {
return "Маршрут громадським транспортом: " + from + " -> автобус №12 -> метро -> " + to;
}
};
class Navigator {
private:
RouteStrategy* strategy;
public:
Navigator() : strategy(nullptr) {}
void setStrategy(RouteStrategy* newStrategy) {
strategy = newStrategy;
}
void buildRoute(const string& from, const string& to) {
if (strategy == nullptr) {
cout << "Стратегія не обрана." << endl;
return;
}
cout << strategy->buildRoute(from, to) << endl;
}
};
int main() {
Navigator navigator;
RoadStrategy roadStrategy;
WalkingStrategy walkingStrategy;
PublicTransportStrategy publicTransportStrategy;
string from;
string to;
int choice;
cout << "Введіть початкову точку: ";
getline(cin, from);
cout << "Введіть пункт призначення: ";
getline(cin, to);
cout << "\nОберіть спосіб пересування:" << endl;
cout << "1 - Автомобіль" << endl;
cout << "2 - Пішки" << endl;
cout << "3 - Громадський транспорт" << endl;
cout << "Ваш вибір: ";
cin >> choice;
switch (choice) {
case 1:
navigator.setStrategy(&roadStrategy);
break;
case 2:
navigator.setStrategy(&walkingStrategy);
break;
case 3:
navigator.setStrategy(&publicTransportStrategy);
break;
default:
cout << "Невірний вибір." << endl;
return 0;
}
cout << endl;
navigator.buildRoute(from, to);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment