hi, ich habe eben einen Algorithmus zum Umwandeln von Römischen Zahlen in Dezimalzahlen geschrieben, was kann ich da noch dran optimieren ?
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:
| function RomanToDec(iRoman: string): integer; const aRomans: array [1..7] of string = ('I', 'V', 'X', 'L', 'C', 'D', 'M'); aArabics: array [1..7] of integer = (1, 5, 10, 50, 100, 500, 1000); var i, j, k, l: integer; addition: boolean; begin if length(iRoman) = 1 then for i := 7 downto 1 do if aRomans[i] = iRoman[1] then begin result := aArabics[i]; exit; end; result := 0; i := length(iRoman); while i > 1 do begin for j := 1 to 7 do if (aRomans[j] = iRoman[i]) then begin addition := false; for k := 1 to 7 do if (aRomans[k] = iRoman[i-1]) then if (aArabics[k] < aArabics[j]) then begin result := result + (aArabics[j] - aArabics[k]); addition := true; dec(i); end; if addition = false then result := result + aArabics[j]; if (i = 2) then for l := 7 downto 1 do if aRomans[l] = iRoman[1] then result := result + aArabics[l]; end; dec(i); end; end; |
mfg