Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------Вариант 4----------------------------
- #include <iostream>
- #include <string>
- #include <clocale>
- #include <windows.h>
- #include <vector>
- struct books
- {
- int UDK;
- char authorName[40];
- char bookName[40];
- int year;
- int count;
- };
- using namespace std;
- void Pause()
- {
- cout << endl << "---Продолжить - Esc.";
- while (!GetAsyncKeyState(VK_ESCAPE));
- system("cls");
- }
- int IntInput() // Проверка на Int
- {
- int imputNumber;
- while (!(cin >> imputNumber) || (cin.peek() != '\n'))
- {
- cin.clear();
- while (cin.get() != '\n');
- cout << "Это не целое число. Попробуйте еще раз.\n";
- }
- return imputNumber;
- }
- void AddBook(vector <books> &Books)
- {
- books Book;
- cout << endl << "Введите УДК: ";
- do
- {
- bool isAlreadyBe = false;
- Book.UDK = IntInput();
- for (int i = 0; i < (int)Books.size(); i++)
- if (Books[i].UDK == Book.UDK)
- isAlreadyBe = true;
- if (isAlreadyBe)
- cout << endl << "Книга с таким УДК уже есть в списке. Введите другой УДК: ";
- else break;
- } while (true);
- cout << endl << "Введите имя автора книги: ";
- cin.get(); //Типо костыль, но не совсем
- cin.getline(Book.authorName, 40);
- cout << endl << "Введите название книги: ";
- cin.getline(Book.bookName, 40);
- cout << endl << "Введите год издания книги: ";
- Book.year = IntInput();
- cout << endl << "Введите количество книг: ";
- Book.count = IntInput();
- Books.push_back(Book);
- }
- void RemoveBook(vector <books> &Books)
- {
- cout << "Введите индекс книги, которую нужно удалить: ";
- int UDK = IntInput();
- bool isHave = false;
- for (int i = 0; i < (int)Books.size(); i++)
- {
- if (Books[i].UDK == UDK)
- {
- Books.erase(Books.begin() + i);
- cout << "Книга удалена";
- isHave = true;
- break;
- }
- }
- if (!isHave) cout << "Такой книги нет";
- }
- void ShowBook(vector <books> &Books)
- {
- for (int i = 0; i < (int)Books.size(); i++)
- {
- cout << "---Книга #" << i + 1 << endl;
- cout << "УДК: " << Books[i].UDK << endl;
- cout << "Автор: " << Books[i].authorName << endl;
- cout << "Название: " << Books[i].bookName << endl;
- cout << "Год выпуска: " << Books[i].year << endl;
- cout << "Количество: " << Books[i].count << endl;
- }
- }
- void FindBooks(vector <books> &Books)
- {
- cout << "Введите минимальное количество книг для поискового фильтра: ";
- int count = IntInput();
- for (int i = 0; i < (int)Books.size(); i++)
- {
- if (Books[i].count > count)
- {
- cout << "---Книга #" << i + 1 << endl;
- cout << "УДК: " << Books[i].UDK << endl;
- cout << "Автор: " << Books[i].authorName << endl;
- cout << "Название: " << Books[i].bookName << endl;
- cout << "Год выпуска: " << Books[i].year << endl;
- cout << "Количество: " << Books[i].count << endl;
- }
- }
- }
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- vector <books> Books;
- int option;
- while (true)
- {
- system("cls");
- cout << "---Выберите опцию: " << endl;
- cout << "1 - Добавить книгу" << endl;
- cout << "2 - Удалить книгу" << endl;
- cout << "3 - Показать все книги" << endl;
- cout << "4 - Найти книгу по фильтру" << endl;
- cout << "5 - Выход" << endl;
- option = IntInput();
- system("cls");
- switch (option)
- {
- case 1: AddBook(Books); break;
- case 2: RemoveBook(Books); break;
- case 3: ShowBook(Books); break;
- case 4: FindBooks(Books); break;
- case 5: return 0;
- }
- Pause();
- }
- }
- ----------------------------------------Дополнительно----------------------------------------
- #include <iostream>
- #include <clocale>
- #include <conio.h>
- #include <windows.h>
- #include <string>
- using namespace std;
- int IntInput() // Проверка на Int
- {
- int imputNumber;
- while (!(cin >> imputNumber) || (cin.peek() != '\n'))
- {
- cin.clear();
- while (cin.get() != '\n');
- cout << "Это не целое число. Попробуйте еще раз.\n";
- }
- return imputNumber;
- }
- class Node
- {
- public:
- int UDK;
- char authorName[40];
- char bookName[40];
- int year;
- int count;
- int pos;
- Node* next;
- Node()
- {
- UDK = 0;
- authorName[40] = ' ';
- bookName[40] = ' ';
- year = 0;
- count = 0;
- pos = 0;
- next = NULL;
- }
- };
- void Numeric(Node* head)
- {
- Node* ptr = head;
- int idx = 1;
- while (ptr)
- {
- ptr->pos = idx;
- idx++;
- ptr = ptr->next;
- }
- }
- class List
- {
- private:
- Node* head;
- int Length;
- public:
- List() { head = NULL; Length = 0; }
- ~List()
- {
- Node* ptr = head, *tmp = NULL;
- if (head)
- {
- while (ptr->next)
- {
- tmp = ptr;
- delete[] ptr;
- ptr = tmp;
- }
- delete[] ptr;
- }
- }
- //Методы
- void Show(bool flag)
- {
- cout << "Текущий список:" << endl;
- Node* ptr = head;
- int i = 0;
- if (!flag)
- {
- if (head == NULL)
- {
- cout << "Список пуст." << endl;
- }
- while (ptr)
- {
- cout << "---Книга: " << ++i << endl;
- cout << "УДК: " << ptr->UDK << endl;
- cout << "Автор: " << ptr->authorName << endl;
- cout << "Название: " << ptr->bookName << endl;
- cout << "Год издания: " << ptr->year << endl;
- cout << "Количество: " << ptr->count << endl;
- ptr = ptr->next;
- }
- cout << endl;
- system("pause");
- }
- else
- {
- if (head == NULL)
- {
- cout << "Список пуст." << endl;
- }
- else
- {
- Numeric(head);
- while (ptr)
- {
- cout << "---Книга: " << ++i << endl;
- cout << "УДК: " << ptr->UDK << endl;
- cout << "Автор: " << ptr->authorName << endl;
- cout << "Название: " << ptr->bookName << endl;
- cout << "Год издания: " << ptr->year << endl;
- cout << "Количество: " << ptr->count << endl;
- ptr = ptr->next;
- }
- }
- }
- }
- void Add()
- {
- cin.clear();
- cin.sync();
- cout << "Введите УДК: ";
- int UDK = IntInput();
- char authorName[40], bookName[40];
- cout << "Введите автора: ";
- cin.get();
- cin.getline(authorName, 40);
- cout << "Введите название: ";
- cin.getline(bookName, 40);
- cout << "Введите год издания: ";
- int year = IntInput();
- cout << "Введите количество: ";
- int count = IntInput();
- Node* ptr = NULL;
- ptr = new Node;
- ptr->UDK = UDK;
- strcpy_s(ptr->authorName, authorName);
- strcpy_s(ptr->bookName, bookName);
- ptr->year = year;
- ptr->count = count;
- if (head == NULL)
- ptr->next = NULL;
- else
- ptr->next = head;
- head = ptr;
- Length++;
- cout << "Книга - " << bookName << " добавлена" << endl;
- cout << endl;
- system("pause");
- }
- void Delete()
- {
- if (head)
- {
- Show(true);
- cout << "Выберите элемент, введя его номер позиции: ";
- cin.clear();
- cin.sync();
- int temp = IntInput();
- if (temp > Length || temp == 0)
- {
- cout << "Ввели неправильный номер элемента. Повторите ввод." << endl;
- }
- else
- {
- Node* ptr = head, *tmp = NULL, *ToDel = NULL, *prev = NULL;
- int position = 0, Value = 0;
- while (ptr)
- {
- if (ptr->pos == temp)
- {
- ToDel = ptr;
- Length--;
- position = ptr->pos;
- Value = ptr->UDK;
- break;
- }
- else
- {
- prev = ptr;
- ptr = ptr->next;
- }
- }
- if (ToDel)
- {
- if (ToDel == head)
- {
- tmp = ToDel->next;
- delete[] ToDel;
- head = tmp;
- }
- else
- {
- if (ToDel->next == NULL)
- {
- delete[] ToDel;
- prev->next = NULL;
- }
- else
- {
- prev->next = ToDel->next;
- delete[] ToDel;
- }
- }
- cout << "Элемента списка под номером {" << position << ") " << Value << "} удален успешно." << endl;
- }
- }
- cout << endl;
- system("pause");
- }
- }
- void Clean()
- {
- Node* ptr = head, *tmp = NULL;
- if (head)
- {
- while (ptr->next)
- {
- tmp = ptr->next;
- delete[] ptr;
- ptr = tmp;
- }
- delete[] ptr;
- head = NULL;
- Length = 0;
- cout << "Список успешно очищен." << endl;
- }
- else
- {
- cout << "Список пуст. Нечего очищать." << endl;
- }
- cout << endl;
- system("pause");
- }
- void Find()
- {
- cout << "Введите минимальное количество книг для поискового фильтра: ";
- int count = IntInput();
- Node* ptr = head;
- int i = 0;
- while (ptr)
- {
- if (ptr->count > count)
- {
- cout << "---Книга: " << ++i << endl;
- cout << "УДК: " << ptr->UDK << endl;
- cout << "Автор: " << ptr->authorName << endl;
- cout << "Название: " << ptr->bookName << endl;
- cout << "Год издания: " << ptr->year << endl;
- cout << "Количество: " << ptr->count << endl;
- ptr = ptr->next;
- }
- }
- cout << endl;
- system("pause");
- }
- };
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- List point;
- int buf = 0;
- while (true)
- {
- system("cls");
- cout << "---Выберите опцию: " << endl;
- cout << "1 - Добавление элемента в список" << endl;
- cout << "2 - Удаление элемента из списка" << endl;
- cout << "3 - Вывод списка" << endl;
- cout << "4 - Очистка списка" << endl;
- cout << "5 - Поиск по фильтру" << endl;
- cout << "ESC - Выход" << endl;
- switch (_getch())
- {
- case 49:
- cout << endl;
- cout << "Добавление элемента в список." << endl;
- point.Add();
- break;
- case 50:
- cout << endl;
- cout << "Удаление элемента из списка." << endl;
- point.Delete();
- break;
- case 51:
- cout << endl;
- cout << "Вывод списка." << endl;
- point.Show(false);
- break;
- case 52:
- cout << endl;
- cout << "Очистка списка." << endl;
- point.Clean();
- break;
- case 53:
- cout << endl;
- cout << "Поиск книг по фильтру." << endl;
- point.Find();
- break;
- case 27://ESC button
- return 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment