-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cpp
More file actions
85 lines (59 loc) · 1.99 KB
/
Copy pathDatabase.cpp
File metadata and controls
85 lines (59 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Database.h"
Database::Database() {}
static void limparLinha(std::string& linha) {
while (!linha.empty() && (linha.back() == '\r' || linha.back() == '\n' || linha.back() == ' ')) {
linha.pop_back();
}
}
bool Database::readCsv(std::string database) {
std::ifstream arquivo(database);
if (!arquivo) return false;
std::string linhaAtual;
if (std::getline(arquivo, linhaAtual)) {
limparLinha(linhaAtual);
std::stringstream linhaAtualStream(linhaAtual);
std::string coluna;
std::getline(linhaAtualStream, coluna, ',');
while (std::getline(linhaAtualStream, coluna, ',')) {
strNames.push_back(coluna);
}
}
while (std::getline(arquivo, linhaAtual)) {
limparLinha(linhaAtual);
std::stringstream linhaAtualStream(linhaAtual);
std::string pessoaData;
Profile pessoa;
std::getline(linhaAtualStream, pessoaData, ',');
pessoa.name = pessoaData;
for (int i = 0; i < (int)strNames.size(); i++) {
std::getline(linhaAtualStream, pessoaData, ',');
int valor = std::stoi(pessoaData);
std::string str = strNames[i];
pessoa.strCounts[str] = valor;
}
profiles.push_back(pessoa);
}
arquivo.close();
return true;
}
std::vector<std::string> Database::getStrNames() const {
return strNames;
}
std::string Database::search(const std::map<std::string, int>& profile) const {
for (const auto& pessoa : profiles) {
bool combina = true;
for (const auto& par : pessoa.strCounts) {
auto desconhecidoPar = profile.find(par.first);
if (desconhecidoPar == profile.end() || desconhecidoPar->second != par.second) {
combina = false;
break;
}
}
if (combina) return pessoa.name;
}
return "";
}