[fpc-pascal] Re: StrUtils.RomanToInt oddities
Bart
bartjunk64 at gmail.com
Mon Sep 23 18:18:21 CEST 2013
On 9/23/13, Lukasz Sokol <el.es.cr at gmail.com> wrote:
> function TryRomanToInt(AInput:String; out AResult: integer):boolean;
> var i, Len, N, Np1 : integer;
[snip]
>
>
> if N >= Np1 then AResult := AResult + N
> else AResult := AResult - N;
>
> end;
> else // i = Len-1 = last char we just add (consider : MCM : add 1000,
Compiler doesn't like the else here. I removed the ; after the end 1 line above.
> sub 100, add 1000 = 1900)
> begin
> AResult := AResult + N;
> end;
> end; // for
> // if the above, after all characters are through, has resulted in 0 or
> less,
> // we invalidate everything at the end (consider : CMLM, IIIM )
>
> Result := AResult > 0;
> if not Result then AResult := 0;
> end;
>
> (only mind-compiled ;) tests welcome ;) )
>
It produces "odd" output though:
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
Enter Roman numeral: LL //should be illegal
TryRomanToInt('LL') -> 50
StrUtils.RomanToInt('LL') = 100
Enter Roman numeral: MM
TryRomanToInt('MM') -> 1050 //obviously should be 2000
StrUtils.RomanToInt('MM') = 2000
Enter Roman numeral: II
TryRomanToInt('II') -> 1051 // nice one!
StrUtils.RomanToInt('II') = 2
So, it's a little bit flawed.
Anyhow, let's not focus upon what the new code should be.
The question was: is current behaviour (accepting IIMIIC etc.) a bug or not.
Bart
More information about the fpc-pascal
mailing list