OpenMesh
int2roman.hh
1 #ifndef INT2ROMAN_HH
2 #define INT2ROMAN_HH
3 
4 #include <string>
5 
6 std::string int2roman( size_t decimal, size_t length = 30 )
7 {
8  assert( decimal > 0 && decimal < 1000 );
9 
10  const size_t nrows = 4;
11  const size_t ncols = 4;
12 
13  static size_t table_arabs[ nrows ][ ncols ] = { { 1000, 1000, 1000, 1000 },
14  { 900, 500, 400, 100 },
15  { 90, 50, 40, 10 },
16  { 9, 5, 4, 1 } };
17 
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" } };
22 
23  size_t power; // power of ten
24  size_t index; // Indexes thru values to subtract
25 
26  std::string roman = "";
27  roman.reserve(length);
28 
29  for ( power = 0; power < nrows; power++ )
30  for ( index = 0; index < ncols; index++ )
31  while ( decimal >= table_arabs[ power ][ index ] )
32  {
33  roman += table_romans[ power ][ index ];
34  decimal -= table_arabs[ power ][ index ];
35  }
36 
37  return roman;
38 }
39 
40 #endif

Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .