size : 1745
uploaded_on : Tue May 18 00:00:00 1999
modified_on : Wed Dec 8 14:03:40 1999
title : Roman numerals to Decimal
org_filename : RomanNumbers.txt
author : hambar
authoremail : hambar@my-dejanews.com
description : Roman numbers conversion
keywords :
tested : not tested yet
submitted_by : The CKB Crew
submitted_by_email : ckb@netalive.org
uploaded_by : nobody
modified_by : nobody
owner : nobody
lang : pas
file-type : text/plain
category : pascal-alg-maths
__END_OF_HEADER__
function RomanToNum (const S: String): longint;
const SymbolStr = 'MDCLXVI';
var Loop: integer;
Number, ThisVal, LastVal: LongInt;
Txt: String;
Lookup : array [0..7] of LongInt;
begin
Lookup[0] := 0;
Lookup[1] := 1000;
Lookup[2] := 500;
Lookup[3] := 100;
Lookup[4] := 50;
Lookup[5] := 10;
Lookup[6] := 5;
Lookup[7] := 1;
LastVal := 0;
Result := 0;
for Loop := 1 to length (S) do
begin
ThisVal := Lookup[Pos (S[Loop], SymbolStr)];
If ThisVal = 0 then
begin
ShowMessage ('Invalid Roman Numeral symbol: ' + S[Loop]);
Exit;
end;
if ThisVal > LastVal then
LastVal := ThisVal - LastVal
else
begin
Result := Result + LastVal;
LastVal := ThisVal;
end;
end;
Result := Result + LastVal;
end;
function NumToRoman (Num: longint): string;
function BuildRoman (var N:longint; Val: longint; const Symbol:
String): String;
var Loop: integer;
begin
Result := '';
N := N div Val;
for loop := 1 to N do
Result := Result + Symbol;
N := N mod Val;
end;
begin
Result := BuildRoman (Num, 1000, 'M');
Result := Result + BuildRoman (Num, 900, 'CM');
Result := Result + BuildRoman (Num, 500, 'D');
Result := Result + BuildRoman (Num, 400, 'CD');
Result := Result + BuildRoman (Num, 100, 'C');
Result := Result + BuildRoman (Num, 90, 'XC');
Result := Result + BuildRoman (Num, 50, 'L');
Result := Result + BuildRoman (Num, 40, 'XL');
Result := Result + BuildRoman (Num, 10, 'X');
Result := Result + BuildRoman (Num, 9, 'IX');
Result := Result + BuildRoman (Num, 5, 'V');
Result := Result + BuildRoman (Num, 4, 'IV');
Result := Result + BuildRoman (Num, 1, 'I');
end;