Solved

Roman Numeral Converter

Posted on 1998-04-14
13
293 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
ID: 1217468
Are you looking for psuedo code? or an actual program??  
0
 

Author Comment

by:weinrj
ID: 1217469
actual program.
0
 
LVL 6

Expert Comment

by:tecbuilder
ID: 1217470
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
ID: 1217471
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
ID: 1217472
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
ID: 1217473
ok
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

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

Author Comment

by:weinrj
ID: 1217475
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
ID: 1217476
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
ID: 1217477
Do you handle such input as
VIIIII
or
IC
?

0
 
LVL 6

Expert Comment

by:tecbuilder
ID: 1217478
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
ID: 1217479
thank you...it works perfectly...please email me the source code.

0
 
LVL 6

Expert Comment

by:tecbuilder
ID: 1217480
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi TDBX too many connection problem 2 1,152
Convert date 6 400
Can C++Builder Form Application wait until termination on command prompt ? 6 355
Mydac connection data base issue 3 140
In the hope of saving someone else's sanity... About a year ago we bought a Cisco 1921 router with two ADSL/VDSL EHWIC cards to load balance local network traffic over the two broadband lines we have, but we couldn't get the routing to work consi…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

22 Experts available now in Live!

Get 1:1 Help Now