Advertisement
LittleMax

Untitled

Jun 10th, 2019
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. //Структура для задания
  9. struct Supplier {
  10.     int section;
  11.     string name;
  12.     double price;
  13.     int num;
  14.  
  15.     Supplier(int section = 0,  string name = "None", double price = 0, int num = 0)
  16.     {
  17.         this->section = section;
  18.         this->price = price;
  19.         this->num = num;
  20.  
  21.         this->name = name;
  22.     }
  23. };
  24.  
  25. //Структура для Линкед листа
  26. struct Link {
  27.     Link* prev;
  28.     Link* next;
  29.     Supplier* sup;
  30.  
  31.     Link(Supplier* sup, Link* prev = NULL, Link* next = NULL)
  32.     {
  33.         this->sup = sup;
  34.         this->prev = prev;
  35.         this->next = next;
  36.     }
  37. };
  38.  
  39. //Ф-я для добавления в линкед лист
  40. Link* add(Link* c, const char* name, int price, int section)
  41. {
  42.     while (c->next != NULL)
  43.     {
  44.         if (c->sup->name == name && c->sup->price == price)
  45.         {
  46.             c->sup->num++;
  47.             return c;
  48.         }
  49.         else
  50.         {
  51.             c = c->next;
  52.         }
  53.     }
  54.  
  55.     if (c->sup->name == name && c->sup->price == price)
  56.     {
  57.         c->sup->num++;
  58.         return c;
  59.     }
  60.  
  61.     Link* n = new Link(new Supplier(section, name, price, 1), c);
  62.     c->next = n;
  63.  
  64.     return n;
  65. }
  66.  
  67. //Функция для отрисовки всех элементов
  68. void show(Link* c)
  69. {
  70.     while (c->prev != NULL)
  71.     {
  72.         c = c->prev;
  73.     }
  74.  
  75.     while (c != NULL)
  76.     {
  77.         cout << "Name: " << c->sup->name << " Price: " << c->sup->price << " Count: " << c->sup->num << endl;
  78.         c = c->next;
  79.     }
  80. }
  81.  
  82. //Поиск по секции
  83. void findsec(Link* c, int sect)
  84. {
  85.     while (c->prev != NULL)
  86.         c = c->prev;
  87.  
  88.     vector<string> res;
  89.  
  90.     while (c != NULL)
  91.     {
  92.         if (c->sup->section == sect)
  93.             res.push_back(c->sup->name);
  94.         c = c->next;
  95.     }
  96.  
  97.     sort(res.begin(), res.end());
  98.  
  99.     for (auto m : res)
  100.         cout << m << endl;
  101. }
  102.  
  103. //Поиск по количеству меньше заданного
  104. void lessThan(Link* c, int count)
  105. {
  106.     while (c->prev != NULL)
  107.         c = c->prev;
  108.  
  109.     vector<string> res;
  110.  
  111.     while (c != NULL)
  112.     {
  113.         if (c->sup->num < count)
  114.             res.push_back(c->sup->name);
  115.         c = c->next;
  116.     }
  117.  
  118.     for (auto q : res)
  119.         cout << q << endl;
  120. }
  121.  
  122. //Функция для загрузки данных с файла
  123. Link* addFromFile(Link* current)
  124. {
  125.     char name[30];
  126.     int section;
  127.     double price;
  128.     int num;
  129.  
  130.     ifstream is("data.txt");
  131.     ofstream os("temp.txt");
  132.  
  133.     is >> name >> section >> price >> num;
  134.     is.ignore();
  135.     string line;
  136.     while (getline(is, line))
  137.     {
  138.         os << line << endl;
  139.     }
  140.  
  141.     is.close();
  142.     os.close();
  143.     remove("data.txt");
  144.     rename("temp.txt", "data.txt");
  145.  
  146.     Link* n = new Link(new Supplier(section, name, price, num), current);
  147.  
  148.     current->next = n;
  149.  
  150.     return n;
  151. }
  152.  
  153. //Функция для сохранения на файл
  154. void save(Link* c)
  155. {
  156.     while (c->prev != NULL)
  157.     {
  158.         c = c->prev;
  159.     }
  160.  
  161.     ofstream of("data.txt");
  162.  
  163.     while (c != NULL)
  164.     {
  165.         of << c->sup->name << " " << c->sup->section << " " << c->sup->price << " "
  166.             << c->sup->num << endl;
  167.         c = c->next;
  168.     }
  169.     of.close();
  170. }
  171.  
  172. int main()
  173. {
  174.     int i;
  175.     Link* warehouse = new Link(new Supplier(1, "bananas", 100, 4));
  176.     cout << "How many items do you want to add?" << endl;
  177.     cin >> i;
  178.     for (int j = 0; j < i; j++)
  179.     {
  180.         warehouse = addFromFile(warehouse);
  181.     }
  182.     //findsec(warehouse, 1);
  183.     //lessThan(warehouse, 200);
  184.  
  185.     show(warehouse);
  186.     save(warehouse);
  187.     return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement