: : :
This message was edited by zibadian at 2005-4-17 1:7:16
: : : : i want to know the part from roman calculator's transfer.
: : : : how can i transfer roman number to decimal number
: : : :
: : : Here is two working, partial functions:
: : :
: : : const
: : : cRomanChars = 'IVXLCDM';
: : : cRomanValues: array[1..7] of integer = (1, 5, 10, 50, 100, 500, 1000);
: : :
: : : function RomanToDecimal(s: string): integer;
: : : var
: : : i: integer;
: : : begin
: : : Result := 0;
: : : if Length(s) = 0 then Exit; { No number given, exit the function }
: : : for i := 1 to Length(s) do
: : : if Pos(s[i], cRomanChars) = 0 then { Invalid character found }
: : : begin
: : : Result := -1; { Give error result }
: : : Exit; { Exit the function }
: : : end else
: : : Result := Result + cRomanValues[Pos(s[i], cRomanChars)];
: : : end;
: : :
: : : function DecimalToRoman(Number: integer): string;
: : : var
: : : i, j: integer;
: : : s: string;
: : : begin
: : : i := 7;
: : : Result := '';
: : : while (Number > 0) and (i > 0) do
: : : begin
: : : while Number - cRomanValues[i] >= 0 do begin
: : : Result := Result + cRomanChars[i]; { Add roman digit to result }
: : : Number := Number - cRomanValues[i]; { Remove the value }
: : : end;
: : : dec(i);
: : : end;
: : : i := 6;
: : : repeat
: : : s := '';
: : : for j := 1 to 4 do { create a string with 4 the same roman digits }
: : : s := s + cRomanChars[i];
: : : j := Pos(s, Result);
: : : while j > 0 do
: : : begin
: : : Delete(Result, j, 4); { change 4 roman digits into small-large combination}
: : : Insert(cRomanChars[i]+cRomanChars[i+1], Result, j); {ie: IIII to IV }
: : : j := Pos(s, Result);
: : : end;
: : : dec(i);
: : : until i = 0;
: : : end;
: : :
: : : This is only a partial function, because it will convert IX as 11. A possible solution to this is to replace values like IX into the alternate notation VIIII, before performing the actual conversion.
: : : The change back to decimal still needs some work, because it will generate VIV for 9, LXL for 90, etc. You need to change those into a the correct short notation to be complete. That change is very similar to changing the IIII to IV.
: : : I know this is only part of the answer to the question, but at least it is a start.
: : :
: : thank you very much,i will try myself then,but if i have any problem
: : can i ask u again?
: :
: Sure, but I want to see that you have done some coding on it.
:
that need to wait until i finish the program