Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <fstream>
- using namespace std;
- struct date
- {
- int day;
- int month;
- int year;
- };
- struct candy
- {
- int code;
- int manufactury_code;
- date date_production;
- int shelf_life;
- int taste;
- int filling;
- int calorie;
- int rus;
- date end_shelf;
- };
- candy* create(ifstream &f, int n)
- {
- int i;
- char*data = new char[10];
- candy* a = new candy[n];
- for (i = 0; i < n; i++)
- {
- f >> a[i].code >> a[i].manufactury_code;
- f >> data;
- a[i].date_production.day = (int(data[0]) - 48) * 10 + int(data[1]) - 48;
- a[i].date_production.month = (int(data[3]) - 48) * 10 + int(data[4]) - 48;
- a[i].date_production.year = (int(data[6]) - 48) * 1000 + (int(data[7]) - 48) * 100 + (int(data[8] - 48)) * 10 + int(data[9] - 48);
- f >> a[i].shelf_life >> a[i].taste >> a[i].filling >> a[i].calorie >> a[i].rus;
- a[i].end_shelf.day = a[i].date_production.day;
- a[i].end_shelf.month = a[i].date_production.month + a[i].shelf_life;
- a[i].end_shelf.year = a[i].date_production.year;
- while (a[i].end_shelf.month > 12)
- {
- a[i].end_shelf.month -= 12;
- a[i].end_shelf.year += 1;
- }
- }
- return a;
- }
- bool compare(date candies, date today)
- {
- if (candies.year > today.year)
- return false;
- else
- if (candies.year < today.year)
- return true;
- else
- if (candies.month > today.month)
- return false;
- else
- if (candies.month < today.month)
- return true;
- else
- if (candies.day >= today.day)
- return false;
- else
- return true;
- }
- void cTerm(candy* a, date daynow, int n, ofstream &out)
- {
- int i, k = 0;
- for (i = 0; i < n; i++)
- if (compare(a[i].end_shelf, daynow))
- {
- out.write((char*)&a[i].code, sizeof(a[i].code));
- out.write((char*)&a[i].manufactury_code, sizeof(a[i].manufactury_code));
- out.write((char*)&a[i].end_shelf, sizeof(a[i].end_shelf));
- k++;
- }
- cout << k << endl;
- }
- void cSortCalories(candy* a, int n, ofstream &out)
- {
- int i, j;
- candy t;
- for (i = 0; i < n; i++)
- for (j = 0; j < n - 1; j++)
- if (a[j].calorie < a[j + 1].calorie)
- {
- t = a[j];
- a[j] = a[j + 1];
- a[j + 1] = t;
- }
- for (i = 0; i < n; i++)
- {
- out.write((char*)&a[i].code, sizeof(a[i].code));
- out.write((char*)&a[i].manufactury_code, sizeof(a[i].manufactury_code));
- out.write((char*)&a[i].taste, sizeof(a[i].taste));
- out.write((char*)&a[i].calorie, sizeof(a[i].calorie));
- }
- cout << n << endl;
- }
- void cTaste(candy*a, int n, ofstream &out)
- {
- int i;
- int k = 0;
- for (i = 0; i < n; i++)
- if ((a[i].rus == 1) && (a[i].taste > 80))
- {
- out.write((char*)&a[i].code, sizeof(a[i].code));
- out.write((char*)&a[i].manufactury_code, sizeof(a[i].manufactury_code));
- out.write((char*)&a[i].date_production, sizeof(a[i].date_production));
- out.write((char*)&a[i].shelf_life, sizeof(a[i].shelf_life));
- out.write((char*)&a[i].taste, sizeof(a[i].taste));
- out.write((char*)&a[i].filling, sizeof(a[i].filling));
- out.write((char*)&a[i].calorie, sizeof(a[i].calorie));
- out.write((char*)&a[i].rus, sizeof(a[i].rus));
- k++;
- }
- cout << k << endl;
- }
- void cFilling(candy*a, int n, ofstream &out)
- {
- int i;
- int k = 0;
- for (i = 0; i < n; i++)
- if ((a[i].taste > 0) && (a[i].taste < 50) && (a[i].filling % 3 == 0))
- {
- out.write((char*)&a[i].code, sizeof(a[i].code));
- out.write((char*)&a[i].manufactury_code, sizeof(a[i].manufactury_code));
- out.write((char*)&a[i].date_production, sizeof(a[i].date_production));
- out.write((char*)&a[i].taste, sizeof(a[i].taste));
- out.write((char*)&a[i].filling, sizeof(a[i].filling));
- k++;
- }
- cout << k << endl;
- }
- int main()
- {
- ifstream inputcandy("inputcandy.txt");
- ofstream out1("output1.bin", ios::binary);
- ofstream out2("output2.bin", ios::binary);
- ofstream out3("output3.bin", ios::binary);
- ofstream out4("output4.bin", ios::binary);
- int n;
- cout << "Enter number of candies: ";
- cin >> n;
- candy*a = create(inputcandy, n);
- char*b = new char[10];
- date daynow;
- cout << "Enter today's date: ";
- cin >> b;
- daynow.day = (int(b[0]) - 48) * 10 + int(b[1]) - 48;
- daynow.month = (int(b[3]) - 48) * 10 + int(b[4]) - 48;
- daynow.year = (int(b[6]) - 48) * 1000 + (int(b[7]) - 48) * 100 + (int(b[8] - 48)) * 10 +
- int(b[9] - 48);
- cTerm(a, daynow, n, out1);
- cSortCalories(a, n, out2);
- cTaste(a, n, out3);
- cFilling(a, n, out4);
- inputcandy.close();
- out1.close();
- out2.close();
- out3.close();
- out4.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement