Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Roman Numeral Converter

Posted on 1998-04-14
13
Medium Priority
?
335 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
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

927 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