Advertisement
LittleMax

Untitled

Jun 4th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #define SIZE 3
  5. using namespace std;
  6.  
  7. struct Tovar
  8. {
  9.     int section;
  10.     char name[30];
  11.     double price;
  12.     int number;
  13.  
  14.     Tovar() {}
  15.     Tovar(int section, char n[30], double price, int number)
  16.     {
  17.         this->section = section;
  18.         memcpy(this->name, n, sizeof(n));
  19.         this->price = price;
  20.         this->number = number;
  21.     }
  22. };
  23.  
  24. bool compare(Tovar t, char name[30], int price)
  25. {
  26.     for (int i = 0; name[i] != '\0'; i++)
  27.     {
  28.         if (name[i] != t.name[i])
  29.             return false;
  30.     }
  31.  
  32.     return true;
  33. }
  34.  
  35. void add(Tovar* t)
  36. {
  37.     char name[30];
  38.     int section = 23;
  39.     double price;
  40.     int number = 1;
  41.  
  42.     int position = 0;
  43.     int i = 0;
  44.     while (i < SIZE)
  45.     {
  46.         cout << "Enter the name of product and price" << endl;
  47.         cin >> name >> price;
  48.         bool isAdded = false;
  49.  
  50.         for (int j = 0; j < SIZE; j++)
  51.         {
  52.             if (compare(t[j], name, price))
  53.             {
  54.                 t[j].number++;
  55.                 isAdded = true;
  56.                 break;
  57.             }
  58.         }
  59.  
  60.         if (!isAdded)
  61.         {
  62.             t[i].price = price;
  63.             t[i].section = 24;
  64.             t[i].number = number;
  65.             memcpy(t[i].name, name, sizeof(name));
  66.             isAdded = false;
  67.             i++;
  68.         }
  69.     }
  70. }
  71.  
  72. void show(Tovar* t)
  73. {
  74.     for (int i = 0; i < SIZE; i++)
  75.     {
  76.         cout << "________________________" << endl;
  77.         cout << "Name: " << t[i].name << endl;
  78.         cout << "Price: " << t[i].price << endl;
  79.         cout << "Number: " << t[i].number << endl;
  80.         cout << "________________________" << endl;
  81.     }
  82. }
  83.  
  84. bool comparisonFunc(const char *c1, const char *c2)
  85. {
  86.     return strcmp(c1, c2) < 0;
  87. }
  88.  
  89. void findBySection(Tovar* t, int section)
  90. {
  91.     vector<char*> names;
  92.  
  93.     for (int i = 0; i < SIZE; i++)
  94.     {
  95.         if (t[i].section == section)
  96.             names.push_back(t[i].name);
  97.     }
  98.  
  99.     sort(names.begin(), names.end(), comparisonFunc);
  100.  
  101.     cout << "The products of section number " << section << endl;
  102.     for (auto x : names)
  103.         cout << x << endl;
  104. }
  105.  
  106. void findByNumber(Tovar* t, int number)
  107. {
  108.     vector<char*> products;
  109.  
  110.     for (int i = 0; i < SIZE; i++)
  111.     {
  112.         if (t[i].number < number)
  113.             products.push_back(t[i].name);
  114.     }
  115.  
  116.     for (auto x : products)
  117.         cout << x << endl;
  118. }
  119.  
  120. int main()
  121. {
  122.     Tovar* t = new Tovar[SIZE];
  123.     add(t);
  124.     show(t);
  125.  
  126.     findByNumber(t, 2);
  127.     //findBySection(t, 24);
  128.  
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement