Solved

Roman Numeral Converter

Posted on 1998-04-14
13
287 Views
Last Modified: 2010-04-16
I would like to make a simple program for absoluteley no use what-so-ever, expect for my own amusement
that would ask the user to enter string.  If user inputs "25" the output would be XXV, if the user inputed XXV, the
output would be 25.  I dont want a menu, I want it to automatically to check for which way to convert.  Thanks.

I think this is simple, but don't know how exactly to do it.  thanks/..
0
Comment
Question by:weinrj
13 Comments
 
LVL 2

Expert Comment

by:kellyjj
Comment Utility
Are you looking for psuedo code? or an actual program??  
0
 

Author Comment

by:weinrj
Comment Utility
actual program.
0
 
LVL 6

Expert Comment

by:tecbuilder
Comment Utility
If you are wanting someone to write the code for you, you need to up the points quite a bit.  It still takes some one time to do the work.  My suggestion is to offer around 150 points.  For 25 points all you'll get is how you need to go about doing it.
0
 
LVL 3

Expert Comment

by:jlove1
Comment Utility
Are you asking for the COMPLETE source code to such a program, or just a description on how to accomplish the task (with code fragments)? If you're wanting Complete source, you will have to add some more points to this.. try starting at 200... I'll do it myself for 400. but you may find someone desprate for points that'll do it for 200.
0
 
LVL 6

Expert Comment

by:tecbuilder
Comment Utility
jlove1,  400 points?!  For the code for this question?  I can see you really want to move up on the top 15 list fast.  It seems to me that you are the one desperate, for points.  The code for this quesiton is not difficult at all and doesn't warrant anything more than 200.  Calling anyone who answers the question desperate I believe is a bit extreme.  I have not seen too many experts here that are desperate to answer questions.
0
 

Author Comment

by:weinrj
Comment Utility
ok
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 6

Expert Comment

by:tecbuilder
Comment Utility
If you will post your email address I will send you the program.
0
 

Author Comment

by:weinrj
Comment Utility
I will give you the points, the letter grade, etc., once i get the program via email.  
If it is acceptable, I will grade it, and raise you 200 pts.  Once you email it, post a new message as an answer stating you sent it.  based on that, i will then be able to post the points.

please send email to me at jwnrb@cybernex.net
0
 
LVL 6

Accepted Solution

by:
tecbuilder earned 200 total points
Comment Utility
I have just sent you the executable.  After you grade my answer I will send you the source code if you want it.  This was one of the those programs that was just fun to do.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Do you handle such input as
VIIIII
or
IC
?

0
 
LVL 6

Expert Comment

by:tecbuilder
Comment Utility
Yes, the program will evaluate IC as 99 and VIIIII as 10.  If the program is accepted I will post the code here.
0
 

Author Comment

by:weinrj
Comment Utility
thank you...it works perfectly...please email me the source code.

0
 
LVL 6

Expert Comment

by:tecbuilder
Comment Utility
program RomanNumeral;
uses Crt;

type str2 = string[2];

const
  valRN: array[1..13] of word = (1,4,5,9,10,40,50,90,100,400,500,900,1000);
  textRN: array[1..13] of str2 = ('I','IV','V','IX','X','XL','L','XC','C','CD','D','CM','M');

var
  ans: string;
  isOk: boolean;
  d, m: word;

procedure convertInteger(var T: string);
var
  N,                       { value that is being worked on }
  numRN, code: integer;
  i,j: byte;
  outRN: string;           { keeps track of letters }

begin
  Val(T, N, code);   { convert string value to number value }

  outRN := '';       { initialize }

  repeat
    i := 14;
    { Get the value in array that is just less than current value.}
    repeat
      dec(i);
    until valRN[i] <= N;

    { Get how many times the letter needs to be written. }
    numRN := N div valRN[i];

    { Write the letter. }
    for j := 1 to numRN do outRN := outRN + textRN[i];

    { The remainder is what needs to be worked on. }
    N := N mod valRN[i];
   until N = 0;

   T := 'The roman numeral for '+T+' is '+outRN;
end;

procedure convertRoman(var T: string);
const
  tmp1: array[1..7] of word = (1,5,10,50,100,500,1000);
  tmp2: array[1..7] of str2 = ('I','V','X','L','C','D','M');
var
  i,j: byte;
  ttl: word;                      { total value }
  RN: array[1..100] of word;      { seperates letters for analysis }
  tmp: string;
begin
  for i := 1 to 40 do RN[i] := 0;    { Initialize }

  ttl := 0;
  for i := 1 to length(T) do begin
    j := 8;

    { Get position of letter in the array. }
    repeat          
      dec(j);
    until tmp2[j] = T[i];

    { Assign value of letter. }
    RN[i] := tmp1[j];
  end;

  for i := 1 to length(T) do begin
    if i > 1 then begin

      { Add value of letter to the total value. }
      ttl := ttl + RN[i];

      { Need to re-evaluate number because number before current number
        is less than the current number.  For example:  IX = 10-1 = 9}
      if RN[i-1] < RN[i] then begin
        ttl := ttl - (RN[i-1]+RN[i]) ;
        ttl := ttl + (RN[i] - RN[i-1]);
      end;
    end
    else ttl := RN[i];
  end;

  { Convert number to string value. }
  Str(ttl, tmp);
  T := 'The numeric value for '+T+' is '+ tmp;
end;

var
  i: byte;
  done: boolean;
label alldone;
begin
  ClrScr; done := false;

  Writeln('Enter a zero (0) when done.'); Writeln;
  repeat
    Write('Enter a roman numeral or an integer --> ');
    Readln(ans);
    if ans = '0' then begin done := true; goto alldone; end;
    for i := 1 to length(ans) do ans[i] := Upcase(ans[i]);
    i := 1;

    { Validate user input. }
    repeat
                        { 0 - 9, I, V, X, C, D, L, M}
      if ord(ans[i]) in [48..57,73,86,88,67,68,76,77]
      then isOk := true
      else isOk := false;
      inc(i);
    until (i > length(ans)) or not isOk;

    if isOk then begin     { value entered is an integer or roman numeral }
      if ord(ans[1]) in [48..57]  { a numeric value was entered }
      then convertInteger(ans)
      else convertRoman(ans);
    end
    else ans := 'An invalid character was entered.';

    Writeln(ans);
    ReadKey;
    Writeln;

alldone:
  until done;
end.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

HOW TO: Upload an ISO image to a VMware datastore for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere Host Client, and checking its MD5 checksum signature is correct.  It's a good idea to compare checksums, because many installat…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now