-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJosephus.h
95 lines (82 loc) · 2.55 KB
/
Josephus.h
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
#pragma once
#include "CircularList.h"
#include <exception>
#include <sstream>
#include <fstream>
//--------------------------------------------
class FileException : std::exception
{
public:
explicit FileException(std::string msg) : std::exception(), msg{ msg } {}
virtual const char* what() const noexcept { return msg.c_str(); }
private:
std::string msg;
};
//--------------------------------------------
template<typename T>
class JosephusProblemSolver
{
public:
JosephusProblemSolver() = default;
CircularList<T> operator()(size_t numberOfSoldiers = 41,
size_t numberOfSurvivors = 1,
size_t executionStep = 2) const;
CircularList<T> operator()(std::string filename,
size_t numberOfSurvivors = 1,
size_t executionStep = 2) const;
};
//--------------------------------------------
template<typename T>
CircularList<T> JosephusProblemSolver<T>::operator()(size_t numberOfSoldiers,
size_t numberOfSurvivors,
size_t executionStep) const
{
CircularList<T> cl{};
cl.generateN(numberOfSoldiers, [n = 1]() mutable { return n++; });
cl.removeEveryNUntilMRemains(executionStep, numberOfSurvivors);
return cl;
}
template<typename T>
CircularList<T> JosephusProblemSolver<T>::operator()(std::string filename,
size_t numberOfSurvivors,
size_t executionStep) const
{
CircularList<T> cl{};
size_t numberOfSoldiers{};
if (cl.pushFromFile(filename)) {
numberOfSoldiers = cl.size();
cl.removeEveryNUntilMRemains(executionStep, numberOfSurvivors);
}
else {
std::stringstream msg;
msg << "File Exception: " << filename << " does not exist or invalid format.";
throw FileException(msg.str());
}
std::stringstream outputFilename{};
outputFilename << "solution_" << filename;
std::ofstream outputFile(outputFilename.str(), std::ios::out | std::ios::trunc);
if (!outputFile) {
std::stringstream msg;
msg << "File Exception: Unable to create " << outputFilename.str() << ".";
throw FileException(msg.str());
}
std::stringstream headline;
headline << "The Josephus Problem:" << std::endl
<< "Initial parameters:" << std::endl
<< "Number of soldiers: " << numberOfSoldiers << std::endl
<< "Number of survivors: " << numberOfSurvivors << std::endl
<< "Execution step: " << executionStep << std::endl
<< "Survivors:" << std::endl;
cl.print(headline, "\n");
if (!(outputFile << headline.str())) {
outputFile.close();
std::stringstream msg;
msg << "File Exception: Error while writing to " << outputFilename.str() << ".";
throw FileException(msg.str());
}
else {
outputFile.close();
}
return cl;
}
//--------------------------------------------