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
|
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <string>
#include <vector>
void loop(const std::vector<std::string>& args)
{
//I am to lazy to use proper exception classes right now
if (args.size() != 3) throw "invalid arguments number";
int begin = std::stoi(args[0]);
int end = std::stoi(args[1]);
int increment = std::stoi(args[2]);
if(begin > end || !(increment > 0) ) throw "invalid argument";
for(int i = begin; i < end; i += increment)
std::cout << i << ' ';
std::cout << '\n';
}
void add(const std::vector<std::string>& args)
{
if (args.size() != 2) throw "invalid arguments number";
int lhs = std::stoi(args[0]);
int rhs = std::stoi(args[1]);
std::cout << (lhs + rhs) << '\n';
}
void echo(const std::vector<std::string>& args)
{
for(const auto& s: args)
std::cout << s << '\n';
}
std::vector<std::string> tokenize(std::string in)
{
std::istringstream s(in);
return {std::istream_iterator<std::string>(s), std::istream_iterator<std::string>()};
}
int main()
{
std::map<std::string, std::function<void(const std::vector<std::string>&)>> func = {
{"loop", loop}, {"add", add}, {"echo", echo},
{"exit", [](const std::vector<std::string>&){std::exit(0);} },
};
std::string input;
while(std::getline(std::cin, input)){
auto tokens = tokenize(input);
func.at(tokens[0])({tokens.begin() + 1, tokens.end()});
}
}
|
echo hello, world
hello,
world
loop 0 10 2
0 2 4 6 8
add 1 1
2
exit
Process returned 0 (0x0) execution time : 38.020 s
Press any key to continue. |