Execute a Markov algorithm: Difference between revisions
→{{header|C++}}: Make it more structured.
(→{{header|Python}}: Add the last stretch goal to the assertions.) |
(→{{header|C++}}: Make it more structured.) |
||
Line 271:
#include <vector>
#include <string>
struct rule
{
Line 284:
}
};
std::string const whitespace = " \t";
std::string::size_type const npos = std::string::npos;
bool is_whitespace(char c)
{
Line 293:
}
std::vector<rule> read_rules(std::ifstream& rulefile)
int main(int argc, char* argv[])▼
{
if (argc != 3)▼
{▼
std::cout << "usage:\n " << argv[0] << " rulefile text\n";▼
return EXIT_FAILURE;▼
}▼
std::ifstream rulefile(argv[1]);▼
std::vector<rule> rules;
std::string line;
Line 307 ⟶ 300:
{
std::string::size_type pos;
// remove comments
pos = line.find('#');
if (pos != npos)
line.resize(pos);
// ignore lines consisting only of whitespace
if (line.find_first_not_of(whitespace) == npos)
continue;
// find "->" surrounded by whitespace
pos = line.find("->");
while (pos != npos && (pos == 0 || !is_whitespace(line[pos-1])))
pos = line.find("->", pos+1);
if (pos == npos || line.length() < pos+3 || !is_whitespace(line[pos+2]))
{
Line 327 ⟶ 320:
return EXIT_FAILURE;
}
std::string pattern = line.substr(0, pos-1);
std::string replacement = line.substr(pos+3);
// remove additional separating whitespace
pattern.erase(pattern.find_last_not_of(whitespace)+1);
replacement.erase(0, replacement.find_first_not_of(whitespace));
// test for terminal rule
bool terminal = !replacement.empty() && replacement[0] == '.';
if (terminal)
replacement.erase(0,1);
rules.push_back(rule(pattern, replacement, terminal));
}
return rules;
std::string text = argv[2];▼
}
std::string markov(std::vector<rule> rules, std::string input)
{
std::string& output = input;
std::vector<rule>::iterator iter = rules.begin();
// Loop through each rule, transforming our current version
// with each rule.
while (iter != rules.end())
{
std::string::size_type pos =
if (pos != npos)
{
if (iter->terminal)
break;
Line 358 ⟶ 359:
}
return output;
std::cout << text << "\n";▼
}
▲int main(int argc, char* argv[])
{
▲ if (argc != 3)
▲ {
▲ std::cout << "usage:\n " << argv[0] << " rulefile text\n";
▲ return EXIT_FAILURE;
▲ }
▲ std::ifstream rulefile(argv[1]);
std::vector<rule> rules = read_rules(rulefile);
std::string output = markov(rules, input);
}
</lang>
|