Developer Documentation
int2roman.cc
1 #include <OpenMesh/Core/System/config.hh>
2 #if defined(OM_CC_MIPS)
3 # include <assert.h>
4 #else
5 # include <cassert>
6 #endif
7 #include "int2roman.hh"
8 
9 
10 std::string int2roman( size_t decimal, size_t length )
11 {
12  assert( decimal > 0 && decimal < 1000 );
13 
14  const size_t nrows = 4;
15  const size_t ncols = 4;
16 
17  static size_t table_arabs[ nrows ][ ncols ] = { { 1000, 1000, 1000, 1000 },
18  { 900, 500, 400, 100 },
19  { 90, 50, 40, 10 },
20  { 9, 5, 4, 1 } };
21 
22  static char *table_romans[ nrows ][ ncols ] = { { "M", "M", "M", "M" },
23  { "CM", "D", "CD", "C" },
24  { "XC", "L", "XL", "X" },
25  { "IX", "V", "IV", "I" } };
26 
27  size_t power; // power of ten
28  size_t index; // Indexes thru values to subtract
29 
30  std::string roman;
31 
32  roman.reserve(length);
33 
34  roman[ 0 ] = '\0';
35 
36  for ( power = 0; power < nrows; power++ )
37  for ( index = 0; index < ncols; index++ )
38  while ( decimal >= table_arabs[ power ][ index ] )
39  {
40  roman += table_romans[ power ][ index ];
41  decimal -= table_arabs[ power ][ index ];
42  }
43 
44  return roman;
45 }