Created
May 2, 2025 03:22
-
-
Save Helen460/9e6dfe20662163a7c3d5441cdce418b5 to your computer and use it in GitHub Desktop.
9-DZ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <fstream> | |
| #include <string> | |
| #include <windows.h> | |
| using namespace std; | |
| // Структура для зберігання даних про продукт | |
| struct Product { | |
| string name; | |
| string category; | |
| float price = 0; | |
| int quantity = 0; | |
| }; | |
| // Перелік кольорів для тексту | |
| enum class Color : unsigned short { | |
| BLACK, DARKBLUE, DARKGREEN, TURQUOISE, DARKRED, PURPLE, DARKYELLOW, GREY, | |
| DARKGREY, BLUE, GREEN, CYAN, RED, MAGENTA, YELLOW, WHITE | |
| }; | |
| // Вивід тексту у кольорі в заданій позиції | |
| void show_text(int x, int y, Color color, const string& text) { | |
| HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
| COORD position = { SHORT(x), SHORT(y) }; | |
| SetConsoleCursorPosition(h, position); | |
| SetConsoleTextAttribute(h, (WORD)color); | |
| cout << text << "\n"; | |
| SetConsoleTextAttribute(h, (WORD)Color::WHITE); | |
| } | |
| // Показати меню | |
| void show_menu() { | |
| cout << "\nМеню:\n"; | |
| cout << "1. Переглянути всі продукти\n"; | |
| cout << "2. Додати новий продукт\n"; | |
| cout << "3. Знайти продукт за назвою\n"; | |
| cout << "4. Оновити продукт\n"; | |
| cout << "5. Видалити продукт\n"; | |
| cout << "6. Зберегти у файл\n"; | |
| cout << "7. Вийти\n"; | |
| cout << "Ваш вибір: "; | |
| } | |
| // Вивід списку продуктів | |
| void view_products(Product* products, int count) { | |
| if (count == 0) { | |
| cout << "База порожня.\n"; | |
| return; | |
| } | |
| cout << "\nНазва\t\tКатегорія\t\tЦіна\tКількість\n"; | |
| cout << "-----------------------------------------------------\n"; | |
| for (int i = 0; i < count; i++) { | |
| cout << products[i].name << "\t\t" | |
| << products[i].category << "\t\t" | |
| << products[i].price << "\t" | |
| << products[i].quantity << "\n"; | |
| } | |
| } | |
| // Додати новий продукт | |
| void add_product(Product*& products, int& count) { | |
| Product new_product; | |
| cout << "Назва продукту: "; | |
| getline(cin >> ws, new_product.name); | |
| cout << "Категорія: "; | |
| getline(cin >> ws, new_product.category); | |
| cout << "Ціна: "; | |
| while (!(cin >> new_product.price)) { | |
| cout << "Помилка. Введіть число: "; | |
| cin.clear(); | |
| cin.ignore(1000, '\n'); | |
| } | |
| cout << "Кількість: "; | |
| while (!(cin >> new_product.quantity)) { | |
| cout << "Помилка. Введіть ціле число: "; | |
| cin.clear(); | |
| cin.ignore(1000, '\n'); | |
| } | |
| cin.ignore(); | |
| Product* temp = new Product[count + 1]; | |
| for (int i = 0; i < count; i++) { | |
| temp[i] = products[i]; | |
| } | |
| temp[count] = new_product; | |
| delete[] products; | |
| products = temp; | |
| count++; | |
| cout << "Продукт додано.\n"; | |
| } | |
| // Пошук продукту за назвою | |
| void find_product(Product* products, int count) { | |
| string name; | |
| cout << "Введіть назву продукту: "; | |
| getline(cin >> ws, name); | |
| for (int i = 0; i < count; i++) { | |
| if (products[i].name == name) { | |
| cout << "Знайдено: " << products[i].name << ", " << products[i].category | |
| << ", " << products[i].price << " грн, " | |
| << products[i].quantity << " шт.\n"; | |
| return; | |
| } | |
| } | |
| cout << "Продукт не знайдено.\n"; | |
| } | |
| // Оновлення продукту | |
| void update_product(Product* products, int count) { | |
| string name; | |
| cout << "Назва продукту для оновлення: "; | |
| getline(cin >> ws, name); | |
| for (int i = 0; i < count; i++) { | |
| if (products[i].name == name) { | |
| cout << "1. Нова ціна\n2. Нова кількість\nВаш вибір: "; | |
| int option; | |
| cin >> option; | |
| if (option == 1) { | |
| cout << "Нова ціна: "; | |
| cin >> products[i].price; | |
| } | |
| else if (option == 2) { | |
| cout << "Нова кількість: "; | |
| cin >> products[i].quantity; | |
| } | |
| else { | |
| cout << "Невірний вибір.\n"; | |
| } | |
| cin.ignore(); | |
| return; | |
| } | |
| } | |
| cout << "Продукт не знайдено.\n"; | |
| } | |
| // Видалення продукту | |
| void delete_product(Product*& products, int& count) { | |
| string name; | |
| cout << "Назва продукту для видалення: "; | |
| getline(cin >> ws, name); | |
| int index = -1; | |
| for (int i = 0; i < count; i++) { | |
| if (products[i].name == name) { | |
| index = i; | |
| break; | |
| } | |
| } | |
| if (index == -1) { | |
| cout << "Продукт не знайдено.\n"; | |
| return; | |
| } | |
| Product* temp = new Product[count - 1]; | |
| for (int i = 0, j = 0; i < count; i++) { | |
| if (i != index) { | |
| temp[j++] = products[i]; | |
| } | |
| } | |
| delete[] products; | |
| products = temp; | |
| count--; | |
| cout << "Продукт видалено.\n"; | |
| } | |
| // Зберегти базу у файл | |
| void save_to_file(Product* products, int count) { | |
| ofstream file("products.txt"); | |
| if (!file) { | |
| cout << "Помилка відкриття файлу.\n"; | |
| return; | |
| } | |
| for (int i = 0; i < count; i++) { | |
| file << products[i].name << "," << products[i].category << "," | |
| << products[i].price << "," << products[i].quantity << "\n"; | |
| } | |
| file.close(); | |
| cout << "Дані збережено в 'products.txt'.\n"; | |
| } | |
| // Завантаження з файлу | |
| void load_from_file(Product*& products, int& count) { | |
| ifstream file("products.txt"); | |
| if (!file) return; | |
| string name, category; | |
| float price; | |
| int quantity; | |
| while (getline(file, name, ',') && | |
| getline(file, category, ',') && | |
| file >> price && | |
| file.ignore() && | |
| file >> quantity) { | |
| file.ignore(); | |
| Product* temp = new Product[count + 1]; | |
| for (int i = 0; i < count; i++) { | |
| temp[i] = products[i]; | |
| } | |
| temp[count++] = { name, category, price, quantity }; | |
| delete[] products; | |
| products = temp; | |
| } | |
| file.close(); | |
| } | |
| // Головна функція | |
| int main() { | |
| SetConsoleCP(65001); | |
| SetConsoleOutputCP(65001); | |
| SetConsoleOutputCP(1251); | |
| show_text(5, 1, Color::YELLOW, "Консольний застосунок — База продуктів"); | |
| Product* products = nullptr; | |
| int count = 0; | |
| load_from_file(products, count); | |
| int choice; | |
| while (true) { | |
| show_menu(); | |
| cin >> choice; | |
| cin.ignore(); | |
| switch (choice) { | |
| case 1: view_products(products, count); break; | |
| case 2: add_product(products, count); break; | |
| case 3: find_product(products, count); break; | |
| case 4: update_product(products, count); break; | |
| case 5: delete_product(products, count); break; | |
| case 6: save_to_file(products, count); break; | |
| case 7: | |
| save_to_file(products, count); | |
| delete[] products; | |
| cout << "Програма завершена.\n"; | |
| return 0; | |
| default: | |
| cout << "Невірний вибір. Спробуйте ще раз.\n"; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment