-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.cpp
83 lines (68 loc) · 1.87 KB
/
main.cpp
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
#include "Person.h"
#include "GenePool.h"
#include "Query.h"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <vector>
// This file provides a standard main loop.
// You can edit it if you want, but you shouldn't need to.
// Gradescope will use the original version.
// Helper class to sort person pointers by name:
struct Compare {
bool operator () (const Person* a, const Person* b) const {
return a->name() < b->name();
}
};
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "USAGE: ./genepool [datafile.tsv]\n";
return 1;
}
GenePool* pool = nullptr;
try {
// Read the database file:
std::ifstream stream(argv[1]);
if(stream.fail()) {
std::cout << "Error opening database file.\n";
return 1;
}
pool = new GenePool(stream);
}
catch(const std::exception& e) {
std::cerr << "Error reading database: " << e.what() << "\n";
return 1;
}
std::string line;
std::cout << "> ";
while(std::getline(std::cin, line)) {
try {
// Parse and run the query:
Query query(line);
std::set<Person*> result = query.run(*pool);
// Make sure everyone is valid:
if(result.count(nullptr) != 0) {
throw std::runtime_error("Result set contained a null pointer.");
}
// Sort the people by name for consistent output:
std::vector<Person*> people(result.begin(), result.end());
std::sort(people.begin(), people.end(), Compare());
// Print the result:
for(Person* person: people) {
std::cout << " - " << person->name() << std::endl;
}
if(people.empty()) {
std::cout << " (no results)\n";
}
}
catch(const std::exception& e) {
// Print the error message:
std::cout << e.what() << std::endl;
}
std::cout << "> ";
}
std::cout << '\n';
delete pool;
return 0;
}