Advertisement
zakharovafm

бинарные файлы

Apr 25th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <fstream>
  4. using namespace std;
  5. struct date
  6. {
  7.     int day;
  8.     int month;
  9.     int year;
  10. };
  11. struct candy
  12. {
  13.     int code;
  14.     int manufactury_code;
  15.     date date_production;
  16.     int shelf_life;
  17.     int taste;
  18.     int filling;
  19.     int calorie;
  20.     int rus;
  21.     date end_shelf;
  22. };
  23. candy* create(ifstream &f, int n)
  24. {
  25.     int i;
  26.     char*data = new char[10];
  27.     candy* a = new candy[n];
  28.     for (i = 0; i < n; i++)
  29.     {
  30.         f >> a[i].code >> a[i].manufactury_code;
  31.         f >> data;
  32.         a[i].date_production.day = (int(data[0]) - 48) * 10 + int(data[1]) - 48;
  33.         a[i].date_production.month = (int(data[3]) - 48) * 10 + int(data[4]) - 48;
  34.         a[i].date_production.year = (int(data[6]) - 48) * 1000 + (int(data[7]) - 48) * 100 + (int(data[8] - 48)) * 10 + int(data[9] - 48);
  35.         f >> a[i].shelf_life >> a[i].taste >> a[i].filling >> a[i].calorie >> a[i].rus;
  36.         a[i].end_shelf.day = a[i].date_production.day;
  37.         a[i].end_shelf.month = a[i].date_production.month + a[i].shelf_life;
  38.         a[i].end_shelf.year = a[i].date_production.year;
  39.         while (a[i].end_shelf.month > 12)
  40.         {
  41.             a[i].end_shelf.month -= 12;
  42.             a[i].end_shelf.year += 1;
  43.         }
  44.     }
  45.     return a;
  46. }
  47. bool compare(date candies, date today)
  48. {
  49.     if (candies.year > today.year)
  50.         return false;
  51.     else
  52.         if (candies.year < today.year)
  53.             return true;
  54.         else
  55.             if (candies.month > today.month)
  56.                 return false;
  57.             else
  58.                 if (candies.month < today.month)
  59.                     return true;
  60.                 else
  61.                     if (candies.day >= today.day)
  62.                         return false;
  63.                     else
  64.                         return true;
  65. }
  66. void cTerm(candy* a, date daynow, int n, ofstream &out)
  67. {
  68.     int i, k = 0;
  69.     for (i = 0; i < n; i++)
  70.         if (compare(a[i].end_shelf, daynow))
  71.         {
  72.             out.write((char*)&a[i].code, sizeof(a[i].code));
  73.             out.write((char*)&a[i].manufactury_code, sizeof(a[i].manufactury_code));
  74.             out.write((char*)&a[i].end_shelf, sizeof(a[i].end_shelf));
  75.             k++;
  76.         }
  77.     cout << k << endl;
  78. }
  79. void cSortCalories(candy* a, int n, ofstream &out)
  80. {
  81.     int i, j;
  82.     candy t;
  83.     for (i = 0; i < n; i++)
  84.         for (j = 0; j < n - 1; j++)
  85.             if (a[j].calorie < a[j + 1].calorie)
  86.             {
  87.                 t = a[j];
  88.                 a[j] = a[j + 1];
  89.                 a[j + 1] = t;
  90.             }
  91.     for (i = 0; i < n; i++)
  92.     {
  93.         out.write((char*)&a[i].code, sizeof(a[i].code));
  94.         out.write((char*)&a[i].manufactury_code, sizeof(a[i].manufactury_code));
  95.         out.write((char*)&a[i].taste, sizeof(a[i].taste));
  96.         out.write((char*)&a[i].calorie, sizeof(a[i].calorie));
  97.     }
  98.     cout << n << endl;
  99. }
  100. void cTaste(candy*a, int n, ofstream &out)
  101. {
  102.     int i;
  103.     int k = 0;
  104.     for (i = 0; i < n; i++)
  105.         if ((a[i].rus == 1) && (a[i].taste > 80))
  106.         {
  107.             out.write((char*)&a[i].code, sizeof(a[i].code));
  108.             out.write((char*)&a[i].manufactury_code, sizeof(a[i].manufactury_code));
  109.             out.write((char*)&a[i].date_production, sizeof(a[i].date_production));
  110.             out.write((char*)&a[i].shelf_life, sizeof(a[i].shelf_life));
  111.             out.write((char*)&a[i].taste, sizeof(a[i].taste));
  112.             out.write((char*)&a[i].filling, sizeof(a[i].filling));
  113.             out.write((char*)&a[i].calorie, sizeof(a[i].calorie));
  114.             out.write((char*)&a[i].rus, sizeof(a[i].rus));
  115.             k++;
  116.         }
  117.     cout << k << endl;
  118. }
  119. void cFilling(candy*a, int n, ofstream &out)
  120. {
  121.     int i;
  122.     int k = 0;
  123.     for (i = 0; i < n; i++)
  124.         if ((a[i].taste > 0) && (a[i].taste < 50) && (a[i].filling % 3 == 0))
  125.         {
  126.             out.write((char*)&a[i].code, sizeof(a[i].code));
  127.             out.write((char*)&a[i].manufactury_code, sizeof(a[i].manufactury_code));
  128.             out.write((char*)&a[i].date_production, sizeof(a[i].date_production));
  129.             out.write((char*)&a[i].taste, sizeof(a[i].taste));
  130.             out.write((char*)&a[i].filling, sizeof(a[i].filling));
  131.             k++;
  132.         }
  133.     cout << k << endl;
  134. }
  135.  
  136. int main()
  137. {
  138.     ifstream inputcandy("inputcandy.txt");
  139.     ofstream out1("output1.bin", ios::binary);
  140.     ofstream out2("output2.bin", ios::binary);
  141.     ofstream out3("output3.bin", ios::binary);
  142.     ofstream out4("output4.bin", ios::binary);
  143.     int n;
  144.     cout << "Enter number of candies: ";
  145.     cin >> n;
  146.     candy*a = create(inputcandy, n);
  147.     char*b = new char[10];
  148.     date daynow;
  149.     cout << "Enter today's date: ";
  150.     cin >> b;
  151.     daynow.day = (int(b[0]) - 48) * 10 + int(b[1]) - 48;
  152.     daynow.month = (int(b[3]) - 48) * 10 + int(b[4]) - 48;
  153.     daynow.year = (int(b[6]) - 48) * 1000 + (int(b[7]) - 48) * 100 + (int(b[8] - 48)) * 10 +
  154.         int(b[9] - 48);
  155.     cTerm(a, daynow, n, out1);
  156.     cSortCalories(a, n, out2);
  157.     cTaste(a, n, out3);
  158.     cFilling(a, n, out4);
  159.     inputcandy.close();
  160.     out1.close();
  161.     out2.close();
  162.     out3.close();
  163.     out4.close();
  164.  
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement