Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #pragma once
  2. #include<iostream>
  3. #include<fstream>
  4. #include<string>
  5. #include<vector>
  6.  
  7. using std::cout;
  8. using std::string;
  9. using std::fstream;
  10. using std::vector;
  11.  
  12. string solveEx1(string filepath_input, string filepath_output) {
  13.     vector<std::pair<int, string>> contents;
  14.     fstream file;
  15.     file.open(filepath_input.c_str(), std::ios::in);
  16.     string nameTmp;
  17.     int ageTmp;
  18.     while (!file.eof()) {
  19.         file >> nameTmp;
  20.         file >> ageTmp;
  21.         contents.push_back(std::make_pair(ageTmp, nameTmp));
  22.     }
  23.  
  24.     // Search for max and min age
  25.     int min_ = contents[0].first;
  26.     int max_ = contents[0].first;
  27.  
  28.     for (unsigned int i = 0; i < contents.size(); i++) {
  29.         if (min_ > contents[i].first)
  30.             min_ = contents[i].first;
  31.         if (max_ < contents[i].first)
  32.             max_ = contents[i].first;
  33.     }
  34.    
  35.     string ret_max_names = "";
  36.     string ret_min_names = "";
  37.  
  38.     for (unsigned int i = 0; i < contents.size(); i++) {
  39.         if (contents[i].first == min_) {
  40.             if(ret_min_names.find(contents[i].second) == string::npos)
  41.                 ret_min_names = ret_min_names + contents[i].second;
  42.             if (i != contents.size() - 1)
  43.                 ret_min_names += " ";
  44.         }
  45.            
  46.         if (contents[i].first == max_) {
  47.             if (ret_max_names.find(contents[i].second) == string::npos)
  48.                 ret_max_names = ret_max_names + contents[i].second;
  49.             if (i != contents.size() - 1)
  50.                 ret_max_names += " ";
  51.         }
  52.     }
  53.  
  54.     /*
  55.     cout << "\n";
  56.     cout << min_ << " " << ret_min_names << "\n";
  57.     cout << max_ << " " << ret_max_names << "\n";
  58.     */
  59.  
  60.     string ret = std::to_string(min_) + " " + ret_min_names + "\n";
  61.     ret += std::to_string(max_) + " " + ret_max_names + "\n";
  62.  
  63.     fstream file_output;
  64.     file_output.open(filepath_output.c_str(), std::ios::out);
  65.     file_output << ret;
  66.     cout << "[+] Saved results to: " << filepath_output << "\n";
  67.     return nameTmp;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement