Einzelnen Beitrag anzeigen

Daniel B
(Gast)

n/a Beiträge
 
#3

Re: Dezimalzahlen in Römische Zahlen umwandeln und umgekehrt

  Alt 12. Jul 2003, 20:50
Hallo,

hier ist noch ne weitere Lösung.
Delphi-Quellcode:
function RomanToDec2(Roman: String): LongInt;
const
  Romans: Array[1..18] of String = ('I', 'V', 'X', 'L', 'C', 'D', 'M', 'IV', 'IX', 'XL',
                                    'XC', 'IC', 'CD', 'CM', 'IM', '(M)', '[M]', '{M}');
  Arabics: Array[1..18] of Integer = (1, 5, 10, 50, 100, 500, 1000, 4, 9, 40,
                                      90, 99, 400, 900, 999, 10000, 100000, 1000000);
var
  iFor, iPos: Integer;
begin
  Result := 0;
  repeat
    for iFor := High(Romans) downto Low(Romans) do
    begin
      repeat
        iPos := Pos(Romans[iFor], Roman);
        if iPos > 0 then
        begin
          Inc(Result, Arabics[iFor]);
          Delete(Roman, iPos, Length(Romans[iFor]));
        end;
      until iPos = 0;
    end;
  until Length(Roman) = 0;
end;
Grüsse, Daniel
  Mit Zitat antworten Zitat