Yet more Roman Number Conversions

Yes – I know this is another obscure ALGOL68 post, but I just can’t help liking this archaic language for all it’s strangeness, and it can get pretty strange!

The routine below does exactly the same job as the previous version and it looks like it was part of a series of course work exercises as it was originally written the day before. Again the purpose of the code is to convert an integer to a string of roman numerals, but this solution takes a rather different approach. This example illustrates rather well one of the ‘odd’ features of ALGOL68, which is the ability to use the result from any block of code in an expression, without assigning it to anything. Indeed the blocks of ‘code’ in the case statements in this example are just constants.

I also like the way you can get a string of characters just by multiplying a character by an integer, and that any character multiplied by zero is a null string…

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

  OP ROMAN = (INT value) STRING: (
    STRING numeral := "";
    INT thousands := value OVER 1000;
    INT hundreds := (value - thousands * 1000) OVER 100;
    INT tens := (value - thousands * 1000 - hundreds * 100) OVER 10;
    INT units := (value - thousands * 1000 - hundreds * 100 - tens * 10);
    numeral := numeral + thousands * "M";
    numeral := numeral +
      CASE hundreds + 1
        IN "","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"
      ESAC;
    numeral := numeral +
      CASE tens + 1
        IN "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"
      ESAC;
    numeral := numeral +
      CASE units + 1
        IN "","I","II","III","IV","V","VI","VII","VIII","IX"
      ESAC );

  PRIO ROMAN = 9;

 Algol68G
 
  # Convert a decimal value into a properly formed string of roman
    numerals - 21 Nov 84 - MEJT #

  OP ROMAN = (INT value) STRING: (
    STRING numeral := "";
    INT thousands := value OVER 1000;
    INT hundreds := (value - thousands * 1000) OVER 100;
    INT tens := (value - thousands * 1000 - hundreds * 100) OVER 10;
    INT units := (value - thousands * 1000 - hundreds * 100 - tens * 10);
    numeral := numeral + thousands * "M";
    numeral := numeral +
      CASE hundreds + 1
        IN "","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"
      ESAC;
    numeral := numeral +
      CASE tens + 1
        IN "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"
      ESAC;
    numeral := numeral +
      CASE units + 1
        IN "","I","II","III","IV","V","VI","VII","VIII","IX"
      ESAC );

  PRIO ROMAN = 9;

Since the two different routines do the same thing the output should be exactly the same as before.

 
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)

That is all for now – I promise!

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

2 Responses to Yet more Roman Number Conversions

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