Advertisement
hotsbefastt

SVP_LabTask5_v2

Dec 17th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <cmath>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. class IVehicle {
  11. public:
  12.     virtual bool drive(int kilometers) = 0;
  13.     virtual void refuel() = 0;
  14.     virtual void printStatus() = 0;
  15.     virtual ~IVehicle() = default;
  16. };
  17.  
  18. class AbstractCar : public IVehicle {
  19. public:
  20.     AbstractCar(double fuelConsumptionLitresPerKm, double fuelLevelMax, const string& name):
  21.         m_fuelConsumptionLitresPerKm(fuelConsumptionLitresPerKm),
  22.         m_fuelLevelMax(fuelLevelMax),
  23.         m_fuelLevel(fuelLevelMax),
  24.         m_mileageKm(0),
  25.         m_name(name) {}
  26.  
  27.     bool drive(int kilometers) override {
  28.         double distanceLeft = kilometers;
  29.         int refuelCount = 0;
  30.  
  31.         while (distanceLeft > 0) {
  32.             double requiredFuel = (distanceLeft / 100.0) * m_fuelConsumptionLitresPerKm;
  33.  
  34.             if (requiredFuel > m_fuelLevel) {
  35.                 double possibleDistance = (m_fuelLevel / m_fuelConsumptionLitresPerKm) * 100.0;
  36.  
  37.                 if (possibleDistance > 0) {
  38.                     m_mileageKm += possibleDistance;
  39.                     distanceLeft -= possibleDistance;
  40.                     m_fuelLevel = 0;
  41.                     cout << m_name << ": Drove " << possibleDistance << " km and ran out of fuel." << endl;
  42.                 }
  43.  
  44.                 cout << m_name << ": Additional fuel to complete the route." << endl;
  45.                 refuel();
  46.                 refuelCount++;
  47.             } else {
  48.                 m_mileageKm += distanceLeft;
  49.                 m_fuelLevel -= requiredFuel;
  50.                 cout << m_name << ": Successfully drove " << distanceLeft << " km." << endl;
  51.                 distanceLeft = 0;
  52.             }
  53.         }
  54.  
  55.         if (refuelCount > 0) {
  56.             cout << m_name << ": Total additional refuels: " << refuelCount << endl;
  57.         }
  58.         return true;
  59.     }
  60.  
  61.     void refuel() override {
  62.         m_fuelLevel = m_fuelLevelMax;
  63.         cout << m_name << ": Refueled to max capacity." << endl;
  64.     }
  65.  
  66.     void printStatus() override {
  67.         cout << m_name << " status: Mileage(km)=" << m_mileageKm
  68.             << ", Fuel level(l)=" << m_fuelLevel << endl;
  69.     }
  70.  
  71. protected:
  72.     double m_fuelConsumptionLitresPerKm;
  73.     double m_fuelLevel;
  74.     double m_fuelLevelMax;
  75.     int m_mileageKm;
  76.     string m_name;
  77. };
  78.  
  79. class Sedan : public AbstractCar {
  80. public:
  81.     Sedan() : AbstractCar(7.0, 50.0, "Sedan") {}
  82. };
  83.  
  84. class Suv : public AbstractCar {
  85. public:
  86.     Suv() : AbstractCar(11.0, 80.0, "Suv") {}
  87. };
  88.  
  89. class Bus : public AbstractCar {
  90. public:
  91.     Bus() : AbstractCar(25.0, 120.0, "Bus") {}
  92. };
  93.  
  94. class Bicycle : public IVehicle {
  95. public:
  96.     Bicycle() : m_mileageKm(0), m_name("Bicycle") {}
  97.  
  98.     bool drive(int kilometers) override {
  99.         m_mileageKm += kilometers;
  100.         cout << m_name << ": Drove " << kilometers << " km." << endl;
  101.         return true;
  102.     }
  103.  
  104.     void refuel() override {
  105.         // No operation for Bicycle
  106.     }
  107.  
  108.     void printStatus() override {
  109.         cout << m_name << " status: Mileage(km)=" << m_mileageKm << endl;
  110.     }
  111.  
  112. private:
  113.     int m_mileageKm;
  114.     string m_name;
  115. };
  116.  
  117. struct RoutePoint {
  118.     double xKm;
  119.     double yKm;
  120.     string name;
  121.  
  122.     RoutePoint(double x, double y, const string& pointName) : xKm(x), yKm(y), name(pointName) {}
  123. };
  124.  
  125. class Route {
  126. public:
  127.     void addPoint(const RoutePoint& point) {
  128.         points.push_back(point);
  129.     }
  130.  
  131.     void run(IVehicle* vehicle) {
  132.         if (points.size() < 2) {
  133.             cout << "Error: Route must have at least 2 points." << endl;
  134.             return;
  135.         }
  136.  
  137.         for (size_t i = 1; i < points.size(); ++i) {
  138.             const RoutePoint& from = points[i - 1];
  139.             const RoutePoint& to = points[i];
  140.             double distance = calculateDistance(from, to);
  141.  
  142.             cout << "Traveling from " << from.name << " to " << to.name << " (Distance: " << distance << " km)" << endl;
  143.  
  144.             while (!vehicle->drive(static_cast<int>(distance))) {
  145.                 vehicle->refuel();
  146.             }
  147.         }
  148.  
  149.         cout << "Route completed." << endl;
  150.     }
  151.  
  152. private:
  153.     vector<RoutePoint> points;
  154.  
  155.     double calculateDistance(const RoutePoint& from, const RoutePoint& to) const {
  156.         double dx = to.xKm - from.xKm;
  157.         double dy = to.yKm - from.yKm;
  158.         return sqrt(dx * dx + dy * dy);
  159.     }
  160. };
  161.  
  162. int main(int argc, char* argv[]) {
  163.     if (argc < 2) {
  164.         cerr << "Error: No route file specified. Usage: lab5.exe route.txt" << endl;
  165.         return 1;
  166.     }
  167.  
  168.     string fileName = argv[1];
  169.     ifstream inputFile(fileName);
  170.  
  171.     if (!inputFile.is_open()) {
  172.         cerr << "Error: Could not open file " << fileName << endl;
  173.         return 1;
  174.     }
  175.  
  176.     Route route;
  177.     string line;
  178.  
  179.     while (getline(inputFile, line)) {
  180.         istringstream iss(line);
  181.         double x, y;
  182.         string name;
  183.  
  184.         if (!(iss >> x >> y)) {
  185.             cerr << "Error: Invalid data format in file." << endl;
  186.             return 1;
  187.         }
  188.  
  189.         getline(iss, name);
  190.         if (name.empty()) {
  191.             cerr << "Error: Point name missing in file." << endl;
  192.             return 1;
  193.         }
  194.  
  195.         route.addPoint(RoutePoint(x, y, name));
  196.     }
  197.  
  198.     inputFile.close();
  199.  
  200.     IVehicle* vehicles[4] = { new Sedan, new Suv, new Bus, new Bicycle };
  201.     for (int i = 0; i < 4; ++i) {
  202.         cout << "Running route with ";
  203.         vehicles[i]->printStatus();
  204.         route.run(vehicles[i]);
  205.         cout << endl;
  206.     }
  207.  
  208.     for (int i = 0; i < 4; ++i)
  209.         delete vehicles[i];
  210.  
  211.     return 0;
  212. }
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement