Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<fstream>
- #include<string>
- using namespace std;
- struct Date
- {
- int day;
- int month;
- int year;
- };
- struct Candy
- {
- int code_of_candy;
- int code_of_manufact;
- Date date_of_production;
- int expiration_date;
- double taste;
- int stuffing;
- double calorie;
- bool russia;
- };
- bool expiration_test(int day_p, int month_p, int year_p, int expiration_date)
- {
- Date c = { 0 };
- int now = 28 + (3* 30) + (2019* 365);
- int last_day = day_p + (month_p * 30) + (year_p * 365) + (expiration_date * 30);
- if (last_day <= now)
- return 1;
- else
- return 0;
- }
- void date_of_last_day(int month_p, int year_p, int expiration_date, int lm, int ly)
- {
- int last_month = month_p + expiration_date;
- if (last_month > 12)
- {
- year_p = last_month / 12;
- last_month %= 12;
- }
- lm = last_month;
- ly = year_p;
- }
- void create_candy(Candy *a, int n, ifstream&f)
- {
- string dat;
- for (int i = 0; i < n; i++)
- {
- f >> a[i].code_of_candy;
- f >> a[i].code_of_manufact;
- f >> dat;
- a[i].date_of_production.day = (dat[0] - '0') * 10 + (dat[1] - '0');
- a[i].date_of_production.month = (dat[3] - '0') * 10 + (dat[4] - '0');
- a[i].date_of_production.year = (dat[6] - '0') * 1000 + (dat[7] - '0') * 100 + (dat[8] - '0') * 10 + (dat[9] - '0');
- f >> a[i].expiration_date;
- f >> a[i].taste;
- f >> a[i].stuffing;
- f >> a[i].calorie;
- f >> a[i].russia;
- }
- }
- void output1(ofstream&out1, Candy *a, int n)
- {
- int k = 0;
- int lm = 0, ly = 0;
- for (int i = 0; i < n; i++)
- {
- date_of_last_day(a[i].date_of_production.month, a[i].date_of_production.year, a[i].expiration_date, lm, ly);
- 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))
- {
- out1.write((char*)&a[i].code_of_candy, sizeof(a[i].code_of_candy));
- out1.write((char*)&a[i].code_of_manufact, sizeof(a[i].code_of_manufact));
- out1.write((char*)&a[i].date_of_production.day, sizeof(a[i].date_of_production.day));
- out1.write((char*)&lm, sizeof(lm));
- out1.write((char*)&ly, sizeof(ly));
- k++;
- }
- }cout << "Quantity of candy in out1=" << k;
- cout << endl;
- }
- void sort_calorie(Candy *a, int n)
- {
- int i, j;
- Candy b;
- for (i = 0; i < n; i++)
- {
- for (j = 0; j > n - 1; j++)
- {
- if (a[j - 1].calorie > a[j].calorie)
- {
- b = a[j - 1];
- a[j - 1] = a[j];
- a[j] = b;
- }
- }
- }
- }
- void output2(ofstream&out2, Candy *a, int n)
- {
- sort_calorie(a, n);
- for (int i = 0; i < n; i++)
- {
- out2.write((char*)&a[i].code_of_candy, sizeof(a[i].code_of_candy));
- out2.write((char*)&a[i].code_of_manufact, sizeof(a[i].code_of_manufact));
- out2.write((char*)&a[i].taste, sizeof(a[i].taste));
- out2.write((char*)&a[i].stuffing, sizeof(a[i].stuffing));
- }
- cout << "Quantity in out2=" << n;
- cout << endl;
- }
- void output3(ofstream&out3, Candy *a, int n)
- {
- int k = 0;
- for (int i = 0; i < n; i++)
- {
- if (a[i].russia && a[i].taste > 80)
- {
- out3.write((char*)&a[i].code_of_candy, sizeof(a[i].code_of_candy));
- out3.write((char*)&a[i].code_of_manufact, sizeof(a[i].code_of_manufact));
- out3.write((char*)&a[i].date_of_production.day, sizeof(a[i].date_of_production.day));
- out3.write((char*)&a[i].date_of_production.month, sizeof(a[i].date_of_production.month));
- out3.write((char*)&a[i].date_of_production.year, sizeof(a[i].date_of_production.year));
- out3.write((char*)&a[i].taste, sizeof(a[i].taste));
- out3.write((char*)&a[i].stuffing, sizeof(a[i].stuffing));
- out3.write((char*)&a[i].calorie, sizeof(a[i].calorie));
- out3.write((char*)&a[i].russia, sizeof(a[i].russia));
- k++;
- }
- }
- cout << "Quantity in out3=" << k;
- cout << endl;
- }
- void output4(ofstream&out4, Candy *a, int n)
- {
- int k = 0;
- for (int i = 0; i < n; i++)
- {
- if (a[i].taste <= 50 & a[i].stuffing % 3 == 0)
- {
- out4.write((char*)&a[i].code_of_candy, sizeof(a[i].code_of_candy));
- out4.write((char*)&a[i].code_of_manufact, sizeof(a[i].code_of_manufact));
- out4.write((char*)&a[i].date_of_production.day, sizeof(a[i].date_of_production.day));
- out4.write((char*)&a[i].date_of_production.month, sizeof(a[i].date_of_production.month));
- out4.write((char*)&a[i].date_of_production.year, sizeof(a[i].date_of_production.year));
- out4.write((char*)&a[i].taste, sizeof(a[i].taste));
- out4.write((char*)&a[i].stuffing, sizeof(a[i].stuffing));
- k++;
- }
- }
- cout << "Quantity in out4=" << k;
- cout << endl;
- }
- int main()
- {
- ifstream in("lolipop (1).txt");
- ofstream out1("outt1.bin", ios::binary);
- ofstream out2("outt2.bin", ios::binary);
- ofstream out3("outt3.bin", ios::binary);
- ofstream out4("outt4.bin", ios::binary);
- if (in.eof())
- {
- cout << "Can't open file in";
- exit(1);
- }
- int n;
- cout << "Enter quantity of candy=";
- cin >> n;
- cout << endl;
- Candy *a;
- a = new Candy[n];
- create_candy(a, n, in);
- output1(out1, a, n);
- output2(out2, a, n);
- output3(out3, a, n);
- output4(out4, a, n);
- in.close();
- out1.close();
- out2.close();
- out3.close();
- out4.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement