File: int2roman.cc

package info (click to toggle)
openmesh 11.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,080 kB
  • sloc: cpp: 56,379; ansic: 5,600; perl: 1,374; sh: 119; makefile: 18
file content (42 lines) | stat: -rw-r--r-- 1,335 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <OpenMesh/Core/System/config.hh>
#if defined(OM_CC_MIPS)
#  include <assert.h>
#else
#  include <cassert>
#endif
#include "int2roman.hh"


std::string int2roman( size_t decimal, size_t length )
{
  assert( decimal > 0 && decimal < 1000 );

  const size_t nrows = 4;
  const size_t ncols = 4;

  static size_t table_arabs[ nrows ][ ncols ] = { { 1000, 1000, 1000, 1000 },
                                                  {  900,  500,  400,  100 },
                                                  {   90,   50,   40,   10 },
                                                  {    9,    5,    4,    1 } };

  static char *table_romans[ nrows ][ ncols ] = { {  "M",  "M",  "M", "M" },
                                                  { "CM",  "D", "CD", "C" },
                                                  { "XC",  "L", "XL", "X" },
                                                  { "IX",  "V", "IV", "I" } };

  size_t power;	// power of ten
  size_t index;  // Indexes thru values to subtract
  
  std::string roman = "";
  roman.reserve(length);

  for ( power = 0; power < nrows; power++ )
    for ( index = 0; index < ncols; index++ )
	while ( decimal >= table_arabs[ power ][ index ] ) 
        {
	   roman   += table_romans[ power ][ index ];
	   decimal -= table_arabs[ power ][ index ];
	}

  return roman;
}