Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class Room {
  6.     int capacity;
  7.     bool occupied;
  8.  
  9. public:
  10.     Room(int sz = 1, bool o = 0);
  11.     bool is_occupied() { return (occupied ? 1 : 0); }
  12.     int get_capacity() { return capacity; }
  13.     void reserve() { occupied = 1; }
  14.     void free() { occupied = 0; }
  15.     void set_capacity(int k) { capacity = k; }
  16. };
  17.  
  18. Room::Room(int sz, bool o) {
  19.     capacity = sz;
  20.     occupied = o;
  21. }
  22.  
  23. class Hotel {
  24.     int room_cnt;
  25.     Room *rooms;
  26.     public:
  27.       Hotel(ifstream &file, int room_count);
  28.       Hotel(const Hotel &);
  29.       ~Hotel() { delete [] rooms; }
  30.  
  31.       int get_room_count() { return room_cnt; }
  32.       Room& get_room(int p) { return rooms[p]; }
  33.       int count_free();
  34.       int find_room(int k);
  35.       void free_room(int p) { rooms[p].free(); }
  36. };
  37.  
  38. Hotel::Hotel (ifstream &file, int room_c) {
  39.     room_cnt = room_c;
  40.     rooms = new Room [room_c];
  41.     int rmsz;
  42.     for (int i = 0; i < room_cnt; i++) {
  43.         file >> rmsz;
  44.         rooms[i].set_capacity(rmsz);
  45.         file >> rmsz;
  46.         if (rmsz)
  47.             rooms[i].reserve();
  48.         else
  49.             rooms[i].free();
  50.     }
  51. }
  52.  
  53. Hotel::Hotel (const Hotel &h) {
  54.     for (int i = 0; i < room_cnt; ++i){
  55.             rooms[i] = h.rooms[i];
  56.         }
  57.     room_cnt = h.room_cnt;
  58. }
  59.  
  60. int Hotel::count_free() {
  61.     int cnt = 0;
  62.     for (int i = 0; i < room_cnt; ++i) {
  63.         if (!rooms[i].is_occupied())
  64.             cnt++;
  65.     }
  66.     return cnt;
  67. }
  68.  
  69. int Hotel::find_room(int k) {
  70.     for (int i = 0; i < room_cnt; ++i) {
  71.         if (rooms[i].get_capacity() == k && !rooms[i].is_occupied()) {
  72.             rooms[i].reserve();
  73.             return i;
  74.         }
  75.     }
  76.     return -1;
  77. }
  78.  
  79. Hotel load_hotel(ifstream &file) {
  80.     file.open("source.txt", ios::in);
  81.     int n, ch;
  82.     string c;
  83.     file >> n;
  84.     Hotel h = Hotel(file, n);
  85.     file >> ch;
  86.     for(int i = 0; i < ch; ++i) {
  87.         file >> c;
  88.         if(c == "RESERVE"){
  89.             file >> n;
  90.             h.find_room(n);
  91.         } else {
  92.             file >> n;
  93.             h.free_room(n);
  94.         }
  95.     }
  96.     return h;
  97. }
  98.  
  99. void save_hotel(ofstream &file, Hotel h, Hotel h) {
  100.     file.open("destination.txt", ios::out);
  101.     file << h.get_room_count() << endl;
  102.     for(int i = 0; i < h.get_room_count(); ++i) {
  103.         file << h.get_room(i).get_capacity() << " " << h.get_room(i).is_occupied() << endl;
  104.     }
  105. }
  106.  
  107. int main() {
  108.     ifstream in; ofstream out;
  109.     save_hotel(out, load_hotel(in));
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement