Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstring>
  4. #include <fstream>
  5. #include <cstdio>
  6. #include <cmath>
  7. using namespace std;
  8.  
  9. //ПЕРСОНАЛЬНО НАЙДУ И ВЫРЕЖУ ГОРЛО ТОМУ, КТО СПИШЕТ НИКАК НЕ ИЗМЕНИВ КОД
  10.  
  11. class Room {
  12. int capacity; // Количество гостей, на которое комната рассчитана
  13. bool occupied; // Занята ли комната
  14.  
  15. public:
  16. Room(int capacity = 1, bool occupied = false);
  17. Room(const Room& tmp);
  18. bool is_occupied(){return occupied;} // Метод, который позволяет узнать, занята ли комната
  19. int get_capacity(){return capacity;} // Метод, который получает количество гостей, которые умещаются в комнату
  20. void reserve(){this->occupied = 1;} // Занять номер
  21. void free(){this->occupied = 0;} // Освободить номер
  22. };
  23. Room::Room(int capacity, bool occupied){
  24. cin >> capacity;
  25. this->capacity = capacity;
  26. this->occupied = occupied;
  27. }
  28. Room::Room(const Room &tmp){
  29. this->capacity = tmp.capacity;
  30. this->occupied = tmp.occupied;
  31. }
  32.  
  33. //------------------------
  34. class Hotel {
  35. int room_count;
  36. Room* rooms; // Дин. массив комнат
  37.  
  38. public:
  39. Hotel(int room_count);
  40. Hotel(const Hotel &tmp);
  41. ~Hotel();
  42.  
  43. void free_room(int num){this->rooms[num].free();}
  44. int get_room_count(){return room_count;} // Узнать количество комнат
  45. Room& get_room(int p); // Получить комнату под номером p
  46. int count_free(); // Подсчитать количество свободных комнат
  47. int find_room(int k); // Найти комнату ровно на k гостей
  48. };
  49. Hotel::Hotel(int room_count){
  50. this->room_count = room_count;
  51. this->rooms = new Room[this->room_count];
  52. for (int i = 0; i < room_count; ++i){
  53. rooms[i].free();
  54. }
  55. }
  56. Hotel::Hotel(const Hotel &tmp){
  57. for (int i = 0; i < this->room_count; ++i){
  58. this->rooms[i] = tmp.rooms[i];
  59. }
  60. this->room_count = tmp.room_count;
  61. }
  62.  
  63. Hotel::~Hotel(){
  64. delete [] rooms;
  65. }
  66. Room& Hotel::get_room(int p){
  67. return rooms[p];
  68. }
  69.  
  70. int Hotel::count_free(){
  71. int count = 0;
  72. for (int i = 0; i < room_count; ++i){
  73. if (!rooms[i].is_occupied())
  74. ++count;
  75. }
  76. return count;
  77. }
  78. int Hotel::find_room(int k){
  79. for (int i = 0; i < room_count; ++i){
  80. if (!rooms[i].is_occupied() && rooms[i].get_capacity() == k){
  81. rooms[i].reserve();
  82. return i;
  83. }
  84. }
  85. return -1;
  86. }
  87.  
  88. int main() {
  89. int n, rooms;
  90. cin >>rooms;
  91. Hotel a(rooms);
  92. cin >> n;
  93. for (int i = 0; i < n; ++i){
  94. char command[8] = {};
  95. cin >> command;
  96. if (!strcmp(command, "RESERVE")){
  97. int ppl;
  98. cin >> ppl;
  99. cout << a.find_room(ppl) << endl;
  100. }
  101. if (!strcmp(command, "FREE")){
  102. int num;
  103. cin >> num;
  104. a.free_room(num);
  105. }
  106. if (!strcmp(command, "STATUS")){
  107. cout << a.count_free() << endl;
  108. }
  109. }
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement