-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBorrowers.cpp
More file actions
72 lines (61 loc) · 2.36 KB
/
Borrowers.cpp
File metadata and controls
72 lines (61 loc) · 2.36 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
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <regex>
using namespace std;
int main(){
string line, cmd, title;
//<Titulo,Status>
// Status: 0: Na bibliota, 1:Emprestado, 2 : Devolvido mas ainda não foi reposto
map<string,int> livros;
map<string,set<string>> autor_livros;
vector<string> vetLivros;
//Busca pelo titulo e autor do livro na linha
regex regexpTitleAutor("(\".+\") by (.+)$");
smatch match;
while(true){
getline(cin, line);
if(line == "END")
break;
regex_search(line, match, regexpTitleAutor);
livros[match[1]] = 0;
autor_livros[match[2]].insert(match[1]);
}
//Gera um vetor ordenado de livros
for(auto m : autor_livros)
for(auto l: m.second)
vetLivros.push_back(l);
//Lê os comandos
regex regexpComando ("(\".+\")");
while(true){
cin >> cmd;
if(cmd == "END"){
break;
}
if(cmd == "SHELVE"){
for (auto livro=vetLivros.begin(); livro!=vetLivros.end(); ++livro){
int &status = livros[*livro];
if(status == 2){
cout << "Put " << *livro;
auto livro_anterior = livro;
if(livro_anterior != vetLivros.begin())
livro_anterior--;
while(livros[*livro_anterior] != 0 && livro_anterior != vetLivros.begin()){livro_anterior--;}
if(livro_anterior == vetLivros.begin() && livros[*livro_anterior] != 0){
cout << " first" <<endl;
}else
cout << " after " << *livro_anterior << endl;
status = 0;
}
}
cout << "END" << endl;
}
else{
getline(cin, title);
regex_search(title, match, regexpComando);
// 1:Emprestado, 2 : Devolvido mas ainda não foi reposto
livros[match[0]] = cmd == "BORROW" ? 1 : 2;
}
}
}