Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <math.h>
- #include <vector>
- #include <algorithm>
- #include <fstream>
- #include <string>
- using namespace std;
- struct carsInfo
- {
- char model[21];
- char country[21];
- float fuelConsumption;
- int carryingCapacity;
- float totalFuelConsumption;
- };
- bool operator < (const carsInfo &a, const carsInfo &b)
- {
- return a.totalFuelConsumption < b.totalFuelConsumption;
- }
- int main()
- {
- ifstream fin("input.bin", ios::binary | ios::in);
- setlocale(LC_ALL, "Russian");
- char country[21] = "Россия";
- int carsQty;
- fin.read((char*)&carsQty, sizeof(carsQty));
- string fileName;
- cout << "Введите имя и расширение выходного файла в формате: имя.расширение" << endl;
- cin >> fileName;
- ofstream fout(fileName, ios::binary | ios::out);
- int loadWeight, distance;
- cout << "Введите груз и расстояние, на которое его нужно перевезти" << endl;
- cin >> loadWeight >> distance;
- vector <carsInfo> allCars;
- for (int i = 0; i < carsQty; i++)
- {
- carsInfo currentCar;
- fin.read((char*)¤tCar, sizeof(currentCar) - sizeof(currentCar.totalFuelConsumption));
- currentCar.totalFuelConsumption = floor((loadWeight / currentCar.carryingCapacity) + 1) * currentCar.fuelConsumption * distance / 100;
- allCars.push_back(currentCar);
- }
- fin.close();
- sort(allCars.begin(), allCars.end());
- int carsCounter = 0;
- for (int i = 0; i < carsQty; i++)
- {
- static float minTotalFuelConsumption = allCars[i].totalFuelConsumption;
- if (allCars[i].totalFuelConsumption == minTotalFuelConsumption)
- {
- carsCounter++;
- }
- else
- {
- break;
- }
- }
- fout.write((char*)&carsCounter, sizeof(carsCounter));
- for (int i = 0; i < carsCounter; i++)
- {
- if (allCars[i].country[0] == 'Р' && allCars[i].country[1] == 'о' && allCars[i].country[2] == 'с')
- {
- fout.write((char*)&allCars[i], sizeof(allCars[i]) - sizeof(allCars[i].totalFuelConsumption));
- }
- }
- for (int i = 0; i < carsCounter; i++)
- {
- if (allCars[i].country[0] != 'Р' && allCars[i].country[1] != 'о' && allCars[i].country[2] != 'с')
- {
- fout.write((char*)&allCars[i], sizeof(allCars[i]) - sizeof(allCars[i].totalFuelConsumption));
- }
- }
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment