-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook_dynamic.cpp
More file actions
93 lines (84 loc) · 3.12 KB
/
Copy pathbook_dynamic.cpp
File metadata and controls
93 lines (84 loc) · 3.12 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
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct Book {
string name;
string author;
int age;
};
int main() {
int n;
int countBook = 0;
Book* books = nullptr;
do {
cout << "\n=========== MENU ===========" << endl;
cout << "1. Добавить книгу" << endl;
cout << "2. Найти по автору" << endl;
cout << "3. Сортировка по годам" << endl;
cout << "4. Вывод всех книг" << endl;
cout << "5. Выход" << endl;
cout << ": ";
cin >> n;
cin.ignore();
switch (n) {
case 1: {
Book* newBooks = new Book[countBook + 1];
for (int i = 0; i < countBook; i++) {
newBooks[i] = books[i];
}
cout << "Введите название книги: ";
cin >> newBooks[countBook].name;
cout << "Введите автора книги: ";
cin >> newBooks[countBook].author;
cout << "Введите год выпуска книги: ";
cin >> newBooks[countBook].age;
delete[] books;
books = newBooks;
countBook++;
cout << "Книга добавлена!" << endl;
break;
}
case 2: {
string a;
cout << "Введите автора книги для поиска: ";
getline(cin, a);
bool found = false;
for (int i = 0; i < countBook; i++) {
if (books[i].author == a) {
cout << "Книга найдена: " << endl;
cout << books[i].age << " — " << books[i].name << " (" << books[i].author << ")" << endl;
found = true;
}
}
if (!found) cout << "❌ Книги этого автора не найдены." << endl;
break;
}
case 3: {
if (countBook > 1) {
sort(books, books + countBook, [](const Book& a, const Book& b) {
return a.age < b.age;
});
cout << "✅ Книги отсортированы по годам." << endl;
} else {
cout << "Недостаточно книг для сортировки" << endl;
}
break;
}
case 4: {
if (countBook == 0) {
cout << "Библиотека пуста." << endl;
} else {
cout << "\nВсе книги в библиотеке:\n";
for (int i = 0; i < countBook; i++) {
cout << books[i].age << " — " << books[i].name << " (" << books[i].author << ")" << endl;
}
}
break;
}
default:
cout << "Неверный пункт меню!";
break;
}
} while(n != 5);
}