Written By TheVBProgramer .
This VB6 program
converts an integer in the range of 1 through 3999 to its equivalent in Roman
numerals. It also converts a Roman numeral string to its decimal equivalent.
The rules for
Roman numerals are as follows:
basic symbols are I (= 1), V (= 5), X (= 10), L (= 50), C (= 100), D (= 500), and M (=
a letter is immediately followed by one of equal or lesser value, the two
numbers are added; thus, XX = 20, XV = 15, VI = 6.
a letter is immediately followed by one of greater value, the first is
subtracted from the second; thus IV = 4, XL = 40, CM = 900.
bar over a letter multiplies it by 1000; thus, an X with a bar over it =
10,000. Such numbers will not be
addressed by this project.
from 1 to 10 are: I, II, III, IV, V, VI, VII, VIII, IX, X.
XLVII = 47, CXVI
= 116, MCXX = 1120, MCMXIV = 1914.
Note that a
given symbol appears no more than three times consecutively in a number. This is why 4 is written as "IV"
instead of "IIII", and 40 is written as "XL" instead of
most challenging part of converting a Roman Numerals number to decimal is not
the conversion itself, but rather validating the input. Basic validation would
include converting keystroke input to uppercase and allowing only the
characters I, V, X, L, C, D, and M to be entered. Beyond that, the following
rules should be applied:
L, or V may each only appear at most one time in the string
C, X, or I may appear no more that three times consecutively in the string
following pairs of letters are invalid in all cases: IL, IC, ID, IM, XD, XM,
VX, VL, VC, VD, VM, LC, LD, LM, DM.
a letter has been subtracted from, neither it nor its "5 counterpart"
may appear again in the string - so neither X nor V can follow IX, neither C
nor L may follow XC, and neither M nor D may follow CM.
a letter has been used as a subtraction modifier, it cannot appear again in
the string - so C cannot follow CD or
CM, X cannot follow XL or XC, and I cannot follow IV or IX.
I, X, or C (or their "5-counterparts" V, L, and D) appears in a
string, the I, X, or C cannot subsequently be used as subtraction modifiers -
so IV or IX cannot follow I or V, XL or XC cannot follow X or L, and CD or CM
cannot follow C or D.
are shown below.
When the user
selects "Roman Numerals to Decimal Number" for the conversion type,
note that the captions change and the reverse conversion is performed:
Download Visual Basic 6 sample source code