Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Room {
- int capacity;
- bool occupied;
- public:
- Room(int sz = 1, bool o = 0);
- bool is_occupied() { return (occupied ? 1 : 0); }
- int get_capacity() { return capacity; }
- void reserve() { occupied = 1; }
- void free() { occupied = 0; }
- void set_capacity(int k) { capacity = k; }
- };
- Room::Room(int sz, bool o) {
- capacity = sz;
- occupied = o;
- }
- class Hotel {
- int room_cnt;
- Room *rooms;
- public:
- Hotel(ifstream &file, int room_count);
- Hotel(const Hotel &);
- ~Hotel() { delete [] rooms; }
- int get_room_count() { return room_cnt; }
- Room& get_room(int p) { return rooms[p]; }
- int count_free();
- int find_room(int k);
- void free_room(int p) { rooms[p].free(); }
- };
- Hotel::Hotel (ifstream &file, int room_c) {
- room_cnt = room_c;
- rooms = new Room [room_c];
- int rmsz;
- for (int i = 0; i < room_cnt; i++) {
- file >> rmsz;
- rooms[i].set_capacity(rmsz);
- file >> rmsz;
- if (rmsz)
- rooms[i].reserve();
- else
- rooms[i].free();
- }
- }
- Hotel::Hotel (const Hotel &h) {
- for (int i = 0; i < room_cnt; ++i){
- rooms[i] = h.rooms[i];
- }
- room_cnt = h.room_cnt;
- }
- int Hotel::count_free() {
- int cnt = 0;
- for (int i = 0; i < room_cnt; ++i) {
- if (!rooms[i].is_occupied())
- cnt++;
- }
- return cnt;
- }
- int Hotel::find_room(int k) {
- for (int i = 0; i < room_cnt; ++i) {
- if (rooms[i].get_capacity() == k && !rooms[i].is_occupied()) {
- rooms[i].reserve();
- return i;
- }
- }
- return -1;
- }
- Hotel load_hotel(ifstream &file) {
- file.open("source.txt", ios::in);
- int n, ch;
- string c;
- file >> n;
- Hotel h = Hotel(file, n);
- file >> ch;
- for(int i = 0; i < ch; ++i) {
- file >> c;
- if(c == "RESERVE"){
- file >> n;
- h.find_room(n);
- } else {
- file >> n;
- h.free_room(n);
- }
- }
- return h;
- }
- void save_hotel(ofstream &file, Hotel h, Hotel h) {
- file.open("destination.txt", ios::out);
- file << h.get_room_count() << endl;
- for(int i = 0; i < h.get_room_count(); ++i) {
- file << h.get_room(i).get_capacity() << " " << h.get_room(i).is_occupied() << endl;
- }
- }
- int main() {
- ifstream in; ofstream out;
- save_hotel(out, load_hotel(in));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement