Solved

Roman Numeral Converter

Posted on 1998-04-14
13
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

628 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