More Roman Number Conversions

Last time I looked at updating my original post I got a little side tracked and ended up rewriting the original code rather than adding a routine to convert an integer to roman numerals.

Adding the following code, which I originally wrote on 22 Nov 84, to the program will define an operator to perform the conversion of integers to properly formed strings of roman numerals.

  { Convert a decimal value into a properly formed string of roman
    numerals - 22 Nov 84 - MEJT }

  OP ROMAN = (INT value) STRING: (
    INT number := value; STRING numeral := "";
    WHILE (number > 0) DO (
      IF number >= 1000 THEN
        ( number -:= 1000; numeral +:= "M")
      ELIF number >= 900 THEN
        ( number -:= 900; numeral +:= "CM")
      ELIF number >= 500 THEN
        ( number -:= 500; numeral +:= "D")
      ELIF number >= 400 THEN
        ( number -:= 400; numeral +:= "CD")
      ELIF number >= 100 THEN
        ( number -:= 100; numeral +:= "C")
      ELIF number >= 90 THEN
        ( number -:= 90; numeral +:= "XC")
      ELIF number >= 50 THEN
        ( number -:= 50; numeral +:= "L")
      ELIF number >= 40 THEN
        ( number -:= 40; numeral +:= "XL")
      ELIF number >= 10 THEN
        ( number -:= 10; numeral +:= "X")
      ELIF number >= 9 THEN
        ( number -:= 9; numeral +:= "IX")
      ELIF number >= 5 THEN
        ( number -:= 5; numeral +:= "V")
      ELIF number >= 4 THEN
        ( number -:= 4; numeral +:= "IV")
      ELSE
        ( number -:= 1; numeral +:= "I")
      FI )
    OD;
    numeral );

  PRIO ROMAN = 9;

I then updated the procedure I wrote to check the results to include a test to ensure that the roman number being checked was not illegal (although the program was able to find the correct integer values some of my test cases were strings of improper roman numerals).

  { Prints result of decimal conversion and then converts the result back
    into roman numerals to check conversion and test to see if original
    roman numeral was properly formed - 22 Jan 16 - MEJT }

  PROC check = (STRING roman, INT value) VOID: (
    write ((roman," "*(12-UPB(roman)),"= ", fixed(DECIMAL(roman),-12,0),
    ((DECIMAL(ROMAN(value)) = value AND DECIMAL(roman) = value |
      ((roman = ROMAN(value) |
        " - Ok" | " - Invalid (should be " + ROMAN(value) + ")"))
    | " - Error")),
    newline)) );

Then I re-ordered the test cases and added a few more.

  { Test cases to test conversion routines including several examples
    of badly formed roman numerals - 22 Jan 16 - MEJT }

  check ("MMXII", 2012);
  check ("MCMXCIX", 1999);
  check ("MCMX", 1910);
  check ("MCMLXIII", 1963);
  check ("MDCLXIV", 1664);
  check ("XCIX", 99);
  check ("LXXXIII", 83);
  check ("LXXVIII", 78);
  check ("VII", 7);
  check ("II", 2);
  check ("I", 1);
  check ("", 0);
  write (newline);
  check ("MIM", 1999); {Invlaid}
  check ("MDCCCCX", 1910); {Invlaid}
  check ("LXXXXVIIII", 99); {Invlaid}
  check ("XXCIII", 83); {Invlaid}
  check ("LXXIIX", 78); {Invlaid}
  check ("IIIV", 2) ) {Invlaid}

The program now produces the following output.

MMXII       =         2012 - Ok
MCMXCIX     =         1999 - Ok
MCMX        =         1910 - Ok
MCMLXIII    =         1963 - Ok
MDCLXIV     =         1664 - Ok
XCIX        =           99 - Ok
LXXXIII     =           83 - Ok
LXXVIII     =           78 - Ok
VII         =            7 - Ok
II          =            2 - Ok
I           =            1 - Ok
            =            0 - Ok

MIM         =         1999 - Invalid (should be MCMXCIX)
MDCCCCX     =         1910 - Invalid (should be MCMX)
LXXXXVIIII  =           99 - Invalid (should be XCIX)
XXCIII      =           83 - Invalid (should be LXXXIII)
LXXIIX      =           78 - Invalid (should be LXXVIII)
IIIV        =            2 - Invalid (should be II)

Advertisements
This entry was posted in Programming and tagged . Bookmark the permalink.

One Response to More Roman Number Conversions

  1. nevillednz says:

    I completely understand your fascination with Algol68.

    The languages designer were not (too much) inflicted with by “favourite features” of the “100 notables” and they also had a tool kit of ideas grown out of the “Cambrian Period” of Computer hardware/instruction design.

    Here are some examples of this task in various languages (Inc Algol68) at Rosettacode…. * https://www.rosettacode.org/wiki/Roman_numerals/Encode#ALGOL_68

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s