Weinig credit voor mij deze keer. Ik zocht een manier om een getal om te zetten naar romeinse cijfers. Ik vond een functie om dit te doen. Deze was geschreven in C en heb ik vrijwel letterlijk vertaald naar Delphi. Het lijkt allemaal wel te werken, maar ik weet niet hoe het gaat met ongeldige invoer.
De basis is er in ieder geval:Code:// Delphi vertaling van de functies op // http://www.nio.ntnu.no/archive/2001_2002/2/4l.php3 function RomToInt(Rom: string): Integer; // Vertaalt Romeinse notatie naar Integer; function Value(rd: Char): Integer; begin case rd of 'M': Result := 1000; 'D': Result := 500; 'C': Result := 100; 'L': Result := 50; 'X': Result := 10; 'V': Result := 5; 'I': Result := 1; else Result := 0; end; end; var i, d, lastd, val: Integer; begin val := 0; lastd := 0; for i := 1 to Length(Rom) + 1do begin d := Value(Rom[i]); if lastd < d then Dec(val, lastd) else Inc(val, lastd); lastd := d; end; Result := val; end; function IntToRom(i: Integer): string; // Vertaalt Integer naar Romeinse notatie; const M: array[0..3] of Char = ('I', 'X', 'C', 'M'); D: array[0..3] of Char = ('V', 'L', 'D', '-'); var deg, coef: Integer; begin coef := 100; for deg := 3 downto 1 do begin while i >= 10 * coef do begin Result := Result + M[deg]; Dec(i, 10 * coef); end; if i >= 9 * coef then begin Result := Result + M[deg - 1]; Result := Result + M[deg]; Dec(i, 9 * coef); end; if i >= 5 * coef then begin Result := Result + D[deg - 1]; Dec(i, 5 * coef); end; if i >= 4 * coef then begin Result := Result + M[deg - 1]; Result := Result + D[deg - 1]; Dec(i, 4 * coef); end; coef := coef div 10; end; Result := Result + StringOfChar('I', i); end;
Bookmarks