## Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only

any one can really help me?the roman calculator?please Posted by cainhk on 16 Apr 2005 at 8:46 PM
i want to know the part from roman calculator's transfer.
how can i transfer roman number to decimal number
Re: any one can really help me?the roman calculator?please Posted by zibadian on 17 Apr 2005 at 12:22 AM
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.
Re: any one can really help me?the roman calculator?please Posted by cainhk on 17 Apr 2005 at 1:12 AM
: 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?
Re: any one can really help me?the roman calculator?please Posted by zibadian on 17 Apr 2005 at 1:30 AM
: : 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.
Re: any one can really help me?the roman calculator?please Posted by cainhk on 17 Apr 2005 at 1:34 AM
: : : 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

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.