Roman Numeral Converter

Programming Quality Challenge #1

OVERVIEW

Write a program to convert roman numerals into their arabic equivalent.

INPUT REQUIREMENTS

Read one or more roman numerals from standard input. Do not display an input prompt. Process one line at a time. Each input line contains only one roman numeral, starting in column one. Assume the characters are all upper case with no embedded blanks.

OUTPUT REQUIREMENTS

The arabic equivalent of each input roman numeral is displayed on standard output, starting in column one.

FUNCTIONAL REQUIREMENTS

Here are the arabic equivalents for roman symbols:

The "basic" roman symbols                                 The "auxiliary" roman symbols
 

I         X      C       M                                           V     L      D

1        10     100    1000                                      5     50     500


Convert the roman numeral to arabic processing the symbols from left to right according to the following rules:

1. A symbol following one of greater or equal value adds to its value. (E.g., XII = 12)
2. A symbol preceding one of greater value subtracts its value.(E.g., IV = 4; XL = 40)


ERROR HANDLING REQUIREMENTS

In each of the error conditions below, display the given message and skip the numeral and continue processing the next line.

"Invalid character in input. Valid characters are I,V,X,L,C,D,M."

Only the listed characters are valid. "Invalid numeral: can't subtract auxiliary symbol." It is not permitted to subtract an "auxiliary" symbol. (CML, not LM = 950; XLV not VL, = 45). "Invalid numeral: two consecutive subtractions." Can't do two subtractions in a row, thus LIVX is illegal. "Invalid numeral: additions don't decrease." Additions must decrease, as you go from left to right. Thus, each symbol added must have a value equal or less than the last symbol which was added. Thus, LIIX is wrong, cause we added L, added I, subtracted I, then try to add X.
However, XIV is fine because we add X, subtract I, then add V (which is less than the last symbol added, X).
TESTING

Submit a printout of executing your program using this acceptance test data.
 

Spring 2008: Submission instructions will be provided in class.




Planned Revisions for future use

The program can handle inputs between 1 and 3999.

Rule 4 - Repeated Use of V, L and D

The numerals that represent numbers beginning with a '5' (V, L and D) may only appear once in each Roman numeral.  This rule permits XVI but not VIV.

Rule 1 - Repetition

A single letter may be repeated up to three times consecutively with each occurrence of the value being additive.  This means that I is one, II means two and III is three.  However, IIII is incorrect for four.

Rule 2 - Additive Combination

Larger numerals must be placed to the left of the smaller numerals to continue the additive combination.  So VI equals six and MDCLXI is 1,661.

Rule 3 - Subtractive Combination

A small-value numeral may be placed to the left of a larger value.  Where this occurs, for example IX, the smaller numeral is subtracted from the larger.  This means that IX is nine and IV is four.  The subtracted digit must be at least one tenth of the value of the larger numeral and must be either I, X or C.  Accordingly, ninety-nine is not IC but rather XCIX.  The XC part represents ninety and the IX adds the nine.  In addition, once a value has been subtracted from another, no further numeral or pair may match or exceed the subtracted value.  This disallows values such as MCMD or CMC.

Rule 5 - Reducing Values

The fourth rule compares the size of value of each the numeral as read from left to right.  The value must never increase from one letter to the next.  Where there is a subtractive numeral, this rule applies to the combined value of the two numerals involved in the subtraction when compared to the previous letter.  This means that XIX is acceptable but XIM and IIV are not.