x
Solved

# Visual Basic creating code for Roman Numerals

Posted on 2007-11-16
Medium Priority
1,521 Views
I do not know anything about Roman Numerals and I have a problem that request code for it. I'm sure what this is asking me to do.

Consider Roman numerals: different letters have numeric values, which are added or subtracted depending on their position. Remember that I, V, X, L, C, D, and M represent, respective, 1, 5, 10, 50, 100, 500, and 1000. (One mnemonic device for remembering this is the sentence "If Victor's Xray Looks Clear, Don't Medicate.") Thus, in Roman numerals, "LIX" would be 59, because you add 50 to (10 - 1).

We're going to do a simplified version of this: in our version, instead of position indicating whether you add or subtract, we're going to use case. In our system, UPPER-CASE LETTERS are positive, and lower-case letters are negative. Thus, in our system, to write 59, you could use "LiX", or "LXi", or any other combination of L, X, and i.

Any letter or symbol not in the set {I, V, X, L, C, D, M, i, v, x, l, c, d, m} is simply ignored. Thus, "Mommy" gives the value -1000, because there is one positive thousand (M), and two negative thousands (mm). The o, and the y, have no value and are ignored. "Midway" gives 499, because there is one positive thousand (M), one negative 500 (d), and one negative 1 (i). The other letters are ignored. `villi' gives -107. `MCMLXXI' is 2171, because we add all the values. (In actual Roman numerals, that would give 1971, because the `CM' means to subtract 100 from the 1000.) You may or may not find it useful to be reminded that Mid(foo, n, 1) returns the nth character of the string foo.

0
Question by:apr2505
• 2

LVL 13

Expert Comment

ID: 20301595
This sounds a lot like a homework assignment.  EE is a resource to help you resolve specific problems with the efforts you're already making--not to write code for you.

http://www.experts-exchange.com/help.jsp#hi105

If this isn't homework, you really need to come here with a more specific question, rather than a description of the project.  Then the experts will HELP you with your own work.
0

LVL 17

Accepted Solution

Shanmuga Sundaram earned 2000 total points
ID: 20303636
Private Function NumberToRoman(decimalvalue As Long) As String

Dim nThousands As Long
Dim nFiveHundreds As Long
Dim nHundreds As Long
Dim nFifties As Long
Dim nTens As Long
Dim nFives As Long
Dim nOnes As Long
Dim tmp As String

'take the value passed and split it out
'to values representing the number of
'ones, tens, hundreds, etc
nOnes = decimalvalue
nThousands = nOnes \ 1000
nOnes = nOnes - nThousands * 1000
nFiveHundreds = nOnes \ 500
nOnes = nOnes - nFiveHundreds * 500
nHundreds = nOnes \ 100
nOnes = nOnes - nHundreds * 100
nFifties = nOnes \ 50
nOnes = nOnes - nFifties * 50
nTens = nOnes \ 10
nOnes = nOnes - nTens * 10
nFives = nOnes \ 5
nOnes = nOnes - nFives * 5

'using VB's String function, create
'a series of strings representing
'the number of each respective denomination
tmp = String(nThousands, "M")

'handle those cases where the denominator
'value is on either side of a roman numeral
If nHundreds = 4 Then
If nFiveHundreds = 1 Then
tmp = tmp & "CM"
Else
tmp = tmp & "CD"
End If
Else
'not a 4, so create the string
tmp = tmp & String(nFiveHundreds, "D") & String(nHundreds, "C")
End If

If nTens = 4 Then
If nFifties = 1 Then
tmp = tmp & "XC"
Else
tmp = tmp & "XL"
End If
Else
tmp = tmp & String(nFifties, "L") & String(nTens, "X")
End If

If nOnes = 4 Then
If nFives = 1 Then
tmp = tmp & "IX"
Else
tmp = tmp & "IV"
End If
Else
tmp = tmp & String(nFives, "V") & String(nOnes, "I")
End If

NumberToRoman = tmp
Debug.Print NumberToRoman
End Function

Private Sub Command1_Click()
NumberToRoman (Text1.Text)
End Sub
0

LVL 17

Expert Comment

ID: 20303638
Sorry, I was in a hurry. so i just coded for decimal values to roman conversion.
0

Author Comment

ID: 20306378
Ok when the program is ran an error pops up highlighting NumberToRoman=tmp stating Compile Error; Argument not Optional.  Should put NumberToRoman=tmp+NumberToRoman? Now I've split the code in half because there is a cmdCompute and cmdConvert. I put the Dim etc put under cmdCompute and put the IF statements under cmdConvert. was that the right way? Here is what it looks like

Dim nThousands As Long
Dim nFiveHundreds As Long
Dim nHundreds As Long
Dim nFifties As Long
Dim nTens As Long
Dim nFives As Long
Dim nOnes As Long
Dim tmp As String

nOnes = decimalvalue
nThousands = nOnes \ 1000
nOnes = nOnes - nThousands * 1000
nFiveHundreds = nOnes \ 500
nOnes = nOnes - nFiveHundreds * 500
nHundreds = nOnes \ 100
nOnes = nOnes - nHundreds * 100
nFifties = nOnes \ 50
nOnes = nOnes - nFifties * 50
nTens = nOnes \ 10
nOnes = nOnes - nTens * 10
nFives = nOnes \ 5
nOnes = nOnes - nFives * 5
tmp = String(nThousands, "M")
End Sub

Private Sub cmdConvert_Click()

If nHundreds = 4 Then
If nFiveHundreds = 1 Then
tmp = tmp & "CM"
Else
tmp = tmp & "CD"
End If
Else
'not a 4, so create the string
tmp = tmp & String(nFiveHundreds, "D") & String(nHundreds, "C")
End If

If nTens = 4 Then
If nFifties = 1 Then
tmp = tmp & "XC"
Else
tmp = tmp & "XL"
End If
Else
tmp = tmp & String(nFifties, "L") & String(nTens, "X")
End If

If nOnes = 4 Then
If nFives = 1 Then
tmp = tmp & "IX"
Else
tmp = tmp & "IV"
End If
Else
tmp = tmp & String(nFives, "V") & String(nOnes, "I")
End If

NumberToRoman = tmp
Debug.Print NumberToRoman
End Function

Private Sub Command1_Click()
NumberToRoman (Text1.Text)
End Sub
End Sub
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.