Advertisement
Sitisom

Candies

Apr 2nd, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <fstream>
  5. #include<chrono>
  6. #include<sstream>
  7. #include<string>
  8. #include<vector>
  9. using namespace std;
  10.  
  11. vector<string> split(const string& s, char delimiter)
  12. {
  13.     vector<string> tokens;
  14.     string token;
  15.     istringstream tokenStream(s);
  16.     while (std::getline(tokenStream, token, delimiter))
  17.     {
  18.         tokens.push_back(token);
  19.     }
  20.     return tokens;
  21. }
  22.  
  23. struct Date
  24. {
  25.     int day;
  26.     int month;
  27.     int year;
  28. };
  29.  
  30. Date current_time() {
  31.     Date nowadays;
  32.  
  33.     struct tm tim;
  34.     time_t current_time = time(0);
  35.     localtime_s(&tim, &current_time);
  36.     int* currentDate = new int[3]{ tim.tm_mday, tim.tm_mon + 1, tim.tm_year + 1900 };
  37.  
  38.     nowadays.day = currentDate[0];
  39.     nowadays.month = currentDate[1];
  40.     nowadays.year = currentDate[2];
  41.     return nowadays;
  42. }
  43.  
  44. struct Candies
  45. {
  46.     int code;
  47.     int code_pr;
  48.     Date date;
  49.     int expiryMonths;
  50.     int taste;
  51.     int topping;
  52.     int calories;
  53.     int madeIn;
  54.     Date expiryDate;
  55.  
  56.     vector<Candies> Create(ifstream &f)
  57.     {
  58.         vector<Candies> candies;
  59.         string line;
  60.         while(getline(f, line)) {
  61.             Candies candie;
  62.             vector<string> parsed_line = split(line, ' ');
  63.             vector<string> parsed_date = split(parsed_line[2], '.');
  64.             candie.code = stoi(parsed_line[0]);
  65.             candie.code_pr = stoi(parsed_line[1]);
  66.             candie.date.day = stoi(parsed_date[0]);
  67.             candie.date.month = stoi(parsed_date[1]);
  68.             candie.date.year = stoi(parsed_date[2]);
  69.             candie.expiryMonths = stoi(parsed_line[3]);
  70.             candie.taste = stoi(parsed_line[4]);
  71.             candie.topping = stoi(parsed_line[5]);
  72.             candie.calories = stoi(parsed_line[6]);
  73.             candie.madeIn = stoi(parsed_line[7]);
  74.             candie.expiryDate = candie.date;
  75.             candie.expiryDate.month += candie.expiryMonths;
  76.             while (candie.expiryDate.month > 12)
  77.             {
  78.                 candie.expiryDate.month -= 12;
  79.                 candie.expiryDate.year += 1;
  80.             }
  81.             candies.push_back(candie);
  82.         }
  83.         return candies;
  84.     }
  85.  
  86.     bool isEatable()
  87.     {
  88.         Date nowadays = current_time();
  89.         if (expiryDate.year > nowadays.year)
  90.             return false;
  91.         else
  92.             if (expiryDate.year < nowadays.year)
  93.                 return true;
  94.             else
  95.                 if (expiryDate.month > nowadays.month)
  96.                     return false;
  97.                 else
  98.                     if (expiryDate.month < nowadays.month)
  99.                         return true;
  100.                     else
  101.                         if (expiryDate.day >= nowadays.day)
  102.                             return false;
  103.                         else
  104.                             return true;
  105.     }
  106. };
  107.  
  108. void CheckOfDate(vector<Candies> &a, ofstream &out)
  109. {
  110.     unsigned int i;
  111.     int k = 0;
  112.     for (i = 0; i < a.size(); i++)
  113.         if (a[i].isEatable())
  114.         {
  115.             out.write((char*)&a[i].code, sizeof(a[i].code));
  116.             out.write((char*)&a[i].code_pr, sizeof(a[i].code_pr));
  117.             out.write((char*)&a[i].expiryDate, sizeof(a[i].expiryDate));
  118.             k++;
  119.         }
  120.     cout << k << endl;
  121. }
  122.  
  123. void CheckOfCalories(vector<Candies> &a, ofstream &out)
  124. {
  125.     unsigned int i, j;
  126.     Candies t;
  127.     for (i = 0; i < a.size(); i++)
  128.         for (j = 0; j < a.size() - 1; j++)
  129.             if (a[j].calories < a[j + 1].calories)
  130.             {
  131.                 t = a[j];
  132.                 a[j] = a[j + 1];
  133.                 a[j + 1] = t;
  134.             }
  135.     for (i = 0; i < a.size(); i++)
  136.     {
  137.         out.write((char*)&a[i].code, sizeof(a[i].code));
  138.         out.write((char*)&a[i].code_pr, sizeof(a[i].code_pr));
  139.         out.write((char*)&a[i].taste, sizeof(a[i].taste));
  140.         out.write((char*)&a[i].calories, sizeof(a[i].calories));
  141.     }
  142.     cout << a.size() << endl;
  143. }
  144.  
  145. void CheckOfTaste(vector<Candies> &a, ofstream &out)
  146. {
  147.     unsigned int i;
  148.     int k = 0;
  149.     for (i = 0; i < a.size(); i++)
  150.         if ((a[i].madeIn == 1) && (a[i].taste > 80))
  151.         {
  152.             out.write((char*)&a[i], sizeof(a[i]));
  153.             k++;
  154.         }
  155.     cout << k << endl;
  156. }
  157.  
  158. void CheckOfTopping(vector<Candies> &a, ofstream &out)
  159. {
  160.     unsigned int i;
  161.     int k = 0;
  162.     for (i = 0; i < a.size(); i++)
  163.         if ((a[i].taste > 0) && (a[i].taste < 50) && (a[i].topping % 3 == 0))
  164.         {
  165.             out.write((char*)&a[i].code, sizeof(a[i].code));
  166.             out.write((char*)&a[i].code_pr, sizeof(a[i].code_pr));
  167.             out.write((char*)&a[i].date, sizeof(a[i].date));
  168.             out.write((char*)&a[i].taste, sizeof(a[i].taste));
  169.             out.write((char*)&a[i].topping, sizeof(a[i].topping));
  170.             k++;
  171.         }
  172.     cout << k << endl;
  173. }
  174.  
  175. int main()
  176. {
  177.     setlocale(LC_ALL, "Russian");
  178.     ifstream in("candies.txt");
  179.     ofstream out1("out1.bin", ios::binary);
  180.     ofstream out2("out2.bin", ios::binary);
  181.     ofstream out3("out3.bin", ios::binary);
  182.     ofstream out4("out4.bin", ios::binary);
  183.     Candies candie_init;
  184.     vector<Candies> candies = candie_init.Create(in);
  185.    
  186.     Date nowadays = current_time();
  187.  
  188.     CheckOfDate(candies, out1);
  189.     CheckOfCalories(candies, out2);
  190.     CheckOfTaste(candies, out3);
  191.     CheckOfTopping(candies, out4);
  192.  
  193.     in.close();
  194.     out1.close();
  195.     out2.close();
  196.     out3.close();
  197.     out4.close();
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement