-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
300 lines (273 loc) · 6.57 KB
/
Copy pathmain.cpp
File metadata and controls
300 lines (273 loc) · 6.57 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <map>
#include "euler.h"
typedef std::pair<int,euler_problem_info> problem_entry_t;
static std::map<int,euler_problem_info>& problems()
{
static std::map<int,euler_problem_info> p;
return p;
}
void register_problem(const euler_problem_info &info) noexcept
{
try
{
problems().insert(problem_entry_t(info.id, info));
}
catch (const std::exception &ex)
{
// Can't do much here as this function is called in static initializer.
(void)ex;
}
}
static bool s_verbose = false;
bool verbose() noexcept
{
return s_verbose;
}
static void usage()
{
std::cout << "usage: euler [options] id ..." << std::endl;
std::cout << "where: id is the problem number to solve." << std::endl;
std::cout << "options:" << std::endl;
std::cout << " -h display this help screen" << std::endl;
std::cout << " -l list available solutions" << std::endl;
#ifdef _MSC_VER
std::cout << " -p pause before exit" << std::endl;
#endif
std::cout << " -r run regression test" << std::endl;
std::cout << " -s display statistics" << std::endl;
std::cout << " -t time the calculation" << std::endl;
std::cout << " -v display detailed information" << std::endl;
}
static void list_problems()
{
std::for_each(problems().cbegin(), problems().cend(), [](const problem_entry_t &ent) {
const euler_problem_info &p = ent.second;
std::cout.width(4);
std::cout << p.id << " " << p.title << std::endl;
});
}
static PROBLEM_FUNC find_problem(int number)
{
auto it = problems().find(number);
if (it == problems().end())
{
return nullptr;
}
else
{
return it->second.routine;
}
}
static bool run_regression(int id, bool timing)
{
//typedef std::chrono::high_resolution_clock clock;
if (id == 0)
{
std::cout << " ID STATUS" << (timing? " TIME" : "") << std::endl;
std::cout << "------------" << (timing? "-----------" : "") << std::endl;
}
double total_time = 0;
int nfailed = 0;
for (const auto &id_problem: problems())
{
const euler_problem_info &p = id_problem.second;
if ((id != 0 && p.id != id) || p.routine == nullptr)
{
continue;
}
// Print problem id.
std::cout << std::setw(4) << p.id << " ";
std::cout.flush();
// Redirect stdout buffer to a string.
std::ostringstream ss;
std::streambuf *oldbuf = std::cout.rdbuf(ss.rdbuf());
// Execute and time the routine. The output is stored in ss.
//clock::time_point start = clock::now();
p.routine();
//std::chrono::duration<double> t1 = clock::now() - start;
//total_time += t1.count();
// Restore stdout.
std::cout.rdbuf(oldbuf);
// Get result from the buffer. Only retain the first line.
std::string result = ss.str();
auto k = result.find_first_of('\n');
if (k != result.npos)
{
result.resize(k);
}
// Check routine result.
bool ok = (p.answer != nullptr && result == p.answer);
// Print test status.
std::cout << (ok? "OK " : "FAILED");
#if 0
if (timing)
{
std::cout << " [" << std::setw(7) << std::setprecision(3)
<< std::fixed << t1.count() << "]";
}
#endif
std::cout << std::endl;
if (!ok)
{
++nfailed;
}
}
// Display regression test summary.
if (id == 0)
{
std::cout << "------------" << (timing? "-----------" : "") << std::endl;
if (nfailed == 0)
{
std::cout << " ALL";
}
else
{
std::cout << std::setw(4) << nfailed;
}
std::cout << ' ' << (nfailed > 0? "FAILED" : "OK ");
if (timing)
{
std::cout << " [" << std::setw(7) << std::setprecision(3)
<< std::fixed << total_time << "]";
}
std::cout << std::endl;
}
return (nfailed == 0);
}
static void run_solution(int id)
{
PROBLEM_FUNC f = find_problem(id);
f();
}
static void display_statistics()
{
std::cout << "Number of solutions: " << problems().size() << std::endl;
if (!problems().empty())
{
std::cout << "Progress:" << std::endl;
std::cout << "---------+----------+----------+----------+----------+----------+" << std::endl;
int max_id = problems().crbegin()->first;
for (int i = 1; i <= max_id; i += 50)
{
std::cout << ' ';
std::cout.width(3);
std::cout << i << "-";
std::cout.width(3);
std::cout << (i+49) << " |";
for (int j = 0; j <= 49; j++)
{
char c = (find_problem(i + j) != nullptr)? '*' : ' ';
std::cout << c;
if (j < 49 && j % 10 == 9)
{
std::cout << ' ';
}
}
std::cout << "|" << std::endl;
}
std::cout << "---------+----------+----------+----------+----------+----------+" << std::endl;
}
}
int main(int argc, char *argv[])
{
enum {
action_default,
action_regression,
action_stat,
action_list,
action_help
} action = action_default;
bool timing = false;
#ifdef _MSC_VER
bool pause = false;
#endif
int id = 0;
bool test_ok = true;
// Parse command line arguments.
for (int i = 1; i < argc; i++)
{
const char *s = argv[i];
if (s[0] == '-')
{
for (int j = 1; s[j] != '\0'; j++)
{
switch (s[j])
{
case 'h':
action = action_help;
break;
case 'l':
action = action_list;
break;
#ifdef _MSC_VER
case 'p':
pause = true;
break;
#endif
case 'r':
action = action_regression;
break;
case 's':
action = action_stat;
break;
case 't':
timing = true;
break;
case 'v':
s_verbose = true;
break;
default:
std::cerr << "Unknown option: -" << s[j] << std::endl;
return 2;
}
}
}
else
{
char *endptr;
id = strtol(s, &endptr, 10);
if (*endptr != '\0')
{
std::cerr << "Problem ID must be an integer." << std::endl;
return 3;
}
if (find_problem(id) == nullptr)
{
std::cerr << "Cannot find problem #" << id << "." << std::endl;
return 3;
}
}
}
switch (action)
{
case action_help:
usage();
break;
case action_list:
list_problems();
break;
case action_stat:
display_statistics();
break;
case action_regression:
test_ok = run_regression(id, timing);
break;
default:
if (id == 0)
{
usage();
return 2;
}
run_solution(id);
break;
}
#ifdef _MSC_VER
if (pause)
system("PAUSE");
#endif
return (test_ok)? 0 : 1;
}