Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <fstream>
- using namespace std;
- //Структура для задания
- struct Supplier {
- int section;
- string name;
- double price;
- int num;
- Supplier(int section = 0, string name = "None", double price = 0, int num = 0)
- {
- this->section = section;
- this->price = price;
- this->num = num;
- this->name = name;
- }
- };
- //Структура для Линкед листа
- struct Link {
- Link* prev;
- Link* next;
- Supplier* sup;
- Link(Supplier* sup, Link* prev = NULL, Link* next = NULL)
- {
- this->sup = sup;
- this->prev = prev;
- this->next = next;
- }
- };
- //Ф-я для добавления в линкед лист
- Link* add(Link* c, const char* name, int price, int section)
- {
- while (c->next != NULL)
- {
- if (c->sup->name == name && c->sup->price == price)
- {
- c->sup->num++;
- return c;
- }
- else
- {
- c = c->next;
- }
- }
- if (c->sup->name == name && c->sup->price == price)
- {
- c->sup->num++;
- return c;
- }
- Link* n = new Link(new Supplier(section, name, price, 1), c);
- c->next = n;
- return n;
- }
- //Функция для отрисовки всех элементов
- void show(Link* c)
- {
- while (c->prev != NULL)
- {
- c = c->prev;
- }
- while (c != NULL)
- {
- cout << "Name: " << c->sup->name << " Price: " << c->sup->price << " Count: " << c->sup->num << endl;
- c = c->next;
- }
- }
- //Поиск по секции
- void findsec(Link* c, int sect)
- {
- while (c->prev != NULL)
- c = c->prev;
- vector<string> res;
- while (c != NULL)
- {
- if (c->sup->section == sect)
- res.push_back(c->sup->name);
- c = c->next;
- }
- sort(res.begin(), res.end());
- for (auto m : res)
- cout << m << endl;
- }
- //Поиск по количеству меньше заданного
- void lessThan(Link* c, int count)
- {
- while (c->prev != NULL)
- c = c->prev;
- vector<string> res;
- while (c != NULL)
- {
- if (c->sup->num < count)
- res.push_back(c->sup->name);
- c = c->next;
- }
- for (auto q : res)
- cout << q << endl;
- }
- //Функция для загрузки данных с файла
- Link* addFromFile(Link* current)
- {
- char name[30];
- int section;
- double price;
- int num;
- ifstream is("data.txt");
- ofstream os("temp.txt");
- is >> name >> section >> price >> num;
- is.ignore();
- string line;
- while (getline(is, line))
- {
- os << line << endl;
- }
- is.close();
- os.close();
- remove("data.txt");
- rename("temp.txt", "data.txt");
- Link* n = new Link(new Supplier(section, name, price, num), current);
- current->next = n;
- return n;
- }
- //Функция для сохранения на файл
- void save(Link* c)
- {
- while (c->prev != NULL)
- {
- c = c->prev;
- }
- ofstream of("data.txt");
- while (c != NULL)
- {
- of << c->sup->name << " " << c->sup->section << " " << c->sup->price << " "
- << c->sup->num << endl;
- c = c->next;
- }
- of.close();
- }
- int main()
- {
- int i;
- Link* warehouse = new Link(new Supplier(1, "bananas", 100, 4));
- cout << "How many items do you want to add?" << endl;
- cin >> i;
- for (int j = 0; j < i; j++)
- {
- warehouse = addFromFile(warehouse);
- }
- //findsec(warehouse, 1);
- //lessThan(warehouse, 200);
- show(warehouse);
- save(warehouse);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement