Solved

Roman Numeral to numeric and visa versa

Posted on 2010-09-06
3
666 Views
Last Modified: 2012-05-10

I need a function that takes a string as parameter and does one of two things:

1.) converts it to Roman Numeral if its a numeric

or

2.) converts it to numeric if its Roman a Numeral


Example:   function convertValue(Value: String): String;


Note:  if a user types anything other than 0..9, or  I,V,X,L,C,D,M
0
Comment
Question by:Looking_4_Answers
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Mahdi78
ID: 33614753
0
 
LVL 9

Accepted Solution

by:
Mahdi78 earned 500 total points
ID: 33614784
This what you want, drop 2 TEdit and TButton


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function RomanToArabic(const romanNumber : string) : integer ;
 const
   romanChars = 'IVXLCDMvxlcdm?!#' ;
   decades : array [0..8] of integer = (0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000) ;
   OneFive : array [boolean] of byte = (1, 5) ;
 var
   newValue, oldValue : integer ;
   cIdx, P : byte ;
 begin
   result := 0;
   oldValue := 0 ;
   for cIdx := Length(romanNumber) downto 1 do
   begin
     P := Succ(Pos(romanNumber[cIdx], romanChars)) ;
     newValue := OneFive[Odd(P)] * decades[P div 2] ;
     if newValue = 0 then
     begin
       result := -1;
       Exit;
     end ;
     if newValue < oldValue then newValue := - newValue ;
     Inc(result, newValue) ;
     oldValue := newValue
   end ;
 end;

function ArabicToRoman(Decimal: Longint): string;
const
  Numbers: array[1..13] of Integer =
    (1, 4, 5, 9, 10, 40, 50, 90, 100,
    400, 500, 900, 1000);
  Romans: array[1..13] of string =
    ('I', 'IV', 'V', 'IX', 'X', 'XL',
    'L', 'XC', 'C', 'CD', 'D', 'CM', 'M');
var
  i: Integer;
begin
  Result := '';
  for i := 13 downto 1 do
    while (Decimal >= Numbers[i]) do
    begin
      Decimal := Decimal - Numbers[i];
      Result  := Result + Romans[i];
    end;
end;

function convertValue(Value: String): String;
  function IsStrANumber(const S: string): Boolean;
  var
    P: PChar;
  begin
    P      := PChar(S);
    Result := False;
    while P^ <> #0 do
    begin
      if not (P^ in ['0'..'9']) then Exit;
      Inc(P);
    end;
    Result := True;
  end;
begin
if IsStrANumber(Value) then Result := ArabicToRoman(StrToInt(Value))
else Result := IntToStr(RomanToArabic(Value));

end;


procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text := convertValue(Edit1.Text);
end;

end.

Open in new window

0
 

Author Closing Comment

by:Looking_4_Answers
ID: 33666334
That will doit!  Thanks
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
proper way to parse text with delphi 7 122
Delphi XE10 Round Image 2 129
Delphi and Access based Enumeration 9 64
Delphi: sending SMS on android platform 1 21
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question