Skip to content

Instantly share code, notes, and snippets.

@eltonvs
Created September 20, 2016 14:37
Show Gist options
  • Save eltonvs/963727dd9a72ef9b74aba803d5cee2a0 to your computer and use it in GitHub Desktop.
Save eltonvs/963727dd9a72ef9b74aba803d5cee2a0 to your computer and use it in GitHub Desktop.
asdasdasdas
// Tire isso dps, to usando so pra n me preocupar com os includes kkk
#include <bits/stdc++.h>
#define POS_NUMBER 12
#define POS_NAME 10
#define POS_ENTOURAGE 18
#define POS_CITY 41
#define POS_STATUS 16
struct Candidate {
int number;
std::string status;
std::string name;
std::string entourage;
std::string city;
Candidate() {}
};
std::ostream & operator<<(std::ostream &o, Candidate &c) {
o << "Number: " << c.number
<<"\nName: " << c.name
<< "\nEntourage: " << c.entourage
<< "\nCity: " << c.city << "\n";
return o;
}
std::istream & operator>>(std::istream &o, Candidate &c) {
std::vector<std::string> data;
std::string token;
// Get data without quotes
while (std::getline(o, token, ';'))
data.push_back(token);
c.number = std::atoi(data[POS_NUMBER].substr(1, data[POS_NUMBER].size() - 2).c_str());
c.name = data[POS_NAME];
c.entourage = data[POS_ENTOURAGE];
c.city = data[POS_CITY];
c.status = data[POS_STATUS].substr(1, data[POS_STATUS].size() - 2);
return o;
}
bool filter_city(Candidate c, std::string city_name) {
city_name = "\"" + city_name + "\"";
if (c.city.size() != city_name.size())
return false;
for (auto i = 0u; i < city_name.size(); i++)
if (tolower(city_name[i]) != tolower(c.city[i]))
return false;
return true;
}
int main(int argc, char *argv[]) {
std::string file_name, filter, output_file_name;
if (argc >= 3) {
file_name = argv[1];
filter = argv[2];
output_file_name = filter + ".dat";
} else {
std::cerr << "The input must to have a file and a filter\n";
return EXIT_FAILURE;
}
std::ifstream input_file(file_name);
if (!input_file.is_open()) {
std::cerr << "The input file cannot be opened right now, please try again later\n";
return EXIT_FAILURE;
}
std::ofstream output_file(output_file_name);
if (!output_file.is_open()) {
std::cout << output_file_name << "\n";
std::cerr << "The output file cannot be opened right now, please try again later\n";
input_file.close();
return EXIT_FAILURE;
}
std::string line;
std::vector<Candidate> filtered_candidates;
Candidate tmp;
while (std::getline(input_file, line)) {
std::stringstream ss(line);
ss >> tmp;
// Append candidate only if is "DEFERIDO"
if (tmp.status == "DEFERIDO" && filter_city(tmp, filter))
filtered_candidates.push_back(tmp);
}
for (Candidate c : filtered_candidates)
output_file << c << "\n";
// Close opened files
input_file.close();
output_file.close();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment