Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. void plus300(vector<auta> &x, vector<auta> &plus) {
  2.     vector<auta>::iterator iter;
  3.     for (auto itr = x.begin(); itr != x.end(); ++itr) {
  4.         if (itr->power >= 300) {
  5.             iter = itr;
  6.             plus.push_back(*iter);
  7.         }
  8.     }
  9.  
  10.     cout << plus.size() << " samochodow przekroczylo bariere 300" << endl << endl;
  11. }
  12.  
  13. bool najmniejszy(auta a1, auta a2) {
  14.     if (a1.year < a2.year)
  15.         return true;
  16.     else
  17.         return false;
  18. }
  19.  
  20. void first300(vector<auta> &plus) {
  21.     vector<auta>::iterator itr = min_element(plus.begin(), plus.end(), najmniejszy);
  22.    
  23.     cout << "Pierwszy bariere 300 przekroczyl " << itr->model << " w roku " << itr->year << endl;
  24. }
  25.  
  26. bool sortByYear(auta a1, auta a2) {
  27.     if (a1.model < a2.model)
  28.         return true;
  29.     else
  30.         return false;
  31. }
  32.  
  33. void sortowanie(vector<auta> &x) {
  34.     fstream plik;
  35.     plik.open("cars_sorted.txt", ios::out);
  36.  
  37.     if (plik.is_open()) {
  38.         sort(x.begin(), x.end(), sortByYear);
  39.         for (auto &itr : x) {
  40.             plik << itr.year << "|" << itr.country << "|" << itr.model << "|" << itr.power << endl;
  41.         }
  42.     }
  43.     else
  44.         cout << "ERROR SORT!" << endl;
  45. }
  46.  
  47. bool theFastest(auta a1, auta a2) {
  48.     if (a1.power < a2.power)
  49.         return true;
  50.     else
  51.         return false;
  52. }
  53.  
  54. void najszybszy(vector<auta> &x) {
  55.     vector<auta>::iterator itr = max_element(x.begin(), x.end(), theFastest);
  56.  
  57.     cout << "Model: " << itr->model << endl;
  58.     cout << "Rok: " << itr->year << endl;
  59.     cout << "Kraj: " << itr->country << endl;
  60.     cout << "Moc: " << itr->power << endl;
  61. }
  62.  
  63. void zad1() {
  64.     fstream file;
  65.     file.open("auta2.txt", ios::in);
  66.     vector<auta> v_cars;
  67.     vector<auta> above300;
  68.     auta car;
  69.  
  70.     if (file.is_open()) {
  71.         while (!file.eof()) {
  72.             char tmp;
  73.             string lastString;
  74.             file >> car.year; file >> tmp;
  75.             getline(file, car.country, ';');
  76.             getline(file, car.model, ';');
  77.             file >> car.power; file >> lastString;
  78.  
  79.             v_cars.push_back(car);
  80.         }
  81.        
  82.         plus300(v_cars, above300);
  83.  
  84.         first300(above300);
  85.  
  86.         sortowanie(v_cars);
  87.  
  88.         najszybszy(v_cars);
  89.        
  90.         /*for (auto &itr : v_cars)
  91.             cout << itr.year << "|" << itr.country << "|" << itr.model << "|" << itr.power << endl;*/
  92.     }
  93.     else
  94.         cout << "ERROR!" << endl;
  95.  
  96.     file.close();
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement