exercism-solutions/cpp/trinary/trinary.cpp

35 lines
427 B
C++

#include "trinary.h"
namespace trinary {
namespace {
inline bool is_digit(char ch)
{
return ch >= '0' && ch <= '2';
}
inline int digit_to_decimal(char ch)
{
return ch - '0';
}
}
int to_decimal(const std::string &input)
{
int result = 0;
for (auto ch : input) {
if (!is_digit(ch))
return 0;
result *= 3;
result += digit_to_decimal(ch);
}
return result;
}
}