Initial commit

This commit is contained in:
2026-04-01 22:17:05 +03:00
parent 4604e3afa1
commit dd7178167d
10 changed files with 293 additions and 0 deletions
+83
View File
@@ -0,0 +1,83 @@
#include "input_resolver.h"
#include <string>
#include <iostream>
#define delimiter " "
struct ValidationResult
{
bool ok()
{
return isItOk;
}
char incorrect_token()
{
return in_t;
}
int next_arg()
{
return nextArg;
}
bool isItOk;
char in_t;
int nextArg;
};
ValidationResult validate_token(const std::string &token)
{
static const std::string allowedTokens{"+-*/%$qp"};
char incorrectToken;
size_t digitpos = -1, counter = 0;
for (const char c: token)
{
if (allowedTokens.find(c) == allowedTokens.npos && !isdigit(c) && !isspace(c))
return {false, c};
if (digitpos == -1 && isdigit(c))
digitpos = counter;
counter++;
}
if (digitpos == -1)
return {true, ' ', 0};
int next = stoi(token.substr(digitpos));
return {true, ' ', next};
}
Command resolveCommand()
{
std::string st;
getline(std::cin, st);
std::string token = st.substr(0, st.find(delimiter));
auto validation_result = validate_token(token);
if (!validation_result.ok())
{
throw std::runtime_error{"invalid token " + validation_result.incorrect_token()};
}
bool shouldQuit = token.find("q") != token.npos;
bool shouldPrint = token.find("p") != token.npos;
char operation;
for (const char c: token)
{
static const std::string operators {"+-*/%"};
if(operators.find(c) != operators.npos)
{
operation = c;
}
}
return {shouldQuit, operation, validation_result.next_arg(), shouldPrint};
}