Advertisement
LittleMax

Untitled

Jun 5th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <fstream>
  5. #define SIZE 3
  6. using namespace std;
  7.  
  8. enum month {
  9.     jan = 1, feb, mar, apr, may, june, jul, aug, sep, oct, nov, dec
  10. };
  11.  
  12. typedef struct Date {
  13.     int day;
  14.     month mon;
  15.     int year;
  16. }date;
  17.  
  18. struct Tovar
  19. {
  20.     int section;
  21.     char name[30];
  22.     double price;
  23.     int number;
  24.     int countMon;
  25.     date dt;
  26.  
  27.     Tovar() {}
  28.     Tovar(int section, char n[30], double price, int number, int day, month mon, int year, int countMon)
  29.     {
  30.         this->section = section;
  31.         memcpy(this->name, n, sizeof(n));
  32.         this->price = price;
  33.         this->number = number;
  34.         this->dt.day = day;
  35.         this->dt.mon = mon;
  36.         this->dt.year = year;
  37.         this->countMon = countMon;
  38.     }
  39. };
  40.  
  41. //Функция для сравнения
  42. bool compare(Tovar t, char name[30], int price)
  43. {
  44.     for (int i = 0; name[i] != '\0'; i++)
  45.     {
  46.         if (name[i] != t.name[i])
  47.             return false;
  48.     }
  49.  
  50.     return true;
  51. }
  52.  
  53. //Ф-я для сохранения результатов в файле
  54. void save(Tovar* t)
  55. {
  56.     ofstream file;
  57.     file.open("data.txt");
  58.  
  59.     for (int i = 0; i < SIZE; i++)
  60.     {
  61.         file << t[i].name << " " << t[i].price << " " << t[i].section << " " << t[i].number
  62.             << " " << t[i].dt.day << " " << t[i].dt.mon << " " << t[i].dt.year << " " << t[i].countMon << endl;
  63.     }
  64.  
  65.     file.close();
  66. }
  67.  
  68. //Ф-я для загрузки результатов с файла
  69. void load(Tovar* t)
  70. {
  71.     char name[30];
  72.     int section = 23;
  73.     double price;
  74.     int number = 1;
  75.     int day, year;
  76.     int mon;
  77.     int countMon;
  78.  
  79.     ifstream file("data.txt");
  80.  
  81.     if (file.is_open())
  82.     {
  83.         for (int i = 0; i < SIZE; i++)
  84.         {
  85.             file >> name >> price >> section >> number >> day >> mon >> year >> countMon;
  86.  
  87.             memcpy(t[i].name, name, sizeof(name));
  88.             t[i].price = price;
  89.             t[i].section = section;
  90.             t[i].number = number;
  91.             t[i].dt.day = day;
  92.             t[i].dt.mon = (month)mon;
  93.             t[i].dt.year = year;
  94.             t[i].countMon = countMon;
  95.         }
  96.  
  97.         file.close();
  98.     }
  99.     else
  100.     {
  101.         cout << "Unable to open file" << endl;
  102.     }
  103. }
  104.  
  105. //Ф-я заполнения массива студентов
  106. void init_products(Tovar* t)
  107. {
  108.     char name[30];
  109.     int section = 23;
  110.     double price;
  111.     int number = 1;
  112.     int day, year;
  113.     int mon;
  114.     int countMon;
  115.  
  116.     int position = 0;
  117.     int i = 0;
  118.     while (i < SIZE)
  119.     {
  120.         cout << "Enter the name of product, price and date of creation d/m/y and shelf life in month" << endl;
  121.         cin >> name >> price >> day >> mon >> year >> countMon;
  122.         bool isAdded = false;
  123.  
  124.         for (int j = 0; j < SIZE; j++)
  125.         {
  126.             if (compare(t[j], name, price))
  127.             {
  128.                 t[j].number++;
  129.                 isAdded = true;
  130.                 break;
  131.             }
  132.         }
  133.  
  134.         if (!isAdded)
  135.         {
  136.             t[i].price = price;
  137.             t[i].section = 24;
  138.             t[i].number = number;
  139.             t[i].dt.day = day;
  140.             t[i].dt.mon = (month)mon;
  141.             t[i].dt.year = year;
  142.             t[i].countMon = countMon;
  143.             memcpy(t[i].name, name, sizeof(name));
  144.             isAdded = false;
  145.             i++;
  146.         }
  147.     }
  148.  
  149.     save(t);
  150. }
  151.  
  152. //Функция вывода студентов
  153. void show(Tovar* t)
  154. {
  155.     for (int i = 0; i < SIZE; i++)
  156.     {
  157.         cout << "________________________" << endl;
  158.         cout << "Name: " << t[i].name << endl;
  159.         cout << "Price: " << t[i].price << endl;
  160.         cout << "Number: " << t[i].number << endl;
  161.         cout << "________________________" << endl;
  162.     }
  163. }
  164.  
  165. //Функция сравнения символов
  166. bool comparisonFunc(const char *c1, const char *c2)
  167. {
  168.     return strcmp(c1, c2) < 0;
  169. }
  170.  
  171. //Функция для нахождения по секции
  172. void findBySection(Tovar* t, int section)
  173. {
  174.     vector<char*> names;
  175.  
  176.     for (int i = 0; i < SIZE; i++)
  177.     {
  178.         if (t[i].section == section)
  179.             names.push_back(t[i].name);
  180.     }
  181.  
  182.     sort(names.begin(), names.end(), comparisonFunc);
  183.  
  184.     cout << "The products of section number " << section << endl;
  185.     for (auto x : names)
  186.         cout << x << endl;
  187. }
  188.  
  189. //Функция для нахождения по количеству товаров
  190. void findByNumber(Tovar* t, int number)
  191. {
  192.     vector<char*> products;
  193.  
  194.     for (int i = 0; i < SIZE; i++)
  195.     {
  196.         if (t[i].number < number)
  197.             products.push_back(t[i].name);
  198.     }
  199.  
  200.     for (auto x : products)
  201.         cout << x << endl;
  202. }
  203.  
  204. //Функция для находения просроченных товаров
  205. void findOutOfDate(Tovar* t, int day, int mon, int year)
  206. {
  207.     vector<char*> products;
  208.  
  209.     month end;
  210.  
  211.     for (int i = 0; i < SIZE; i++)
  212.     {
  213.         end = (month)((t[i].dt.mon + t[i].countMon) % 12);
  214.  
  215.         if (t[i].dt.year < year)
  216.         {
  217.             products.push_back(t[i].name);
  218.         }
  219.         else
  220.         {
  221.             if (end < mon)
  222.             {
  223.                 products.push_back(t[i].name);
  224.             }
  225.             else
  226.             {
  227.                 if (t[i].dt.day < day && t[i].dt.year < year && t[i].dt.mon < mon)
  228.                 {
  229.                     products.push_back(t[i].name);
  230.                 }
  231.             }
  232.         }
  233.     }
  234.  
  235.  
  236.     for (auto x : products)
  237.         cout << x << endl;
  238. }
  239.  
  240. int main()
  241. {
  242.     Tovar* t = new Tovar[SIZE];
  243.     init_products(t);//функция для заполнения массива с клавы
  244.     //load(t);//Функция для заполнения масива из файла
  245.     show(t);
  246.  
  247.     //findOutOfDate(t, 2, 1, 2018);
  248.     //findByNumber(t, 2);
  249.     //findBySection(t, 24);
  250.  
  251.     return 0;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement