exercism-solutions/cpp/roman-numerals/roman_numerals.cpp

46 lines
728 B
C++

#include "roman_numerals.h"
namespace roman {
using namespace std;
string convert(int number)
{
struct digit_t
{
int value;
string presentation;
};
static const digit_t digits[] = {
{1000, "M" },
{900, "CM"},
{500, "D" },
{400, "CD"},
{100, "C" },
{90, "XC"},
{50, "L" },
{40, "XL"},
{10, "X" },
{9, "IX"},
{5, "V" },
{4, "IV"},
{1, "I" }
};
string result;
for (auto &digit : digits) {
while (number >= digit.value) {
result += digit.presentation;
number -= digit.value;
}
}
return result;
}
}