Advertisement
hinagawa

Untitled

Mar 25th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.95 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include<fstream>
  4. #include<string>
  5. using namespace std;
  6. struct Date
  7. {
  8.     int day;
  9.     int month;
  10.     int year;
  11.  
  12. };
  13. struct Candy
  14. {
  15.     int code_of_candy;
  16.     int code_of_manufact;
  17.     Date date_of_production;
  18.     int expiration_date;
  19.     double taste;
  20.     int stuffing;
  21.     double calorie;
  22.     bool russia;
  23. };
  24. void read_current_date(Date current_date)
  25. {
  26.    
  27.     cout << "Enter day=";
  28.     cin >> current_date.day;
  29.     cout << "Enter month=";
  30.     cin >> current_date.month;
  31.     cout << "Enter year=";
  32.     cin >> current_date.year;
  33. }
  34. bool expiration_test(int day_p, int month_p, int year_p, int expiration_date)
  35. {
  36.     Date c = { 0 };
  37.     read_current_date(c);
  38.     int now = c.day + (c.month * 30) + (c.year * 365);
  39.     int last_day = day_p + (month_p * 30) + (year_p * 365) + (expiration_date * 30);
  40.     if (last_day <= now)
  41.         return 1;//istek
  42.     else
  43.         return 0;// ne istek
  44.  
  45.  
  46. }
  47. void date_of_last_day(int month_p, int year_p, int expiration_date, int lm, int ly)
  48. {
  49.     int last_month = month_p + expiration_date;
  50.     if (last_month > 12)
  51.     {
  52.         year_p = last_month / 12;
  53.         last_month %= 12;
  54.  
  55.     }
  56.     lm = last_month;
  57.     ly = year_p;
  58.  
  59.  
  60.  
  61. }
  62. void create_candy(Candy *a, int n, ifstream&f)
  63. {
  64.     string dat;
  65.     for (int i = 0; i < n; i++)
  66.     {
  67.         f >> a[i].code_of_candy;
  68.         f >> a[i].code_of_manufact;
  69.         f >> dat;
  70.         a[i].date_of_production.day = (dat[0] - '0') * 10 + (dat[1] - '0');
  71.         a[i].date_of_production.month = (dat[3] - '0') * 10 + (dat[4] - '0');
  72.         a[i].date_of_production.year = (dat[6] - '0') * 1000 + (dat[7] - '0') * 100 + (dat[8] - '0') * 10 + (dat[9] - '0');
  73.         f >> a[i].expiration_date;
  74.         f >> a[i].taste;
  75.         f >> a[i].stuffing;
  76.         f >> a[i].calorie;
  77.         f >> a[i].russia;
  78.        
  79.     }
  80.  
  81. }
  82. void output1(ofstream&out1, Candy *a, int n)
  83. {
  84.     int k = 0;
  85.     int lm=0, ly = 0;
  86.    
  87.     for (int i = 0; i < n; i++)
  88.     {
  89.         date_of_last_day(a[i].date_of_production.month, a[i].date_of_production.year, a[i].expiration_date, lm, ly);
  90.         if (expiration_test(a[i].date_of_production.day, a[i].date_of_production.month, a[i].date_of_production.year, a[i].expiration_date))
  91.         {
  92.             out1.write((char*)&a[i].code_of_candy, sizeof(a[i].code_of_candy));
  93.             out1.write((char*)&a[i].code_of_manufact, sizeof(a[i].code_of_manufact));
  94.             out1.write((char*)&a[i].date_of_production.day, sizeof(a[i].date_of_production.day));
  95.             out1.write((char*)&lm, sizeof(lm));
  96.             out1.write((char*)&ly, sizeof(ly));
  97.             k++;
  98.  
  99.            
  100.         }
  101.         cout << "Quantity of candy=" << k;
  102.     }
  103. }
  104. void sort_calorie(Candy *a, int n)
  105. {
  106.     int i, j;
  107.     Candy b;
  108.     for (i = 0; i < n; i++)
  109.     {
  110.         for (j = 0; j > n - 1; j++)
  111.         {
  112.             if (a[j - 1].calorie > a[j].calorie)
  113.             {
  114.                 b = a[j - 1];
  115.                 a[j - 1] = a[j];
  116.                 a[j] = b;
  117.             }
  118.  
  119.         }
  120.     }
  121. }
  122.  
  123. void output2(ofstream&out2, Candy *a, int n)
  124. {
  125.     sort_calorie(a, n);
  126.     for (int i = 0; i < n; i++)
  127.     {
  128.         out2.write((char*)&a[i].code_of_candy, sizeof(a[i].code_of_candy));
  129.         out2.write((char*)&a[i].code_of_manufact, sizeof(a[i].code_of_manufact));
  130.         out2.write((char*)&a[i].taste, sizeof(a[i].taste));
  131.         out2.write((char*)&a[i].stuffing, sizeof(a[i].stuffing));
  132.  
  133.     }
  134.     cout << "Quantity=" << n;
  135.  
  136. }
  137. void output3(ofstream&out3, Candy *a, int n)
  138. {
  139.     int k = 0;
  140.     for (int i = 0; i < n; i++)
  141.     {
  142.         if (a[i].russia && a[i].taste > 80)
  143.         {
  144.             out3.write((char*)&a[i].code_of_candy, sizeof(a[i].code_of_candy));
  145.             out3.write((char*)&a[i].code_of_manufact, sizeof(a[i].code_of_manufact));
  146.             out3.write((char*)&a[i].date_of_production.day, sizeof(a[i].date_of_production.day));
  147.             out3.write((char*)&a[i].date_of_production.month, sizeof(a[i].date_of_production.month));
  148.             out3.write((char*)&a[i].date_of_production.year, sizeof(a[i].date_of_production.year));
  149.             out3.write((char*)&a[i].taste, sizeof(a[i].taste));
  150.             out3.write((char*)&a[i].stuffing, sizeof(a[i].stuffing));
  151.             out3.write((char*)&a[i].calorie, sizeof(a[i].calorie));
  152.             out3.write((char*)&a[i].russia, sizeof(a[i].russia));
  153.             k++;
  154.         }
  155.  
  156.     }
  157.     cout << "Quantity=" << k;
  158. }
  159. void output4(ofstream&out4, Candy *a, int n)
  160. {
  161.     int k = 0;
  162.     for (int i = 0; i < n; i++)
  163.     {
  164.         if (a[i].taste <= 50 & a[i].stuffing % 3==0)
  165.         {
  166.             out4.write((char*)&a[i].code_of_candy, sizeof(a[i].code_of_candy));
  167.             out4.write((char*)&a[i].code_of_manufact, sizeof(a[i].code_of_manufact));
  168.             out4.write((char*)&a[i].date_of_production.day, sizeof(a[i].date_of_production.day));
  169.             out4.write((char*)&a[i].date_of_production.month, sizeof(a[i].date_of_production.month));
  170.             out4.write((char*)&a[i].date_of_production.year, sizeof(a[i].date_of_production.year));
  171.             out4.write((char*)&a[i].taste, sizeof(a[i].taste));
  172.             out4.write((char*)&a[i].stuffing, sizeof(a[i].stuffing));
  173.             k++;
  174.         }
  175.     }
  176.     cout << "Quantity=" << k;
  177.  
  178. }
  179. int main()
  180. {
  181.     ifstream in("lolipop(1).txt");
  182.     ofstream out1("outt1.bin");
  183.     ofstream out2("outt2.bin");
  184.     ofstream out3("outt3.bin");
  185.     ofstream out4("outt4.bin");
  186.     if (in.eof())
  187.     {
  188.         cout << "Can't open file in";
  189.         exit(1);
  190.     }
  191.     int n;
  192.     cout << "Enter quantity of candy=";
  193.     cin >> n;
  194.     Candy *a;
  195.     a = new Candy[n];
  196.     create_candy(a, n, in);
  197.     output1(out1, a, n);
  198.     output2(out2, a, n);
  199.     output3(out3, a, n);
  200.     output4(out4, a, n);
  201.     in.close();
  202.     out1.close();
  203.     out2.close();
  204.     out3.close();
  205.     out4.close();
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement