-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcinema.cpp
More file actions
444 lines (390 loc) · 15.9 KB
/
cinema.cpp
File metadata and controls
444 lines (390 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <iomanip>
#include <memory>
#include <stdexcept>
/// Класс "Киносеанс"
class MovieSession {
private:
std::tm sessionDateTime;
MovieSession* previousSession;
MovieSession* nextSession;
public:
MovieSession(std::tm dateTime)
: sessionDateTime(dateTime), previousSession(nullptr), nextSession(nullptr) {}
std::tm getDateTime() const { return sessionDateTime; }
void setDateTime(const std::tm& dateTime) { sessionDateTime = dateTime; }
MovieSession* getPrevious() const { return previousSession; }
void setPrevious(MovieSession* prev) { previousSession = prev; }
MovieSession* getNext() const { return nextSession; }
void setNext(MovieSession* next) { nextSession = next; }
};
/// Класс "Расписание сеансов фильма" — двусвязный список
class SessionSchedule {
private:
MovieSession* headSession;
public:
// Copy constructor
SessionSchedule(const SessionSchedule& other) : headSession(nullptr) {
MovieSession* current = other.headSession;
while (current) {
addSession(current->getDateTime());
current = current->getNext();
}
}
// Assignment operator
SessionSchedule& operator=(const SessionSchedule& other) {
if (this != &other) {
// Clean up existing sessions
MovieSession* current = headSession;
while (current) {
MovieSession* toDelete = current;
current = current->getNext();
delete toDelete;
}
headSession = nullptr;
// Copy sessions from the other schedule
current = other.headSession;
while (current) {
addSession(current->getDateTime());
current = current->getNext();
}
}
return *this;
}
void insertOrdered(MovieSession* newSession) {
std::tm newSessionTime = newSession->getDateTime(); // Create a copy to avoid modifying the original
std::tm headSessionTime = headSession ? headSession->getDateTime() : std::tm();
if (!headSession) headSessionTime = std::tm();
if (!headSession) headSessionTime = std::tm();
if (!headSession || std::mktime(&newSessionTime) < std::mktime(&headSessionTime)) {
newSession->setNext(headSession);
if (headSession) headSession->setPrevious(newSession);
headSession = newSession;
return;
}
MovieSession* current = headSession;
while (current->getNext()) {
std::tm nextSessionTime = current->getNext()->getDateTime(); // Create a copy
if (std::mktime(&nextSessionTime) >= std::mktime(&newSessionTime)) break;
current = current->getNext();
}
newSession->setNext(current->getNext());
if (current->getNext()) current->getNext()->setPrevious(newSession);
current->setNext(newSession);
newSession->setPrevious(current);
}
public:
SessionSchedule() : headSession(nullptr) {}
~SessionSchedule() {
MovieSession* current = headSession;
while (current) {
MovieSession* toDelete = current;
current = current->getNext();
delete toDelete;
}
}
void addSession(const std::tm& dateTime) {
MovieSession* newSession = new MovieSession(dateTime);
insertOrdered(newSession);
}
bool removeSession(const std::tm& dateTime) {
MovieSession* current = headSession;
while (current) {
std::tm currentDateTime = current->getDateTime(); // Create a copy
std::tm targetDateTime = dateTime; // Create a copy
if (std::difftime(std::mktime(¤tDateTime), std::mktime(&targetDateTime)) == 0) {
if (current->getPrevious()) current->getPrevious()->setNext(current->getNext());
else headSession = current->getNext();
if (current->getNext()) current->getNext()->setPrevious(current->getPrevious());
delete current;
return true;
}
current = current->getNext();
}
return false;
}
bool hasSession(const std::tm& dateTime) const {
MovieSession* current = headSession;
while (current) {
std::tm currentDateTime = current->getDateTime(); // Create a copy
std::tm targetDateTime = dateTime; // Create a copy
if (std::difftime(std::mktime(¤tDateTime), std::mktime(&targetDateTime)) == 0)
return true;
current = current->getNext();
}
return false;
}
int getSessionCount() const {
int count = 0;
MovieSession* current = headSession;
while (current) {
count++;
current = current->getNext();
}
return count;
}
std::vector<std::tm> getAllSessionTimes() const {
std::vector<std::tm> times;
MovieSession* current = headSession;
while (current) {
times.push_back(current->getDateTime());
current = current->getNext();
}
return times;
}
};
/// Класс "Кинофильм"
class Movie {
private:
std::string title;
int durationMinutes;
SessionSchedule schedule;
public:
Movie(const std::string& title, int durationMinutes)
: title(title), durationMinutes(durationMinutes) {}
// Copy constructor for deep copy
Movie(const Movie& other)
: title(other.title), durationMinutes(other.durationMinutes), schedule(other.schedule) {}
// Assignment operator for deep copy
Movie& operator=(const Movie& other) {
if (this != &other) {
title = other.title;
durationMinutes = other.durationMinutes;
schedule = other.schedule;
}
return *this;
}
std::string getTitle() const { return title; }
void setTitle(const std::string& newTitle) { title = newTitle; }
int getDurationMinutes() const { return durationMinutes; }
void setDurationMinutes(int minutes) { durationMinutes = minutes; }
int getTotalSessionCount() const { return schedule.getSessionCount(); }
int getTotalScreenTime() const { return schedule.getSessionCount() * durationMinutes; }
void addSessionToSchedule(const std::tm& dateTime) { schedule.addSession(dateTime); }
bool removeSessionFromSchedule(const std::tm& dateTime) { return schedule.removeSession(dateTime); }
bool hasSessionAt(const std::tm& dateTime) const { return schedule.hasSession(dateTime); }
std::vector<std::tm> getSessionTimes() const { return schedule.getAllSessionTimes(); }
};
/// Класс "Расписание кинотеатра" — кольцевая очередь фильмов
class CinemaSchedule {
private:
int capacity;
std::vector<std::unique_ptr<Movie> > movieArray;
int headIndex;
int tailIndex;
int movieCount;
public:
CinemaSchedule(int maxMovies)
: capacity(maxMovies), movieArray(maxMovies), headIndex(0), tailIndex(0), movieCount(0) {}
bool isFull() const { return movieCount == capacity; }
bool isEmpty() const { return movieCount == 0; }
bool addMovie(Movie&& movie) {
if (isFull()) return false;
movieArray[tailIndex] = std::make_unique<Movie>(std::move(movie));
tailIndex = (tailIndex + 1) % capacity;
movieCount++;
return true;
}
bool removeMovie(const std::string& title) {
if (isEmpty()) return false;
for (int i = 0; i < movieCount; ++i) {
int index = (headIndex + i) % capacity;
if (movieArray[index]->getTitle() == title) {
movieArray[index].reset();
for (int j = i; j < movieCount - 1; ++j) {
int currentIndex = (headIndex + j) % capacity;
int nextIndex = (headIndex + j + 1) % capacity;
movieArray[currentIndex] = std::move(movieArray[nextIndex]);
}
tailIndex = (tailIndex - 1 + capacity) % capacity;
movieCount--;
return true;
}
}
return false;
}
int getMovieCount() const { return movieCount; }
int getCapacity() const { return capacity; }
Movie* getMovieAt(int index) const {
if (index < 0 || index >= movieCount)
throw std::out_of_range("Invalid movie index");
return movieArray[(headIndex + index) % capacity].get();
}
};
// Функция для считывания времени
std::tm readTime() {
std::tm time = {};
int year, month, day, hour, minute;
while (true) {
std::cout << "Введите год (например, 2025): ";
std::cin >> year;
if (year >= 1900) break;
std::cout << "Неверный год. Попробуйте снова.\n";
}
while (true) {
std::cout << "Введите месяц (1-12): ";
std::cin >> month;
if (month >= 1 && month <= 12) break;
std::cout << "Неверный месяц. Попробуйте снова.\n";
}
while (true) {
std::cout << "Введите день (1-31): ";
std::cin >> day;
if (day >= 1 && day <= 31) break;
std::cout << "Неверный день. Попробуйте снова.\n";
}
while (true) {
std::cout << "Введите час (0-23): ";
std::cin >> hour;
if (hour >= 0 && hour <= 23) break;
std::cout << "Неверный час. Попробуйте снова.\n";
}
while (true) {
std::cout << "Введите минуту (0-59): ";
std::cin >> minute;
if (minute >= 0 && minute <= 59) break;
std::cout << "Неверная минута. Попробуйте снова.\n";
}
time.tm_year = year - 1900;
time.tm_mon = month - 1;
time.tm_mday = day;
time.tm_hour = hour;
time.tm_min = minute;
return time;
}
// Основное консольное меню
void displayMenu() {
std::cout << "\nМеню:\n";
std::cout << "1. Добавить фильм\n";
std::cout << "2. Добавить сеанс\n";
std::cout << "3. Удалить фильм\n";
std::cout << "4. Удалить сеанс\n";
std::cout << "5. Просмотреть все фильмы\n";
std::cout << "6. Просмотреть сеансы фильма\n";
std::cout << "0. Выйти\n";
}
int main() {
CinemaSchedule cinema(5);
int choice;
while (true) {
displayMenu();
std::cout << "Ваш выбор: ";
std::cin >> choice;
switch (choice) {
case 1: {
std::string title;
int duration;
std::cout << "Введите название фильма: ";
std::cin.ignore();
std::getline(std::cin, title);
std::cout << "Введите продолжительность фильма в минутах: ";
std::cin >> duration;
Movie movie(title, duration);
if (cinema.addMovie(std::move(movie))) {
std::cout << "Фильм добавлен!\n";
} else {
std::cout << "Расписание заполнено, невозможно добавить фильм.\n";
}
break;
}
case 2: {
std::string movieTitle;
std::cout << "Введите название фильма: ";
std::cin.ignore();
std::getline(std::cin, movieTitle);
bool movieFound = false;
for (int i = 0; i < cinema.getMovieCount(); ++i) {
Movie* movie = cinema.getMovieAt(i);
if (movie->getTitle() == movieTitle) {
movieFound = true;
std::cout << "Добавление сеанса для фильма " << movieTitle << ".\n";
std::tm sessionTime = readTime();
movie->addSessionToSchedule(sessionTime);
std::cout << "Сеанс добавлен.\n";
break;
}
}
if (!movieFound) {
std::cout << "Фильм не найден.\n";
}
break;
}
case 3: {
std::string movieTitle;
std::cout << "Введите название фильма для удаления: ";
std::cin.ignore();
std::getline(std::cin, movieTitle);
if (cinema.removeMovie(movieTitle)) {
std::cout << "Фильм удалён.\n";
} else {
std::cout << "Фильм не найден.\n";
}
break;
}
case 4: {
std::string movieTitle;
std::cout << "Введите название фильма для удаления сеанса: ";
std::cin.ignore();
std::getline(std::cin, movieTitle);
bool movieFound = false;
for (int i = 0; i < cinema.getMovieCount(); ++i) {
Movie* movie = cinema.getMovieAt(i);
if (movie->getTitle() == movieTitle) {
movieFound = true;
std::cout << "Удаление сеанса для фильма " << movieTitle << ".\n";
std::tm sessionTime = readTime();
if (movie->removeSessionFromSchedule(sessionTime)) {
std::cout << "Сеанс удалён.\n";
} else {
std::cout << "Сеанс не найден.\n";
}
break;
}
}
if (!movieFound) {
std::cout << "Фильм не найден.\n";
}
break;
}
case 5: {
std::cout << "Список фильмов:\n";
for (int i = 0; i < cinema.getMovieCount(); ++i) {
Movie* movie = cinema.getMovieAt(i);
std::cout << "Фильм: " << movie->getTitle() << ", продолжительность: " << movie->getDurationMinutes() << " минут\n";
}
break;
}
case 6: {
std::string movieTitle;
std::cout << "Введите название фильма для просмотра сеансов: ";
std::cin.ignore();
std::getline(std::cin, movieTitle);
bool movieFound = false;
for (int i = 0; i < cinema.getMovieCount(); ++i) {
Movie* movie = cinema.getMovieAt(i);
if (movie->getTitle() == movieTitle) {
movieFound = true;
std::cout << "Сеансы фильма " << movieTitle << ":\n";
std::vector<std::tm> sessionTimes = movie->getSessionTimes();
for (const std::tm& time : sessionTimes) {
std::cout << std::put_time(&time, "%Y-%m-%d %H:%M") << "\n";
}
break;
}
}
if (!movieFound) {
std::cout << "Фильм не найден.\n";
}
break;
}
case 0:
std::cout << "Выход...\n";
return 0;
default:
std::cout << "Неверный выбор, попробуйте снова.\n";
}
}
}