Advertisement
hinagawa

Бинарные фаилы_конфеты

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