Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <cmath>
- #include <sstream>
- using namespace std;
- class IVehicle {
- public:
- virtual bool drive(int kilometers) = 0;
- virtual void refuel() = 0;
- virtual void printStatus() = 0;
- virtual ~IVehicle() = default;
- };
- class AbstractCar : public IVehicle {
- public:
- AbstractCar(double fuelConsumptionLitresPerKm, double fuelLevelMax, const string& name):
- m_fuelConsumptionLitresPerKm(fuelConsumptionLitresPerKm),
- m_fuelLevelMax(fuelLevelMax),
- m_fuelLevel(fuelLevelMax),
- m_mileageKm(0),
- m_name(name) {}
- bool drive(int kilometers) override {
- double distanceLeft = kilometers;
- int refuelCount = 0;
- while (distanceLeft > 0) {
- double requiredFuel = (distanceLeft / 100.0) * m_fuelConsumptionLitresPerKm;
- if (requiredFuel > m_fuelLevel) {
- double possibleDistance = (m_fuelLevel / m_fuelConsumptionLitresPerKm) * 100.0;
- if (possibleDistance > 0) {
- m_mileageKm += possibleDistance;
- distanceLeft -= possibleDistance;
- m_fuelLevel = 0;
- cout << m_name << ": Drove " << possibleDistance << " km and ran out of fuel." << endl;
- }
- cout << m_name << ": Additional fuel to complete the route." << endl;
- refuel();
- refuelCount++;
- } else {
- m_mileageKm += distanceLeft;
- m_fuelLevel -= requiredFuel;
- cout << m_name << ": Successfully drove " << distanceLeft << " km." << endl;
- distanceLeft = 0;
- }
- }
- if (refuelCount > 0) {
- cout << m_name << ": Total additional refuels: " << refuelCount << endl;
- }
- return true;
- }
- void refuel() override {
- m_fuelLevel = m_fuelLevelMax;
- cout << m_name << ": Refueled to max capacity." << endl;
- }
- void printStatus() override {
- cout << m_name << " status: Mileage(km)=" << m_mileageKm
- << ", Fuel level(l)=" << m_fuelLevel << endl;
- }
- protected:
- double m_fuelConsumptionLitresPerKm;
- double m_fuelLevel;
- double m_fuelLevelMax;
- int m_mileageKm;
- string m_name;
- };
- class Sedan : public AbstractCar {
- public:
- Sedan() : AbstractCar(7.0, 50.0, "Sedan") {}
- };
- class Suv : public AbstractCar {
- public:
- Suv() : AbstractCar(11.0, 80.0, "Suv") {}
- };
- class Bus : public AbstractCar {
- public:
- Bus() : AbstractCar(25.0, 120.0, "Bus") {}
- };
- class Bicycle : public IVehicle {
- public:
- Bicycle() : m_mileageKm(0), m_name("Bicycle") {}
- bool drive(int kilometers) override {
- m_mileageKm += kilometers;
- cout << m_name << ": Drove " << kilometers << " km." << endl;
- return true;
- }
- void refuel() override {
- // No operation for Bicycle
- }
- void printStatus() override {
- cout << m_name << " status: Mileage(km)=" << m_mileageKm << endl;
- }
- private:
- int m_mileageKm;
- string m_name;
- };
- struct RoutePoint {
- double xKm;
- double yKm;
- string name;
- RoutePoint(double x, double y, const string& pointName) : xKm(x), yKm(y), name(pointName) {}
- };
- class Route {
- public:
- void addPoint(const RoutePoint& point) {
- points.push_back(point);
- }
- void run(IVehicle* vehicle) {
- if (points.size() < 2) {
- cout << "Error: Route must have at least 2 points." << endl;
- return;
- }
- for (size_t i = 1; i < points.size(); ++i) {
- const RoutePoint& from = points[i - 1];
- const RoutePoint& to = points[i];
- double distance = calculateDistance(from, to);
- cout << "Traveling from " << from.name << " to " << to.name << " (Distance: " << distance << " km)" << endl;
- while (!vehicle->drive(static_cast<int>(distance))) {
- vehicle->refuel();
- }
- }
- cout << "Route completed." << endl;
- }
- private:
- vector<RoutePoint> points;
- double calculateDistance(const RoutePoint& from, const RoutePoint& to) const {
- double dx = to.xKm - from.xKm;
- double dy = to.yKm - from.yKm;
- return sqrt(dx * dx + dy * dy);
- }
- };
- int main(int argc, char* argv[]) {
- if (argc < 2) {
- cerr << "Error: No route file specified. Usage: lab5.exe route.txt" << endl;
- return 1;
- }
- string fileName = argv[1];
- ifstream inputFile(fileName);
- if (!inputFile.is_open()) {
- cerr << "Error: Could not open file " << fileName << endl;
- return 1;
- }
- Route route;
- string line;
- while (getline(inputFile, line)) {
- istringstream iss(line);
- double x, y;
- string name;
- if (!(iss >> x >> y)) {
- cerr << "Error: Invalid data format in file." << endl;
- return 1;
- }
- getline(iss, name);
- if (name.empty()) {
- cerr << "Error: Point name missing in file." << endl;
- return 1;
- }
- route.addPoint(RoutePoint(x, y, name));
- }
- inputFile.close();
- IVehicle* vehicles[4] = { new Sedan, new Suv, new Bus, new Bicycle };
- for (int i = 0; i < 4; ++i) {
- cout << "Running route with ";
- vehicles[i]->printStatus();
- route.run(vehicles[i]);
- cout << endl;
- }
- for (int i = 0; i < 4; ++i)
- delete vehicles[i];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement