Advertisement
yurdmitriev

Untitled

Apr 14th, 2020
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <queue>
  4. #include <windows.h>
  5.  
  6. using namespace std;
  7.  
  8. void addElement(string, string, float, int);
  9. void removeElement(int);
  10. void loadQueue();
  11. void writeQueue();
  12. void listQueue();
  13. void findMinPrice();
  14.  
  15. queue<struct Product> products;
  16.  
  17. struct Product
  18. {
  19.     string name;
  20.     string manufacturer;
  21.     float price;
  22.     int year;
  23. };
  24.  
  25. int main()
  26. {
  27.     SetConsoleCP(1251);
  28.     SetConsoleOutputCP(1251);
  29.  
  30.     loadQueue();
  31.  
  32.     while(true)
  33.     {
  34.         int choice;
  35.         system("cls");
  36.         cout << "Выберите нужное вам действие: " << endl;
  37.         cout << "1) Добавить товар" << endl
  38.         << "2) Удалить товар" << endl
  39.         << "3) Редактировать данные"
  40.         << "4) Вывести все товары" << endl
  41.         << "5) Найти товар с наименьшей ценой" << endl
  42.         << "8)"
  43.         << "0) Выход" << endl
  44.         << "Продолжение следует..." << endl;
  45.         cin >> choice;
  46.  
  47.         switch(choice)
  48.         {
  49.             case 1:
  50.             {
  51.                 string name;
  52.                 string manufacturer;
  53.                 float price;
  54.                 int year;
  55.  
  56.                 system("cls");
  57.                 cout << "Введите название товара: ";
  58.                 cin >> name;
  59.                 cout << "Введите производителя товара: ";
  60.                 cin >> manufacturer;
  61.                 cout << "Введите цену: ";
  62.                 cin >> price;
  63.                 cout << "Введите год производства: ";
  64.                 cin >> year;
  65.                 addElement(name, manufacturer, price, year);
  66.                 break;
  67.             }
  68.             case 2:
  69.             {
  70.                 if(products.size() > 0)
  71.                 {
  72.                     int index;
  73.                     cout << "Список товаров с индексами: " << endl;
  74.                     listQueue();
  75.                     cout << "Введите индекс товара, который нужно удалить: ";
  76.                     cin >> index;
  77.                     removeElement(index);
  78.                 }
  79.                 else
  80.                 {
  81.                     cout << "Очередь пуста! ";
  82.                     system("pause");
  83.                 }
  84.                 break;
  85.             }
  86.             case 4:
  87.             {
  88.                 listQueue();
  89.                 system("pause");
  90.                 break;
  91.             }
  92.             case 5:
  93.             {
  94.                 findMinPrice();
  95.                 break;
  96.             }
  97.             case 0:
  98.             {
  99.                 exit(1);
  100.                 break;
  101.             }
  102.         }
  103.     }
  104.  
  105.     return 0;
  106. }
  107.  
  108. void listQueue()
  109. {
  110.     for(int i = 0; i < products.size(); i++)
  111.     {
  112.         Product temp = products.front();
  113.         cout << "[" << i+1 << "] " << temp.name << " " << temp.manufacturer << " " << temp.price << " " << temp.year << endl;
  114.         products.pop();
  115.         products.push(temp);
  116.     }
  117. }
  118.  
  119. void loadQueue()
  120. {
  121.     Product *temp = new Product;
  122.  
  123.     ifstream file;
  124.     file.open("products.txt");
  125.  
  126.     while(file >> temp->name >> temp->manufacturer >> temp->price >> temp->year)
  127.     {
  128.         products.push(*temp);
  129.     }
  130.  
  131.     file.close();
  132. }
  133.  
  134. void writeQueue()
  135. {
  136.     fstream prods;
  137.     prods.open("products.txt", ios::out);
  138.  
  139.     for(int i = 0; i < products.size(); i++)
  140.     {
  141.         Product temp = products.front();
  142.         prods << temp.name << " " << temp.manufacturer << " " << temp.price << " " << temp.year << endl;
  143.         products.pop();
  144.         products.push(temp);
  145.     }
  146.  
  147.     prods.close();
  148. }
  149.  
  150. void addElement(string name, string manufacturer, float price, int year)
  151. {
  152.     Product *temp = new Product;
  153.    
  154.     temp->name = name;
  155.     temp->manufacturer = manufacturer;
  156.     temp->price = price;
  157.     temp->year = year;
  158.  
  159.     products.push(*temp);
  160.  
  161.     cout << "Теперь в очереди " << products.size() << " элементов" << endl;
  162.  
  163.     writeQueue();
  164.  
  165.     system("pause");
  166. }
  167.  
  168. void removeElement(int index)
  169. {
  170.     for(int i = 0; i < products.size(); i++)
  171.     {
  172.         Product temp;
  173.         if(i == index - 1)
  174.         {
  175.             products.pop();
  176.         }
  177.         else
  178.         {
  179.             temp = products.front();
  180.             products.pop();
  181.             products.push(temp);
  182.         }
  183.     }
  184.  
  185.     cout << "Теперь в очереди " << products.size() << " элементов" << endl;
  186.  
  187.     writeQueue();
  188.  
  189.     system("pause");
  190. }
  191.  
  192. void findMinPrice()
  193. {
  194.     //float min = products.front().price;
  195.     Product result = products.front();
  196.  
  197.     for(int i = 0; i < products.size(); i++)
  198.     {
  199.         Product temp = products.front();
  200.  
  201.         if(temp.price < result.price)
  202.             result = temp;
  203.         //prods << temp.name << " " << temp.manufacturer << " " << temp.price << " " << temp.year << endl;
  204.         products.pop();
  205.         products.push(temp);
  206.     }
  207.  
  208.     cout << "Товар с наименьшей ценой: " << endl
  209.     << result.name << " " << result.manufacturer << " " << result.price << " " << result.year << endl;
  210.  
  211.     system("pause");
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement