-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtinyroman.pas
50 lines (41 loc) · 1.04 KB
/
dtinyroman.pas
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
43
44
45
46
47
48
49
50
unit dtinyroman;
// Tiny integer to roman numerals converter (roughly tested).
// - rlyeh, public domain | wtrmrkrlyeh
// - pascal port by Doj
{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}
interface
function Romanize(i: PtrInt): AnsiString;
implementation
const
table: array[0 .. 31 - 1] of AnsiString = (
'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX',
'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC',
'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM',
'M', 'MM', 'MMM', 'MMMM'
);
function Romanize(i: PtrInt): AnsiString;
var
base, _mod: PtrInt;
begin
Result := '';
base := 0;
while i > 0 do begin
_mod := i mod 10;
if _mod > 0 then
Result := table[(_mod - 1) + base * 9] + Result;
Inc(base);
i := i div 10;
end;
end;
// begin
// Assert(Romanize(0) = '' );
// Assert(Romanize(10) = 'X' );
// Assert(Romanize(1990) = 'MCMXC' );
// Assert(Romanize(2008) = 'MMVIII' );
// Assert(Romanize(99) = 'XCIX' );
// Assert(Romanize(47) = 'XLVII' );
// end.
end.