Skip to content

Instantly share code, notes, and snippets.

@SPACE-DOGGO
Last active November 15, 2022 21:15
Show Gist options
  • Save SPACE-DOGGO/4a7e6739efc532dc3889ad3ad7fc0f81 to your computer and use it in GitHub Desktop.
Save SPACE-DOGGO/4a7e6739efc532dc3889ad3ad7fc0f81 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
struct Book
{
char name[20];
char author[20];
char publisher[20];
char genre[20];
};
void PrintByIndex(const vector<Book>& b, const size_t index);
int main()
{
const unsigned int NumberOfElements = 10;
const string items[] =
{
"1. Редактировать",
"2. Напечатать все",
"3. Найти по автору",
"4. Найти по названию",
"5. Сортировать по имени",
"6. Сортировать по автору",
"7. Сортировать по издателю",
};
unsigned int key;
unsigned int book_number;
string author;
string name;
vector<Book> books(NumberOfElements);
for (auto& value : books)
{
cout << "Имя, автор, издатель, жанр: ";
gets_s(value.name);
gets_s(value.author);
gets_s(value.publisher);
gets_s(value.genre);
}
while (true)
{
for (const auto& value : items)
{
cout << value << endl;
}
cout << ">> ";
cin >> key;
switch (key)
{
case 1:
cout << "Номер книги: ";
cin >> book_number;
cout << "Новое имя, автор, издатель, жанр: ";
gets_s(books[book_number].name);
gets_s(books[book_number].author);
gets_s(books[book_number].publisher);
gets_s(books[book_number].genre);
break;
case 2:
for (size_t i = 0; i < NumberOfElements; ++i)
{
PrintByIndex(books, i);
}
break;
case 3:
cout << "Автор: ";
cin >> author;
for (size_t i = 0; i < NumberOfElements; ++i)
{
if (books[i].author == author)
{
PrintByIndex(books, i);
}
}
break;
case 4:
cout << "Название: ";
cin >> name;
for (size_t i = 0; i < NumberOfElements; ++i)
{
if (books[i].name == name)
{
PrintByIndex(books, i);
}
}
break;
case 5:
sort(books.begin(), books.end(), [](const Book& b1, const Book& b2) { return b1.name < b2.name; });
break;
case 6:
sort(books.begin(), books.end(), [](const Book& b1, const Book& b2) { return b1.author < b2.author; });
break;
case 7:
sort(books.begin(), books.end(), [](const Book& b1, const Book& b2) { return b1.publisher < b2.publisher; });
break;
default:
break;
}
}
}
void PrintByIndex(const vector<Book>& b, const size_t index)
{
cout << "[" << index << "] "
<< b[index].name << " " << b[index].author << " "
<< b[index].publisher << " " << b[index].genre << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment