Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <stdio.h>
- #include <fstream>
- #include<chrono>
- #include<sstream>
- #include<string>
- #include<vector>
- using namespace std;
- vector<string> split(const string& s, char delimiter)
- {
- vector<string> tokens;
- string token;
- istringstream tokenStream(s);
- while (std::getline(tokenStream, token, delimiter))
- {
- tokens.push_back(token);
- }
- return tokens;
- }
- struct Date
- {
- int day;
- int month;
- int year;
- };
- Date current_time() {
- Date nowadays;
- struct tm tim;
- time_t current_time = time(0);
- localtime_s(&tim, ¤t_time);
- int* currentDate = new int[3]{ tim.tm_mday, tim.tm_mon + 1, tim.tm_year + 1900 };
- nowadays.day = currentDate[0];
- nowadays.month = currentDate[1];
- nowadays.year = currentDate[2];
- return nowadays;
- }
- struct Candies
- {
- int code;
- int code_pr;
- Date date;
- int expiryMonths;
- int taste;
- int topping;
- int calories;
- int madeIn;
- Date expiryDate;
- vector<Candies> Create(ifstream &f)
- {
- vector<Candies> candies;
- string line;
- while(getline(f, line)) {
- Candies candie;
- vector<string> parsed_line = split(line, ' ');
- vector<string> parsed_date = split(parsed_line[2], '.');
- candie.code = stoi(parsed_line[0]);
- candie.code_pr = stoi(parsed_line[1]);
- candie.date.day = stoi(parsed_date[0]);
- candie.date.month = stoi(parsed_date[1]);
- candie.date.year = stoi(parsed_date[2]);
- candie.expiryMonths = stoi(parsed_line[3]);
- candie.taste = stoi(parsed_line[4]);
- candie.topping = stoi(parsed_line[5]);
- candie.calories = stoi(parsed_line[6]);
- candie.madeIn = stoi(parsed_line[7]);
- candie.expiryDate = candie.date;
- candie.expiryDate.month += candie.expiryMonths;
- while (candie.expiryDate.month > 12)
- {
- candie.expiryDate.month -= 12;
- candie.expiryDate.year += 1;
- }
- candies.push_back(candie);
- }
- return candies;
- }
- bool isEatable()
- {
- Date nowadays = current_time();
- if (expiryDate.year > nowadays.year)
- return false;
- else
- if (expiryDate.year < nowadays.year)
- return true;
- else
- if (expiryDate.month > nowadays.month)
- return false;
- else
- if (expiryDate.month < nowadays.month)
- return true;
- else
- if (expiryDate.day >= nowadays.day)
- return false;
- else
- return true;
- }
- };
- void CheckOfDate(vector<Candies> &a, ofstream &out)
- {
- unsigned int i;
- int k = 0;
- for (i = 0; i < a.size(); i++)
- if (a[i].isEatable())
- {
- out.write((char*)&a[i].code, sizeof(a[i].code));
- out.write((char*)&a[i].code_pr, sizeof(a[i].code_pr));
- out.write((char*)&a[i].expiryDate, sizeof(a[i].expiryDate));
- k++;
- }
- cout << k << endl;
- }
- void CheckOfCalories(vector<Candies> &a, ofstream &out)
- {
- unsigned int i, j;
- Candies t;
- for (i = 0; i < a.size(); i++)
- for (j = 0; j < a.size() - 1; j++)
- if (a[j].calories < a[j + 1].calories)
- {
- t = a[j];
- a[j] = a[j + 1];
- a[j + 1] = t;
- }
- for (i = 0; i < a.size(); i++)
- {
- out.write((char*)&a[i].code, sizeof(a[i].code));
- out.write((char*)&a[i].code_pr, sizeof(a[i].code_pr));
- out.write((char*)&a[i].taste, sizeof(a[i].taste));
- out.write((char*)&a[i].calories, sizeof(a[i].calories));
- }
- cout << a.size() << endl;
- }
- void CheckOfTaste(vector<Candies> &a, ofstream &out)
- {
- unsigned int i;
- int k = 0;
- for (i = 0; i < a.size(); i++)
- if ((a[i].madeIn == 1) && (a[i].taste > 80))
- {
- out.write((char*)&a[i], sizeof(a[i]));
- k++;
- }
- cout << k << endl;
- }
- void CheckOfTopping(vector<Candies> &a, ofstream &out)
- {
- unsigned int i;
- int k = 0;
- for (i = 0; i < a.size(); i++)
- if ((a[i].taste > 0) && (a[i].taste < 50) && (a[i].topping % 3 == 0))
- {
- out.write((char*)&a[i].code, sizeof(a[i].code));
- out.write((char*)&a[i].code_pr, sizeof(a[i].code_pr));
- out.write((char*)&a[i].date, sizeof(a[i].date));
- out.write((char*)&a[i].taste, sizeof(a[i].taste));
- out.write((char*)&a[i].topping, sizeof(a[i].topping));
- k++;
- }
- cout << k << endl;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- ifstream in("candies.txt");
- ofstream out1("out1.bin", ios::binary);
- ofstream out2("out2.bin", ios::binary);
- ofstream out3("out3.bin", ios::binary);
- ofstream out4("out4.bin", ios::binary);
- Candies candie_init;
- vector<Candies> candies = candie_init.Create(in);
- Date nowadays = current_time();
- CheckOfDate(candies, out1);
- CheckOfCalories(candies, out2);
- CheckOfTaste(candies, out3);
- CheckOfTopping(candies, out4);
- in.close();
- out1.close();
- out2.close();
- out3.close();
- out4.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement