-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.cpp
91 lines (74 loc) · 2.25 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
84
85
86
87
88
89
90
91
#include "WordList.h"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
// This file provides an interactive loop.
// It's much easier to use in conjunction with helper.py.
// See the hints section in the readme for details.
std::vector<Point> read_points(const std::string& line) {
Point point;
std::vector<Point> results;
std::istringstream stream(line);
while(stream >> point.x) {
if(!(stream >> point.y)) {
throw std::runtime_error("Odd number of points.");
}
results.push_back(point);
}
if(results.size() == 0) {
throw std::runtime_error("No points given.");
}
return results;
}
int main(int argc, char** argv) {
if(argc != 2) {
std::cout << "USAGE: " << argv[0] << " [words-file]\n";
return 1;
}
// Change these to test different cases!
size_t maxcount = 5;
float cutoff = 0;
// Read in the words file
WordList* wordlist = nullptr;
try {
std::ifstream stream(argv[1]);
if(stream.fail()) {
std::cout << "Could not open file: " << argv[1] << '\n';
return 1;
}
wordlist = new WordList(stream);
}
catch(const std::exception& e) {
std::cout << "Error reading words file: " << e.what() << '\n';
return 1;
}
// Set floating point format
std::cout << std::setprecision(3);
std::cout << std::fixed;
// Interactive loop
std::string line;
std::cout << "> ";
while(std::getline(std::cin, line)) {
try {
auto points = read_points(line);
auto heap = wordlist->correct(points, maxcount, cutoff);
std::vector<Heap::Entry> entries;
while(heap.count() > 0) {
entries.push_back(heap.pop());
}
for(auto itr = entries.rbegin(); itr != entries.rend(); ++itr) {
std::cout << " - " << itr->score << ": " << itr->value << '\n';
}
if(entries.size() == 0) {
std::cout << " (no results)\n";
}
}
catch(const std::exception& e) {
std::cout << "Error: " << e.what() << '\n';
}
std::cout << "> ";
}
delete wordlist;
return 0;
}