Advertisement
SillyWolfy

lab 10

May 13th, 2024
935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.57 KB | None | 0 0
  1. #define NOMINMAX
  2. #include <windows.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <limits>
  6. using namespace std;
  7.  
  8. template<typename T>
  9. struct Node {
  10.     T data;
  11.     Node* next;
  12.  
  13.     Node(T val) : data(val), next(nullptr) {}
  14. };
  15.  
  16. template<typename T>
  17. class LinkedList {
  18. private:
  19.     Node<T>* head;
  20.  
  21. public:
  22.     LinkedList() : head(nullptr) {}
  23.     void push_back(T value) {
  24.         Node<T>* newNode = new Node<T>(value);
  25.         if (head == nullptr) {
  26.             head = newNode;
  27.         }
  28.         else {
  29.             Node<T>* temp = head;
  30.             while (temp->next != nullptr) {
  31.                 temp = temp->next;
  32.             }
  33.             temp->next = newNode;
  34.         }
  35.     }
  36.     void remove_at(int position) {
  37.         Node<T>* temp = head;
  38.         if (position == 0) {
  39.             head = head->next;
  40.             delete temp;
  41.             return;
  42.         }
  43.         for (int i = 0; temp != nullptr and i < position - 1; ++i) {
  44.             temp = temp->next;
  45.         }
  46.         Node<T>* nodeToDelete = temp->next;
  47.         temp->next = temp->next->next;
  48.         delete nodeToDelete;
  49.     }
  50.     void print() const {
  51.         Node<T>* temp = head;
  52.         while (temp != nullptr) {
  53.             temp->data.print();
  54.             cout << '\n';
  55.             temp = temp->next;
  56.         }
  57.     }
  58.     int size() const {
  59.         int count = 0;
  60.         Node<T>* temp = head;
  61.         while (temp != nullptr) {
  62.             count++;
  63.             temp = temp->next;
  64.         }
  65.         return count;
  66.     }
  67.     T& get(int index) {
  68.         Node<T>* temp = head;
  69.         for (int i = 0; temp != nullptr and i < index; ++i) {
  70.             temp = temp->next;
  71.         }
  72.         return temp->data;
  73.     }
  74.     ~LinkedList() {
  75.         Node<T>* current = head;
  76.         Node<T>* nextNode;
  77.         while (current != nullptr) {
  78.             nextNode = current->next;
  79.             delete current;
  80.             current = nextNode;
  81.         }
  82.     }
  83. };
  84.  
  85. class Product
  86. {
  87. public:
  88.     Product();
  89.     Product(string namee, int weightt, int pricee, int lifee) {
  90.         this->name = namee;
  91.         this->weight = weightt;
  92.         this->price = pricee;
  93.         this->life = lifee;
  94.     }
  95.     void print() const {
  96.         cout << "Название продукта: " << this->name << '\n'
  97.             << "Вес продукта: " << this->weight << '\n'
  98.             << "Цена продукта: " << this->price << '\n'
  99.             << "Срок годности продукта: " << this->life << '\n';
  100.         if (life <= 2) {
  101.             cout << "Данный товар идёт по уценке!\n";
  102.         }
  103.     }
  104.     string get_name() const {
  105.         return this->name;
  106.     }
  107. private:
  108.     string name;
  109.     int weight;
  110.     int price;
  111.     int life;
  112. };
  113.  
  114. void add_product(LinkedList<Product>& database) {
  115.     cout << "\tДобавление продукта в список\n";
  116.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  117.     cout << "Введите название продукта: ";
  118.     string name;
  119.     getline(cin, name);
  120.     cout << '\n';
  121.     cout << "Введите вес продукта: ";
  122.     int weight;
  123.     while (true) {
  124.         cin >> weight;
  125.         if (cin.fail() or weight <= 0) {
  126.             cin.clear();
  127.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  128.             cout << "Некорректный ввод. Пожалуйста, введите положительное число: ";
  129.         }
  130.         else {
  131.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  132.             break;
  133.         }
  134.     }
  135.     cout << '\n';
  136.     cout << "Введите цену продукта: ";
  137.     int price;
  138.     while (true) {
  139.         cin >> price;
  140.         if (cin.fail() or price <= 0) {
  141.             cin.clear();
  142.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  143.             cout << "Некорректный ввод. Пожалуйста, введите положительное число: ";
  144.         }
  145.         else {
  146.             break;
  147.         }
  148.     }
  149.     cout << '\n';
  150.     cout << "Введите срок годности продукта в днях продукта: ";
  151.     int life;
  152.     while (true) {
  153.         cin >> life;
  154.         if (cin.fail() or life <= 0) {
  155.             cin.clear();
  156.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  157.             cout << "Некорректный ввод. Пожалуйста, введите положительное число: ";
  158.         }
  159.         else {
  160.             break;
  161.         }
  162.     }
  163.     database.push_back(Product(name, weight, price, life));
  164. }
  165.  
  166. void delete_product(LinkedList<Product>& database, int index) {
  167.     database.remove_at(index - 1);
  168. }
  169.  
  170. int main()
  171. {
  172.     SetConsoleOutputCP(1251);
  173.     SetConsoleCP(1251);
  174.     setlocale(LC_ALL, "Russian");
  175.     LinkedList<Product> DateBase;
  176.     while (true) {
  177.         cout << "\tMeню\n";
  178.         cout << "Необходимо выбрать один из пунктов:\n";
  179.         cout << "1) Добавить продукт в список\n";
  180.         cout << "2) Удалить продукт из списка\n";
  181.         cout << "3) Вывести список\n";
  182.         cout << "4) Закончить программу\n";
  183.         cout << "Введите номер необходимого пункта: ";
  184.         int choice;
  185.         while (true) {
  186.             cin >> choice;
  187.             if (cin.fail() or choice <= 0) {
  188.                 cin.clear();
  189.                 cin.ignore();
  190.                 cout << "Некорректный ввод. Пожалуйста, введите положительное число: ";
  191.             }
  192.             else {
  193.                 break;
  194.             }
  195.         }
  196.         switch (choice) {
  197.         case 1: {
  198.             system("cls");
  199.             add_product(DateBase);
  200.             system("pause");
  201.             system("cls");
  202.             break;
  203.         }
  204.         case 2: {
  205.             system("cls");
  206.             int dbSize = DateBase.size();
  207.             if (dbSize == 0) {
  208.                 cout << "В вашем списке ничего нет!\n";
  209.                 system("pause");
  210.                 system("cls");
  211.                 break;
  212.             }
  213.             for (int i = 0; i < dbSize; ++i) {
  214.                 cout << i + 1 << ". " << DateBase.get(i).get_name() << '\n';
  215.             }
  216.             cout << "Введите индекс продукта, который нужно удалить: ";
  217.             int index;
  218.             while (true) {
  219.                 cin >> index;
  220.                 if (cin.fail() or index <= 0 or index > dbSize) {
  221.                     cin.clear();
  222.                     cin.ignore();
  223.                     cout << "Некорректный ввод. Пожалуйста, введите число из доступного диапазона: ";
  224.                 }
  225.                 else {
  226.                     break;
  227.                 }
  228.             }
  229.             delete_product(DateBase, index);
  230.             system("pause");
  231.             system("cls");
  232.             break;
  233.         }
  234.         case 3: {
  235.             system("cls");
  236.             if (DateBase.size() == 0) {
  237.                 cout << "В вашем списке ничего нет!\n";
  238.                 system("pause");
  239.                 system("cls");
  240.                 break;
  241.             }
  242.             DateBase.print();
  243.             system("pause");
  244.             system("cls");
  245.             break;
  246.         }
  247.         case 4: {
  248.             return 0;
  249.         }
  250.         default:
  251.             system("cls");
  252.             break;
  253.         }
  254.     }
  255. }
  256.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement