6 std::string int2roman(
size_t decimal,
size_t length = 30 )
8 assert( decimal > 0 && decimal < 1000 );
10 const size_t nrows = 4;
11 const size_t ncols = 4;
13 static size_t table_arabs[ nrows ][ ncols ] = { { 1000, 1000, 1000, 1000 },
14 { 900, 500, 400, 100 },
18 static const char *table_romans[ nrows ][ ncols ] = { {
"M",
"M",
"M",
"M" },
19 {
"CM",
"D",
"CD",
"C" },
20 {
"XC",
"L",
"XL",
"X" },
21 {
"IX",
"V",
"IV",
"I" } };
26 std::string roman =
"";
27 roman.reserve(length);
29 for ( power = 0; power < nrows; power++ )
30 for ( index = 0; index < ncols; index++ )
31 while ( decimal >= table_arabs[ power ][ index ] )
33 roman += table_romans[ power ][ index ];
34 decimal -= table_arabs[ power ][ index ];