trinary: initial commit

This commit is contained in:
Dmitry Kokorin 2016-04-07 15:39:30 +03:00
parent 0f1fb185f9
commit 32aef4e595
2 changed files with 46 additions and 0 deletions

35
cpp/trinary/trinary.cpp Normal file
View file

@ -0,0 +1,35 @@
#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;
}
}

11
cpp/trinary/trinary.h Normal file
View file

@ -0,0 +1,11 @@
#pragma once
#define EXERCISM_RUN_ALL_TESTS
#include <string>
namespace trinary {
int to_decimal(const std::string &input);
}