Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <queue>
- #include <windows.h>
- using namespace std;
- void addElement(string, string, float, int);
- void removeElement(int);
- void loadQueue();
- void writeQueue();
- void listQueue();
- void findMinPrice();
- queue<struct Product> products;
- struct Product
- {
- string name;
- string manufacturer;
- float price;
- int year;
- };
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- loadQueue();
- while(true)
- {
- int choice;
- system("cls");
- cout << "Выберите нужное вам действие: " << endl;
- cout << "1) Добавить товар" << endl
- << "2) Удалить товар" << endl
- << "3) Редактировать данные"
- << "4) Вывести все товары" << endl
- << "5) Найти товар с наименьшей ценой" << endl
- << "8)"
- << "0) Выход" << endl
- << "Продолжение следует..." << endl;
- cin >> choice;
- switch(choice)
- {
- case 1:
- {
- string name;
- string manufacturer;
- float price;
- int year;
- system("cls");
- cout << "Введите название товара: ";
- cin >> name;
- cout << "Введите производителя товара: ";
- cin >> manufacturer;
- cout << "Введите цену: ";
- cin >> price;
- cout << "Введите год производства: ";
- cin >> year;
- addElement(name, manufacturer, price, year);
- break;
- }
- case 2:
- {
- if(products.size() > 0)
- {
- int index;
- cout << "Список товаров с индексами: " << endl;
- listQueue();
- cout << "Введите индекс товара, который нужно удалить: ";
- cin >> index;
- removeElement(index);
- }
- else
- {
- cout << "Очередь пуста! ";
- system("pause");
- }
- break;
- }
- case 4:
- {
- listQueue();
- system("pause");
- break;
- }
- case 5:
- {
- findMinPrice();
- break;
- }
- case 0:
- {
- exit(1);
- break;
- }
- }
- }
- return 0;
- }
- void listQueue()
- {
- for(int i = 0; i < products.size(); i++)
- {
- Product temp = products.front();
- cout << "[" << i+1 << "] " << temp.name << " " << temp.manufacturer << " " << temp.price << " " << temp.year << endl;
- products.pop();
- products.push(temp);
- }
- }
- void loadQueue()
- {
- Product *temp = new Product;
- ifstream file;
- file.open("products.txt");
- while(file >> temp->name >> temp->manufacturer >> temp->price >> temp->year)
- {
- products.push(*temp);
- }
- file.close();
- }
- void writeQueue()
- {
- fstream prods;
- prods.open("products.txt", ios::out);
- for(int i = 0; i < products.size(); i++)
- {
- Product temp = products.front();
- prods << temp.name << " " << temp.manufacturer << " " << temp.price << " " << temp.year << endl;
- products.pop();
- products.push(temp);
- }
- prods.close();
- }
- void addElement(string name, string manufacturer, float price, int year)
- {
- Product *temp = new Product;
- temp->name = name;
- temp->manufacturer = manufacturer;
- temp->price = price;
- temp->year = year;
- products.push(*temp);
- cout << "Теперь в очереди " << products.size() << " элементов" << endl;
- writeQueue();
- system("pause");
- }
- void removeElement(int index)
- {
- for(int i = 0; i < products.size(); i++)
- {
- Product temp;
- if(i == index - 1)
- {
- products.pop();
- }
- else
- {
- temp = products.front();
- products.pop();
- products.push(temp);
- }
- }
- cout << "Теперь в очереди " << products.size() << " элементов" << endl;
- writeQueue();
- system("pause");
- }
- void findMinPrice()
- {
- //float min = products.front().price;
- Product result = products.front();
- for(int i = 0; i < products.size(); i++)
- {
- Product temp = products.front();
- if(temp.price < result.price)
- result = temp;
- //prods << temp.name << " " << temp.manufacturer << " " << temp.price << " " << temp.year << endl;
- products.pop();
- products.push(temp);
- }
- cout << "Товар с наименьшей ценой: " << endl
- << result.name << " " << result.manufacturer << " " << result.price << " " << result.year << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement