Advertisement
newvol

Bussing

Mar 14th, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. class Car {
  5. protected:
  6.   int pos;
  7. public:
  8.   Car() {
  9.     this->pos = 0;
  10.   }
  11.   void move(int direction) {
  12.     this->pos += direction;
  13.   }
  14.   int get_position() {
  15.     return this->pos;
  16.   }
  17. };
  18.  
  19. class Bus: public Car {
  20.   int capacity;
  21.   int psg_count;
  22.   int *passengers;
  23. public:
  24.   Bus(int capacity) {
  25.     this->capacity = capacity;
  26.     psg_count = 0;
  27.     passengers = new int[capacity];
  28.   }
  29.   ~Bus() {
  30.     delete[] passengers;
  31.   }
  32.   bool add_passenger(int destination) {
  33.     if (capacity == psg_count) {
  34.       return false;
  35.     } else {
  36.       this->passengers[psg_count] = destination;
  37.       psg_count++;
  38.       return true;
  39.     }
  40.   }
  41.   void release() {
  42.     for (int i = 0; i < this->capacity; i++) {
  43.       if (this->pos == passengers[i]) {
  44.         psg_count--;
  45.         passengers[i] = 0;
  46.       }
  47.     }
  48.   }
  49.   int get_passenger_count() {
  50.     return psg_count;
  51.   }
  52. };
  53. int main() {
  54.   string cmd;
  55.   int cmd_count, capacity, temp;
  56.   cin >> cmd_count >> capacity;
  57.   Bus bussing(capacity);
  58.   for (int i = 0; i < cmd_count; i++) {
  59.     cin >> cmd;
  60.     if (cmd == "MOVE") {
  61.       cin >> temp;
  62.       bussing.move(temp);
  63.     } else if (cmd == "POSITION") {
  64.       cout << bussing.get_position() << endl;
  65.     } else if (cmd == "PASSENGER") {
  66.       cin >> temp;
  67.       if (bussing.add_passenger(temp)) {
  68.         cout << "SUCCESS" << endl;
  69.       } else {
  70.         cout << ":(" << endl;
  71.       }
  72.     } else if (cmd == "RELEASE") {
  73.       bussing.release();
  74.     } else {
  75.       cout << bussing.get_passenger_count() << endl;
  76.     }
  77.   }
  78.   return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement