Linkin_Park

Lab5

Dec 25th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <math.h>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <fstream>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. struct carsInfo
  12. {
  13.     char model[21];
  14.     char country[21];
  15.     float fuelConsumption;
  16.     int carryingCapacity;
  17.     float totalFuelConsumption;
  18. };
  19.  
  20. bool operator < (const carsInfo &a, const carsInfo &b)
  21. {
  22.     return a.totalFuelConsumption < b.totalFuelConsumption;
  23. }
  24.  
  25. int main()
  26. {
  27.     ifstream fin("input.bin", ios::binary | ios::in);
  28.     setlocale(LC_ALL, "Russian");
  29.    
  30.     char country[21] = "Россия";
  31.     int carsQty;
  32.     fin.read((char*)&carsQty, sizeof(carsQty));
  33.  
  34.     string fileName;
  35.     cout << "Введите имя и расширение выходного файла в формате: имя.расширение" << endl;
  36.     cin >> fileName;
  37.  
  38.     ofstream fout(fileName, ios::binary | ios::out);
  39.    
  40.     int loadWeight, distance;
  41.     cout << "Введите груз и расстояние, на которое его нужно перевезти" << endl;
  42.     cin >> loadWeight >> distance;
  43.  
  44.     vector <carsInfo> allCars;
  45.  
  46.     for (int i = 0; i < carsQty; i++)
  47.     {
  48.         carsInfo currentCar;
  49.         fin.read((char*)&currentCar, sizeof(currentCar) - sizeof(currentCar.totalFuelConsumption));
  50.         currentCar.totalFuelConsumption = floor((loadWeight / currentCar.carryingCapacity) + 1) * currentCar.fuelConsumption * distance / 100;
  51.         allCars.push_back(currentCar);
  52.     }
  53.  
  54.     fin.close();
  55.  
  56.     sort(allCars.begin(), allCars.end());
  57.  
  58.     int carsCounter = 0;
  59.  
  60.     for (int i = 0; i < carsQty; i++)
  61.     {
  62.         static float minTotalFuelConsumption = allCars[i].totalFuelConsumption;
  63.  
  64.         if (allCars[i].totalFuelConsumption == minTotalFuelConsumption)
  65.         {
  66.             carsCounter++;
  67.         }
  68.         else
  69.         {
  70.             break;
  71.         }
  72.     }
  73.  
  74.     fout.write((char*)&carsCounter, sizeof(carsCounter));
  75.  
  76.     for (int i = 0; i < carsCounter; i++)
  77.     {
  78.         if (allCars[i].country[0] == 'Р' && allCars[i].country[1] == 'о' && allCars[i].country[2] == 'с')
  79.         {
  80.             fout.write((char*)&allCars[i], sizeof(allCars[i]) - sizeof(allCars[i].totalFuelConsumption));
  81.         }
  82.     }
  83.  
  84.     for (int i = 0; i < carsCounter; i++)
  85.     {
  86.         if (allCars[i].country[0] != 'Р' && allCars[i].country[1] != 'о' && allCars[i].country[2] != 'с')
  87.         {
  88.             fout.write((char*)&allCars[i], sizeof(allCars[i]) - sizeof(allCars[i].totalFuelConsumption));
  89.         }
  90.     }
  91.  
  92.     fout.close();
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment